feat: make transformers and the vLLM client optional dependencies (#31)#70
Draft
hallerite wants to merge 4 commits into
Draft
feat: make transformers and the vLLM client optional dependencies (#31)#70hallerite wants to merge 4 commits into
hallerite wants to merge 4 commits into
Conversation
`transformers` (+ `fastokens`) and the `openai`/`httpx`-based vLLM generate client are no longer core dependencies. Text-only renderers now work with a bring-your-own tokenizer and none of the heavy deps installed. - Add `Tokenizer` + `Processor` structural protocols in `base.py`; type the renderer `tokenizer`/`processor` params against them instead of `transformers.PreTrainedTokenizer`, so importing a renderer no longer drags in `transformers`. - Move `transformers` + `fastokens` to the `[transformers]` extra and `openai` + `httpx` to the `[vllm]` extra. `_require_transformers()` raises a clear "install renderers[transformers]" error on the lazy paths (`load_tokenizer`, offset attribution, VLM processors). - `renderers.client` is opt-in: no longer imported by `renderers/__init__`, and `OverlongPromptError` moves with it (importable from `renderers.client`). - Add `tests/test_no_transformers.py` proving text render/parse and `import renderers` work with `transformers`/`fastokens`/`openai`/`httpx` import-blocked, and that `load_tokenizer` errors clearly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
# Conflicts: # pyproject.toml
ApprovabilityVerdict: Approved This PR restructures You can customize Macroscope's approvability policy. Learn more. |
…e from Tokenizer Brings in #68 (examples), #69 (harmony floor), #71 (qwen3.5 hard-coded enable_thinking). The only qwen35.py conflict is resolved by keeping #71's hard-coded `_ENABLE_THINKING_DEFAULTS` table (no `apply_chat_template` probe) on top of #31's `Tokenizer`/`Processor` type hints. Now that #71 removed the last hand-coded-renderer call to `apply_chat_template`, drop it from the `Tokenizer` protocol so a plain `tokenizers.Tokenizer` wrapper satisfies it. `apply_chat_template` moves to a new `ChatTemplateTokenizer(Tokenizer, Protocol)` subtype, required only by `DefaultRenderer` (the generic chat-template fallback).
Member
Author
|
not really satisfied with the way |
# Conflicts: # renderers/base.py
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.
Closes #31.
Why
transformersis a heavy dependency, and downstreams that keep training deps lightweight (e.g. TorchTitan/TorchTune, which load tokenizers viatokenizers) shouldn't have to pull it in just to use a renderer. This makes the heavy/engine-specific pieces opt-in, so the base install is lightweight and text-only renderers work with a bring-your-own tokenizer.What changed
Tokenizer+Processorprotocols (renderers/base.py) — structural types replacingtransformers.PreTrainedTokenizerin every renderer'stokenizer/processorannotations. The module-levelfrom transformers... import PreTrainedTokenizeris gone from all 13 renderer modules, soimport renderers.<model>no longer drags intransformers.transformers+fastokens→[transformers]extra. Needed only by the convenience helpers (load_tokenizer,create_renderer*), the offset-attribution fallback inattribute_text_segments, and the VLM renderers (image processors)._require_transformers()raises a clearpip install 'renderers[transformers]'error on those lazy paths when it's missing.renderers.client→[vllm]extra. The vLLM/inference/v1/generateclient is the only thing needingopenai+httpx; it's no longer imported byrenderers/__init__(soimport renderersstays free of HTTP/engine deps).OverlongPromptErroris now imported fromrenderers.client(no top-level re-export).Result — base
pip install rendererscore deps are just:numpy, tiktoken, jinja2, openai-harmony, prime-pydantic-config. Heavy bits arerenderers[transformers]andrenderers[vllm](composable).Caveats (documented in the README)
A bring-your-own tokenizer must satisfy the
Tokenizerprotocol (encode/decode/convert_tokens_to_ids/apply_chat_template+name_or_path/unk_token_id/eos_token_id), and per-token training attribution additionally needstokenizer(..., return_offsets_mapping=True)— without it, attribution falls back to a vanilla HF tokenizer (the extra).Tests
tests/test_no_transformers.py: subprocess-blockstransformers/fastokens/openai/httpx, then assertsimport renderers+ a text renderer's render/parse work, that no blocked module leaks intosys.modules, and thatload_tokenizererrors with the install hint.ruff+tyclean.🤖 Generated with Claude Code
Note
Medium Risk
Breaking for consumers that imported
OverlongPromptErrorfromrenderersor assumedtransformers/openaion base install; behavior is documented and guarded with new boundary tests.Overview
This PR makes the base
renderersinstall lightweight by moving heavy deps behind optional extras and letting text renderers run with a bring-your-own tokenizer.Packaging:
transformersandfastokensare no longer core dependencies; they install viarenderers[transformers](used byload_tokenizer/create_renderer*, offset attribution fallback, and VLMs).openaiandhttpxmove torenderers[vllm]forrenderers.client. Dev deps mirror both extras so CI still exercises those paths.Typing / imports: New
Tokenizer,ChatTemplateTokenizer, andProcessorprotocols inrenderers/base.pyreplacePreTrainedTokenizeron all renderer modules, so importing renderers no longer pulls intransformers. VLMs loadAutoProcessorthrough_require_transformers(), which raises a clear install hint if the extra is missing.Public API:
OverlongPromptErroris dropped fromrendererstop-level exports; usefrom renderers.client import OverlongPromptError.Tokenizer,ChatTemplateTokenizer, andProcessorare exported from the package root.Tests / docs:
tests/test_no_transformers.pysubprocess-blocks optional deps and checks text render/parse, no leaked imports, andload_tokenizererrors. README documents extras, protocol expectations, and attribution caveats.Reviewed by Cursor Bugbot for commit 5062105. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Make
transformersand vLLM client optional dependenciestransformers,fastokens,openai, andhttpxout of base dependencies into optional extras:renderers[transformers]andrenderers[vllm].Tokenizer,ChatTemplateTokenizer, andProcessorprotocols in renderers/base.py so renderer classes no longer import fromtransformersat module scope._require_transformers()helper that raises a clearImportErrorpointing to therenderers[transformers]extra whentransformersis not installed, replacing generic import failures.OverlongPromptErroris no longer exported from the top-levelrendererspackage.transformersproduces the correct error.Macroscope summarized 5062105. (Automatic summaries will resume when PR exits draft mode or review begins).