Skip to content

Commit 6debb29

Browse files
authored
Merge pull request #23 from liulanzheng/main
update python api and doc for model
2 parents cf2a3b7 + f81ffde commit 6debb29

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Currently, the OSS connector is composed of two libraries: OSS Model Connector a
1212

1313
- [OSS Model Connector](https://aliyun.github.io/oss-connector-for-ai-ml/#/modelconnector/introduction) focuses on AI inference scenarios, loading large model files from OSS into local AI inference frameworks.
1414

15-
The core part of is OSS Connector for AI/ML is implemented in C++ using [PhotonLibOS](https://github.com/alibaba/PhotonLibOS). This repository only contains the code of Python.
15+
The core component of the OSS Connector for AI/ML is implemented in C++ using [PhotonLibOS](https://github.com/alibaba/PhotonLibOS) and is provided as dynamic link libraries within wheel packages. This repository only contains the code of Python.
1616

1717
For details, please refer to [ossconnector.github.io](https://ossconnector.github.io/) or [aliyun.github.io/oss-connector-for-ai-ml](https://aliyun.github.io/oss-connector-for-ai-ml).
1818

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Currently, the OSS connector is composed of two libraries: OSS Model Connector a
88

99
- [OSS Model Connector](https://aliyun.github.io/oss-connector-for-ai-ml/#/modelconnector/introduction) focuses on AI inference scenarios, loading large model files from OSS into local AI inference frameworks.
1010

11-
The core part of is OSS Connector for AI/ML is implemented in C++ using [PhotonLibOS](https://github.com/alibaba/PhotonLibOS). This repository only contains the code of Python.
11+
The core component of the OSS Connector for AI/ML is implemented in C++ using [PhotonLibOS](https://github.com/alibaba/PhotonLibOS) and is provided as dynamic link libraries within wheel packages. This repository only contains the code of Python.
1212

1313

1414
## License

docs/modelconnector/framworks.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ total 136G
5656
-rw-r--r-- 1 root root 2.7M Sep 25 15:43 vocab.json
5757
```
5858

59+
Another common scenario is like the Stable Diffusion web UI, where a large number of models are stored in one or several folders, and there might be situations where models need to be switched during use.
60+
5961
The OssModelConnector offers a method to directly pass in an OSS directory to the inference frameworks and read the models directly from OSS.
6062

61-
Compared to downloading before loading to framworks, the OssModelConnector allows for simultaneous downloading and loading, achieving faster model deployment speeds.
63+
Compared to the FUSE-based mounting solution, OssModelConnector has a significant performance advantage. Compared to downloading before loading to framworks, the OssModelConnector allows for simultaneous downloading and loading, achieving faster model deployment speeds.
6264

6365
## Usage
6466

@@ -123,4 +125,32 @@ llm = LLM(model=model_dir, trust_remote_code=True)
123125
connector.close()
124126

125127
# do inference
126-
```
128+
```
129+
130+
# Stable Diffusion web UI
131+
132+
Edit launch.py to initalize and configure OssModelConnector.
133+
134+
```python
135+
from modules import launch_utils
136+
137+
import oss2
138+
from oss2.credentials import EnvironmentVariableCredentialsProvider
139+
from ossmodelconnector import OssModelConnector
140+
141+
...
142+
143+
def main():
144+
...
145+
146+
147+
if __name__ == "__main__":
148+
connector = OssModelConnector(endpoint='oss-cn-beijing-internal.aliyuncs.com',
149+
cred_provider=EnvironmentVariableCredentialsProvider(),
150+
config_path='/etc/connector.json')
151+
connector.prepare_directory('oss://ai-testset/Stable-diffusion/', '/root/stable-diffusion-webui/models/Stable-diffusion')
152+
153+
main()
154+
```
155+
156+
Currently, prepare_directory() loads all models into memory, which can put pressure on memory and even cause crashes in scenarios with a large number of models. In the future, prepare_directory() will support lazy loading, downloading models only when switching to or open them, and it will include a garbage collection feature to release memory for unused models after a specified time.

docs/modelconnector/python_apis.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Users can create an OssModelConnector in Python and call its provided methods to
2222

2323
```python
2424
connector = OssModelConnector(endpoint=ENDPOINT,
25-
cred_provider=EnvironmentVariableCredentialsProvider(),
26-
config_path='/tmp/config.json')
25+
cred_provider=EnvironmentVariableCredentialsProvider(),
26+
config_path='/tmp/config.json')
2727
```
2828

2929
- List objects

oss-model-connector/ossmodelconnector/oss_model_connector.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,25 @@ def open(self, uri, binary = True):
107107
return self._connector.open(uri, True, True, binary)
108108

109109
def _from_file_helper(self, filename, shared, nbytes):
110-
file = self._connector.open(filename, True, True)
111-
return UntypedStorageEx(file, nbytes)
112-
113-
def _connector_open(self, *args, **kwargs):
114-
filename = args[0]
115-
if isinstance(filename, pathlib.Path):
116-
filename = str(filename)
117-
open_mode = 'r' if len(args) == 1 else args[1]
118110
if self._hook_dir and filename.startswith(self._hook_dir):
111+
file = self._connector.open(filename, True, True)
112+
return UntypedStorageEx(file, nbytes)
113+
else:
114+
return self._origin_from_file(filename, shared, nbytes)
115+
116+
def _connector_open(self, file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
117+
if isinstance(file, pathlib.Path):
118+
file = str(file)
119+
if self._hook_dir and file.startswith(self._hook_dir):
119120
binary = False
120-
if open_mode == "rb":
121+
if 'b' in mode:
121122
binary = True
122123
try:
123-
return self.open(filename, binary)
124+
return self.open(file, binary)
124125
except:
125-
return self._origin_open(*args, **kwargs)
126+
return self._origin_open(file, mode, buffering, encoding, errors, newline, closefd, opener)
126127
else:
127-
return self._origin_open(*args, **kwargs)
128+
return self._origin_open(file, mode, buffering, encoding, errors, newline, closefd, opener)
128129

129130
def prepare_directory(self, uri: str, dir: str, libc_hook: bool = False):
130131
"""

0 commit comments

Comments
 (0)