onevision-encoder-large-tf57
transformers 5.7+ idiomatic variant of lmms-lab-encoder/onevision-encoder-large.
Weights are byte-identical to the upstream model (same
safetensors SHA-256). Only
modeling_onevision_encoder.py and
config.json (
transformers_version field) differ.
Why this variant
Upstream modeling_onevision_encoder.py is written against the transformers 4.x API surface and does not load correctly under transformers >= 5.0:
_supports_flash_attn_2 was renamed to _supports_flash_attn.
- The v5 fast-init / meta-tensor path skips re-initialization of
persistent=False buffers, leaving VideoRotaryEmbeddingSplit466.inv_freq_{t,h,w} filled with uninitialized memory. RoPE then produces garbage and downstream attention diverges (max diff up to 700+ vs upstream).
add_start_docstrings* / replace_return_docstrings decorators are removed in v5.
- Manual eager-only attention is replaced by the v5
ALL_ATTENTION_FUNCTIONS interface dispatching across eager, sdpa, flash_attention_2, flex_attention.
v5-only notice
This variant requires transformers >= 5.7.0 and will not load under transformers 4.x. Use the upstream model dir for v4 environments.
Diff vs upstream
| File | Change |
|---|
model.safetensors | unchanged (byte-identical) |
config.json | transformers_version: 4.57.3 -> 5.7.0 |
configuration_onevision_encoder.py | unchanged |
preprocessor_config.json | unchanged |
modeling_onevision_encoder.py | full v5-idiom rewrite: _supports_flash_attn/_supports_sdpa/_supports_flex_attn/_supports_attention_backend, ALL_ATTENTION_FUNCTIONS.get_interface(...) dispatch, @auto_docstring + @can_return_tuple, removed v4 docstring decorators and use_return_dict branches, _init_weights hook calls VideoRotaryEmbeddingSplit466.reset_inv_freqs() to fix the inv_freq init bug. |
Usage
1from transformers import AutoModel
2
3model = AutoModel.from_pretrained(
4 "path/to/onevision-encoder-large-tf57",
5 trust_remote_code=True,
6) # default attn_implementation = "flash_attention_2" (set in config.json)
Override the default if you need a different backend:
1model = AutoModel.from_pretrained(..., attn_implementation="sdpa")
2# supported: "flash_attention_2" (default), "sdpa", "eager", "flex_attention"
Dtype contract: weights are saved in bfloat16. The default flash_attention_2 backend requires fp16/bf16 inputs. If you must use fp32, override with attn_implementation="sdpa" or "eager".
Tested with transformers==5.7.0, torch>=2.4, flash-attn>=2.7.
Equivalence verification
Cross-version (upstream tf 4.57.3 vs this tf 5.7.0) on 11 input shapes (single image / multi-frame video / batched / non-square / visible_indices):
| dtype | attn | result |
|---|
| fp32 | eager | bit-identical (max_diff = 0.0 across all 22 tensors) |
| bf16 | eager | bit-identical (max_diff = 0.0 across all 22 tensors) |
Plus 7 v5-only scenario tests, all PASSED:
- eager vs sdpa equivalence (max=7.5e-5)
- save_pretrained then from_pretrained bit-identical round-trip
- cpu vs cuda equivalence (max=4.1e-5)
- fp32/bf16/fp16 dtype preservation
- gradient flow (389/399 params receive non-zero grad)
- runtime
_attn_implementation switch
from_pretrained idempotency (two loads bit-identical)
Plus real-input end-to-end tests on a real JPEG (1332x725) and a real MP4 (decord, 4 frames @ 512x512), preprocessed through AutoImageProcessor (CLIPImageProcessor):
| path | result |
|---|
| image: PIL -> processor -> model fwd | finite, lhs=(1,1024,1024), pool=(1,1024) |
| video: decord -> 5D (1,3,4,448,448) -> model fwd | finite, lhs=(1,4096,1024), pool=(1,1024) |
| model-only equivalence on identical pixel_values (v4 vs v5) | bit-identical (max_diff = 0.0 on image+video) |
Note: Raw pixel_values from CLIPImageProcessor differ by ~1e-2 between transformers 4.57.3 and 5.7.0 due to upstream resize/normalize changes in transformers itself (independent of this variant). When the same pixel_values are fed to both versions, this model is bit-identical.
Reproduce with tools/upgrade_v5/run_all.sh from the OneVision-Encoder repo.
Changelog
- tf57: full v5-idiom rewrite; weights unchanged.
The original model card from upstream follows.