[Bug Fix] Remove eager re-exports from inference __init__ to avoid he…#56
Merged
yubofredwang merged 2 commits intomainfrom Mar 30, 2026
Merged
[Bug Fix] Remove eager re-exports from inference __init__ to avoid he…#56yubofredwang merged 2 commits intomainfrom
yubofredwang merged 2 commits intomainfrom
Conversation
…avy import side-effects The __init__.py files in torchspec/inference/ and torchspec/inference/engine/ eagerly imported all engine classes, causing vllm and other heavy dependencies to load at package import time. Replace with direct module-path imports where needed.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes eager package-level re-exports in torchspec.inference (and torchspec.inference.engine) to prevent import-time loading of optional inference backends (notably SGLang), and updates the training entrypoint to import the inference factory function directly.
Changes:
- Update
torchspec/train_entry.pyto importprepare_inference_enginesfromtorchspec.inference.factory. - Remove re-exported symbols and
__all__fromtorchspec/inference/__init__.py. - Remove re-exported engine symbols and
__all__fromtorchspec/inference/engine/__init__.py.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| torchspec/train_entry.py | Switches to explicit import from torchspec.inference.factory after removing torchspec.inference re-exports. |
| torchspec/inference/init.py | Removes inference factory re-exports to avoid eager imports and optional dependency loading. |
| torchspec/inference/engine/init.py | Removes engine class re-exports to avoid eager imports and optional dependency loading. |
Comments suppressed due to low confidence (2)
torchspec/inference/engine/init.py:20
- Dropping all re-exports from
torchspec.inference.enginebreaks prior imports likefrom torchspec.inference.engine import VllmEngine/InferenceEngine. If you still want to avoid importing optional dependencies at module import time, a lazy__getattr__-based re-export (optionally guarded with try/except and a targeted ImportError message whensglang/vllmare missing) would preserve the API without the eager dependency loading.
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
torchspec/inference/init.py:20
- Removing the re-exports from
torchspec.inferenceis a breaking API change (e.g.,from torchspec.inference import prepare_inference_engineswill now fail). To avoid eager imports while preserving the public entrypoints, consider using a lazy-export pattern (e.g., module-level__getattr__/__all__, liketorchspec.transfer.mooncake.__init__) that importsfactoryonly when the attribute is accessed and can surface a clearer error for missing optional deps.
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
These imports are useless and block vllm to run without SGLang deps, removing