Skip to content

Commit d5bddf2

Browse files
build(deps): support Transformers 5.20 (#6490)
Signed-off-by: Kamran Jafari <kjafarisadeg@nvidia.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8a0ebe5 commit d5bddf2

21 files changed

Lines changed: 170 additions & 50 deletions

File tree

‎docs/fern/versions/nightly/pages/models/minimax/minimax-m3.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Bridge applies its full partial-3D vision RoPE correction only on Transformers
99
[HF PR #49164](https://github.com/huggingface/transformers/pull/49164), so Bridge
1010
leaves those versions unpatched.
1111

12-
The native 5.18/5.19 implementation stores inverse frequencies in buffers.
12+
The native 5.18-5.20 implementation stores inverse frequencies in buffers.
1313
Explicit `.half()` or `.bfloat16()` casts can round these buffers, producing
1414
different results from the earlier FP32-per-forward calculation. Bridge accepts
1515
this precision difference and does not promise bitwise equality across these

‎docs/fern/versions/nightly/pages/models/nemotron/nemotron3.5-super-vl.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ For language-only conversion and training without media components, see [the tex
1414

1515
## Native Hugging Face processor compatibility
1616

17-
With Transformers 5.19.0, `AutoBridge.from_hf_pretrained(..., trust_remote_code=False)`
17+
With Transformers 5.18.0, 5.19.0, or 5.20.0, `AutoBridge.from_hf_pretrained(..., trust_remote_code=False)`
1818
uses a Bridge-local processor loading adapter for Super VL. It removes a legacy
1919
read-only video configuration field in memory and does not require an audio feature
2020
extractor for this audio-free model. The source checkpoint and installed Transformers
2121
code are not modified; image/video processing and token expansion use the native HF
2222
implementation. Remote-code loading remains available on older supported HF versions.
2323

24-
The adapter is version-guarded to Transformers 5.19.0. Processor artifacts can be saved
24+
The adapter is version-guarded to Transformers 5.18.0, 5.19.0, and 5.20.0. Processor artifacts can be saved
2525
and reloaded through Bridge, but ordinary `AutoProcessor.from_pretrained` outside
26-
Bridge still encounters the native 5.19.0 audio-component requirement. This workaround
26+
Bridge still encounters the native 5.18.0-5.20.0 audio-component requirement. This workaround
2727
does not imply standalone HF processor compatibility or full model forward parity.
2828
The wrapper's `hf_pretrained.model` accessor selects `AutoModelForImageTextToText`
2929
for native Super VL and retains `AutoModelForCausalLM` for legacy remote-code

‎docs/models/minimax/minimax-m3.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Bridge applies its full partial-3D vision RoPE correction only on Transformers
99
[HF PR #49164](https://github.com/huggingface/transformers/pull/49164), so Bridge
1010
leaves those versions unpatched.
1111

12-
The native 5.18/5.19 implementation stores inverse frequencies in buffers.
12+
The native 5.18-5.20 implementation stores inverse frequencies in buffers.
1313
Explicit `.half()` or `.bfloat16()` casts can round these buffers, producing
1414
different results from the earlier FP32-per-forward calculation. Bridge accepts
1515
this precision difference and does not promise bitwise equality across these

‎docs/models/nemotron/nemotron3.5-super-vl.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ For language-only conversion and training without media components, see [the tex
1414

1515
## Native Hugging Face processor compatibility
1616

17-
With Transformers 5.19.0, `AutoBridge.from_hf_pretrained(..., trust_remote_code=False)`
17+
With Transformers 5.18.0, 5.19.0, or 5.20.0, `AutoBridge.from_hf_pretrained(..., trust_remote_code=False)`
1818
uses a Bridge-local processor loading adapter for Super VL. It removes a legacy
1919
read-only video configuration field in memory and does not require an audio feature
2020
extractor for this audio-free model. The source checkpoint and installed Transformers
2121
code are not modified; image/video processing and token expansion use the native HF
2222
implementation. Remote-code loading remains available on older supported HF versions.
2323

24-
The adapter is version-guarded to Transformers 5.19.0. Processor artifacts can be saved
24+
The adapter is version-guarded to Transformers 5.18.0, 5.19.0, and 5.20.0. Processor artifacts can be saved
2525
and reloaded through Bridge, but ordinary `AutoProcessor.from_pretrained` outside
26-
Bridge still encounters the native 5.19.0 audio-component requirement. This workaround
26+
Bridge still encounters the native 5.18.0-5.20.0 audio-component requirement. This workaround
2727
does not imply standalone HF processor compatibility or full model forward parity.
2828
The wrapper's `hf_pretrained.model` accessor selects `AutoModelForImageTextToText`
2929
for native Super VL and retains `AutoModelForCausalLM` for legacy remote-code

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ classifiers = [
7171
"Topic :: Utilities",
7272
]
7373
dependencies = [
74-
"transformers>=5.15,<5.20",
74+
"transformers>=5.15,<5.21",
7575
"mistral-common>=1.10.0",
7676
"peft>=0.20.0,<=0.21.0",
7777
"datasets>=2.20.0",

‎src/megatron/bridge/models/bailing/modeling_bailing_moe_v2.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,10 @@ def __init__(self, config: BailingMoeV2Config, layer_idx: Optional[int] = None):
482482
self.hidden_size = config.hidden_size
483483
self.num_heads = config.num_attention_heads
484484
self.head_dim = config.head_dim or self.hidden_size // self.num_heads
485-
partial_rotary_factor = config.partial_rotary_factor if hasattr(config, "partial_rotary_factor") else 1.0
485+
partial_rotary_factor = getattr(config, "partial_rotary_factor", None)
486+
if partial_rotary_factor is None:
487+
# Transformers 5.20+ keeps partial_rotary_factor only in rope_parameters.
488+
partial_rotary_factor = (getattr(config, "rope_parameters", None) or {}).get("partial_rotary_factor", 1.0)
486489
self.rope_dim = int(self.head_dim * partial_rotary_factor)
487490
self.num_key_value_heads = config.num_key_value_heads
488491
self.num_key_value_groups = self.num_heads // self.num_key_value_heads

‎src/megatron/bridge/models/conversion/model_bridge.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
)
8181
from megatron.bridge.models.conversion.quant_bridge import MegatronQuantizationBridge
8282
from megatron.bridge.models.conversion.transformers_compat import (
83+
partial_rotary_factor_from_hf,
8384
rope_theta_from_hf,
8485
)
8586
from megatron.bridge.models.conversion.utils import (
@@ -697,6 +698,12 @@ def _hf_config_to_megatron_kwargs(self, hf_config: PretrainedConfig) -> dict[str
697698
else:
698699
value = getattr(hf_config, hf_name, None)
699700
has_value = hasattr(hf_config, hf_name)
701+
if not has_value and hf_name == "partial_rotary_factor":
702+
# Transformers 5.20+ keeps partial_rotary_factor only in rope_parameters.
703+
try:
704+
value, has_value = partial_rotary_factor_from_hf(hf_config), True
705+
except ValueError:
706+
pass
700707
if (
701708
has_value
702709
and megatron_name not in provider_kwargs

‎src/megatron/bridge/models/conversion/transformers_compat.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,38 @@ def rope_theta_from_hf(config) -> float:
8787
)
8888

8989

90+
def partial_rotary_factor_from_hf(config) -> float:
91+
"""Extract partial_rotary_factor from a HuggingFace config.
92+
93+
Transformers 5.19 and earlier expose it as a ``partial_rotary_factor``
94+
attribute (and also in ``rope_parameters``). Transformers 5.20+ keeps it
95+
only in the flat ``rope_parameters`` dictionary.
96+
97+
Args:
98+
config: HuggingFace configuration object.
99+
100+
Returns:
101+
float: The fraction of each attention head dimension that gets rotary embeddings.
102+
103+
Raises:
104+
ValueError: If partial_rotary_factor is not found in either format.
105+
"""
106+
partial_rotary_factor = getattr(config, "partial_rotary_factor", None)
107+
if partial_rotary_factor is not None:
108+
return partial_rotary_factor
109+
110+
rope_parameters = getattr(config, "rope_parameters", None)
111+
if isinstance(rope_parameters, dict):
112+
partial_rotary_factor = rope_parameters.get("partial_rotary_factor")
113+
if partial_rotary_factor is not None:
114+
return partial_rotary_factor
115+
116+
raise ValueError(
117+
"partial_rotary_factor not found in config. Expected either a 'partial_rotary_factor' attribute "
118+
"(transformers <5.20) or 'rope_parameters[\"partial_rotary_factor\"]' (transformers >=5.20)."
119+
)
120+
121+
90122
def rope_local_base_freq_from_hf(config) -> float:
91123
"""Extract rope_local_base_freq from a HuggingFace config.
92124

‎src/megatron/bridge/models/glm_vl/glm_45v_bridge.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
QKVMapping,
2626
ReplicatedMapping,
2727
)
28-
from megatron.bridge.models.conversion.transformers_compat import rope_theta_from_hf
28+
from megatron.bridge.models.conversion.transformers_compat import partial_rotary_factor_from_hf, rope_theta_from_hf
2929
from megatron.bridge.models.glm_vl.glm_45v_provider import GLM45VModelProvider
3030
from megatron.bridge.models.glm_vl.modeling_glm_45v import GLM45VModel
3131
from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM
@@ -51,7 +51,7 @@ def provider_bridge(self, hf_pretrained: PreTrainedCausalLM) -> GLM45VModelProvi
5151
kv_channels=text_config.head_dim,
5252
hidden_size=text_config.hidden_size,
5353
rotary_base=rope_theta_from_hf(text_config),
54-
rotary_percent=text_config.partial_rotary_factor,
54+
rotary_percent=partial_rotary_factor_from_hf(text_config),
5555
init_method_std=text_config.initializer_range,
5656
ffn_hidden_size=text_config.intermediate_size,
5757
seq_length=text_config.max_position_embeddings,

‎src/megatron/bridge/models/nemotron_omni/hf_pretrained.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM
2222

2323

24+
# Bound the processor workaround to the releases whose loading behavior we tested.
25+
# Future releases use their own loader and surface errors rather than silently
26+
# dropping a required VLM artifact during conversion/export.
27+
_PROCESSOR_ADAPTER_RELEASES = ("5.18.0", "5.19.0", "5.20.0")
28+
29+
2430
class Nemotron35VLPretrained(PreTrainedCausalLM):
2531
"""Keep native processor compatibility separate from legacy remote-code loading."""
2632

@@ -38,23 +44,20 @@ def _get_auto_model_class(self) -> type:
3844
# uses it: native exports have no remote CausalLM registration.
3945
if self._uses_remote_model():
4046
return super()._get_auto_model_class()
41-
if Version(transformers.__version__) < Version("5.19.0"):
42-
raise ValueError("Native Super-VL requires Transformers >=5.19.0; use trust_remote_code=True otherwise.")
47+
if Version(transformers.__version__) < Version("5.18.0"):
48+
raise ValueError("Native Super-VL requires Transformers >=5.18.0; use trust_remote_code=True otherwise.")
4349
return AutoModelForImageTextToText
4450

4551
def _load_processor(self) -> ProcessorMixin | None:
4652
if self.model_name_or_path is None or self._uses_remote_model():
4753
return super()._load_processor()
4854

4955
version = Version(transformers.__version__)
50-
if version < Version("5.19.0"):
51-
raise ValueError("Native Super-VL requires Transformers >=5.19.0; use trust_remote_code=True otherwise.")
56+
if version < Version("5.18.0"):
57+
raise ValueError("Native Super-VL requires Transformers >=5.18.0; use trust_remote_code=True otherwise.")
5258

5359
processor_cls = AutoProcessor
54-
# Bound the workaround to the release whose loading behavior we tested.
55-
# Future releases use their own loader and surface errors rather than
56-
# silently dropping a required VLM artifact during conversion/export.
57-
if version.base_version == "5.19.0" and getattr(self.config, "audio_config", None) is None:
60+
if version.base_version in _PROCESSOR_ADAPTER_RELEASES and getattr(self.config, "audio_config", None) is None:
5861
from megatron.bridge.models.nemotron_omni.processing_super_vl import Nemotron35VLProcessor
5962

6063
processor_cls = Nemotron35VLProcessor

0 commit comments

Comments
 (0)