Skip to content

Commit c734630

Browse files
numb3r3ZiniuYu
andauthored
refactor: use the new cas server (#128)
* fix: use new clip server * fix: use new clip server * fix: address comment Co-authored-by: Ziniu Yu <[email protected]> * fix: address comment Co-authored-by: Ziniu Yu <[email protected]> * fix: use new clip server * fix: add cas token env in flow parser * fix: add cas token env in flow parser * fix: add cas token env in flow parser * fix: disable external Co-authored-by: Ziniu Yu <[email protected]>
1 parent 5116d57 commit c734630

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

README.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DALL·E Flow is in client-server architecture.
2929

3030
## Updates
3131

32+
- ⚠️ **2022/10/26** To use CLIP-as-service available at `grpcs://api.clip.jina.ai:2096` (requires `jina >= v3.11.0`), you need first get an access token from [here](https://console.clip.jina.ai/get_started). See [Use the CLIP-as-service](#use-the-clip-as-service) for more details.
3233
- 🌟 **2022/9/25** Automated [CLIP-based segmentation](https://github.com/timojl/clipseg) from a prompt has been added.
3334
- 🌟 **2022/8/17** Text to image for [Stable Diffusion](https://github.com/CompVis/stable-diffusion) has been added. In order to use it you will need to agree to their ToS, download the weights, then enable the flag in docker or `flow_parser.py`.
3435
- ⚠️ **2022/8/8** Started using CLIP-as-service as an [external executor](https://docs.jina.ai/fundamentals/flow/add-executors/#external-executors). Now you can easily [deploy your own CLIP executor](#run-your-own-clip) if you want. There is [a small breaking change](https://github.com/jina-ai/dalle-flow/pull/74/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R103) as a result of this improvement, so [please _reopen_ the notebook in Google Colab](https://colab.research.google.com/github/jina-ai/dalle-flow/blob/main/client.ipynb).
@@ -170,7 +171,7 @@ DALL·E Flow needs one GPU with 21GB VRAM at its peak. All services are squeezed
170171

171172
The following reasonable tricks can be used for further reducing VRAM:
172173
- SwinIR can be moved to CPU (-3GB)
173-
- CLIP can be delegated to [CLIP-as-service demo server](https://github.com/jina-ai/clip-as-service#text--image-embedding) (-3GB)
174+
- CLIP can be delegated to [CLIP-as-service free server](https://console.clip.jina.ai/get_started) (-3GB)
174175

175176

176177
It requires at least 50GB free space on the hard drive, mostly for downloading pretrained models.
@@ -399,10 +400,53 @@ Congrats! Now you should be able to [run the client](#client).
399400

400401
You can modify and extend the server flow as you like, e.g. changing the model, adding persistence, or even auto-posting to Instagram/OpenSea. With Jina and DocArray, you can easily make DALL·E Flow [cloud-native and ready for production](https://github.com/jina-ai/jina).
401402

402-
### Run your own CLIP
403403

404-
By default [`CLIPTorchEncoder`](https://hub.jina.ai/executor/gzpbl8jh) runs as an [external executor](https://docs.jina.ai/fundamentals/flow/add-executors/#external-executors).
405-
If you want to run your own CLIP, you can do that by removing external executor related configs (`host, port, tls and external`) from [`flow.yml`](./flow.yml).
404+
### Use the CLIP-as-service
405+
406+
To reduce the usage of vRAM, you can use the `CLIP-as-service` as an external executor freely available at `grpcs://api.clip.jina.ai:2096`.
407+
First, make sure you have created an access token from [console website](https://console.clip.jina.ai/get_started), or CLI as following
408+
409+
```bash
410+
jina auth token create <name of PAT> -e <expiration days>
411+
```
412+
413+
Then, you need to change the executor related configs (`host`, `port`, `external`, `tls` and `grpc_metadata`) from [`flow.yml`](./flow.yml).
414+
415+
```yaml
416+
...
417+
- name: clip_encoder
418+
uses: jinahub+docker://CLIPTorchEncoder/latest-gpu
419+
host: 'api.clip.jina.ai'
420+
port: 2096
421+
tls: true
422+
external: true
423+
grpc_metadata:
424+
authorization: "<your access token>"
425+
needs: [gateway]
426+
...
427+
- name: rerank
428+
uses: jinahub+docker://CLIPTorchEncoder/latest-gpu
429+
host: 'api.clip.jina.ai'
430+
port: 2096
431+
uses_requests:
432+
'/': rank
433+
tls: true
434+
external: true
435+
grpc_metadata:
436+
authorization: "<your access token>"
437+
needs: [dalle, diffusion]
438+
```
439+
440+
You can also use the `flow_parser.py` to automatically generate and run the flow with using the `CLIP-as-service` as external executor:
441+
442+
```bash
443+
python flow_parser.py --cas-token "<your access token>'
444+
jina flow --uses flow.tmp.yml
445+
```
446+
447+
> ⚠️ `grpc_metadata` is only available after Jina `v3.11.0`. If you are using an older version, please upgrade to the latest version.
448+
449+
Now, you can use the free `CLIP-as-service` in your flow.
406450

407451
<!-- start support-pitch -->
408452
## Support

flow-jcloud.yml

-8
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ executors:
3030
uses_with:
3131
name: ViT-L-14-336::openai
3232
replicas: 1
33-
host: 'demo-cas.jina.ai'
34-
port: 2096
35-
tls: true
36-
external: true
3733
needs: [gateway]
3834
gpus: all
3935
jcloud:
@@ -58,10 +54,6 @@ executors:
5854
uses_requests:
5955
'/': rank
6056
replicas: 1
61-
host: 'demo-cas.jina.ai'
62-
port: 2096
63-
tls: true
64-
external: true
6557
needs: [dalle, diffusion]
6658
gpus: all
6759
jcloud:

flow.yml

-8
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ executors:
1515
replicas: 1 # change this if you have larger VRAM
1616
- name: clip_encoder
1717
uses: jinahub+docker://CLIPTorchEncoder/latest-gpu
18-
host: 'demo-cas.jina.ai'
19-
port: 2096
20-
tls: true
21-
external: true
2218
needs: [gateway]
2319
- name: diffusion
2420
uses: executors/glid3/config.yml
@@ -30,12 +26,8 @@ executors:
3026
needs: [clip_encoder]
3127
- name: rerank
3228
uses: jinahub+docker://CLIPTorchEncoder/latest-gpu
33-
host: 'demo-cas.jina.ai'
34-
port: 2096
3529
uses_requests:
3630
'/': rank
37-
tls: true
38-
external: true
3931
needs: [dalle, diffusion]
4032
- name: upscaler
4133
uses: executors/swinir/config.yml

flow_parser.py

+22
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ENV_GPUS_GLID3XL = 'GPUS_GLID3XL'
3333
ENV_GPUS_SWINIR = 'GPUS_SWINIR'
3434
ENV_GPUS_STABLE_DIFFUSION = 'GPUS_STABLE_DIFFUSION'
35+
ENV_CAS_TOKEN = 'CAS_TOKEN'
3536

3637
FLOW_KEY_ENV = 'env'
3738
FLOW_KEY_ENV_CUDA_DEV = 'CUDA_VISIBLE_DEVICES'
@@ -44,6 +45,9 @@
4445
SWINIR_FLOW_NAME = 'upscaler'
4546
STABLE_DIFFUSION_FLOW_NAME = 'stable'
4647

48+
CLIP_AS_SERVICE_HOST = os.environ.get('CLIP_AS_SERVICE_HOST', 'api.clip.jina.ai')
49+
CLIP_AS_SERVICE_PORT = os.environ.get('CLIP_AS_SERVICE_PORT', '2096')
50+
4751

4852
def represent_ordereddict(dumper, data):
4953
'''
@@ -103,6 +107,11 @@ def represent_ordereddict(dumper, data):
103107
action='store_true',
104108
help="Enable Stable Diffusion executor (default false)",
105109
required=False)
110+
parser.add_argument('--cas-token',
111+
dest='cas_token',
112+
help="Token to authenticate with the CAS service (default ''). If not set, the CAS service will not be used.",
113+
default='',
114+
required=False)
106115
parser.add_argument('--gpus-dalle-mega',
107116
dest='gpus_dalle_mega',
108117
help="GPU device ID(s) for DALLE-MEGA (default 0)",
@@ -152,6 +161,7 @@ def represent_ordereddict(dumper, data):
152161
args.get('gpus_stable_diffusion')
153162
gpus_swinir = os.environ.get(ENV_GPUS_SWINIR, False) or \
154163
args.get('gpus_swinir')
164+
cas_token = os.environ.get(ENV_CAS_TOKEN, '') or args.get('cas_token')
155165

156166
CLIPSEG_DICT = OrderedDict({
157167
'env': {
@@ -174,6 +184,7 @@ def represent_ordereddict(dumper, data):
174184
'uses': f'executors/{STABLE_DIFFUSION_FLOW_NAME}/config.yml',
175185
})
176186

187+
177188
def _filter_out(flow_exec_list, name):
178189
return list(filter(lambda exc: exc['name'] != name, flow_exec_list))
179190

@@ -185,6 +196,17 @@ def _filter_out(flow_exec_list, name):
185196
print(exc)
186197
sys.exit(1)
187198

199+
# If the cas_token is not empty, we will use the clip-as-a-service as external executor
200+
if cas_token:
201+
for ext in flow_as_dict['executors']:
202+
if ext['name'] in [CAS_FLOW_NAME, RERANK_FLOW_NAME]:
203+
ext['host'] = CLIP_AS_SERVICE_HOST
204+
ext['port'] = int(CLIP_AS_SERVICE_PORT)
205+
ext['external'] = True
206+
ext['tls'] = True
207+
ext['grpc_metadata'] = {'authorization': cas_token}
208+
209+
188210
# For backwards compatibility, we inject the stable diffusion configuration
189211
# into the flow yml and then remove it if needed.
190212
#

0 commit comments

Comments
 (0)