fix: remove manual rope parameters injection in PretrainedConfig (#23910)
This commit is contained in:
@@ -136,41 +136,18 @@ def _ensure_gguf_version():
|
|||||||
|
|
||||||
|
|
||||||
def _patch_rope_parameters_validation():
|
def _patch_rope_parameters_validation():
|
||||||
"""Fix rope_parameters validation for unregistered model types.
|
"""Guard ``standardize_rope_params()`` against missing
|
||||||
|
``max_position_embeddings``.
|
||||||
|
|
||||||
For unregistered model types (e.g. ``deepseek_v32``), the generic
|
For ``PretrainedConfig``, ``standardize_rope_params()`` accesses
|
||||||
``PretrainedConfig`` lacks a ``rope_parameters`` field so the conversion
|
|
||||||
that injects ``rope_theta`` from the top-level config is skipped.
|
|
||||||
Additionally, ``standardize_rope_params()`` accesses
|
|
||||||
``self.max_position_embeddings`` during ``__post_init__`` before extra
|
``self.max_position_embeddings`` during ``__post_init__`` before extra
|
||||||
kwargs are set as attributes, causing ``AttributeError``.
|
kwargs are set as attributes, causing ``AttributeError``.
|
||||||
|
|
||||||
Fix: (1) patch ``from_dict`` to inject ``rope_theta`` into
|
Fix: guard ``standardize_rope_params`` against missing
|
||||||
``rope_scaling``, (2) guard ``standardize_rope_params`` against missing
|
|
||||||
``max_position_embeddings``.
|
``max_position_embeddings``.
|
||||||
|
|
||||||
TODO(upstream): remove once unregistered model types handle rope
|
|
||||||
standardization correctly in transformers.
|
|
||||||
"""
|
"""
|
||||||
from transformers import PretrainedConfig
|
from transformers import PretrainedConfig
|
||||||
|
|
||||||
original = PretrainedConfig.from_dict.__func__
|
|
||||||
|
|
||||||
@classmethod # type: ignore[misc]
|
|
||||||
def patched(cls, config_dict, **kwargs):
|
|
||||||
rope_scaling = config_dict.get("rope_scaling")
|
|
||||||
rope_theta = config_dict.get("rope_theta")
|
|
||||||
if (
|
|
||||||
isinstance(rope_scaling, dict)
|
|
||||||
and rope_theta is not None
|
|
||||||
and "rope_theta" not in rope_scaling
|
|
||||||
):
|
|
||||||
config_dict = config_dict.copy()
|
|
||||||
config_dict["rope_scaling"] = {**rope_scaling, "rope_theta": rope_theta}
|
|
||||||
return original(cls, config_dict, **kwargs)
|
|
||||||
|
|
||||||
PretrainedConfig.from_dict = patched
|
|
||||||
|
|
||||||
# standardize_rope_params accesses self.max_position_embeddings before
|
# standardize_rope_params accesses self.max_position_embeddings before
|
||||||
# __post_init__ sets extra kwargs — skip when the attribute is absent.
|
# __post_init__ sets extra kwargs — skip when the attribute is absent.
|
||||||
if hasattr(PretrainedConfig, "standardize_rope_params"):
|
if hasattr(PretrainedConfig, "standardize_rope_params"):
|
||||||
|
|||||||
@@ -480,6 +480,15 @@ class TestPatchRemovedSymbols(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class TestPatchRopeParametersValidation(unittest.TestCase):
|
class TestPatchRopeParametersValidation(unittest.TestCase):
|
||||||
|
# -----------------------------------------------------------------------
|
||||||
|
# Test ``rope_theta`` injection into ``rope_scaling``.
|
||||||
|
#
|
||||||
|
# Upstream `transformers.PretrainedConfig` now natively handles this
|
||||||
|
# logic. While the manual injection patch has been removed, these
|
||||||
|
# test cases are retained to ensure regression testing of the
|
||||||
|
# configuration's injection behavior.
|
||||||
|
# -----------------------------------------------------------------------
|
||||||
|
|
||||||
def test_injects_rope_theta_into_rope_scaling(self):
|
def test_injects_rope_theta_into_rope_scaling(self):
|
||||||
config_dict = {
|
config_dict = {
|
||||||
"model_type": "llama",
|
"model_type": "llama",
|
||||||
|
|||||||
Reference in New Issue
Block a user