feat: make transformers and the vLLM client optional dependencies (#31)#70
feat: make transformers and the vLLM client optional dependencies (#31)#70hallerite wants to merge 5 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 Mechanical refactoring to make transformers and vLLM client optional dependencies. Type hints are changed from concrete types to protocols, and lazy imports provide clear error messages when extras are missing. Runtime behavior is unchanged when dependencies are installed. 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).
|
not really satisfied with the way |
# Conflicts: # renderers/base.py
# Conflicts: # renderers/base.py
|
hey @hallerite , thanks for writing this PR! I see that its "approved" (by a bot, so idk if that counts). I am just wondering if you think it will still get merged. Thank you! |
|
@felipemello1 I will try to get this in asap, probably tomorrow or the day after! |
|
awesome, thanks @hallerite . Just to be clear: its not blocking us in TorchTitan. It is just a great feature. It is fine if you want to take your time to get it merged later. Ty! |
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 dependencies inrendererstransformers,fastokens,openai, andhttpxout of core dependencies into optional extras:renderers[transformers]andrenderers[vllm].Tokenizer,ChatTemplateTokenizer, andProcessorprotocols in renderers/base.py so renderer classes no longer import fromtransformersdirectly._require_transformers()helper that raises a clearImportErrorwith an install hint whentransformersis not present and a code path needs it.import renderersno longer pullsopenai/httpxintosys.modules; the vLLM client is only loaded on explicit import fromrenderers.client.OverlongPromptErroris no longer exported asrenderers.OverlongPromptError; callers must import it fromrenderers.clientdirectly.Macroscope summarized f19becd.