|
23 | 23 | # python 06_gpu_and_ml/comfyui/comfyclient.py --modal-workspace $(modal profile current) --prompt "Surreal dreamscape with floating islands, upside-down waterfalls, and impossible geometric structures, all bathed in a soft, ethereal light"
|
24 | 24 | # ```
|
25 | 25 |
|
26 |
| -#  |
| 26 | +#  |
27 | 27 |
|
28 | 28 | # The first inference will take ~1m since the container needs to launch the ComfyUI server and load Flux into memory. Successive calls on a warm container should take a few seconds.
|
29 | 29 |
|
|
64 | 64 | # Add .run_commands(...) calls for any other custom nodes you want to download
|
65 | 65 | )
|
66 | 66 |
|
67 |
| -# We'll also add our own custom node that patches core ComfyUI so that we can use Modal's [memory snapshot](https://modal.com/docs/guide/memory-snapshot) feature to speed up cold starts (more on that on [running as an API](https://modal.com/docs/examples/comfyapp#running-comfyui-as-an-api)). |
68 |
| -image = image.add_local_dir( |
69 |
| - local_path=Path(__file__).parent / "memory_snapshot_helper", |
70 |
| - remote_path="/root/comfy/ComfyUI/custom_nodes/memory_snapshot_helper", |
71 |
| - copy=True, |
72 |
| -) |
73 | 67 | # See [this post](https://modal.com/blog/comfyui-custom-nodes) for more examples
|
74 | 68 | # on how to install popular custom nodes like ComfyUI Impact Pack and ComfyUI IPAdapter Plus.
|
75 | 69 |
|
@@ -164,29 +158,17 @@ def ui():
|
164 | 158 | scaledown_window=300, # 5 minute container keep alive after it processes an input
|
165 | 159 | gpu="L40S",
|
166 | 160 | volumes={"/cache": vol},
|
167 |
| - enable_memory_snapshot=True, # snapshot container state for faster cold starts |
168 | 161 | )
|
169 | 162 | @modal.concurrent(max_inputs=5) # run 5 inputs per container
|
170 | 163 | class ComfyUI:
|
171 | 164 | port: int = 8000
|
172 | 165 |
|
173 |
| - @modal.enter(snap=True) |
| 166 | + @modal.enter() |
174 | 167 | def launch_comfy_background(self):
|
| 168 | + # launch the ComfyUI server exactly once when the container starts |
175 | 169 | cmd = f"comfy launch --background -- --port {self.port}"
|
176 | 170 | subprocess.run(cmd, shell=True, check=True)
|
177 | 171 |
|
178 |
| - @modal.enter(snap=False) |
179 |
| - def restore_snapshot(self): |
180 |
| - # initialize GPU for ComfyUI after snapshot restore |
181 |
| - # note: requires patching core ComfyUI, see the memory_snapshot_helper directory for more details |
182 |
| - import requests |
183 |
| - |
184 |
| - response = requests.post(f"http://127.0.0.1:{self.port}/cuda/set_device") |
185 |
| - if response.status_code != 200: |
186 |
| - print("Failed to set CUDA device") |
187 |
| - else: |
188 |
| - print("Successfully set CUDA device") |
189 |
| - |
190 | 172 | @modal.method()
|
191 | 173 | def infer(self, workflow_path: str = "/root/workflow_api.json"):
|
192 | 174 | # sometimes the ComfyUI server stops responding (we think because of memory leaks), so this makes sure it's still up
|
@@ -256,10 +238,10 @@ def poll_server_health(self) -> Dict:
|
256 | 238 |
|
257 | 239 | # This serves the `workflow_api.json` in this repo. When deploying your own workflows, make sure you select the "Export (API)" option in the ComfyUI menu:
|
258 | 240 |
|
259 |
| -#  |
| 241 | +#  |
260 | 242 |
|
261 | 243 | # ## More resources
|
262 |
| -# - [Alternative approach](https://modal.com/blog/comfyui-mem-snapshots) for deploying ComfyUI with memory snapshots |
| 244 | +# - Use [memory snapshots](https://modal.com/docs/guide/memory-snapshot) to speed up cold starts (check out the `memory_snapshot` directory on [Github](https://github.com/modal-labs/modal-examples/tree/main/06_gpu_and_ml/comfyui)) |
263 | 245 | # - Run a ComfyUI workflow as a [Python script](https://modal.com/blog/comfyui-prototype-to-production)
|
264 | 246 |
|
265 | 247 | # - When to use [A1111 vs ComfyUI](https://modal.com/blog/a1111-vs-comfyui)
|
|
0 commit comments