-
Notifications
You must be signed in to change notification settings - Fork 253
[build] chore: Upgrade transformers to 5.0 #2068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
8ccb17a
transformers upgrade
022fcef
transformers upgrade
588d3bb
fix rope theta and other misc issues
yaoyu-33 81ad475
fix glm 45 functional test
yaoyu-33 c84fee9
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 caf33c0
vlm fixes
yaoyu-33 fa5ee53
gemma fix
yaoyu-33 a6c5813
fix glm
yaoyu-33 f4c5c69
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 aa0536c
fix unit tests
yaoyu-33 fbb9b96
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 798bcbb
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 d6d9aac
Merge remote-tracking branch 'origin/chore/transformers_5p0' into cho…
yaoyu-33 77e5633
fix deepseek and gemma3
yaoyu-33 04060d9
glm fix
yaoyu-33 1ca98f9
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 34c55cc
update uv.lock
yaoyu-33 a9e73a5
fix nemotronh and qwen3moe
yaoyu-33 96c7291
fix vlm models
yaoyu-33 fcf858e
Merge branch 'chore/transformers_5p0' of github.com:NVIDIA-NeMo/Megat…
yaoyu-33 437a2ad
fix: handle transformers 5.0 rope_theta migration to rope_parameters
yaoyu-33 2414d37
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 5a99c0e
revert unwanted change
yaoyu-33 b5f6253
refactor: use rope compat functions as direct imports instead of stat…
yaoyu-33 2e6bbe8
fix: update tests for transformers 5.0 compatibility
yaoyu-33 5b9cea4
Merge remote-tracking branch 'origin/main' into chore/transformers_5p0
yaoyu-33 05852e1
fix: handle nested text_config in Qwen2.5 VL conversion test for tran…
yaoyu-33 ae12a45
fix: handle rope_parameters for transformers 5.0+ compatibility
yaoyu-33 432173f
fix: handle both pre-5.0 and 5.0+ HF expert weight layouts in Qwen3 V…
yaoyu-33 5ec9171
fix: update nemotron conversion tests for transformers 5.0 compatibility
yaoyu-33 494b4b1
Merge remote-tracking branch 'origin/main' into chore/transformers_5p0
yaoyu-33 c3c8be7
update uv.lock
d69cd23
Merge branch 'main' into chore/transformers_5p0
yaoyu-33 5388a3c
update uv lock
yaoyu-33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
184 changes: 184 additions & 0 deletions
184
src/megatron/bridge/models/conversion/transformers_compat.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Compatibility utilities for HuggingFace transformers 5.0+ configs.""" | ||
|
|
||
|
|
||
| def rope_theta_from_hf(config) -> float: | ||
| """Extract rope_theta from a HuggingFace config. | ||
|
|
||
| This utility method handles the extraction of rope_theta (rotary position | ||
| embedding base frequency) from HuggingFace configs, supporting both the | ||
| legacy format (direct rope_theta attribute) and the new transformers 5.0+ | ||
| format (rope_parameters dictionary). | ||
|
|
||
| Args: | ||
| config: HuggingFace configuration object. | ||
|
|
||
| Returns: | ||
| float: The rope_theta value for rotary embeddings. | ||
|
|
||
| Raises: | ||
| ValueError: If rope_theta is not found in either format. | ||
| """ | ||
| # Check for direct attribute (transformers <5.0) | ||
| if hasattr(config, "rope_theta"): | ||
| rope_theta = config.rope_theta | ||
| if rope_theta is not None: | ||
| return rope_theta | ||
|
|
||
| # Check rope_parameters (transformers >=5.0) | ||
| if hasattr(config, "rope_parameters") and config.rope_parameters: | ||
| # Flat structure: rope_parameters["rope_theta"] | ||
| if "rope_theta" in config.rope_parameters: | ||
| rope_theta = config.rope_parameters["rope_theta"] | ||
| if rope_theta is not None: | ||
| return rope_theta | ||
| # Nested structure for Gemma3 in transformers 5.0+: rope_parameters["global"]["base"] | ||
| if "global" in config.rope_parameters: | ||
| global_params = config.rope_parameters["global"] | ||
| if isinstance(global_params, dict) and "base" in global_params: | ||
| rope_theta = global_params["base"] | ||
| if rope_theta is not None: | ||
| return rope_theta | ||
| # Gemma3 transformers 5.0+ uses "full_attention" key with "rope_theta" | ||
| if "full_attention" in config.rope_parameters: | ||
| full_attn_params = config.rope_parameters["full_attention"] | ||
| if isinstance(full_attn_params, dict) and "rope_theta" in full_attn_params: | ||
| rope_theta = full_attn_params["rope_theta"] | ||
| if rope_theta is not None: | ||
| return rope_theta | ||
|
|
||
| # Fallback to default_theta (transformers 5.0+) | ||
| if hasattr(config, "default_theta") and config.default_theta: | ||
| # default_theta can be a plain float (e.g. NemotronH) or a dict (e.g. Gemma3) | ||
| if isinstance(config.default_theta, (int, float)): | ||
| return float(config.default_theta) | ||
| if isinstance(config.default_theta, dict) and "global" in config.default_theta: | ||
| rope_theta = config.default_theta["global"] | ||
| if rope_theta is not None: | ||
| return rope_theta | ||
|
|
||
| raise ValueError( | ||
| "rope_theta not found in config. Expected either 'rope_theta' attribute " | ||
| "(transformers <5.0), 'rope_parameters[\"rope_theta\"]', " | ||
| '\'rope_parameters["global"]["base"]\', \'rope_parameters["full_attention"]["rope_theta"]\', ' | ||
| "or 'default_theta[\"global\"]' (transformers >=5.0)." | ||
| ) | ||
|
|
||
|
|
||
| def rope_local_base_freq_from_hf(config) -> float: | ||
| """Extract rope_local_base_freq from a HuggingFace config. | ||
|
|
||
| Similar to rope_theta_from_hf but for the local base frequency parameter | ||
| used by some models (e.g., Gemma3). | ||
|
|
||
| Args: | ||
| config: HuggingFace configuration object. | ||
|
|
||
| Returns: | ||
| float: The rope_local_base_freq value. | ||
|
|
||
| Raises: | ||
| ValueError: If rope_local_base_freq is not found in either format. | ||
| """ | ||
| # Check for direct attribute (transformers <5.0) | ||
| if hasattr(config, "rope_local_base_freq"): | ||
| rope_local_base_freq = config.rope_local_base_freq | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
|
|
||
| # Check rope_parameters (transformers >=5.0) | ||
| if hasattr(config, "rope_parameters") and config.rope_parameters: | ||
| # Flat structure: rope_parameters["rope_local_base_freq"] | ||
| if "rope_local_base_freq" in config.rope_parameters: | ||
| rope_local_base_freq = config.rope_parameters["rope_local_base_freq"] | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
| # Nested structure for Gemma3 in transformers 5.0+: rope_parameters["local"]["base"] | ||
| if "local" in config.rope_parameters: | ||
| local_params = config.rope_parameters["local"] | ||
| if isinstance(local_params, dict) and "base" in local_params: | ||
| rope_local_base_freq = local_params["base"] | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
| # Gemma3 transformers 5.0+ uses "sliding_attention" key with "rope_theta" | ||
| if "sliding_attention" in config.rope_parameters: | ||
| sliding_attn_params = config.rope_parameters["sliding_attention"] | ||
| if isinstance(sliding_attn_params, dict) and "rope_theta" in sliding_attn_params: | ||
| rope_local_base_freq = sliding_attn_params["rope_theta"] | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
|
|
||
| # Check rope_scaling as a fallback | ||
| if hasattr(config, "rope_scaling") and config.rope_scaling: | ||
| if "rope_local_base_freq" in config.rope_scaling: | ||
| rope_local_base_freq = config.rope_scaling["rope_local_base_freq"] | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
|
|
||
| # Fallback to default_theta (transformers 5.0+) | ||
| if hasattr(config, "default_theta") and config.default_theta: | ||
| if isinstance(config.default_theta, dict) and "local" in config.default_theta: | ||
| rope_local_base_freq = config.default_theta["local"] | ||
| if rope_local_base_freq is not None: | ||
| return rope_local_base_freq | ||
|
|
||
| raise ValueError( | ||
| "rope_local_base_freq not found in config. Expected either 'rope_local_base_freq' attribute " | ||
| "(transformers <5.0), 'rope_parameters[\"rope_local_base_freq\"]', " | ||
| '\'rope_parameters["local"]["base"]\', \'rope_parameters["sliding_attention"]["rope_theta"]\', ' | ||
| "'rope_scaling[\"rope_local_base_freq\"]', or 'default_theta[\"local\"]' (transformers >=5.0)." | ||
| ) | ||
|
|
||
|
|
||
| def rope_scaling_factor_from_hf(config, default: float = 1.0) -> float: | ||
| """Extract rope scaling factor from a HuggingFace config. | ||
|
|
||
| This utility method handles the extraction of the rope scaling factor from | ||
| HuggingFace configs, supporting both the legacy format (rope_scaling dict) | ||
| and the new transformers 5.0+ format (rope_parameters dictionary). | ||
|
|
||
| Args: | ||
| config: HuggingFace configuration object. | ||
| default: Default value to return if no scaling factor is found. | ||
|
|
||
| Returns: | ||
| float: The rope scaling factor value, or default if not found. | ||
| """ | ||
| # Check rope_scaling (transformers <5.0 and some 5.0+ models) | ||
| if hasattr(config, "rope_scaling") and config.rope_scaling: | ||
| if isinstance(config.rope_scaling, dict) and "factor" in config.rope_scaling: | ||
| factor = config.rope_scaling["factor"] | ||
| if factor is not None: | ||
| return factor | ||
|
|
||
| # Check rope_parameters (transformers >=5.0) | ||
| if hasattr(config, "rope_parameters") and config.rope_parameters: | ||
| # Check for nested structure with layer types (Gemma3 style) | ||
| for layer_type in ["full_attention", "global"]: | ||
| if layer_type in config.rope_parameters: | ||
| layer_params = config.rope_parameters[layer_type] | ||
| if isinstance(layer_params, dict) and "factor" in layer_params: | ||
| factor = layer_params["factor"] | ||
| if factor is not None: | ||
| return factor | ||
| # Check flat structure | ||
| if "factor" in config.rope_parameters: | ||
| factor = config.rope_parameters["factor"] | ||
| if factor is not None: | ||
| return factor | ||
|
|
||
| # Return default if no scaling factor found | ||
| return default |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.