Fix loading with Transformers v5

#14

This PR makes modeling_ovis2_5.py work with Transformers v5 while keeping behaviour on v4 unchanged.

Fixes

  1. is_parallelizable no longer exists in v5, so loading fails with AttributeError: 'Qwen3ForCausalLM' object has no attribute 'is_parallelizable'. The line is removed.
  2. Ovis2_5.__init__ never called post_init(), so v5 doesn't register the tied lm_head. The checkpoint stores no lm_head weights, so lm_head is left uninitialized and the model produces garbage (as reported in discussion #8).
  3. The tie_weights() override now accepts v5's keyword arguments and calls the base implementation before tying the LLM.
  4. v5 materializes non-persistent buffers with torch.empty_like, so indicator_token_indices and the ViT rotary inv_freq held uninitialized memory (out-of-range visual token ids, CUDA device-side asserts). Both are now recomputed in _init_weights.

Testing (RTX PRO 6000, bf16, greedy decoding, image prompt)

  • transformers 4.57.6: output is identical before and after this change.
  • transformers 5.17.0: the original code fails to load; with this change the output matches 4.57.6 (per-token log-probs within ~0.1; the text diverges late on a near-tied token, as expected from bf16 numerics).
  • vLLM's HF-vs-vLLM comparison tests for Ovis2.5-2B (single image, multi-image, video) pass with this change on transformers 5.17.0.
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment