Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Recompute remote code's non-persistent buffers that transformers 5 leaves uninitialised (#11609)
Daniel Hanc78f51c5549a
diff --git a/tests/test_remote_code_non_persistent_buffers.py b/tests/test_remote_code_non_persistent_buffers.pynew file mode 100644index 00000000000..e4ae270876a--- /dev/null+++ b/tests/test_remote_code_non_persistent_buffers.py@@ -0,0 +1,650 @@+# SPDX-License-Identifier: AGPL-3.0-only+"""transformers 5 builds a model on the meta device and gives each non-persistent buffer+empty storage for `_init_weights` to fill. Remote code written for 4.x computes those+buffers (RoPE inv_freq, lightning-attention slopes) in `__init__` and its `_init_weights`+only touches Linear / Embedding, so Ling-2.6-flash loaded with zero RoPE frequencies and+zero decay slopes: first-batch loss 5.03 in 16-bit (11.64 in 4-bit, where the storage+held garbage) against 1.12 for the same model on transformers 4.57.6."""++import importlib.util+import math+import os+import sys+import types++import pytest+import torch+import torch.nn as nn++_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+++def _load_helper():+ path = os.path.join(_ROOT, "unsloth", "models", "_remote_code_buffers.py")+ spec = importlib.util.spec_from_file_location("_unsloth_remote_code_buffers_under_test", path)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def _slopes(n):+ start = 2 ** (-(2 ** -(math.log2(n) - 3)))+ return torch.tensor([start * start**i for i in range(n)], dtype = torch.float)+++def _remote_module():+ """Classes shaped like 4.x remote code, living under transformers_modules.*"""+ from transformers import PretrainedConfig, PreTrainedModel++ name = "transformers_modules.unsloth_test_remote_buffers"+ if name in sys.modules:+ return sys.modules[name]+ module = types.ModuleType(name)+ sys.modules.setdefault("transformers_modules", types.ModuleType("transformers_modules"))+ sys.modules[name] = module++ class TinyRemoteConfig(PretrainedConfig):+ model_type = "unsloth_tiny_remote_buffers"++ def __init__(+ self,+ hidden_size = 16,+ num_heads = 4,+ num_layers = 2,+ rope_theta = 10000.0,+ **kwargs,+ ):+ self.hidden_size = hidden_size+ self.num_heads = num_heads+ self.num_layers = num_layers+ self.rope_theta = rope_theta+ super().__init__(**kwargs)++ class TinyRotary(nn.Module):+ def __init__(+ self,+ config,+ device = None,+ ):+ super().__init__()+ dim = config.hidden_size // config.num_heads+ inv_freq = 1.0 / (+ config.rope_theta ** (torch.arange(0, dim, 2, dtype = torch.float) / dim)+ )+ self.config = config+ self.register_buffer("inv_freq", inv_freq, persistent = False)+ self.original_inv_freq = self.inv_freq++ class TinyLinearAttention(nn.Module):+ def __init__(self, config, layer_idx):+ super().__init__()+ self.config = config+ self.layer_idx = layer_idx+ self.proj = nn.Linear(config.hidden_size, config.hidden_size, bias = False)+ slope = -_slopes(config.num_heads) * (+ 1 - (layer_idx - 1) / (config.num_layers - 1) + 1e-5+ )+ self.register_buffer("slope", slope, persistent = False)+ self.rotary_emb = TinyRotary(config)++ class TinyRemotePreTrainedModel(PreTrainedModel):+ config_class = TinyRemoteConfig+ base_model_prefix = "model"++ def _init_weights(self, module):+ # What 4.x remote code ships: weights only, buffers assumed built in __init__.+ if isinstance(module, nn.Linear):+ module.weight.data.normal_(mean = 0.0, std = 0.02)++ class TinyRemoteModel(TinyRemotePreTrainedModel):+ def __init__(self, config):+ super().__init__(config)+ self.layers = nn.ModuleList(+ TinyLinearAttention(config, i) for i in range(config.num_layers)+ )+ self.post_init()++ for cls in (+ TinyRemoteConfig,+ TinyRotary,+ TinyLinearAttention,+ TinyRemotePreTrainedModel,+ TinyRemoteModel,+ ):+ cls.__module__ = name+ setattr(module, cls.__name__, cls)+ return module+++def _expected(layer_idx, config):+ dim = config.hidden_size // config.num_heads+ inv_freq = 1.0 / (config.rope_theta ** (torch.arange(0, dim, 2, dtype = torch.float) / dim))+ slope = -_slopes(config.num_heads) * (1 - (layer_idx - 1) / (config.num_layers - 1) + 1e-5)+ return slope, inv_freq+++def _saved_model(tmp_path):+ remote = _remote_module()+ config = remote.TinyRemoteConfig()+ remote.TinyRemoteModel(config).save_pretrained(tmp_path)+ return remote, config+++def test_restores_buffers_after_a_transformers_load(tmp_path):+ helper = _load_helper()+ remote, config = _saved_model(tmp_path)+ model = remote.TinyRemoteModel.from_pretrained(tmp_path)+ restored = helper.restore_remote_code_non_persistent_buffers(model)+ if helper._transformers_builds_on_meta():+ assert restored == 2 * config.num_layers+ else:+ assert restored == 0+ for i, layer in enumerate(model.layers):+ slope, inv_freq = _expected(i, config)+ torch.testing.assert_close(layer.slope, slope)+ torch.testing.assert_close(layer.rotary_emb.inv_freq, inv_freq)+ # The alias 4.x code keeps next to the buffer points at the live buffer again.+ assert layer.rotary_emb.original_inv_freq is layer.rotary_emb.inv_freq+++def test_meta_built_buffers_with_empty_storage_are_recomputed():+ # The transformers 5 sequence without a checkpoint: construct on meta, give the+ # non-persistent buffers empty storage, leave them for `_init_weights`.+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("transformers 4.x builds real buffers; the restore is a no-op there")+ remote = _remote_module()+ config = remote.TinyRemoteConfig()+ with torch.device("meta"):+ model = remote.TinyRemoteModel(config)+ for module in model.modules():+ for name in module._non_persistent_buffers_set:+ module._buffers[name] = torch.full(module._buffers[name].shape, 7.0)+ assert helper.restore_remote_code_non_persistent_buffers(model) == 2 * config.num_layers+ for i, layer in enumerate(model.layers):+ slope, inv_freq = _expected(i, config)+ torch.testing.assert_close(layer.slope, slope)+ torch.testing.assert_close(layer.rotary_emb.inv_freq, inv_freq)+++def test_native_modules_and_unrecoverable_constructors_are_left_alone():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Native(nn.Module): # not remote code: transformers' own _init_weights owns its buffers+ def __init__(self):+ super().__init__()+ self.register_buffer("b", torch.full((2,), 3.0), persistent = False)++ native = Native()+ native.b.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(native) == 0+ assert native.b.eq(0).all()++ class NeedsTensor(nn.Module):+ def __init__(self, table):+ super().__init__()+ self.register_buffer("b", table * 2, persistent = False)++ NeedsTensor.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = NeedsTensor(torch.ones(2))+ module.b.zero_()+ # `table` is not recoverable from the instance, so the module is skipped, not guessed.+ assert helper._constructor_kwargs(module) is None+ assert helper.restore_remote_code_non_persistent_buffers(module) == 0++ class KeepsOnlyStride(nn.Module):+ def __init__(+ self,+ ratio = 2,+ device = None,+ ):+ super().__init__()+ self.stride = ratio # `ratio` itself is not kept under its own name+ self.register_buffer("b", torch.full((2,), float(ratio)), persistent = False)++ KeepsOnlyStride.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = KeepsOnlyStride(ratio = 4)+ module.b.zero_()+ # A non-default `ratio` cannot be told from the default, so nothing is rebuilt.+ assert helper._constructor_kwargs(module) is None+ assert helper.restore_remote_code_non_persistent_buffers(module) == 0+ assert module.b.eq(0).all()+++def test_stored_tensor_arguments_skip_the_module():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class OptionalTable(nn.Module):+ def __init__(self, table = None):+ super().__init__()+ self.table = table+ base = torch.ones(2) if table is None else table+ self.register_buffer("b", base * 2, persistent = False)++ OptionalTable.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = OptionalTable(torch.full((2,), 5.0))+ module.b.zero_()+ # Rebuilding with table=None would write 2.0 instead of 10.0, so the module is skipped.+ assert helper._constructor_kwargs(module) is None+ assert helper.restore_remote_code_non_persistent_buffers(module) == 0+ assert module.b.eq(0).all()+++def test_a_stored_meta_device_is_not_passed_back():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class StoresDevice(nn.Module):+ def __init__(+ self,+ scale = 3.0,+ device = None,+ ):+ super().__init__()+ self.scale = scale+ self.device = device+ self.register_buffer("b", torch.full((2,), scale, device = device), persistent = False)++ StoresDevice.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = StoresDevice(scale = 4.0, device = torch.device("meta"))+ module._buffers["b"] = torch.zeros(2)+ assert helper.restore_remote_code_non_persistent_buffers(module) == 1+ torch.testing.assert_close(module.b, torch.full((2,), 4.0))+++def test_variadic_constructors_are_skipped():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class TakesKwargs(nn.Module):+ def __init__(self, **kwargs):+ super().__init__()+ base = kwargs.get("base", 1.0)+ self.register_buffer("b", torch.full((2,), float(base)), persistent = False)++ TakesKwargs.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = TakesKwargs(base = 6.0)+ module.b.zero_()+ # Rebuilding without `base` would write 1.0 instead of 6.0, so the module is skipped.+ assert helper._constructor_kwargs(module) is None+ assert helper.restore_remote_code_non_persistent_buffers(module) == 0+ assert module.b.eq(0).all()+++def test_dtype_is_recovered_from_the_instance_or_the_module_is_skipped():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class EpsFromDtype(nn.Module):+ def __init__(+ self,+ dtype = torch.float32,+ keep = True,+ ):+ super().__init__()+ self.keep = keep+ if keep:+ self.dtype = dtype+ eps = torch.finfo(dtype).eps+ self.register_buffer("b", torch.full((2,), eps, dtype = torch.float32), persistent = False)++ EpsFromDtype.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ module = EpsFromDtype(dtype = torch.float16)+ module.b.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(module) == 1+ torch.testing.assert_close(module.b, torch.full((2,), torch.finfo(torch.float16).eps))++ module = EpsFromDtype(dtype = torch.float16, keep = False)+ module.b.zero_()+ assert helper._constructor_kwargs(module) is None+ assert helper.restore_remote_code_non_persistent_buffers(module) == 0+++def test_equal_but_differently_typed_arguments_do_not_share_a_rebuild():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class TypeSensitive(nn.Module):+ def __init__(self, flag = False):+ super().__init__()+ self.flag = flag+ value = 2.0 if isinstance(flag, bool) else 3.0+ self.register_buffer("b", torch.full((2,), value), persistent = False)++ TypeSensitive.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ parent = nn.Module()+ parent.first, parent.second = TypeSensitive(flag = True), TypeSensitive(flag = 1)+ parent.first.b.zero_()+ parent.second.b.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(parent) == 2+ torch.testing.assert_close(parent.first.b, torch.full((2,), 2.0))+ torch.testing.assert_close(parent.second.b, torch.full((2,), 3.0))+++def test_buffers_the_remote_init_weights_fills_are_left_alone():+ # A remote model whose own _init_weights fills a placeholder buffer already has the right+ # value; the constructor would only give the placeholder back.+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Placeholder(nn.Module):+ def __init__(self):+ super().__init__()+ self.register_buffer("table", torch.zeros(2), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.block = Placeholder()++ def _init_weights(self, module):+ if isinstance(module, Placeholder):+ module.table.fill_(5.0)++ for cls in (Placeholder, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model._init_weights(model.block)+ assert helper.restore_remote_code_non_persistent_buffers(model) == 0+ torch.testing.assert_close(model.block.table, torch.full((2,), 5.0))+++def test_a_module_that_did_not_keep_its_config_is_skipped():+ # In a composite model a child may have been built with text_config or vision_config;+ # rebuilding it from the root config could give different same-shaped buffers.+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class ScaleFromConfig(nn.Module):+ def __init__(self, config):+ super().__init__()+ self.register_buffer("b", torch.full((2,), float(config.scale)), persistent = False)++ ScaleFromConfig.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = nn.Module()+ model.config = types.SimpleNamespace(scale = 1.0)+ model.child = ScaleFromConfig(types.SimpleNamespace(scale = 9.0))+ model.child.b.zero_()+ assert helper._constructor_kwargs(model.child) is None+ assert helper.restore_remote_code_non_persistent_buffers(model) == 0+ assert model.child.b.eq(0).all()+++def test_remote_init_detection_is_scoped_to_the_class_it_names():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Placeholder(nn.Module):+ def __init__(self):+ super().__init__()+ self.register_buffer("table", torch.zeros(2), persistent = False)++ class ComputesTable(nn.Module):+ def __init__(self, base = 4.0):+ super().__init__()+ self.base = base+ self.register_buffer("table", torch.full((2,), base), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.block = Placeholder()+ self.other = ComputesTable()++ def _init_weights(self, module):+ if isinstance(module, Placeholder):+ module.table.fill_(5.0)++ for cls in (Placeholder, ComputesTable, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model._init_weights(model.block)+ model.other.table.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ torch.testing.assert_close(model.block.table, torch.full((2,), 5.0))+ torch.testing.assert_close(model.other.table, torch.full((2,), 4.0))+++def test_prose_in_remote_init_weights_does_not_count_as_initialisation():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Rotary(nn.Module):+ def __init__(self, base = 4.0):+ super().__init__()+ self.base = base+ self.register_buffer("inv_freq", torch.full((2,), base), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.rotary = Rotary()++ def _init_weights(self, module):+ """Rotary.inv_freq is built in the constructor, nothing to do here."""+ # Rotary keeps inv_freq from __init__.+ if isinstance(module, nn.Linear):+ module.weight.data.normal_()++ for cls in (Rotary, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model.rotary.inv_freq.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ torch.testing.assert_close(model.rotary.inv_freq, torch.full((2,), 4.0))+++def test_a_buffer_name_written_for_another_class_does_not_skip_this_one():+ # _init_weights fills `inv_freq` only in another class's branch; this class's `inv_freq`+ # still comes from its constructor.+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Rotary(nn.Module):+ def __init__(self, base = 4.0):+ super().__init__()+ self.base = base+ self.register_buffer("inv_freq", torch.full((2,), base), persistent = False)++ class OtherRotary(nn.Module):+ def __init__(self):+ super().__init__()+ self.register_buffer("inv_freq", torch.zeros(2), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.rotary = Rotary()+ self.other = OtherRotary()++ def _init_weights(self, module):+ if isinstance(module, OtherRotary):+ module.inv_freq.fill_(5.0)++ for cls in (Rotary, OtherRotary, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model._init_weights(model.other)+ model.rotary.inv_freq.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ torch.testing.assert_close(model.rotary.inv_freq, torch.full((2,), 4.0))+ torch.testing.assert_close(model.other.inv_freq, torch.full((2,), 5.0))+++def test_buffers_filled_through_a_helper_are_left_alone():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Placeholder(nn.Module):+ def __init__(self):+ super().__init__()+ self.register_buffer("table", torch.zeros(2), persistent = False)++ def initialize_placeholder(module):+ module.table.fill_(5.0)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.block = Placeholder()++ def _init_weights(self, module):+ if isinstance(module, Placeholder):+ initialize_placeholder(module)++ for cls in (Placeholder, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model._init_weights(model.block)+ assert helper.restore_remote_code_non_persistent_buffers(model) == 0+ torch.testing.assert_close(model.block.table, torch.full((2,), 5.0))+++def test_integer_and_bool_buffers_the_remote_init_weights_fills_are_left_alone():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Tables(nn.Module):+ def __init__(self):+ super().__init__()+ self.register_buffer("index", torch.zeros(3, dtype = torch.long), persistent = False)+ self.register_buffer("mask", torch.zeros(3, dtype = torch.bool), persistent = False)+ self.register_buffer("steps", torch.arange(3), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.block = Tables()++ def _init_weights(self, module):+ if isinstance(module, Tables):+ module.index.copy_(torch.tensor([2, 0, 1]))+ module.mask.fill_(True)++ for cls in (Tables, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model._init_weights(model.block)+ model.block.steps.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ assert model.block.index.tolist() == [2, 0, 1]+ assert model.block.mask.all()+ assert model.block.steps.tolist() == [0, 1, 2]+++def test_a_module_whose_init_weights_raises_is_skipped():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class Rotary(nn.Module):+ def __init__(self, base = 4.0):+ super().__init__()+ self.base = base+ self.register_buffer("inv_freq", torch.full((2,), base), persistent = False)++ class RemoteModel(nn.Module):+ def __init__(self):+ super().__init__()+ self.rotary = Rotary()++ def _init_weights(self, module):+ if isinstance(module, Rotary):+ raise RuntimeError("needs state the probe does not have")++ for cls in (Rotary, RemoteModel):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = RemoteModel()+ model.rotary.inv_freq.fill_(7.0)+ assert helper.restore_remote_code_non_persistent_buffers(model) == 0+ torch.testing.assert_close(model.rotary.inv_freq, torch.full((2,), 7.0))+++def test_float64_loads_rebuild_under_float64():+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")++ class EpsOfDefaultDtype(nn.Module):+ def __init__(self, device = None):+ super().__init__()+ eps = torch.finfo(torch.get_default_dtype()).eps+ self.register_buffer("b", torch.full((2,), eps, dtype = torch.float64), persistent = False)++ EpsOfDefaultDtype.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ model = nn.Module()+ model.dtype = torch.float64+ model.child = EpsOfDefaultDtype()+ model.child.b.zero_()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ torch.testing.assert_close(+ model.child.b, torch.full((2,), torch.finfo(torch.float64).eps, dtype = torch.float64)+ )+++def test_loaders_restore_right_after_from_pretrained():+ for relative, calls in (("unsloth/models/vision.py", 1), ("unsloth/models/llama.py", 2)):+ with open(os.path.join(_ROOT, relative), encoding = "utf-8") as file:+ source = file.read()+ assert source.count("restore_remote_code_non_persistent_buffers(model)") == calls, relative+++def test_each_sub_model_is_probed_with_its_own_init_weights():+ # transformers 5 runs the nearest PreTrainedModel's _init_weights on a module, so the+ # outer model's init filling a rotary does not mean the inner model's rotary was filled.+ helper = _load_helper()+ if not helper._transformers_builds_on_meta():+ pytest.skip("no-op on transformers 4.x")+ from transformers import PreTrainedModel++ remote = _remote_module()++ class Inner(PreTrainedModel):+ config_class = remote.TinyRemoteConfig++ def __init__(self, config):+ super().__init__(config)+ self.proj = nn.Linear(config.hidden_size, config.hidden_size, bias = False)+ self.rotary_emb = remote.TinyRotary(config)++ def _init_weights(self, module):+ if isinstance(module, nn.Linear):+ module.weight.data.normal_()++ class Outer(PreTrainedModel):+ config_class = remote.TinyRemoteConfig++ def __init__(self, config):+ super().__init__(config)+ self.inner = Inner(config)+ self.rotary_emb = remote.TinyRotary(config)++ def _init_weights(self, module):+ if isinstance(module, remote.TinyRotary):+ module.inv_freq.fill_(5.0)++ for cls in (Inner, Outer):+ cls.__module__ = "transformers_modules.unsloth_test_remote_buffers"+ config = remote.TinyRemoteConfig()+ model = Outer(config)+ for rotary in (model.inner.rotary_emb, model.rotary_emb):+ rotary._buffers["inv_freq"] = torch.zeros_like(rotary.inv_freq)+ model.initialize_weights()+ assert helper.restore_remote_code_non_persistent_buffers(model) == 1+ torch.testing.assert_close(model.inner.rotary_emb.inv_freq, _expected(0, config)[1])+ torch.testing.assert_close(model.rotary_emb.inv_freq, torch.full((2,), 5.0))diff --git a/unsloth/models/_remote_code_buffers.py b/unsloth/models/_remote_code_buffers.pynew file mode 100644index 00000000000..1b668f4e965--- /dev/null+++ b/unsloth/models/_remote_code_buffers.py@@ -0,0 +1,222 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.+# This program is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <https://www.gnu.org/licenses/>.++"""transformers 5 gives non-persistent buffers `torch.empty_like` storage and leaves them to+`_init_weights`; 4.x-era remote code computes them in `__init__` (RoPE inv_freq, decay slopes),+so they load as garbage. Rebuild each such module on CPU with meta parameters and copy its+buffers into the loaded ones in place, keeping device placement and dispatch hooks."""++import inspect++import torch++__all__ = ["restore_remote_code_non_persistent_buffers"]++_SCALAR_TYPES = (bool, int, float, str, type(None))+# Arguments that only choose where buffers are built, not their values.+_PLACEMENT_ARGUMENTS = ("device",)+++def _transformers_builds_on_meta():+ try:+ import transformers+ from packaging.version import Version+ return Version(Version(transformers.__version__).base_version).major >= 5+ except Exception:+ return False+++def _is_remote_code_module(module):+ return type(module).__module__.startswith("transformers_modules")+++def _constructor_kwargs(module):+ """Arguments to rebuild ``module`` from its own attributes, or None when a required+ one cannot be recovered."""+ try:+ signature = inspect.signature(type(module).__init__)+ except (TypeError, ValueError):+ return None+ kwargs = {}+ for name, parameter in list(signature.parameters.items())[1:]:+ if parameter.kind in (parameter.VAR_POSITIONAL, parameter.VAR_KEYWORD):+ # Whatever went through *args / **kwargs cannot be recovered from the instance.+ return None+ if name == "config":+ # A composite child may have been built with a sub-config: trust only a kept one.+ value = module.__dict__.get("config", None)+ if value is None:+ return None+ elif name in _PLACEMENT_ARGUMENTS:+ # A stored `self.device` can be the meta device transformers built on.+ value = None if parameter.default is inspect.Parameter.empty else parameter.default+ elif name in module.__dict__:+ value = module.__dict__[name]+ else:+ # Guessing the default for an unkept argument could build plausible but wrong buffers.+ return None+ if value is inspect.Parameter.empty:+ return None+ if isinstance(value, (torch.Tensor, torch.nn.Module)):+ # The buffers may be derived from it and it cannot be reproduced safely.+ return None+ kwargs[name] = value+ return kwargs+++def _modules_with_init_weights(model):+ """Each module of ``model`` once, with the ``_init_weights`` transformers 5 runs on it:+ that of the nearest enclosing PreTrainedModel, as `initialize_weights` dispatches it."""+ try:+ from transformers import PreTrainedModel+ except Exception:+ PreTrainedModel = ()+ seen = set()+ stack = [(model, getattr(model, "_init_weights", None))]+ while stack:+ module, init_weights = stack.pop()+ if isinstance(module, PreTrainedModel):+ init_weights = getattr(module, "_init_weights", None)+ if id(module) in seen:+ continue+ seen.add(id(module))+ yield module, init_weights+ stack.extend((child, init_weights) for child in reversed(list(module.children())))+++def _cache_key(module, kwargs, init_weights):+ parts = []+ for name in sorted(kwargs):+ value = kwargs[name]+ # Typed repr: True and 1, or 0.0 and -0.0, compare equal but may build different buffers.+ if isinstance(value, _SCALAR_TYPES):+ parts.append((name, type(value), repr(value)))+ else:+ parts.append((name, id(value)))+ # The probe result depends on which `_init_weights` runs, so sub-models do not share it.+ owner = getattr(init_weights, "__self__", init_weights)+ return type(module), tuple(parts), id(owner)+++def _written_by_init_weights(fresh, buffers, init_weights):+ """Buffers ``init_weights`` writes on ``fresh``, probed with sentinels (NaN for floats, 0+ then 1 otherwise); None when it raises, since its live writes then cannot be told apart."""+ if init_weights is None or not buffers:+ return set()+ others = [name for name, buffer in buffers.items() if not buffer.is_floating_point()]+ written = set()+ with torch.no_grad():+ for sentinel in (0, 1) if others else (0,):+ for name, buffer in buffers.items():+ fill = float("nan") if buffer.is_floating_point() else sentinel+ fresh._buffers[name] = torch.full_like(buffer, fill)+ try:+ init_weights(fresh)+ except Exception:+ return None+ for name, buffer in buffers.items():+ after = fresh._buffers.get(name, None)+ if after is None or after.is_meta:+ written.add(name)+ elif buffer.is_floating_point():+ if not torch.isnan(after).all():+ written.add(name)+ elif not after.eq(sentinel).all():+ written.add(name)+ return written+++def _fresh_non_persistent_buffers(+ module,+ kwargs,+ dtype,+ init_weights = None,+):+ """Rebuild ``module`` on CPU with meta parameters; return its non-persistent buffers,+ the attributes aliasing them, and the ones ``init_weights`` fills itself."""+ from accelerate import init_empty_weights++ previous_dtype = torch.get_default_dtype()+ try:+ # transformers constructs under the load dtype as the default dtype.+ if dtype in (torch.float16, torch.bfloat16, torch.float32, torch.float64):+ torch.set_default_dtype(dtype)+ with torch.device("cpu"), init_empty_weights(include_buffers = False):+ fresh = type(module)(**kwargs)+ finally:+ torch.set_default_dtype(previous_dtype)+ buffers = {}+ for name in getattr(fresh, "_non_persistent_buffers_set", ()):+ buffer = fresh._buffers.get(name, None)+ if buffer is not None and not buffer.is_meta:+ buffers[name] = buffer.clone()+ aliases = {}+ for attribute, value in fresh.__dict__.items():+ if not isinstance(value, torch.Tensor) or attribute in ("_buffers", "_parameters"):+ continue+ for name in buffers:+ if value is fresh._buffers.get(name, None):+ aliases[attribute] = name+ # After the aliases are read: the probe below replaces fresh's buffers.+ written = _written_by_init_weights(fresh, buffers, init_weights)+ return buffers, aliases, written+++def restore_remote_code_non_persistent_buffers(model):+ """Recompute the non-persistent buffers of remote-code modules that transformers 5+ left uninitialised. Returns the number of buffers restored; a no-op on 4.x, where+ the model is built with real buffers."""+ if model is None or not _transformers_builds_on_meta():+ return 0+ dtype = getattr(model, "dtype", None)+ cache = {}+ restored = 0+ # transformers 5 runs the nearest PreTrainedModel's `_init_weights` on every module after+ # building on meta; a buffer it writes already holds the right value.+ for module, init_weights in _modules_with_init_weights(model):+ own = getattr(module, "_non_persistent_buffers_set", None)+ if not own or not _is_remote_code_module(module):+ continue+ kwargs = _constructor_kwargs(module)+ if kwargs is None:+ continue+ key = _cache_key(module, kwargs, init_weights)+ if key not in cache:+ try:+ cache[key] = _fresh_non_persistent_buffers(module, kwargs, dtype, init_weights)+ except Exception:+ cache[key] = None+ if cache[key] is None:+ continue+ buffers, aliases, written = cache[key]+ if written is None:+ continue+ for name, fresh in buffers.items():+ if name in written:+ continue+ live = module._buffers.get(name, None)+ if live is None or live.is_meta or live.shape != fresh.shape:+ continue+ with torch.no_grad():+ live.copy_(fresh.to(dtype = live.dtype))+ restored += 1+ for attribute, name in aliases.items():+ live = module._buffers.get(name, None)+ if live is not None and not live.is_meta:+ module.__dict__[attribute] = live+ if restored:+ print(+ f"Unsloth: Recomputed {restored} non-persistent buffers (RoPE frequencies and similar) "+ "that transformers 5 does not initialise for remote code."+ )+ return restoreddiff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 94f2cfa177a..e9f2dbba3a0 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -2754,7 +2754,12 @@ class FastLlamaModel:# owns the weights, which it never does here: vLLM has no classification head, so this# branch loaded the weights in-process even though the caller asked for fast_inference.from unsloth.models.vision import _attach_bnb_multidevice_hooks+ from unsloth.models._remote_code_buffers import (+ restore_remote_code_non_persistent_buffers,+ )+ # transformers 5 leaves remote code's non-persistent buffers (RoPE inv_freq) uninitialised.+ restore_remote_code_non_persistent_buffers(model)_attach_bnb_multidevice_hooks(model,load_in_4bit = load_in_4bit,@@ -2802,7 +2807,12 @@ class FastLlamaModel:**kwargs,)from unsloth.models.vision import _attach_bnb_multidevice_hooks+ from unsloth.models._remote_code_buffers import (+ restore_remote_code_non_persistent_buffers,+ )+ # transformers 5 leaves remote code's non-persistent buffers (RoPE inv_freq) uninitialised.+ restore_remote_code_non_persistent_buffers(model)_attach_bnb_multidevice_hooks(model,load_in_4bit = load_in_4bit,diff --git a/unsloth/models/vision.py b/unsloth/models/vision.pyindex 0b6d99aa2bc..d888c9cecde 100644--- a/unsloth/models/vision.py+++ b/unsloth/models/vision.py@@ -83,6 +83,7 @@ from ._utils import (set_task_config_attr,)from ._utils import *+from ._remote_code_buffers import restore_remote_code_non_persistent_buffersfrom ._custom_dtype import resolve_dtype, trusted_custom_dtypefrom .loader_utils import (DEFAULT_DEVICE_MAP,@@ -1753,6 +1754,8 @@ class FastBaseModel:trust_remote_code = trust_remote_code,**kwargs,)+ # transformers 5 leaves remote code's non-persistent buffers (RoPE inv_freq, decay slopes) uninitialised.+ restore_remote_code_non_persistent_buffers(model)# Must precede _attach_bnb_multidevice_hooks: it returns early while offload_embedding is True.offload_embedding = _resolve_offload_embedding(model,