perf: import worker executors lazily via a registry#83
Merged
Conversation
Each executor module was imported eagerly at package load, so importing worker.executors pulled in every executor's dependency tree (vLLM, trl, etc.) even on workers that service none of them. Replace the eager imports and the parallel EXECUTOR_REGISTRY / EXECUTOR_CLASS_NAMES dicts with an ExecutorRegistry Mapping that imports each executor on first access and caches the result, exposing class names through get_executor_class_name. Signed-off-by: Noppanat Wadlom <noppanat.wad@gmail.com>
09d0c22 to
a3ff199
Compare
timzsu
reviewed
Jun 22, 2026
timzsu
left a comment
Collaborator
There was a problem hiding this comment.
One small comment to discuss.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Importing
worker.executorseagerly imported every executor module at package load, so any process touching the package paid the full executor dependency tree (torch, transformers, vLLM, trl, diffusers) even when it needed none of them. This makes executor imports lazy, which speeds up two paths that don't run a full executor set: the supervisor's per-node hardware probe (worker.main --collect-hw, which never initializes executors) andMPExecutor's spawned subprocess (which re-runs the package import to run a single executor).Changes
EXECUTOR_REGISTRY/EXECUTOR_CLASS_NAMESdicts with anExecutorRegistryMappingthat imports each executor on first access and caches it. Class-name lookups move to a module-levelget_executor_class_name.Mappingcontract (lazy resolution,KeyErroron unknown keys, class-name lookup).Design
ExecutorRegistryis aMapping[str, type[Executor] | None]:__getitem__imports on demand and caches, raisingKeyErrorfor unknown keys (returningNoneonly for a known executor whose import failed).__iter__/__len__cover the registered keys without importing them, so capability enumeration stays cheap. Worker startup is unchanged in total cost —initialize_executorsstill resolves every key — but the import work is deferred from package load to initialization, and processes that never initialize executors skip it entirely.Performance
Median startup cost, eager baseline vs lazy, on freshly built CPU and GPU worker images:
import worker.executorsimport worker.executors--collect-hwhardware probe--collect-hwhardware probeMPExecutorspawn (light executor)MPExecutorspawn (heavy executor)MPExecutorusesspawn, so each subprocess re-runs the package import; it now loads only the one executor it runs instead of all of them. The win scales inversely with how heavy that executor's own dependencies are — large for light executors, modest for vLLM/training which need most of the stack regardless. Full worker time-to-ready is unchanged, since a fully-initialized worker still imports every executor.The test suite benefits from the same effect, since collection imports the package without initializing executors:
uv run pytest tests/ --ignore=tests/worker/test_mp_executor_cleanup_gpu.pydrops from 94.50 s to 28.81 s (~3.3×).Test Plan
Validated end-to-end on freshly built CPU and GPU images. On a CPU worker: importing the package inside the image loads no executor module or heavy dependency; the worker advertises the expected capability set (supported CPU types present, GPU-only types absent) and runs an echo task to completion; each GPU-gated executor is resolved and logged as skipped with a reason. On a GPU worker: the GPU-only executors are lazily imported and advertised, vLLM initializes without being skipped, and a TinyLlama vLLM inference task runs to completion. The startup-cost figures above were measured on the same images, comparing the eager baseline image against this build.
Test Result
End-to-end on CPU + GPU images: 31/31 assertions passed. The CPU worker advertised the expected supported task types and ran the echo task to
DONEwhile logging each GPU-gated executor as skipped; the GPU worker advertised and lazily imported the GPU executor set, initialized vLLM without skipping it, and ran the vLLM inference task toDONE.Pre-submission Checklist
pre-commiton the changed files and fixed any issues.[BREAKING]and described migration steps above.