[PR #46] [MERGED] Implement threaded loader components (CORE-43) #85

Closed
opened 2026-10-26 22:24:25 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Comfy-Org/comfy-aimdo/pull/46
Author: @rattus128
Created: 5/6/2026
Status: ✅ Merged
Merged: 5/12/2026
Merged by: @alexisrolland

Base: master ← Head: dev/threaded-load


📝 Commits (10+)

📊 Changes

19 files changed (+881 additions, -17 deletions)

View changed files

📝 comfy_aimdo/host_buffer.py (+62 -6)
📝 src-cuda/dispatch.c (+2 -0)
📝 src-hip/dispatch.c (+10 -0)
➕ src-posix/hostbuf-plat.c (+42 -0)
➕ src-posix/thread-plat.c (+71 -0)
➕ src-posix/xfer-file-plat.c (+24 -0)
➕ src-win/hostbuf-plat.c (+35 -0)
➕ src-win/thread-plat.c (+68 -0)
➕ src-win/xfer-file-plat.c (+40 -0)
📝 src/control.c (+7 -0)
📝 src/gpu_dispatch.h (+5 -0)
➕ src/hostbuf-plat.h (+12 -0)
➕ src/hostbuf-prewarm.c (+98 -0)
➕ src/hostbuf-prewarm.h (+6 -0)
📝 src/hostbuf.c (+198 -11)
📝 src/plat.h (+2 -0)
➕ src/thread-plat.h (+33 -0)
➕ src/xfer-file.c (+154 -0)
➕ src/xfer-file.h (+12 -0)

📄 Description

Contribution Agreement

  • I agree that my contributions are licensed under the GPLv3.
  • I grant Comfy Org the rights to relicense these contributions as outlined in CONTRIBUTING.md.

Currently loading models from disk has a bad mix of single-core CPU and disk activity serialization.

So create two thread pools to:

1: Prefault memory in the background while the main thread continues.
2: Do chunked + threaded transfers from disk to RAM.

If you prefault, cudaHostRegister is really fast (way way faster than cudaHostMemAlloc). So we use the background prefault of some headroom to completely hide this latency. The chunked transfer is significantly faster pageable cudaMemcpyAsync.

In addition to the above, also expand the hostbuf API to be growable with the above prefaulter.

So the usage pattern for this is use the new hostbuf APIs to create temp buffers that are pinned and always transfer via said pinned memory.

With accompanying ComfyUI changes I get the following preliminary speedups.

Windows RTX5060, 64GB, PCIE4x4 NVME, LTX2.3 FP8 (no Lora) 360P, first run on launch:

Before:

Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached.
Prompt executed in 120.73 seconds

After:

Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached.
Prompt executed in 90.43 seconds

Linux RTX5090, 96GB, PCIE5x4 NVME, LTX2.3 FP8 (no Lora), 720P, first run on launch, caches cold:

Before:

Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached.
Prompt executed in 49.66 seconds

After:

Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached.
Prompt executed in 35.50 seconds

This is nsight of the model load on a warm load. The gaps between the green blocks are about what you would expect, given the GPU can DMA master than the memcpy to pin.

image

More data to come Comfy side, along with how to use this in code.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Comfy-Org/comfy-aimdo/pull/46 **Author:** [@rattus128](https://github.com/rattus128) **Created:** 5/6/2026 **Status:** ✅ Merged **Merged:** 5/12/2026 **Merged by:** [@alexisrolland](https://github.com/alexisrolland) **Base:** `master` ← **Head:** `dev/threaded-load` --- ### 📝 Commits (10+) - [`947b1a3`](https://github.com/Comfy-Org/comfy-aimdo/commit/947b1a39873a9df03421fa4ab8ed871933057785) host_buffer: plan api change to grow the buffer - [`6c8c04c`](https://github.com/Comfy-Org/comfy-aimdo/commit/6c8c04cc4b72d2884c5d787f97ee546208cd749e) sketch prewarm hint - [`54b5eaa`](https://github.com/Comfy-Org/comfy-aimdo/commit/54b5eaae7597ce7289970b1049cd8fbe0a7bf5ed) stub file reader - [`5cc2aba`](https://github.com/Comfy-Org/comfy-aimdo/commit/5cc2aba8f663674705dc6cd389e631a9e160bb40) sketch truncate - [`5de82a4`](https://github.com/Comfy-Org/comfy-aimdo/commit/5de82a4a7f8cb775589be528f7705fbcacb3270f) dispatch: Add cuMemHostRegister/Unregister - [`5ef6cac`](https://github.com/Comfy-Org/comfy-aimdo/commit/5ef6cac082cbd56e8c70b20e6b8c910cbc97bad9) threads API - [`a405f38`](https://github.com/Comfy-Org/comfy-aimdo/commit/a405f386eaaed8a0b538df8161034b00acba302d) xfer file - [`0475692`](https://github.com/Comfy-Org/comfy-aimdo/commit/0475692b66dbf675b07f73bb703e08ae616625f8) hostbuf plat - [`8156b25`](https://github.com/Comfy-Org/comfy-aimdo/commit/8156b250f1fe8d56cb530762ec31dbb18f195e66) prewarm - [`3853bd6`](https://github.com/Comfy-Org/comfy-aimdo/commit/3853bd6089cc08474f01e7f9ea43113c38460e23) implement main hostbuf protocol ### 📊 Changes **19 files changed** (+881 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `comfy_aimdo/host_buffer.py` (+62 -6) 📝 `src-cuda/dispatch.c` (+2 -0) 📝 `src-hip/dispatch.c` (+10 -0) ➕ `src-posix/hostbuf-plat.c` (+42 -0) ➕ `src-posix/thread-plat.c` (+71 -0) ➕ `src-posix/xfer-file-plat.c` (+24 -0) ➕ `src-win/hostbuf-plat.c` (+35 -0) ➕ `src-win/thread-plat.c` (+68 -0) ➕ `src-win/xfer-file-plat.c` (+40 -0) 📝 `src/control.c` (+7 -0) 📝 `src/gpu_dispatch.h` (+5 -0) ➕ `src/hostbuf-plat.h` (+12 -0) ➕ `src/hostbuf-prewarm.c` (+98 -0) ➕ `src/hostbuf-prewarm.h` (+6 -0) 📝 `src/hostbuf.c` (+198 -11) 📝 `src/plat.h` (+2 -0) ➕ `src/thread-plat.h` (+33 -0) ➕ `src/xfer-file.c` (+154 -0) ➕ `src/xfer-file.h` (+12 -0) </details> ### 📄 Description ### Contribution Agreement - [X] I agree that my contributions are licensed under the GPLv3. - [X] I grant **Comfy Org** the rights to relicense these contributions as outlined in [CONTRIBUTING.md](./CONTRIBUTING.md). Currently loading models from disk has a bad mix of single-core CPU and disk activity serialization. So create two thread pools to: 1: Prefault memory in the background while the main thread continues. 2: Do chunked + threaded transfers from disk to RAM. If you prefault, cudaHostRegister is really fast (way way faster than cudaHostMemAlloc). So we use the background prefault of some headroom to completely hide this latency. The chunked transfer is significantly faster pageable cudaMemcpyAsync. In addition to the above, also expand the hostbuf API to be growable with the above prefaulter. So the usage pattern for this is use the new hostbuf APIs to create temp buffers that are pinned and always transfer via said pinned memory. With accompanying ComfyUI changes I get the following preliminary speedups. Windows RTX5060, 64GB, PCIE4x4 NVME, LTX2.3 FP8 (no Lora) 360P, first run on launch: Before: ``` Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached. Prompt executed in 120.73 seconds ``` After: ``` Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached. Prompt executed in 90.43 seconds ``` Linux RTX5090, 96GB, PCIE5x4 NVME, LTX2.3 FP8 (no Lora), 720P, first run on launch, caches cold: Before: ``` Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached. Prompt executed in 49.66 seconds ``` After: ``` Model VideoVAE prepared for dynamic VRAM loading. 1384MB Staged. 0 patches attached. Prompt executed in 35.50 seconds ``` This is nsight of the model load on a warm load. The gaps between the green blocks are about what you would expect, given the GPU can DMA master than the memcpy to pin. <img width="1887" height="630" alt="image" src="https://github.com/user-attachments/assets/531f3e0c-f82f-4ee2-ac6f-0228ce8266e9" /> More data to come Comfy side, along with how to use this in code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 22:24:25 +00:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Comfy-Org/comfy-aimdo#85
No description provided.