From 14ee9bdd66ef3f3158a11f344cb3a046ead2f5f0 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Sat, 14 Nov 2026 01:35:23 +0000 Subject: [PATCH 01/18] fix(model): preserve task training and checkpoint state Keep task heads, optimizer state and deterministic continuation aligned across PPU training and cold resume. --- .../Usage Guide/Train-as-a-Service.md | 2 + ...55\347\273\203\346\234\215\345\212\241.md" | 2 + src/twinkle/model/base.py | 23 +++- src/twinkle/model/megatron/bridge/mcore.py | 8 ++ src/twinkle/model/megatron/megatron.py | 65 ++++++++- src/twinkle/model/multi_lora.py | 25 +++- .../transformers/multi_lora_transformers.py | 13 +- .../transformers/strategy/native_fsdp.py | 75 +++++++++++ .../model/transformers/transformers.py | 58 ++++++-- src/twinkle/utils/platforms/gpu.py | 1 + .../test_megatron_generative_reranker.py | 36 +++++ .../test_checkpoint_writer_rank.py | 1 + tests/transformers/test_dev_task_training.py | 14 +- .../test_full_checkpoint_resume.py | 15 ++- .../test_megatron_weight_only_resume.py | 124 +++++++++++++++++- .../transformers/test_native_fsdp_strategy.py | 106 +++++++++++++++ .../test_qwen35_fla_bwd_precision.py | 31 +++++ tests/transformers/test_task_head_buffers.py | 77 +++++++++++ 18 files changed, 635 insertions(+), 41 deletions(-) create mode 100644 tests/model/test_megatron_generative_reranker.py create mode 100644 tests/transformers/test_task_head_buffers.py diff --git a/docs/source_en/Usage Guide/Train-as-a-Service.md b/docs/source_en/Usage Guide/Train-as-a-Service.md index c07e072e1..b9b8c510b 100644 --- a/docs/source_en/Usage Guide/Train-as-a-Service.md +++ b/docs/source_en/Usage Guide/Train-as-a-Service.md @@ -64,6 +64,8 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` +On the Transformers backend, resumable checkpoints (Twinkle's `save_optimizer=True` or Tinker's `save_state()`) require a complete gradient accumulation window, followed by the optimizer step and gradient clearing. Pending gradients are not included in checkpoints, so saving optimizer state before that point is rejected. Weights-only exports remain available. Older checkpoints saved during accumulation can only be used to restart training from model weights. + With the code above, you can train a self-cognition LoRA based on `Qwen/Qwen3.8-27B`. This LoRA will change the model's name and creator to the names specified during training. To perform inference using this LoRA: ```python diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" index fb515944a..a9a300955 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" @@ -65,6 +65,8 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` +在 Transformers 后端,保存可继续训练的状态(Twinkle 的 `save_optimizer=True` 或 Tinker 的 `save_state()`)需要完成当前梯度累积窗口,并执行更新和清梯度。累积中的梯度不包含在 checkpoint 中,因此服务会拒绝此时保存优化器状态;仅导出权重不受此限制。旧的累积中途 checkpoint 只能按仅加载模型权重的方式重新开始训练。 + 通过上述代码,你可以训练一个原模型为`Qwen/Qwen3.8-27B`的自我认知lora。这个lora会改变模型的名称和制造者为训练时指定的名称。使用这个lora进行推理: ```python diff --git a/src/twinkle/model/base.py b/src/twinkle/model/base.py index 0aaca3fa7..739fc2ed9 100644 --- a/src/twinkle/model/base.py +++ b/src/twinkle/model/base.py @@ -4,6 +4,7 @@ import shutil from abc import ABC, abstractmethod from concurrent.futures import Future, ThreadPoolExecutor +from itertools import chain from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Protocol, Type, Union from twinkle import Platform, remote_function @@ -389,14 +390,15 @@ def _attached_head(self, adapter_name): return heads.get(adapter_name) def _task_head_state(self, adapter_name: Optional[str]) -> Dict[str, Any]: - """The adapter's head weights under slot-independent 'task_head.' keys. + """The head's parameters and calibration buffers under slot-independent keys. Default (single-model) form reads the head hanging on the host; MultiLora hosts override this with their slot-pool export.""" head = self._attached_head(adapter_name) if head is None: return {} - return {f'task_head.{name}': p.detach().clone().cpu() for name, p in head.named_parameters()} + return {f'task_head.{name}': tensor.detach().clone().cpu() + for name, tensor in chain(head.named_parameters(), head.named_buffers())} def _task_head_carries_on_this_rank(self) -> bool: """Whether THIS rank is expected to hold the head at all. True by default; @@ -418,20 +420,27 @@ def _restore_task_head_state(self, adapter_name: Optional[str], state: Dict[str, 'carries no head to restore them into.') return params = dict(head.named_parameters()) - expected = {f'task_head.{name}' for name in params} - if set(state) != expected: + tensors = {**params, **dict(head.named_buffers())} + expected = {f'task_head.{name}' for name in tensors} + legacy = {f'task_head.{name}' for name in params} + if set(state) not in (expected, legacy): raise ValueError( f'task_head checkpoint keys {sorted(state)} do not match the head it ' f'restores: expected {sorted(expected)}.') + for key, value in state.items(): + tensor = tensors[key[len('task_head.'):]] + if value.shape != tensor.shape: + raise ValueError(f'task-head shape mismatch for {key}: {value.shape} != {tensor.shape}') with torch.no_grad(): - for name, param in params.items(): - value = state[f'task_head.{name}'].to(device=param.device, dtype=param.dtype) + for key, value in state.items(): + param = tensors[key[len('task_head.'):]] + value = value.to(device=param.device, dtype=param.dtype) param.data.copy_(value) def _save_task_head_checkpoint(self, checkpoint_dir: str, adapter_name: Optional[str]) -> None: """Write the adapter's attached head to its own task_head.safetensors. - Keyed 'task_head.' -- slot-independent, so a re-added adapter may land + Keyed 'task_head.' -- slot-independent, so a re-added adapter may land on a different slot and still restore. Runs on the master rank only; the head file is tiny, so it writes synchronously alongside the (async) weights.""" state = self._task_head_state(adapter_name) diff --git a/src/twinkle/model/megatron/bridge/mcore.py b/src/twinkle/model/megatron/bridge/mcore.py index 03771d18b..d8337f530 100644 --- a/src/twinkle/model/megatron/bridge/mcore.py +++ b/src/twinkle/model/megatron/bridge/mcore.py @@ -25,7 +25,15 @@ def build_model_config(self, hf_config: Any, parallel_kwargs: Dict[str, Any], st # from a compatibility default. mcore's ModelConfig consumes the actual values, not this marker. kwargs.pop('_strict_model_kwargs', None) config_kwargs = hf_to_mcore_config(hf_config) + # Preserve a checkpoint's classification head instead of constructing a + # vocabulary-sized language-model output layer and skipping score weights. + is_seq_cls = any(name.endswith('ForSequenceClassification') + for name in (getattr(hf_config, 'architectures', None) or [])) + if is_seq_cls: + config_kwargs.update(task_type='seq_cls', num_labels=hf_config.num_labels) config_kwargs.update(kwargs) + if is_seq_cls: + config_kwargs['untie_embeddings_and_output_weights'] = True # per-token-mean grad normalization (mcore default is False; twinkle forces True). if 'calculate_per_token_loss' not in config_kwargs: config_kwargs['calculate_per_token_loss'] = True diff --git a/src/twinkle/model/megatron/megatron.py b/src/twinkle/model/megatron/megatron.py index ef89ab462..f12d0131b 100644 --- a/src/twinkle/model/megatron/megatron.py +++ b/src/twinkle/model/megatron/megatron.py @@ -59,6 +59,26 @@ def _first_optimizer_lr(optimizer) -> float: return chained[0].config.lr +def _align_distributed_optimizer_param_order(optimizer) -> None: + """Match checkpoint indices to MCore's FP32-first main-parameter order. + + Some MCore versions index mixed groups in gradient-buffer order, then put + FP32 parameters before low-precision master parameters in the inner optimizer. + A decision head kept in FP32 exposes this mismatch during checkpoint save/load. + """ + from megatron.core.optimizer.distrib_optimizer import DistributedOptimizer + + for part in getattr(optimizer, 'chained_optimizers', None) or [optimizer]: + if not isinstance(part, DistributedOptimizer): + continue + fp32_groups = getattr(part, 'model_fp32_groups', ()) + low_groups = getattr(part, 'model_float16_groups', ()) + for group_index, (fp32, low) in enumerate(zip(fp32_groups, low_groups, strict=True)): + if fp32 and low: + for param_index, param in enumerate([*fp32, *low]): + part.model_param_group_index_map[param] = (group_index, param_index) + + def _gather_vocab_parallel(tensor): """Differentiable vocab all-gather: ``[n, V/TP]`` -> ``[n, V]`` (identity when TP=1). @@ -73,6 +93,13 @@ def _gather_vocab_parallel(tensor): return gather_from_tensor_model_parallel_region(tensor) +def _generative_reranker_score(logits, template): + from twinkle.model.tasks import _resolve_generative_reranker_token_ids + + positive, negative = _resolve_generative_reranker_token_ids(template) + return logits[..., positive:positive + 1] - logits[..., negative:negative + 1] + + def _last_valid_positions(processor, batch, output_tensor): """CP-reassembled output plus each record's last-valid-token position (shared machinery for the seq_cls pool and the decision readout). @@ -230,6 +257,7 @@ def __init__( # ``_attach_task_head``. task_head_loader = kwargs.pop('task_head_loader', None) task = kwargs.pop('task', None) + self._head_task = task or ('decision' if task_head_loader is not None else None) num_labels = kwargs.pop('num_labels', None) if config is not None: self.hf_config = config @@ -428,8 +456,20 @@ def _construct_default_optimizer_group(self): _device_mesh=self.device_mesh, ) + def _apply_deterministic_model_kernels(self): + if not torch.are_deterministic_algorithms_enabled(): + return + from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule + + for model in self.model: + for module in model.modules(): + if type(module).__name__ == 'Qwen3NextGatedDeltaNet': + module.chunk_gated_delta_rule = torch_chunk_gated_delta_rule + module.causal_conv1d_fn = None + def _lazy_wrap_model(self): if not self._model_wrapped: + self._apply_deterministic_model_kernels() self.model = self.strategy.wrap_model(self.model) self._model_wrapped = True @@ -987,6 +1027,10 @@ def forward_step_func(data_iterator, model): else: # Packed: one row [1, total, C]; each segment ends at its last token. unpacked_logits = output_tensor[0, _last_idx] + if task == 'generative_reranker': + unpacked_logits = _gather_vocab_parallel(unpacked_logits) + unpacked_logits = _generative_reranker_score( + unpacked_logits, self.optimizer_group[adapter_name].template) elif task == 'value': # Legacy path -- PPO critic over the bridge-built head: keep its per-token # V(s_t) riding the logits channel ([b, s]); PPOValueLoss masks to response @@ -1528,6 +1572,7 @@ def _create_megatron_optimizer(self, **kwargs): model_chunks=model_chunks, config_overrides=config_overrides, ) + _align_distributed_optimizer_param_order(optimizer) return optimizer def _create_megatron_scheduler(self, optimizer, lr_decay_steps, max_lr=1e-4, **kwargs): @@ -1927,8 +1972,8 @@ def _save_mcore_optimizer( synchronous path.""" from megatron.core import dist_checkpointing from megatron.core import parallel_state as mpu - from megatron.core.dist_checkpointing.serialization import TorchDistSaveShardedStrategy from megatron.core.dist_checkpointing.strategies.fully_parallel import FullyParallelSaveStrategyWrapper + from megatron.core.dist_checkpointing.strategies.torch import TorchDistSaveShardedStrategy iteration = optimizer_config.cur_step iter_dir = os.path.join(checkpoint_dir, f'iter_{iteration:07d}') @@ -1937,6 +1982,13 @@ def _save_mcore_optimizer( # The sharding type must match the data-parallel wrapper the strategy chose at wrap time # (Megatron-FSDP stores optimizer state as DTensors, plain DDP does not). sharded_sd_metadata = self.strategy.get_sharded_sd_metadata() + if (self._head_task == 'decision' + and sharded_sd_metadata.get('distrib_optim_sharding_type') == 'dp_reshardable'): + # A tiny FP32 calibration head can leave an empty local DP bucket. + # MCore's canonical model-space format supports that layout; gather + # on CPU so saving does not duplicate full optimizer state on device. + sharded_sd_metadata.update(distrib_optim_sharding_type='fully_reshardable', + distrib_optim_fully_reshardable_mem_efficient=True) no_save_rng = kwargs.pop('no_save_rng', False) rng_state = None if no_save_rng else self._get_rng_state() @@ -1958,7 +2010,7 @@ def _save_mcore_optimizer( # megatron-core < 0.19 exposed get_default_save_sharded_strategy(); 0.19 made # save(sharded_strategy=None) default to TorchDistSaveShardedStrategy itself -- build that # explicitly, the thread_count tweak and the wrapper below need a concrete instance. - save_strategy = TorchDistSaveShardedStrategy() + save_strategy = TorchDistSaveShardedStrategy(backend='torch_dist', version=1) if hasattr(save_strategy, 'thread_count'): save_strategy.thread_count = thread_count if mpu.get_data_parallel_world_size(with_context_parallel=True) > 1: @@ -1970,15 +2022,16 @@ def _save_mcore_optimizer( # Async staging is only sound under an initialized process group (the metadata finalize # holds a barrier); a single-process run has no skew to hide anyway. async_save = kwargs.pop('async_save', False) and dist.is_initialized() + # Older supported MCore versions use the built-in queue without this option. + save_options = {'async_strategy': 'mcore'} if 'async_strategy' in inspect.signature( + dist_checkpointing.save).parameters else {} async_request = dist_checkpointing.save( state_dict, iter_dir, save_strategy, async_sharded_save=async_save, - # mcore's default 'nvrx' strategy needs the nvidia-resiliency-ext package, which is - # not a twinkle dependency; the built-in 'mcore' strategy drives the SAME - # AsyncCallsQueue the consumption path below schedules into. - async_strategy='mcore', + # Avoid the optional nvidia-resiliency-ext queue on newer MCore. + **save_options, validate_access_integrity=True, content_metadata=sharded_sd_metadata, ) diff --git a/src/twinkle/model/multi_lora.py b/src/twinkle/model/multi_lora.py index 1351a35a8..0b6553a67 100644 --- a/src/twinkle/model/multi_lora.py +++ b/src/twinkle/model/multi_lora.py @@ -3,6 +3,7 @@ from contextlib import contextmanager, nullcontext from copy import deepcopy from dataclasses import dataclass, field +from itertools import chain from peft import LoraConfig, PeftModel, get_peft_model from peft.tuners.lora import Embedding, Linear, LoraLayer from torch.distributed.tensor import distribute_tensor @@ -56,7 +57,8 @@ def build(self, host: torch.nn.Module, build_head: Callable[[], torch.nn.Module] head = build_head() setattr(host, f'task_head_slot_{slot}', head) self._initial_weights[slot] = { - name: parameter.detach().clone().cpu() for name, parameter in head.named_parameters()} + name: tensor.detach().clone().cpu() + for name, tensor in chain(head.named_parameters(), head.named_buffers())} host.task_heads = {} def bind(self, adapter_name: str, host: torch.nn.Module) -> None: @@ -93,7 +95,9 @@ def release(self, adapter_name: str, host: torch.nn.Module, if head is None: return with torch.no_grad(): - for name, parameter in head.named_parameters(): + # Calibration buffers can be nonpersistent in the family head. They still + # belong to this tenant and must not leak when the physical slot is reused. + for name, parameter in chain(head.named_parameters(), head.named_buffers()): saved = self._initial_weights.get(slot, {}).get(name) if saved is not None: write_param(parameter, saved) @@ -118,7 +122,7 @@ def export_state(self, adapter_name: str, host: torch.nn.Module, return {} return { f'task_head.{name}': read_param(parameter).detach().clone().cpu() - for name, parameter in head.named_parameters() + for name, parameter in chain(head.named_parameters(), head.named_buffers()) } def import_state(self, adapter_name: str, host: torch.nn.Module, state: Dict[str, torch.Tensor], @@ -142,16 +146,25 @@ def import_state(self, adapter_name: str, host: torch.nn.Module, state: Dict[str # lives on the last stage; every rank binds the same slot index. return parameters = dict(head.named_parameters()) + tensors = {**parameters, **dict(head.named_buffers())} covered = {key[len(prefix):] for key in head_keys} - if covered != set(parameters): + # Parameter-only checkpoints predate calibration snapshots; keep them loadable. + if covered not in (set(parameters), set(tensors)): raise ValueError( f'checkpoint task-head keys {sorted(covered)} do not match the bound head\'s ' - f'parameters {sorted(parameters)}: the head architecture changed since the ' + f'tensors {sorted(tensors)}: the head architecture changed since the ' 'checkpoint was written.') + for key in head_keys: + tensor = tensors[key[len(prefix):]] + shapes = {tensor.shape} + if hasattr(tensor, 'to_local'): + shapes.add(tensor.to_local().shape) + if state[key].shape not in shapes: + raise ValueError(f'task-head shape mismatch for {key}: {state[key].shape} != {tensor.shape}') with torch.no_grad(): for key in head_keys: name = key[len(prefix):] - parameter = parameters[name] + parameter = tensors[name] write_param(parameter, state[key]) diff --git a/src/twinkle/model/transformers/multi_lora_transformers.py b/src/twinkle/model/transformers/multi_lora_transformers.py index ae71f0d8b..ecabb77a3 100644 --- a/src/twinkle/model/transformers/multi_lora_transformers.py +++ b/src/twinkle/model/transformers/multi_lora_transformers.py @@ -104,12 +104,13 @@ def __init__( else: from transformers import AutoConfig self.hf_config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) - if model_cls is None and hasattr(self.hf_config, 'architectures'): - model_cls = self.hf_config.architectures[0] - if model_cls is None: - model_cls = AutoModelForCausalLM - if isinstance(model_cls, str): - model_cls = getattr(transformers, model_cls) + if model_loader is None or model_id is None: + if model_cls is None and getattr(self.hf_config, 'architectures', None): + model_cls = self.hf_config.architectures[0] + if model_cls is None: + model_cls = AutoModelForCausalLM + if isinstance(model_cls, str): + model_cls = getattr(transformers, model_cls) if model_loader is not None and model_id is not None: processor = model_loader.process_tokenizer(model_loader.build_processor(model_id, self.hf_config)) self._default_tokenizer = processor diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 672265fc6..006d12ad8 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -24,6 +24,35 @@ PEFT_BASE_LAYER_SEGMENT = 'base_layer' +def _shard_builtin_task_heads(model, *, mesh, reshard_after_forward, mp_policy): + # The head wrapper owns unsharding for the readout backward instead of the root wrapper. + heads = [getattr(model, name, None) for name in ('score', 'classifier')] + heads.extend((getattr(model, 'task_heads', None) or {}).values()) + seen = set() + for head in heads: + if (head is None or id(head) in seen or getattr(head, 'keep_fp32', False) + or next(head.parameters(), None) is None): + continue + seen.add(id(head)) + fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) + head._twinkle_independent_fsdp = True + + +def _shard_fp32_task_heads(model, *, mesh, reshard_after_forward): + # Independent FSDP wrappers unshard these externally-called heads and keep per-group dtypes uniform. + for module in model.modules(): + if (getattr(module, '_twinkle_independent_fsdp', False) + or not getattr(module, 'keep_fp32', False) or next(module.parameters(), None) is None): + continue + fully_shard( + module, + mesh=mesh, + reshard_after_forward=reshard_after_forward, + mp_policy=_build_mp_policy('no'), + ) + module._twinkle_independent_fsdp = True + + class NativeFSDPStrategy(TransformerStrategyBase): def __init__(self, @@ -193,6 +222,11 @@ def wrap_model(self, model, optimizer=None, *additional_optimizers): world_size = self.device_mesh.world_size ep_fsdp_mesh_1d = self.ep_fsdp_device_mesh['ep_fsdp'] if ep_enabled else None + _shard_builtin_task_heads( + model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) + _shard_fp32_task_heads( + model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) + for layer_mod, experts_mod in layer_pairs: layer_mod._fsdp_modules = [] @@ -269,8 +303,49 @@ def _prepare_optimizer_state_dict_options(self, *, for_load: bool): full_state_dict=True, cpu_offload=not for_load, broadcast_from_rank0=for_load, + # Adam initializes state lazily, so unused trainable parameters have no entry to restore. + strict=not for_load, ) + def get_independent_task_head_state(self, head): + if not getattr(head, '_twinkle_independent_fsdp', False): + return None + from torch.distributed.checkpoint.state_dict import get_model_state_dict + + state = get_model_state_dict(head, options=self._prepare_optimizer_state_dict_options(for_load=False)) + for name, buffer in head.named_buffers(): + state.setdefault(name, buffer) + return {f'task_head.{name}': tensor.detach().clone().cpu() for name, tensor in state.items()} + + def restore_independent_task_head_state(self, head, state) -> bool: + if not getattr(head, '_twinkle_independent_fsdp', False): + return False + from torch.distributed.checkpoint.state_dict import set_model_state_dict + + tensors = {**dict(head.named_parameters()), **dict(head.named_buffers())} + expected = {f'task_head.{name}' for name in tensors} + legacy = {f'task_head.{name}' for name in head.named_parameters()} + if set(state) not in (expected, legacy): + raise ValueError( + f'task_head checkpoint keys {sorted(state)} do not match the head it restores: ' + f'expected {sorted(expected)}.') + for key, value in state.items(): + tensor = tensors[key[len('task_head.'):]] + if value.shape != tensor.shape: + raise ValueError(f'task-head shape mismatch for {key}: {value.shape} != {tensor.shape}') + persistent = set(head.state_dict()) + model_state = {key[len('task_head.'):]: value for key, value in state.items() + if key[len('task_head.'):] in persistent} + set_model_state_dict( + head, model_state, options=self._prepare_optimizer_state_dict_options(for_load=True)) + with torch.no_grad(): + for key, value in state.items(): + name = key[len('task_head.'):] + if name not in persistent: + tensor = tensors[name] + tensor.copy_(value.to(device=tensor.device, dtype=tensor.dtype)) + return True + def needs_wrapped_optimizer_state(self) -> bool: return self.device_mesh is not None diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index 0a8fc1d94..5c75639a8 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -337,12 +337,13 @@ def __init__( logger.warning_once('loading %s with trust_remote_code=True: the checkpoint\'s own Python code ' 'will execute inside this process. Only use checkpoints you trust.', model_id) self.hf_config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) - if model_cls is None and hasattr(self.hf_config, 'architectures'): - model_cls = self.hf_config.architectures[0] - if model_cls is None: - model_cls = AutoModelForCausalLM - if isinstance(model_cls, str): - model_cls = getattr(transformers, model_cls) + if model_loader is None or model_id is None: + if model_cls is None and getattr(self.hf_config, 'architectures', None): + model_cls = self.hf_config.architectures[0] + if model_cls is None: + model_cls = AutoModelForCausalLM + if isinstance(model_cls, str): + model_cls = getattr(transformers, model_cls) if model_loader is not None and model_id is not None: # Full takeover: the loader builds the processor, then the pretrained model (its own # model_cls, trust_remote_code and dynamic-module handling), then post-processes it @@ -371,6 +372,7 @@ def __init__( # applying runtime overrides such as ``attn_implementation``. Keep Twinkle's # config reference aligned with the config that the model actually uses. self.hf_config = self.model.config + self._apply_deterministic_model_kernels() logger.info_once( f'[TransformersModel] attn_implementation: requested={kwargs.get("attn_implementation")!r}, ' f'resolved={getattr(self.model.config, "_attn_implementation", None)!r}') @@ -667,6 +669,16 @@ def _fill_deepspeed_auto(self, kwargs: Dict[str, Any]) -> None: break fill(int(micro_batch_size), int(ga or 1)) + def _apply_deterministic_model_kernels(self): + if not torch.are_deterministic_algorithms_enabled(): + return + from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule + + for module in self.model.modules(): + if type(module).__name__ == 'Qwen3_5GatedDeltaNet': + module.chunk_gated_delta_rule = torch_chunk_gated_delta_rule + module.causal_conv1d_fn = None + def _lazy_wrap_model(self): if not self._model_wrapped: optimizer_groups = [og for og in self.optimizer_group.values() if og.optimizer is not None] @@ -2468,7 +2480,8 @@ def save(self, name: Optional[str] = None, output_dir: Optional[str] = None, int interval: Save each interval steps. **kwargs: adapter_name: Lora adapter name. - save_optimizer: Whether to save optimizer state. + save_optimizer: Whether to save optimizer state. Requires a complete gradient + accumulation window; pending gradients are not included in checkpoints. safe_serialization: Save weights with safetensors instead of torch serialization. max_shard_size: Maximum size of each full-model weight shard. save_total_limit: Maximum number of completed checkpoint directories to retain. @@ -2594,6 +2607,15 @@ def _save_optimizer(self, output_dir, **kwargs): def _save_training_state(self, output_dir, **kwargs): adapter_name = kwargs.pop('adapter_name', _default_adapter_name) defer_trainer_state = kwargs.pop('defer_trainer_state', False) + optimizer_config = self.optimizer_group[adapter_name] + optimizer_config.require_finalized('save training state') + optimizer = optimizer_config.optimizer + pending_gradients = optimizer is not None and any( + parameter.grad is not None for group in optimizer.param_groups for parameter in group['params']) + if optimizer_config.cur_step % optimizer_config.gradient_accumulation_steps or pending_gradients: + raise ValueError( + 'save_optimizer=True requires a complete gradient accumulation window; ' + 'finish the pending backward/optimizer steps and zero_grad before saving, or save weights only.') # The optimizer/scheduler files are written before _write_weights() gets a chance to # create the checkpoint dir via save_pretrained; torch.save then raises # "Parent directory ... does not exist". This lives HERE (not at the top of save()) so it @@ -2601,8 +2623,6 @@ def _save_training_state(self, output_dir, **kwargs): os.makedirs(output_dir, exist_ok=True) self._save_optimizer(output_dir, adapter_name=adapter_name) - optimizer_config = self.optimizer_group[adapter_name] - # RNG state is per rank, so it is written here rather than below the master-only return: # each rank's generator has advanced by its own dropout draws, and one master-written file # would replay rank 0's stream on every rank. Wherever the forward is stochastic @@ -2838,6 +2858,10 @@ def _restore_training_state(self, checkpoint_dir, *, adapter_name=''): trainer_state_path = os.path.join(checkpoint_dir, 'trainer_state.json') with open(trainer_state_path) as f: trainer_state = json.load(f) + if trainer_state['cur_step'] % trainer_state['gradient_accumulation_steps']: + raise ValueError( + 'Checkpoint was saved during gradient accumulation without pending gradients; ' + 'use resume_only_model=True to start fresh, or resume a complete accumulation window.') adapter_name = adapter_name or self._get_default_group() optimizer_config = self.optimizer_group[adapter_name] @@ -3017,6 +3041,22 @@ def _task_head_host(self): def _task_head_is_master(self) -> bool: return Platform.is_master() + def _task_head_state(self, adapter_name): + head = self._attached_head(adapter_name) + getter = getattr(self.strategy, 'get_independent_task_head_state', None) + if head is not None and getter is not None: + state = getter(head) + if state is not None: + return state + return super()._task_head_state(adapter_name) + + def _restore_task_head_state(self, adapter_name, state) -> None: + head = self._attached_head(adapter_name) + restorer = getattr(self.strategy, 'restore_independent_task_head_state', None) + if head is not None and restorer is not None and restorer(head, state): + return + super()._restore_task_head_state(adapter_name, state) + def _add_task_head(self, adapter_name: str, task, task_head_loader=None, num_labels=None) -> None: """Build one task head worker-side and register it per adapter. diff --git a/src/twinkle/utils/platforms/gpu.py b/src/twinkle/utils/platforms/gpu.py index a038a635b..f6f26b1ba 100644 --- a/src/twinkle/utils/platforms/gpu.py +++ b/src/twinkle/utils/platforms/gpu.py @@ -56,5 +56,6 @@ def apply_determinism() -> None: os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':16:8' os.environ['FLASH_ATTENTION_DETERMINISTIC'] = '1' import torch + torch.backends.cuda.enable_mem_efficient_sdp(False) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py new file mode 100644 index 000000000..9e597bf15 --- /dev/null +++ b/tests/model/test_megatron_generative_reranker.py @@ -0,0 +1,36 @@ +from types import SimpleNamespace + +import torch + +from twinkle.model.megatron.megatron import _generative_reranker_score + + +def test_generative_reranker_scores_yes_minus_no_and_backpropagates(): + tokenizer = SimpleNamespace(convert_tokens_to_ids=lambda token: {'yes': 3, 'no': 7}[token]) + template = SimpleNamespace(processor=object(), tokenizer=tokenizer) + logits = torch.randn(2, 5, 11, requires_grad=True) + + scores = _generative_reranker_score(logits, template) + + assert scores.shape == (2, 5, 1) + torch.testing.assert_close(scores, logits[..., 3:4] - logits[..., 7:8]) + scores.sum().backward() + assert torch.count_nonzero(logits.grad[..., 3]) == 10 + assert torch.count_nonzero(logits.grad[..., 7]) == 10 + assert torch.count_nonzero(logits.grad[..., [0, 1, 2, 4, 5, 6, 8, 9, 10]]) == 0 + + +def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): + from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule + from twinkle.model.megatron.megatron import MegatronModel + + gdn_type = type('Qwen3NextGatedDeltaNet', (torch.nn.Module,), {}) + gdn = gdn_type() + gdn.chunk_gated_delta_rule = lambda *args: None + gdn.causal_conv1d_fn = lambda *args: None + wrapper = MegatronModel.__new__(MegatronModel) + wrapper.model = [gdn] + monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: True) + wrapper._apply_deterministic_model_kernels() + assert gdn.chunk_gated_delta_rule is torch_chunk_gated_delta_rule + assert gdn.causal_conv1d_fn is None diff --git a/tests/transformers/test_checkpoint_writer_rank.py b/tests/transformers/test_checkpoint_writer_rank.py index 1de64461c..5fe173140 100644 --- a/tests/transformers/test_checkpoint_writer_rank.py +++ b/tests/transformers/test_checkpoint_writer_rank.py @@ -23,6 +23,7 @@ def test_full_checkpoint_has_one_writer_after_collectives(self): wrapper.optimizer_group = {'': SimpleNamespace(cur_step=3)} wrapper._save_training_state = Mock() wrapper._save_tokenizer = Mock() + wrapper._save_task_head_checkpoint = Mock() wrapper.submit_async_write = Mock() with patch(f'{module}.Platform.is_master', return_value=rank == 0), \ patch(f'{module}.copy_checkpoint_args'), patch(f'{module}.rotate_checkpoints'): diff --git a/tests/transformers/test_dev_task_training.py b/tests/transformers/test_dev_task_training.py index 34f196522..f747477f6 100644 --- a/tests/transformers/test_dev_task_training.py +++ b/tests/transformers/test_dev_task_training.py @@ -101,6 +101,16 @@ def step(model, name): initial, peer = read(model, 'a'), read(model, 'b') for _ in range(3): step(model, 'a') + with pytest.raises(ValueError, match='complete gradient accumulation window'): + model.save('partial', output_dir=str(tmp_path), adapter_name='a', save_optimizer=True) + assert not (tmp_path / 'partial' / 'trainer_state.json').exists() + model.save('weights-only', output_dir=str(tmp_path), adapter_name='a') + assert (tmp_path / 'weights-only' / 'adapter_model.safetensors').exists() + # A complete backward window still needs its optimizer step and zero_grad. + model.forward_backward(inputs=copy.deepcopy(batch), task=task, adapter_name='a') + with pytest.raises(ValueError, match='zero_grad'): + model.save('before-step', output_dir=str(tmp_path), adapter_name='a', save_optimizer=True) + model.clip_grad_and_step(adapter_name='a') trained = read(model, 'a') assert not torch.equal(initial, trained) torch.testing.assert_close(read(model, 'b'), peer, rtol=0, atol=0) @@ -109,7 +119,7 @@ def step(model, name): assert not parameter.requires_grad torch.testing.assert_close(parameter, base[key], rtol=0, atol=0) saved = model.save('trained', output_dir=str(tmp_path), adapter_name='a', - save_optimizer=True, consumed_train_samples=3 * len(batch)) + save_optimizer=True, consumed_train_samples=4 * len(batch)) if task != 'embedding': head = {k: v for k, v in load_file(saved + '/adapter_model.safetensors').items() if 'score.' in k} assert head and any(torch.count_nonzero(v) for k, v in head.items() if 'lora_B' in k) @@ -128,7 +138,7 @@ def step(model, name): cold = build() add(cold, 'a') progress = cold.resume_from_checkpoint(saved, adapter_name='a') - assert progress['cur_step'] == 3 + assert progress['cur_step'] == 4 step(cold, 'a') step(cold, 'a') torch.testing.assert_close(read(cold, 'a'), continued, rtol=0, atol=1e-6) diff --git a/tests/transformers/test_full_checkpoint_resume.py b/tests/transformers/test_full_checkpoint_resume.py index 1f44a4ad1..d8b7cb530 100644 --- a/tests/transformers/test_full_checkpoint_resume.py +++ b/tests/transformers/test_full_checkpoint_resume.py @@ -21,7 +21,7 @@ def test_full_resume_restores_weights_and_optional_training_state(tmp_path, weig torch.save(saved_optimizer.state_dict(), tmp_path / 'optimizer.pt') save_file({'weight': torch.full((1, 1), 2.)}, str(tmp_path / 'model.safetensors')) (tmp_path / 'trainer_state.json').write_text(json.dumps({ - 'cur_step': 3, 'consumed_train_samples': 12, 'gradient_accumulation_steps': 2, + 'cur_step': 4, 'consumed_train_samples': 12, 'gradient_accumulation_steps': 2, })) wrapper = object.__new__(TransformersModel) wrapper.__dict__['model'] = model @@ -31,14 +31,14 @@ def test_full_resume_restores_weights_and_optional_training_state(tmp_path, weig result = wrapper.resume_from_checkpoint(str(tmp_path), resume_only_model=weights_only) assert model(torch.ones(1, 1)).item() == 2 # dev's weights-only contract starts fresh progress as well as fresh optimizer state. - assert result['cur_step'] == (0 if weights_only else 3) + assert result['cur_step'] == (0 if weights_only else 4) assert result['consumed_train_samples'] == (0 if weights_only else 12) if weights_only: assert not optimizer.state assert wrapper.optimizer_group[''].cur_step == 0 else: torch.testing.assert_close(optimizer.state[model.weight]['exp_avg'], torch.full((1, 1), 0.5)) - assert wrapper.optimizer_group[''].cur_step == 3 + assert wrapper.optimizer_group[''].cur_step == 4 assert wrapper.optimizer_group[''].gradient_accumulation_steps == 2 @@ -166,3 +166,12 @@ def test_rebind_is_a_noop_when_the_optimizer_is_already_bound(): group = SimpleNamespace(lr_scheduler=scheduler, _scheduler_spec=None) TransformersModel._rebind_lr_scheduler(group, built_on) # same optimizer object assert scheduler.base_lrs == [0.05] + + +def test_optimizer_resume_rejects_legacy_partial_accumulation_checkpoint(tmp_path): + wrapper = object.__new__(TransformersModel) + (tmp_path / 'trainer_state.json').write_text(json.dumps({ + 'cur_step': 3, 'gradient_accumulation_steps': 2, + })) + with pytest.raises(ValueError, match='saved during gradient accumulation'): + wrapper._restore_training_state(str(tmp_path)) diff --git a/tests/transformers/test_megatron_weight_only_resume.py b/tests/transformers/test_megatron_weight_only_resume.py index 1fda7208d..4a2fe56db 100644 --- a/tests/transformers/test_megatron_weight_only_resume.py +++ b/tests/transformers/test_megatron_weight_only_resume.py @@ -40,12 +40,15 @@ def test_weights_only_load_refreshes_target_master_params_and_preserves_adam_sta def load_weights(*args, **kwargs): parameter.data.fill_(2.) + bridge = SimpleNamespace( + load_weights=load_weights, _convert_post_process=lambda *args: None, _peft_format=False, + hf_score_key='score.weight', config=SimpleNamespace(task_type='causal_lm')) wrapper = SimpleNamespace( _get_default_group=lambda: 'target', model=[parameter], - strategy=SimpleNamespace(unwrap_model=lambda model: model, - bridge=SimpleNamespace(load_weights=load_weights)), + strategy=SimpleNamespace(unwrap_model=lambda model: model, bridge=bridge), optimizer_group={'target': group, 'other': SimpleNamespace(optimizer=other)}, multi_adapter=SimpleNamespace(save_context=lambda adapter: nullcontext('slot'), load_lora_converter=None), + _load_task_head_checkpoint=lambda *args, **kwargs: None, ) model_class.load(wrapper, str(tmp_path), adapter_name='target', load_optimizer=False) assert parameter.item() == 2. @@ -53,3 +56,120 @@ def load_weights(*args, **kwargs): optimizer.step() torch.testing.assert_close(parameter, expected.model_parameter) assert other.master.item() == other.model_parameter.item() == 11. + + +@pytest.mark.parametrize('task,sharding', [(None, 'dp_reshardable'), ('decision', 'dp_reshardable'), + ('decision', 'fsdp_dtensor')]) +def test_mcore_optimizer_save_uses_versioned_torch_dist_strategy(tmp_path, monkeypatch, task, sharding): + pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') + from megatron.core import dist_checkpointing + from megatron.core import parallel_state as mpu + + captured = {} + + def save(state_dict, checkpoint_dir, sharded_strategy=None, common_strategy=None, + validate_access_integrity=True, async_sharded_save=False, + preprocess_common_before_consistancy_check=None, content_metadata=None): + captured.update(strategy=sharded_strategy, async_save=async_sharded_save, + metadata=content_metadata) + + monkeypatch.setattr(dist_checkpointing, 'save', save) + monkeypatch.setattr(mpu, 'get_data_parallel_world_size', lambda **kwargs: 1) + metadata = {'sharded': True, 'distrib_optim_sharding_type': sharding} + wrapper = SimpleNamespace( + model=object(), _head_task=task, + strategy=SimpleNamespace(get_sharded_sd_metadata=lambda: metadata), + _generate_state_dict=lambda **kwargs: {'optimizer': {}}, + ) + optimizer = SimpleNamespace(cur_step=1, optimizer=object(), lr_scheduler=None) + + MegatronModel._save_mcore_optimizer( + wrapper, str(tmp_path), optimizer, no_save_rng=True, thread_count=3, async_save=True) + + assert captured['strategy'].backend == 'torch_dist' + assert captured['strategy'].version == 1 + assert captured['strategy'].thread_count == 3 + assert captured['async_save'] is False + assert captured['metadata'] is metadata + expected = 'fully_reshardable' if task == 'decision' and sharding == 'dp_reshardable' else sharding + assert metadata['distrib_optim_sharding_type'] == expected + assert metadata.get('distrib_optim_fully_reshardable_mem_efficient', False) == (expected == 'fully_reshardable') + assert (tmp_path / 'latest_checkpointed_iteration.txt').read_text() == '1' + + +def test_mixed_precision_optimizer_checkpoint_uses_the_actual_main_param_order(monkeypatch): + pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') + import torch + from megatron.core import optimizer as mcore_optimizer + from megatron.core.optimizer.distrib_optimizer import DistributedOptimizer + + low = torch.nn.Parameter(torch.ones(8, dtype=torch.bfloat16)) + head = torch.nn.Parameter(torch.ones(3)) + master = torch.nn.Parameter(low.float().clone()) + distributed = object.__new__(DistributedOptimizer) + distributed.model_float16_groups = [[low]] + distributed.model_fp32_groups = [[head]] + # MCore builds these indices in gradient-buffer order, then reorders the + # inner optimizer's main parameters to FP32 first. A mixed group must agree. + distributed.model_param_group_index_map = {low: (0, 0), head: (0, 1)} + distributed.optimizer = torch.optim.Adam([head, master]) + distributed.config = SimpleNamespace(use_precision_aware_optimizer_no_fp8_or_ds_fp8=False) + chained = SimpleNamespace(chained_optimizers=[distributed]) + monkeypatch.setattr(mcore_optimizer, 'get_megatron_optimizer', lambda **kwargs: chained) + wrapper = SimpleNamespace(model=[SimpleNamespace(ddp_config=SimpleNamespace(use_distributed_optimizer=True))], + use_distributed_optimizer=True, mixed_precision='bf16') + + MegatronModel._create_megatron_optimizer(wrapper) + + for param, expected in [(low, master), (head, head)]: + assert distributed._get_main_param_and_optimizer_states(param)['param'] is expected + + +def test_mcore_persisted_classifier_uses_independent_output_head(monkeypatch): + mcore_bridge = pytest.importorskip('mcore_bridge', reason='requires the optional Megatron bridge') + import torch + from mcore_bridge.bridge.gpt_bridge import GPTBridge + + from twinkle import Platform + from twinkle.model.megatron.bridge.mcore import MCoreBridgeBackend + + monkeypatch.setattr(mcore_bridge, 'hf_to_mcore_config', + lambda config: {'untie_embeddings_and_output_weights': False}) + monkeypatch.setattr(mcore_bridge, 'ModelConfig', lambda **kwargs: kwargs) + monkeypatch.setattr(Platform, 'get_platform', lambda: SimpleNamespace( + configure_model_config=lambda config: None)) + strategy = SimpleNamespace(params_type='float32', sequence_parallel=False, variable_seq_lengths=False) + hf_config = SimpleNamespace(architectures=['Qwen2ForSequenceClassification'], num_labels=3) + + config = MCoreBridgeBackend().build_model_config( + hf_config, {}, strategy, untie_embeddings_and_output_weights=False) + + assert config['task_type'] == 'seq_cls' + assert config['num_labels'] == 3 + assert config['untie_embeddings_and_output_weights'] is True + + bridge = object.__new__(GPTBridge) + bridge.config = SimpleNamespace( + task_type=config['task_type'], untie_embeddings_and_output_weights=True, padded_vocab_size=13) + bridge.is_multimodal = False + bridge.tp_size = bridge.etp_size = 1 + bridge.tp_rank = bridge.etp_rank = 0 + bridge._peft_format = False + mg_model = SimpleNamespace( + output_layer=SimpleNamespace(weight=torch.nn.Parameter(torch.zeros(1, 8))), + decoder=SimpleNamespace(final_layernorm=SimpleNamespace(weight=torch.nn.Parameter(torch.zeros(8)))), + ) + score = torch.arange(8, dtype=torch.float32).reshape(1, 8) + embedding = score.repeat(13, 1) + 100 + norm = torch.ones(8) + def lazy(value): + return SimpleNamespace(load=lambda: value) + + bridge._convert_post_process(mg_model, { + 'score.weight': lazy(score), + 'model.embed_tokens.weight': lazy(embedding), + 'model.norm.weight': lazy(norm), + }, '', True) + + torch.testing.assert_close(mg_model.output_layer.weight, score) + torch.testing.assert_close(mg_model.decoder.final_layernorm.weight, norm) diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 63f8f0e8c..558a915d3 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -91,6 +91,7 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t mesh.mesh_dim_names = None strategy = NativeFSDPStrategy(device_mesh=mesh, enable_ep=False) wrapper = SimpleNamespace(model=original, optimizer_group=groups, strategy=strategy, + model_arch={'language_model': [], 'vision_tower': [], 'aligner': []}, _model_wrapped=False, _enable_expert_parallel=False, sp_strategy=None, _maybe_apply_expert_parallel=lambda: None, _ensure_sp_strategy=lambda: None, _ensure_gdn_padding_free_patch=lambda: None, @@ -109,3 +110,108 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t torch.testing.assert_close(replacement['base'], torch.ones(2)) if tenant_count == 1: torch.testing.assert_close(replacement['b'], torch.ones(2)) + + +def test_builtin_score_head_is_sharded_before_the_root_model(): + model = torch.nn.Module() + model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) + model.score = torch.nn.Linear(4, 2).to(torch.bfloat16) + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) + + with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', + return_value=MagicMock()), \ + patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: + strategy.wrap_model(model) + + assert fully_shard.call_count == 2 + head_call, model_call = fully_shard.call_args_list + assert head_call.args[0] is model.score + assert head_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 + assert model_call.args[0] is model + + +def test_attached_standard_head_is_sharded_independently(): + model = torch.nn.Module() + model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) + model.task_heads = {'': torch.nn.Linear(4, 1).to(torch.bfloat16)} + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) + + with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', + return_value=MagicMock()), \ + patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: + strategy.wrap_model(model) + + assert fully_shard.call_count == 2 + assert fully_shard.call_args_list[0].args[0] is model.task_heads[''] + assert model.task_heads['']._twinkle_independent_fsdp + + +def test_optimizer_restore_accepts_lazy_state_for_unused_parameters(): + from torch.distributed.checkpoint.state_dict import get_optimizer_state_dict, set_optimizer_state_dict + + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), enable_ep=False) + model = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 1)) + optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) + model[0](torch.ones(2, 3)).sum().backward() + optimizer.step() + optimizer.zero_grad() + state = get_optimizer_state_dict( + model, optimizer, options=strategy._prepare_optimizer_state_dict_options(for_load=False)) + load_options = strategy._prepare_optimizer_state_dict_options(for_load=True) + load_options.broadcast_from_rank0 = False + set_optimizer_state_dict(model, optimizer, state, options=load_options) + assert optimizer.state[model[0].weight]['step'].item() == 1 + assert len(optimizer.state[model[1].weight]) == 0 + + +def test_fp32_task_head_is_sharded_as_separate_precision_group(): + + class Head(torch.nn.Module): + keep_fp32 = True + + def __init__(self): + super().__init__() + self.bias = torch.nn.Parameter(torch.zeros(4, dtype=torch.float32)) + + class Model(torch.nn.Module): + + def __init__(self): + super().__init__() + self.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) + self.task_head = Head() + self.scoring_head = self.task_head + + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) + model = Model() + with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', + return_value=MagicMock()), \ + patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: + strategy.wrap_model(model) + + assert fully_shard.call_count == 2 + head_call, model_call = fully_shard.call_args_list + assert head_call.args[0] is model.task_head + assert head_call.kwargs['mp_policy'].param_dtype is None + assert head_call.kwargs['mp_policy'].reduce_dtype is None + assert model_call.args[0] is model + assert model_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 + assert model.task_head.bias.dtype is torch.float32 + assert model.task_head._twinkle_independent_fsdp + + +def test_independent_task_head_checkpoint_includes_nonpersistent_buffers(): + head = torch.nn.Linear(2, 1) + head.register_buffer('calibration', torch.tensor([1.0]), persistent=False) + head._twinkle_independent_fsdp = True + strategy = NativeFSDPStrategy(enable_ep=False) + with patch('torch.distributed.checkpoint.state_dict.get_model_state_dict', + return_value={'weight': head.weight.detach(), 'bias': head.bias.detach()}): + state = strategy.get_independent_task_head_state(head) + assert set(state) == {'task_head.weight', 'task_head.bias', 'task_head.calibration'} + + restored = {key: torch.full_like(value, 3.0) for key, value in state.items()} + with patch('torch.distributed.checkpoint.state_dict.set_model_state_dict') as set_state: + assert strategy.restore_independent_task_head_state(head, restored) + set_state.assert_called_once() + assert set_state.call_args.args[1].keys() == {'weight', 'bias'} + torch.testing.assert_close(head.calibration, torch.tensor([3.0])) diff --git a/tests/transformers/test_qwen35_fla_bwd_precision.py b/tests/transformers/test_qwen35_fla_bwd_precision.py index d0fbd5775..61d8b40e6 100644 --- a/tests/transformers/test_qwen35_fla_bwd_precision.py +++ b/tests/transformers/test_qwen35_fla_bwd_precision.py @@ -124,6 +124,37 @@ def _force_fla_off(model: torch.nn.Module) -> None: delattr(la, '_twinkle_npu_patched') +def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): + from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule + from twinkle.model.transformers.transformers import TransformersModel + + gdn_type = type('Qwen3_5GatedDeltaNet', (torch.nn.Module,), {}) + gdn = gdn_type() + gdn.chunk_gated_delta_rule = lambda *args: None + gdn.causal_conv1d_fn = lambda *args: None + wrapper = TransformersModel.__new__(TransformersModel) + torch.nn.Module.__init__(wrapper) + wrapper.model = torch.nn.Module() + wrapper.model.gdn = gdn + monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: True) + wrapper._apply_deterministic_model_kernels() + assert gdn.chunk_gated_delta_rule is torch_chunk_gated_delta_rule + assert gdn.causal_conv1d_fn is None + + +def test_full_determinism_disables_nondeterministic_sdp(monkeypatch): + from twinkle.utils.platforms.gpu import GPU + + calls = [] + monkeypatch.setattr(torch.backends.cuda, 'enable_mem_efficient_sdp', calls.append) + monkeypatch.setattr(torch.backends.cudnn, 'deterministic', False) + monkeypatch.setattr(torch.backends.cudnn, 'benchmark', True) + GPU.apply_determinism() + assert calls == [False] + assert torch.backends.cudnn.deterministic + assert not torch.backends.cudnn.benchmark + + def _force_fla_on(model: torch.nn.Module) -> int: import importlib import twinkle.kernel.ops.fla.npu as fla_mod diff --git a/tests/transformers/test_task_head_buffers.py b/tests/transformers/test_task_head_buffers.py new file mode 100644 index 000000000..a8fcca9df --- /dev/null +++ b/tests/transformers/test_task_head_buffers.py @@ -0,0 +1,77 @@ +# Copyright (c) ModelScope Contributors. All rights reserved. +"""Calibration buffers must follow the tenant through reuse and checkpoint restore.""" +from types import SimpleNamespace + +import pytest +import torch +from torch import nn + +from twinkle.model.base import TrainableModel +from twinkle.model.multi_lora import MultiLoraTaskHeadSlots + + +def _head(): + head = nn.Linear(2, 2) + head.register_buffer('temperature', torch.tensor([1., 2.]), persistent=False) + return head + + +def _write(tensor, value): + tensor.copy_(value.to(tensor)) + + +def test_released_head_restores_nonpersistent_calibration(): + slots, host = MultiLoraTaskHeadSlots(2), nn.Module() + slots.build(host, _head) + slots.bind('a', host) + slots.bind('b', host) + a, b = host.task_heads['a'], host.task_heads['b'] + temperature = a.temperature + a.temperature.fill_(9) + slots.release('a', host, _write) + slots.bind('c', host) + assert host.task_heads['c'].temperature is temperature + torch.testing.assert_close(temperature, torch.tensor([1., 2.])) + torch.testing.assert_close(b.temperature, torch.tensor([1., 2.])) + + +def test_calibrated_checkpoint_moves_to_another_slot(): + slots, host = MultiLoraTaskHeadSlots(2), nn.Module() + slots.build(host, _head) + slots.bind('a', host) + host.task_heads['a'].temperature.fill_(7) + saved = slots.export_state('a', host, lambda tensor: tensor) + slots.release('a', host, _write) + slots.bind('b', host) + slots.bind('a', host) + assert slots.bindings['a'] == 1 + slots.import_state('a', host, saved, _write) + torch.testing.assert_close(host.task_heads['a'].temperature, torch.full((2,), 7.)) + torch.testing.assert_close(host.task_heads['b'].temperature, torch.tensor([1., 2.])) + + +def test_single_head_checkpoint_restores_calibration(tmp_path): + head = _head() + wrapper = SimpleNamespace(_attached_head=lambda name: head, _task_head_is_master=lambda: True, + _task_head_carries_on_this_rank=lambda: True) + for name in ('_task_head_state', '_restore_task_head_state'): + setattr(wrapper, name, getattr(TrainableModel, name).__get__(wrapper)) + head.temperature.fill_(5) + TrainableModel._save_task_head_checkpoint(wrapper, str(tmp_path), None) + head.temperature.zero_() + TrainableModel._load_task_head_checkpoint(wrapper, str(tmp_path), None) + torch.testing.assert_close(head.temperature, torch.full((2,), 5.)) + + +def test_bad_buffer_shape_rejected_before_any_tensor_changes(): + slots, host = MultiLoraTaskHeadSlots(1), nn.Module() + slots.build(host, _head) + slots.bind('a', host) + head = host.task_heads['a'] + before = head.weight.detach().clone() + state = slots.export_state('a', host, lambda tensor: tensor) + state['task_head.weight'].fill_(99) + state['task_head.temperature'] = torch.ones(3) + with pytest.raises(ValueError, match='shape'): + slots.import_state('a', host, state, _write) + torch.testing.assert_close(head.weight, before) From a3ad125d8d0b32b3df017139914e32b5998f45ba Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Sat, 14 Nov 2026 01:35:46 +0000 Subject: [PATCH 02/18] fix(server): preserve family loaders and task data contracts Validate model families before allocation and carry custom components, task heads and encoded tensors through the HTTP training path. --- .../Usage Guide/Server and Client/Server.md | 31 ++++ .../\346\234\215\345\212\241\347\253\257.md" | 31 ++++ src/twinkle/server/config/training.py | 45 +++-- src/twinkle/server/lifecycle/submit.py | 17 +- src/twinkle/server/model/app.py | 35 +++- .../model/backends/transformers_model.py | 8 +- src/twinkle/server/runtime.py | 2 + src/twinkle/signals/response.py | 23 ++- src/twinkle/utils/loader.py | 13 +- .../model/multi_lora_transformers.py | 2 +- tests/server/config/test_training_config.py | 62 +++++-- .../integration/task_http_validation.py | 13 +- .../lifecycle/test_to_backend_inputs.py | 20 +++ tests/server/model/test_task_head.py | 154 ++++++++++++++++++ .../test_model_loader_construction.py | 63 +++++++ .../test_sentence_transformer_tenants.py | 57 +++++++ tests/utils/test_component_imports.py | 26 +++ tests/utils/test_response_token_scores.py | 24 +++ 18 files changed, 570 insertions(+), 56 deletions(-) create mode 100644 tests/transformers/test_model_loader_construction.py create mode 100644 tests/utils/test_component_imports.py create mode 100644 tests/utils/test_response_token_scores.py diff --git a/docs/source_en/Usage Guide/Server and Client/Server.md b/docs/source_en/Usage Guide/Server and Client/Server.md index ecb5f1730..e19b18c03 100644 --- a/docs/source_en/Usage Guide/Server and Client/Server.md +++ b/docs/source_en/Usage Guide/Server and Client/Server.md @@ -511,6 +511,37 @@ Release models when training finishes; Twinkle clients can call `remove_adapter( Complete configurations are available in `cookbook/client/server/transformer/server_config.yaml` and `cookbook/client/server/megatron/server_config.yaml`. +### Swift decision models + +Install the matching Swift dev version on the server and client. A decision family owns its loader, template, readout head and loss; Twinkle supplies training instances and tenant isolation. Add these fields to the `args` of a model application, keeping its device layout and instance limits: + +```yaml +model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink +backend: transformers +train_mode: lora +task_type: decision +model_loader: swift.dev.model.loader:build_model_loader +max_loras: 2 +env_vars: + TWINKLE_TRUST_REMOTE_CODE: '1' +``` + +The loader also supplies the multimodal module paths used by Transformers LoRA targeting. For Megatron, use `backend: megatron` and a supported model/bridge with a matching device group and parallelism layout. Loading through Swift does not add architectures missing from the Megatron bridge. Configure different tasks as separate model applications. + +Encode rows using Swift's `StartLuxPreprocessor` and `StartLuxTemplate`. Configure the existing processor and loss through explicit component imports, without a global registration hook: + +```python +model.set_template('swift.dev.template.Template', template_spec=template_spec) +model.set_processor('swift.dev.processor.server_plugin:DevInputProcessor', template_spec=template_spec) +model.set_loss('swift.dev.loss.decision:ScoringLoss') +``` + +`template_spec` is the JSON representation of the same Swift `TemplateSpec` used by the client encoder. `DevInputProcessor` carries `target_probs` to the decision collator, which creates floating-point loss targets. Token `labels` on the HTTP wire remain integers. Explicit `module:Class` component imports require the trust switch, as does the configured model loader. + +Attached-head checkpoints save parameters and calibration buffers together, including nonpersistent buffers. Removing a LoRA resets both before its head slot is reused. Older parameter-only head checkpoints remain loadable, but cannot recover calibration values that were never saved. + +For full-parameter Megatron decision training, optimizer checkpoints use MCore's native model-space format with CPU aggregation. Reserve host memory for the complete optimizer state; restore uses the format recorded in checkpoint metadata. Other tasks and Megatron FSDP retain their existing formats. + ### Automatic sampling scaling Set instance counts and the scaling target in the `args` of your `import_path: sampler` entry. For example: diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" index 7b51eb47e..3f67daf7a 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" @@ -505,6 +505,37 @@ twinkle-server check-config -c server_config.yaml 完整配置可参考仓库中的 `cookbook/client/server/transformer/server_config.yaml` 和 `cookbook/client/server/megatron/server_config.yaml`。 +### Swift 决策模型 + +服务端和客户端需要安装匹配的 Swift dev 版本。决策模型的 loader、模板、读出头和 loss 复用 Swift 实现,Twinkle 负责训练实例与租户隔离。在 model application 的 `args` 中增加以下配置,并保留该实例的设备布局和数量限制: + +```yaml +model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink +backend: transformers +train_mode: lora +task_type: decision +model_loader: swift.dev.model.loader:build_model_loader +max_loras: 2 +env_vars: + TWINKLE_TRUST_REMOTE_CODE: '1' +``` + +loader 还会提供 Transformers LoRA 选择模块时使用的多模态架构信息。使用 Megatron 时设置 `backend: megatron`,配置匹配的设备组与并行布局,并确认模型架构受对应 bridge 支持;接入 Swift loader 不会补齐 bridge 尚不支持的架构。不同任务仍分别配置 model application。 + +客户端通过 Swift 的 `StartLuxPreprocessor` 和 `StartLuxTemplate` 编码数据,再用明确的组件路径配置已有 processor 和 loss,无需全局注册钩子: + +```python +model.set_template('swift.dev.template.Template', template_spec=template_spec) +model.set_processor('swift.dev.processor.server_plugin:DevInputProcessor', template_spec=template_spec) +model.set_loss('swift.dev.loss.decision:ScoringLoss') +``` + +`template_spec` 是客户端编码所用 Swift `TemplateSpec` 的 JSON 表示。`DevInputProcessor` 将 `target_probs` 交给决策任务的 collator,由它构造浮点训练目标;HTTP 中的 token `labels` 仍保持整数约束。`模块:类名` 组件导入与配置中的 model loader 一样,需要开启信任开关。 + +附加任务头的 checkpoint 同时保存参数与校准 buffer,包括 nonpersistent buffer。移除 LoRA 后,两者都会重置,再将槽位交给新租户。旧的仅参数 checkpoint 仍可加载,但无法恢复旧文件从未保存过的校准值。 + +Megatron 决策模型全参训练保存优化器状态时,使用 MCore 原生模型参数格式并在 CPU 上聚合,需要为完整优化器状态预留主机内存;恢复时读取 checkpoint 自带的格式 metadata。其他任务和 Megatron FSDP 继续使用原有格式。 + ### 采样服务自动扩缩容 在 `import_path: sampler` 的 `args` 中设置实例数量和扩容目标。例如: diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index 259053050..171629e9b 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -3,6 +3,7 @@ from __future__ import annotations import math +from pathlib import Path from typing import TYPE_CHECKING, Any, Literal from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, ValidationInfo, field_validator, model_validator @@ -58,9 +59,10 @@ class TrainingArguments(BaseModel): model_loader: str | None = None gradient_checkpointing: bool | None = Field(default=None, strict=True) mixed_precision: Literal["no", "fp8", "fp16", "bf16"] | None = None + extra_torch_kwargs: dict[str, Any] | None = None # The readout family names the head task this deployment serves (see # ``twinkle/model/tasks.py``); ``causal_lm`` carries no head. - task_type: Literal["causal_lm", "seq_cls", "reranker", "value"] | None = None + task_type: Literal["causal_lm", "embedding", "seq_cls", "reranker", "value", "decision"] | None = None num_labels: int | None = Field(default=None, gt=0, strict=True) language_model_only: bool | None = Field(default=None, strict=True) @@ -70,6 +72,8 @@ def _validate_head(self): raise ValueError('task_type="seq_cls" requires num_labels') if self.num_labels is not None and self.task_type not in ("seq_cls", "reranker", "value"): raise ValueError('num_labels requires a readout task_type (seq_cls / reranker / value)') + if self.task_type == "decision" and self.model_loader is None: + raise ValueError('task_type="decision" requires a family model_loader') return self @model_validator(mode="after") @@ -107,22 +111,28 @@ class ModelArgs(TrainingArguments, LoraArguments): @model_validator(mode="after") def _validate_spectral_backend(self): + if (self.task_type == 'embedding' and self.model_cls is None + and (Path(self.model_id) / 'modules.json').is_file()): + raise ValueError( + 'native sentence-transformers checkpoint requires model_cls; ' + 'the generic embedding wrapper cannot preserve its module pipeline') if self.strategy is not None and self.backend != "transformers": raise ValueError("strategy is only supported by the transformers backend") - if self.backend != "transformers" and ( - self.gradient_checkpointing is not None or self.mixed_precision is not None - ): - raise ValueError("gradient_checkpointing and mixed_precision require the transformers backend") + if self.gradient_checkpointing is not None and self.backend != "transformers": + raise ValueError("gradient_checkpointing requires the transformers backend") + if self.mixed_precision is not None and self.backend == "mock": + raise ValueError("mixed_precision requires the transformers or megatron backend") if self.hybrid is not None: if self.backend != "transformers": raise ValueError("hybrid is only supported by the transformers backend") if self.train_mode == "full": raise ValueError('hybrid is not supported with train_mode="full"') if self.model_cls is not None: - if self.backend != "transformers" or self.train_mode != "lora" or self.hybrid is not None: - raise ValueError("model_cls requires a transformers LoRA deployment without hybrid") - if self.model_loader is not None and self.backend != "transformers": - raise ValueError("model_loader requires the transformers backend") + if (self.backend != "transformers" or self.train_mode not in ("lora", "full") + or self.hybrid is not None): + raise ValueError("model_cls requires a transformers LoRA/full deployment") + if self.model_loader is not None and self.backend == "mock": + raise ValueError("model_loader requires the transformers or megatron backend") if self.language_model_only is not None and self.backend != "megatron": raise ValueError("language_model_only requires the megatron backend") return self @@ -208,17 +218,18 @@ def _validate_backend(self): raise ValueError("device_mesh.device_type must match device_type") if self.config.strategy is not None and self.backend != "transformers": raise ValueError("strategy requires the transformers backend") - if self.backend != "transformers" and ( - self.config.gradient_checkpointing is not None or self.config.mixed_precision is not None - ): - raise ValueError("gradient_checkpointing and mixed_precision require the transformers backend") + if self.config.gradient_checkpointing is not None and self.backend != "transformers": + raise ValueError("gradient_checkpointing requires the transformers backend") + if self.config.mixed_precision is not None and self.backend == "mock": + raise ValueError("mixed_precision requires the transformers or megatron backend") if isinstance(self.config, LoraTrainingConfig) and self.config.hybrid and self.backend != "transformers": raise ValueError("hybrid requires the transformers backend") if self.config.model_cls is not None: - if self.backend != "transformers" or self.mode != "lora" or self.config.hybrid is not None: - raise ValueError("model_cls requires a transformers LoRA deployment without hybrid") - if self.config.model_loader is not None and self.backend != "transformers": - raise ValueError("model_loader requires the transformers backend") + if (self.backend != "transformers" or self.mode not in ("lora", "full") + or getattr(self.config, "hybrid", None) is not None): + raise ValueError("model_cls requires a transformers LoRA/full deployment") + if self.config.model_loader is not None and self.backend == "mock": + raise ValueError("model_loader requires the transformers or megatron backend") return self @property diff --git a/src/twinkle/server/lifecycle/submit.py b/src/twinkle/server/lifecycle/submit.py index 318886216..db0a44bb8 100644 --- a/src/twinkle/server/lifecycle/submit.py +++ b/src/twinkle/server/lifecycle/submit.py @@ -15,7 +15,7 @@ from twinkle.data_format import InputFeature, Trajectory, is_encoded from twinkle.protocol.json_utils import json_safe from twinkle.protocol.types.base import FieldRole, fields_with_role -from twinkle.protocol.types.data import export_batch +from twinkle.protocol.types.data import VLM_TENSOR_FIELDS, export_batch from twinkle.protocol.types.lifecycle import TaskEnvelope from twinkle.protocol.types.model import (AdapterRequest, DataPlaneForwardRequest, ForwardBackwardTaskRequest, TrainingRequest) @@ -46,8 +46,8 @@ def to_backend_inputs(inputs: Any, *, single: bool = False) -> Any: With ``single=True`` exactly one object is returned (the streaming path accepts only one input) and a batch of any other size is a ``ValueError``. Plain dicts pass - through unchanged: the data-plane path resolves rows itself and never goes through - the wire schema. + through with their encoded media tensors restored: JSON lists cannot be passed to + a template's tensor collator, nor concatenated row-wise without losing shape. """ entries = export_batch(inputs) if isinstance(inputs, list) else inputs if single: @@ -67,7 +67,16 @@ def to_backend_inputs(inputs: Any, *, single: bool = False) -> Any: def _as_backend_entry(entry: dict[str, Any]) -> Any: """One exported entry as its ``TypedDict`` shape.""" - return InputFeature(**entry) if is_encoded(entry) else Trajectory(**entry) + if not is_encoded(entry): + return Trajectory(**entry) + result = InputFeature(**entry) + media = VLM_TENSOR_FIELDS.intersection(result) + if media: + import torch + for key in media: + if result[key] is not None and not isinstance(result[key], torch.Tensor): + result[key] = torch.as_tensor(result[key]) + return result def backend_kwargs(body: Any) -> dict[str, Any]: diff --git a/src/twinkle/server/model/app.py b/src/twinkle/server/model/app.py index e40acb753..7e5c09fee 100644 --- a/src/twinkle/server/model/app.py +++ b/src/twinkle/server/model/app.py @@ -53,6 +53,14 @@ def _validate_model_task(kw: dict[str, Any]) -> None: task_type = kw.pop('task_type', None) if task_type is None: return + if task_type == 'decision': + loader = kw.get('model_loader') + head_factory = getattr(loader, 'task_head_loader', None) + if getattr(loader, 'task_type', None) != 'decision' or not callable(head_factory): + raise ValueError('decision requires a family model_loader with task_head_loader()') + kw['task'] = 'decision' + kw['task_head_loader'] = head_factory() + return from transformers import AutoConfig from twinkle.hub import HubOperation @@ -112,19 +120,33 @@ def _make_model_class(base: type) -> type: def _make_transformers_model(kw: dict[str, Any]) -> Any: train_mode = kw.pop('train_mode', 'lora') + # Family-owned module paths keep all-linear LoRA on the language-model side + # of a multimodal backbone, as on the loader's native training path. + arch = getattr(kw.get('model_loader'), 'model_arch', None) + if arch is not None: + kw['model_arch'] = {name: getattr(arch, name) for name in + ('language_model', 'vision_tower', 'aligner', 'lm_head') + if getattr(arch, name, None)} # ``model_cls`` arrives as plain data and is resolved here, so the class object never reaches the # constructor -- it would either be rejected by the remote-class layer in safe mode - # (``TWINKLE_TRUST_REMOTE_CODE=0``) or leak into every worker's constructor kwargs. The schema only - # allows it for a LoRA-mode transformers deployment. + # (``TWINKLE_TRUST_REMOTE_CODE=0``) or leak into every worker's constructor kwargs. The schema + # allows it for non-hybrid transformers deployments. model_cls_spec = kw.pop('model_cls', None) if train_mode == 'full': from .backends.transformers_model import TwinkleCompatFullTransformersModel for key in _LORA_ONLY_CTOR_KWARGS: kw.pop(key, None) - # The plain TransformersModel attaches heads at add_adapter time -- a full-mode - # deployment has no adapter, so an attached-head task_type has nothing to ride on. - if kw.pop('task', None) is not None: - raise ValueError('the transformers full-mode backend serves no task head; use train_mode: lora') + num_labels = kw.pop('num_labels', None) + task = kw.pop('task', None) + if task == 'decision' and getattr(kw.get('model_loader'), 'task_type', None) == 'decision': + # The family loader attaches its single head while constructing the full model. + kw.pop('task_head_loader', None) + elif task is not None: + kw['task'] = task + if num_labels is not None: + kw['num_labels'] = num_labels + if model_cls_spec is not None: + return _make_model_class(_resolve_model_cls(model_cls_spec))(**kw) return TwinkleCompatFullTransformersModel(**kw) from .backends.transformers_model import (TwinkleCompatSpectralHybridTransformersModel, TwinkleCompatTransformersModel) @@ -242,6 +264,7 @@ async def __init__(self, nproc_per_node=nproc_per_node, device_group=self.device_group, device_mesh_dict=device_mesh, + extra_torch_kwargs=kwargs.pop('extra_torch_kwargs', None), ) self.replica_id = self.training_context.instance_id self.max_loras = kwargs.get('max_loras', 5) diff --git a/src/twinkle/server/model/backends/transformers_model.py b/src/twinkle/server/model/backends/transformers_model.py index 2c175e692..7b3aa5b7a 100644 --- a/src/twinkle/server/model/backends/transformers_model.py +++ b/src/twinkle/server/model/backends/transformers_model.py @@ -14,7 +14,7 @@ """ from tinker import types -from twinkle import remote_class, remote_function +from twinkle import DeviceMesh, remote_class, remote_function from twinkle.data_format import InputFeature, Trajectory from twinkle.model import MultiLoraTransformersModel, SpectralHybridTransformersModel from twinkle.model.transformers import TransformersModel @@ -133,3 +133,9 @@ class TwinkleCompatFullTransformersModel(_TransformersTinkerCompatMixin, Transfo target the empty-string default optimizer group created by ``TransformersModel.__init__``. """ + + def __init__(self, *args, device_mesh: DeviceMesh = None, task=None, num_labels=None, + task_head_loader=None, **kwargs): + super().__init__(*args, device_mesh=device_mesh, **kwargs) + if task is not None or task_head_loader is not None: + self._add_task_head('', task or 'decision', task_head_loader, num_labels) diff --git a/src/twinkle/server/runtime.py b/src/twinkle/server/runtime.py index 1a6d8e131..4d9df81e3 100644 --- a/src/twinkle/server/runtime.py +++ b/src/twinkle/server/runtime.py @@ -19,6 +19,7 @@ def init_twinkle_runtime( device_mesh_dict: dict[str, Any], *, ncpu_proc_per_node: int | None = None, + extra_torch_kwargs: dict[str, Any] | None = None, ) -> Any | None: """Initialize the Twinkle distributed runtime and build a DeviceMesh. @@ -43,6 +44,7 @@ def init_twinkle_runtime( 'nproc_per_node': nproc_per_node, 'groups': [device_group], 'lazy_collect': False, + 'extra_torch_kwargs': extra_torch_kwargs, } if ncpu_proc_per_node is not None: init_kwargs['ncpu_proc_per_node'] = ncpu_proc_per_node diff --git a/src/twinkle/signals/response.py b/src/twinkle/signals/response.py index b44fccad1..5996accf0 100644 --- a/src/twinkle/signals/response.py +++ b/src/twinkle/signals/response.py @@ -45,18 +45,23 @@ def response_token_scores(outputs: Any, features: List[Dict[str, Any]], *, outpu ``outputs`` is the forward's return (a pending handle is resolved here) -- a dict whose ``output_key`` channel (``'logps'`` for a causal scorer, ``'logits'`` / ``'values'`` for a value - head) is a ``[B, T]`` tensor or a bare tensor. Full-sequence, right-padded rows are indexed at - each feature's own response positions, so right-padding never shifts them. + head) may contain a batch tensor or variable-length microbatches, including their JSON form. + Full-sequence, right-padded rows are indexed at each feature's response positions. """ outputs = outputs.result() if hasattr(outputs, 'result') else outputs values = outputs.get(output_key) if isinstance(outputs, dict) else outputs if values is None: raise RuntimeError(f'the forward returned no {output_key!r} channel.') - values = torch.as_tensor(values).detach().float().cpu() - if values.dim() == 1: - values = values.unsqueeze(0) - if values.shape[0] != len(features): - raise RuntimeError(f'forward returned {values.shape[0]} rows for {len(features)} features; ' + if isinstance(values, (list, tuple)) and values and isinstance(values[0], (list, tuple, np.ndarray, torch.Tensor)): + rows = [] + for batch in values: + batch = torch.as_tensor(batch).detach().float().cpu() + rows.extend(batch.unbind(0) if batch.dim() > 1 else [batch]) + else: + values = torch.as_tensor(values).detach().float().cpu() + rows = list(values.unbind(0)) if values.dim() > 1 else [values] + if len(rows) != len(features): + raise RuntimeError(f'forward returned {len(rows)} rows for {len(features)} features; ' 'per-row alignment is impossible.') - return [_row_scores(values[i].reshape(-1), response_positions(feature), feature) - for i, feature in enumerate(features)] + return [_row_scores(row.reshape(-1), response_positions(feature), feature) + for row, feature in zip(rows, features, strict=True)] diff --git a/src/twinkle/utils/loader.py b/src/twinkle/utils/loader.py index 4228bdb1e..8e0008927 100644 --- a/src/twinkle/utils/loader.py +++ b/src/twinkle/utils/loader.py @@ -157,7 +157,7 @@ def construct_class( """Try to load a class. Args: - func: The input class or class name/plugin name to load instance from + func: Class/instance, built-in name, plugin path, or trusted ``module:Class`` spec. class_T: The base class of the instance module_T: The module of the class_T **init_args: The args to construct the instruct @@ -179,7 +179,16 @@ def construct_class( cls = getattr(module, func) break else: - cls = Plugin.load_plugin(func, class_T) + module_name, separator, name = func.partition(':') + if (separator and all(part.isidentifier() for part in module_name.split('.')) + and name.isidentifier() and not Path(func).exists()): + if not trust_remote_code(): + raise ValueError('Component import specs require TWINKLE_TRUST_REMOTE_CODE != 0') + cls = getattr(importlib.import_module(module_name), name) + if not isinstance(cls, type) or not issubclass(cls, class_T): + raise TypeError(f'{func!r} must name a {class_T.__name__} subclass') + else: + cls = Plugin.load_plugin(func, class_T) return cls(**init_args) else: # Do nothing by default diff --git a/src/twinkle_client/model/multi_lora_transformers.py b/src/twinkle_client/model/multi_lora_transformers.py index 487af1b49..96b407cc8 100644 --- a/src/twinkle_client/model/multi_lora_transformers.py +++ b/src/twinkle_client/model/multi_lora_transformers.py @@ -422,7 +422,7 @@ def calculate_metric(self, is_training: bool = True, **kwargs) -> model_types.Ca # ------------------------------------------------------------------ # def save(self, name: str, **kwargs) -> model_types.SaveResponse: - """Save model checkpoint.""" + """Save a checkpoint; save_optimizer=True requires a complete accumulation window.""" return self._submit('save', model_types.SaveRequest, model_types.SaveResponse, name=name, **kwargs) def load(self, name: str, **kwargs) -> None: diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index d288bcb4e..6b7b115f8 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -63,13 +63,11 @@ async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construct driver.runtime.deploy.assert_awaited_once() -@pytest.mark.parametrize('mutation', ['full', 'hybrid', 'megatron']) +@pytest.mark.parametrize('mutation', ['hybrid', 'megatron']) def test_custom_model_class_rejects_incompatible_training_modes(mutation): app = model_application() app['args']['model_cls'] = 'worker_only.encoder:Model' - if mutation == 'full': - app['args']['train_mode'] = 'full' - elif mutation == 'hybrid': + if mutation == 'hybrid': app['args']['hybrid'] = {'allocation_path': '/tmp/allocation.json'} else: app['args']['backend'] = 'megatron' @@ -77,6 +75,31 @@ def test_custom_model_class_rejects_incompatible_training_modes(mutation): ServerConfig(applications=[app]) +@pytest.mark.parametrize('backend,train_mode', [ + ('transformers', 'lora'), + ('transformers', 'full'), + ('megatron', 'lora'), + ('megatron', 'full'), +]) +def test_native_st_without_model_cls_is_rejected_before_runtime(tmp_path, backend, train_mode): + (tmp_path / 'modules.json').write_text('[]') + app = model_application() + app['args'].update(model_id=str(tmp_path), task_type='embedding', backend=backend, train_mode=train_mode) + with pytest.raises(ValidationError, match='native sentence-transformers checkpoint requires model_cls'): + ServerConfig(applications=[app]) + + +def test_native_st_with_model_cls_keeps_the_supported_lora_entry(tmp_path): + (tmp_path / 'modules.json').write_text('[]') + app = model_application() + app['args'].update( + model_id=str(tmp_path), + task_type='embedding', + model_cls='swift.dev.model.multi_lora_sentence_transformer_model:MultiLoraSentenceTransformerModel', + ) + ServerConfig(applications=[app]) + + def test_st_example_is_a_cold_on_demand_custom_model(): config = ServerConfig.from_yaml('cookbook/client/swift_model/sentence_transformer_config.yaml') model = config.training_config.models[0] @@ -107,7 +130,7 @@ def test_existing_yaml_uses_explicit_instance_limits_and_business_idle(): for path in paths: config = ServerConfig.from_yaml(path) applications = [app for app in config.applications if app.import_path == 'model'] - for app, model in zip(applications, config.training_config.models): + for app, model in zip(applications, config.training_config.models, strict=True): assert model.config.idle_timeout_s == app.args.idle_timeout_s assert model.config.min_replicas == app.args.min_replicas assert model.config.max_replicas == app.args.max_replicas @@ -185,18 +208,15 @@ def test_existing_yaml_examples_keep_their_model_entries(monkeypatch): paths += list(Path('cookbook/client/async_rl').glob('server_config.yaml')) paths += list(Path('tests/server/config').glob('server_config*e2e*.yaml')) paths += list(Path('tests/server/fixtures').glob('server_config_mock*.yaml')) - assert len(paths) == 9 + assert paths for path in paths: config = ServerConfig.from_yaml(path) models = [app for app in config.applications if app.import_path == 'model'] - assert len(models) == len(config.training_config.models) == 1 - model = config.training_config.models[0] - assert model.model_id == models[0].args.model_id - assert model.route_prefix == models[0].route_prefix - mode = model.config - original = models[0] - expected_min = original.args.min_replicas - assert mode.min_replicas == expected_min + assert len(models) == len(config.training_config.models) > 0 + for original, model in zip(models, config.training_config.models, strict=True): + assert model.model_id == original.args.model_id + assert model.route_prefix == original.route_prefix + assert model.config.min_replicas == original.args.min_replicas assert ServerConfig.model_validate(config.to_yaml_dict()).training_config == config.training_config @@ -417,3 +437,17 @@ def test_transformers_still_accepts_single_device(): app = model_application() app['args'].update(device_group={'ranks': 1, 'device_type': 'cuda'}, device_mesh={'dp_size': 1}) assert ServerConfig(applications=[app]).training_config.models[0].config.num_devices == 1 + + +@pytest.mark.parametrize('backend', ['transformers', 'megatron']) +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_native_backends_preserve_loader_and_precision_configuration(backend, mode): + app = model_application() + app['args'].update(backend=backend, train_mode=mode, mixed_precision='no', + model_loader='worker_only.loader:load') + config = ServerConfig(applications=[app]) + restored = ServerConfig.model_validate(config.to_yaml_dict()) + model = restored.training_config.models[0] + assert model.backend == backend and model.mode == mode + assert model.config.model_loader == 'worker_only.loader:load' + assert model.config.mixed_precision == 'no' diff --git a/tests/server/integration/task_http_validation.py b/tests/server/integration/task_http_validation.py index 3f4468b56..dae555fd7 100644 --- a/tests/server/integration/task_http_validation.py +++ b/tests/server/integration/task_http_validation.py @@ -110,9 +110,18 @@ def weights(saved): initial_weights = weights(save(a, 'initial', 0)) for _ in range(3): step(a) + if args.backend == 'transformers': + from twinkle_client.exceptions import TaskFailedError + try: + save(a, 'partial', 3 * len(batch)) + except TaskFailedError as error: + assert 'complete gradient accumulation window' in str(error) + else: + raise AssertionError('An optimizer checkpoint with pending gradients was accepted') + step(a) trained = read(a) np.testing.assert_allclose(read(b), peer, rtol=0, atol=0) - snapshot = save(a, 'trained', 3 * len(batch)) + snapshot = save(a, 'trained', 4 * len(batch)) changed = weights(snapshot) assert changed.keys() == initial_weights.keys() updates = [key for key in changed if not np.array_equal(changed[key].float().numpy(), @@ -130,7 +139,7 @@ def weights(saved): assert fresh_state['cur_step'] == 0 np.testing.assert_allclose(read(c), initial, rtol=0, atol=2e-5) progress = c.resume_from_checkpoint(snapshot.twinkle_path) - assert progress['cur_step'] == 3 and progress['consumed_train_samples'] == 3 * len(batch) + assert progress['cur_step'] == 4 and progress['consumed_train_samples'] == 4 * len(batch) np.testing.assert_allclose(read(c), trained, rtol=0, atol=2e-5) step(c) step(c) diff --git a/tests/server/lifecycle/test_to_backend_inputs.py b/tests/server/lifecycle/test_to_backend_inputs.py index 4be370375..17b44e297 100644 --- a/tests/server/lifecycle/test_to_backend_inputs.py +++ b/tests/server/lifecycle/test_to_backend_inputs.py @@ -12,6 +12,26 @@ from twinkle.data_format import InputFeature, Trajectory from twinkle.server.lifecycle.submit import to_backend_inputs + +@pytest.mark.parametrize('single', [False, True]) +def test_wire_media_restores_tensor_shape_for_swift_collation(single): + import torch + from swift.dev.template import Template + + from twinkle.protocol.types.data import WireInputFeature + + wire = WireInputFeature(input_ids=[1, 2], pixel_values=[[0.5, 0.25], [0.1, 0.2]], + image_grid_thw=[[1, 1, 2]], target_probs=[[0.2, 0.8]]) + output = to_backend_inputs([wire], single=single) + row = output if single else output[0] + media = Template._data_collator_mm_data(object.__new__(Template), [row]) + assert media['pixel_values'].shape == (2, 2) + assert media['image_grid_thw'].shape == (1, 3) + assert media['pixel_values'].dtype == torch.float32 + assert media['image_grid_thw'].dtype == torch.int64 + assert row['target_probs'] == [[0.2, 0.8]] + assert wire.pixel_values == [[0.5, 0.25], [0.1, 0.2]] + _IF = {'input_ids': [1, 2, 3]} _TRAJ = {'messages': [{'role': 'user', 'content': 'hi'}]} diff --git a/tests/server/model/test_task_head.py b/tests/server/model/test_task_head.py index f30808a7e..4ddafb580 100644 --- a/tests/server/model/test_task_head.py +++ b/tests/server/model/test_task_head.py @@ -119,3 +119,157 @@ def test_transformers_backend_preserves_validated_head_contract(monkeypatch, tas app._validate_model_task(kwargs) app._make_transformers_model(kwargs) assert received == {'model_id': 'unused', 'task': task, 'num_labels': width} + + +@pytest.mark.parametrize('task,width', [('seq_cls', 3), ('reranker', 1)]) +def test_full_persisted_head_does_not_forward_num_labels_to_hf(monkeypatch, task, width): + from twinkle.server.model import app + from twinkle.server.model.backends import transformers_model + + received = {} + monkeypatch.setattr(transformers_model, 'TwinkleCompatFullTransformersModel', + lambda **kwargs: received.update(kwargs)) + app._make_transformers_model({'model_id': 'persisted-classifier', 'train_mode': 'full', + 'num_labels': width, 'max_loras': 2}) + assert received == {'model_id': 'persisted-classifier'} + + +@pytest.mark.parametrize('task,width', [('seq_cls', 3), ('reranker', 1)]) +def test_full_attached_head_reaches_full_model_constructor(monkeypatch, task, width): + from twinkle.server.model import app + from twinkle.server.model.backends import transformers_model + + received = {} + monkeypatch.setattr(transformers_model, 'TwinkleCompatFullTransformersModel', + lambda **kwargs: received.update(kwargs)) + app._make_transformers_model({'model_id': 'causal-backbone', 'train_mode': 'full', + 'task': task, 'num_labels': width}) + assert received == {'model_id': 'causal-backbone', 'task': task, 'num_labels': width} + + +def test_full_model_attaches_head_to_default_optimizer_group(monkeypatch): + from twinkle.server.model.backends import transformers_model + + monkeypatch.setattr(transformers_model.TransformersModel, '__init__', lambda self, **kwargs: None) + attached = [] + monkeypatch.setattr(transformers_model.TransformersModel, '_add_task_head', + lambda self, *args: attached.append(args)) + transformers_model.TwinkleCompatFullTransformersModel( + model_id='causal-backbone', task='seq_cls', num_labels=3) + assert attached == [('', 'seq_cls', None, 3)] + + +def test_server_forwards_full_determinism_to_workers(monkeypatch): + import twinkle + from twinkle.server.runtime import init_twinkle_runtime + + received = {} + monkeypatch.setattr(twinkle, 'initialize', lambda **kwargs: received.update(kwargs)) + mesh = SimpleNamespace(world_size=2) + monkeypatch.setattr(twinkle.DeviceMesh, 'from_sizes', lambda **kwargs: mesh) + result = init_twinkle_runtime( + is_mock=False, nproc_per_node=2, device_group=SimpleNamespace(name='model'), + device_mesh_dict={'fsdp_size': 2}, extra_torch_kwargs={'full_determinism': True}) + assert result is mesh + assert received['extra_torch_kwargs'] == {'full_determinism': True} + + +def test_full_custom_st_class_uses_compat_dispatch(monkeypatch): + from twinkle.server.model import app + + marker = type('SentenceTransformerModel', (), {}) + received = {} + monkeypatch.setattr(app, '_resolve_model_cls', lambda spec: marker) + monkeypatch.setattr(app, '_make_model_class', + lambda base: lambda **kwargs: received.update(base=base, kwargs=kwargs)) + app._make_transformers_model({'model_id': 'st-model', 'train_mode': 'full', + 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'}) + assert received['base'] is marker + assert received['kwargs'] == {'model_id': 'st-model'} + + +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_native_st_model_cls_is_accepted_for_transformers_only(mode): + from twinkle.server.config.training import ModelArgs + + kwargs = {'model_id': 'st-model', 'backend': 'transformers', 'train_mode': mode, + 'device_group': {'name': 'model', 'ranks': 1}, 'device_mesh': {'dp_size': 1}, + 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'} + ModelArgs(**kwargs) + kwargs['backend'] = 'megatron' + with pytest.raises(ValidationError, match='model_cls'): + ModelArgs(**kwargs) + + +def test_full_native_st_model_cls_survives_resolved_config(): + from twinkle.server.config.training import TrainingModelConfig + + config = TrainingModelConfig( + name='st', model_id='st-model', backend='transformers', device_type='cuda', mode='full', + config={'num_devices': 1, 'device_mesh': {'dp_size': 1}, + 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'}) + assert config.config.model_cls.endswith('SentenceTransformerModel') + + +def test_full_remote_class_preserves_device_mesh_contract(monkeypatch): + from twinkle.infra.dispatch import _get_device_mesh_param_name + from twinkle.server.model.backends import transformers_model + + mesh = SimpleNamespace(fsdp_size=2) + received = {} + monkeypatch.setattr(transformers_model.TransformersModel, '__init__', + lambda self, **kwargs: received.update(kwargs)) + transformers_model.TwinkleCompatFullTransformersModel(model_id='causal-backbone', device_mesh=mesh) + assert received['device_mesh'] is mesh + assert _get_device_mesh_param_name( + transformers_model.TwinkleCompatFullTransformersModel.__init__) == 'device_mesh' + + +def test_embedding_deployment_accepts_explicit_task(monkeypatch): + from transformers import AutoConfig + + from twinkle.hub import HubOperation + + args = TrainingArguments(task_type='embedding') + monkeypatch.setattr(HubOperation, 'download_model', lambda path: path) + monkeypatch.setattr(AutoConfig, 'from_pretrained', lambda *a, **kw: SimpleNamespace( + architectures=['Qwen2ForCausalLM'])) + kwargs = {'model_id': 'causal-backbone', 'task_type': args.task_type} + _validate_model_task(kwargs) + assert kwargs == {'model_id': 'causal-backbone'} + + +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_decision_family_supplies_worker_head_factory(mode): + loader = SimpleNamespace(task_type='decision', task_head_loader=lambda: ('family', 'Loader')) + args = TrainingArguments(task_type='decision', model_loader='family:build') + kwargs = {'model_id': 'decision-backbone', 'task_type': args.task_type, + 'model_loader': loader, 'train_mode': mode} + _validate_model_task(kwargs) + assert kwargs['task'] == 'decision' + assert kwargs['task_head_loader'] == ('family', 'Loader') + with pytest.raises(ValueError, match='family model_loader'): + _validate_model_task({'task_type': 'decision', 'model_loader': SimpleNamespace()}) + with pytest.raises(ValidationError, match='model_loader'): + TrainingArguments(task_type='decision') + + +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_transformers_decision_uses_loader_architecture(monkeypatch, mode): + from twinkle.server.model import app + from twinkle.server.model.backends import transformers_model + + received = {} + for name in ('TwinkleCompatFullTransformersModel', 'TwinkleCompatTransformersModel'): + monkeypatch.setattr(transformers_model, name, lambda **kw: received.update(kw)) + loader = SimpleNamespace(task_type='decision', model_arch=SimpleNamespace( + language_model=['model.language_model'], vision_tower=['model.visual'], + aligner=['model.visual.merger'], lm_head='lm_head')) + app._make_transformers_model({'model_id': 'decision-backbone', 'train_mode': mode, + 'task': 'decision', 'task_head_loader': ('family', 'Loader'), + 'model_loader': loader}) + assert received['model_arch']['vision_tower'] == ['model.visual'] + if mode == 'full': + assert 'task' not in received and 'task_head_loader' not in received + else: + assert received['task'] == 'decision' diff --git a/tests/transformers/test_model_loader_construction.py b/tests/transformers/test_model_loader_construction.py new file mode 100644 index 000000000..572ab0f30 --- /dev/null +++ b/tests/transformers/test_model_loader_construction.py @@ -0,0 +1,63 @@ +# Copyright (c) ModelScope Contributors. All rights reserved. +"""A supplied loader owns external architectures, including empty-weight construction.""" +import pytest +import torch + + +@pytest.mark.parametrize('multi', [False, True]) +def test_loader_builds_architecture_outside_transformers(tmp_path, monkeypatch, multi): + from tokenizers import Tokenizer + from tokenizers.models import WordPiece + from transformers import PreTrainedTokenizerFast, Qwen2Config, Qwen2ForCausalLM + + from twinkle import Platform, initialize + from twinkle.model.transformers.multi_lora_transformers import MultiLoraTransformersModel + from twinkle.model.transformers.transformers import TransformersModel + + monkeypatch.setenv('ACCELERATE_USE_CPU', 'true') + monkeypatch.setattr(Platform, 'get_local_device', lambda *args, **kwargs: 'cpu') + initialize(mode='local') + config = Qwen2Config(vocab_size=32, hidden_size=16, intermediate_size=32, num_hidden_layers=1, + num_attention_heads=2, num_key_value_heads=2, pad_token_id=0) + config.architectures = ['ExternalArchitectureOwnedByLoader'] + config.save_pretrained(tmp_path) + tokenizer = PreTrainedTokenizerFast( + tokenizer_object=Tokenizer(WordPiece({'[PAD]': 0, '[UNK]': 1}, unk_token='[UNK]')), + pad_token='[PAD]', unk_token='[UNK]') + tokenizer.save_pretrained(tmp_path) + calls = [] + + class Loader: + def build_config(self, path): + return config + + def process_config(self, value): + return value + + def build_processor(self, path, value): + calls.append('processor') + return tokenizer + + def process_tokenizer(self, value): + return value + + def build_model(self, path, **kwargs): + calls.append('model') + return Qwen2ForCausalLM(kwargs['config']) + + def build_model_from_config(self, value, **kwargs): + calls.append('meta') + return Qwen2ForCausalLM(value) + + def process_model(self, value): + return value + + loader = Loader() + cls = MultiLoraTransformersModel if multi else TransformersModel + wrapper = cls(model_id=str(tmp_path), model_loader=loader, mixed_precision='no', + gradient_checkpointing=False, **({'max_loras': 2, 'max_r': 4, + 'target_modules': ['q_proj']} if multi else {})) + assert calls == ['processor', 'model'] + empty = wrapper._init_empty_model_from_config(None, model_loader=loader, processor=tokenizer) + assert all(p.device.type == 'meta' for p in empty.parameters()) + assert calls[-1] == 'meta' diff --git a/tests/transformers/test_sentence_transformer_tenants.py b/tests/transformers/test_sentence_transformer_tenants.py index d13cf6e99..4bdc2ca5b 100644 --- a/tests/transformers/test_sentence_transformer_tenants.py +++ b/tests/transformers/test_sentence_transformer_tenants.py @@ -106,3 +106,60 @@ def step(name): torch.testing.assert_close(reloaded, saved_vectors, rtol=0, atol=1e-6) model.remove_adapter('b') model.remove_adapter('c') + + +def test_st_full_checkpoint_restores_optimizer_and_continuation(tmp_path, monkeypatch): + from sentence_transformers.base.modules import Transformer, Normalize + from sentence_transformers.sentence_transformer.modules import Pooling + from tokenizers import Tokenizer + from tokenizers.models import WordPiece + from transformers import BertConfig, BertModel, PreTrainedTokenizerFast + from swift.dev.model.sentence_transformer_model import SentenceTransformerModel + from twinkle import Platform, initialize + + monkeypatch.setenv('ACCELERATE_USE_CPU', 'true') + monkeypatch.setattr(Platform, 'get_local_device', lambda *args, **kwargs: 'cpu') + initialize(mode='local') + backbone = tmp_path / 'bert' + BertModel(BertConfig(vocab_size=32, hidden_size=16, num_hidden_layers=1, num_attention_heads=2, + intermediate_size=32, hidden_dropout_prob=0, attention_probs_dropout_prob=0)).save_pretrained(backbone) + vocab = {'[PAD]': 0, '[UNK]': 1, '[CLS]': 2, '[SEP]': 3, '[MASK]': 4} + vocab.update({f't{i}': i for i in range(5, 32)}) + PreTrainedTokenizerFast(tokenizer_object=Tokenizer(WordPiece(vocab, unk_token='[UNK]')), + pad_token='[PAD]', unk_token='[UNK]').save_pretrained(backbone) + original = tmp_path / 'st' + st.SentenceTransformer(modules=[Transformer(str(backbone)), Pooling(16), Normalize()], device='cpu').save( + str(original)) + batch = [{'input_ids': [2, 5 + i, 3], 'attention_mask': [1] * 3, 'labels': [int(i % 3 == 0)]} + for i in range(6)] + + def build(): + model = SentenceTransformerModel(model_id=str(original), mixed_precision='no', + gradient_checkpointing=False) + model.set_processor('InputProcessor', padding_side='right') + model.set_loss('InfonceLoss', temperature=0.1) + model.set_optimizer('AdamW', lr=1e-3) + return model + + def vectors(model): + return model.forward_only(inputs=copy.deepcopy(batch))['embeddings'].cpu() + + def step(model): + model.forward_backward(inputs=copy.deepcopy(batch), task='embedding') + model.clip_grad_and_step() + + model = build() + vectors(model) + step(model) + checkpoint = model.save('trained', output_dir=str(tmp_path), save_optimizer=True, + consumed_train_samples=6) + restored_vectors = vectors(model) + step(model) + continued = vectors(model) + fresh = build() + progress = fresh.resume_from_checkpoint(checkpoint) + assert progress['consumed_train_samples'] == 6 and progress['cur_step'] == 1 + assert fresh.optimizer_group[''].optimizer.state + torch.testing.assert_close(vectors(fresh), restored_vectors, rtol=0, atol=0) + step(fresh) + torch.testing.assert_close(vectors(fresh), continued, rtol=0, atol=0) diff --git a/tests/utils/test_component_imports.py b/tests/utils/test_component_imports.py new file mode 100644 index 000000000..2c96e61c1 --- /dev/null +++ b/tests/utils/test_component_imports.py @@ -0,0 +1,26 @@ +# Copyright (c) ModelScope Contributors. All rights reserved. +"""Explicit component imports preserve the base-class and trust contracts.""" +import pytest + +from twinkle.utils.loader import construct_class + + +def test_import_instantiates_the_requested_class(monkeypatch): + from collections import Counter + + monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '1') + result = construct_class('collections:Counter', dict, [], a=2) + assert type(result) is Counter and result == {'a': 2} + + +@pytest.mark.parametrize('name', ['collections:deque', 'collections:__name__']) +def test_import_rejects_other_component_types(monkeypatch, name): + monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '1') + with pytest.raises(TypeError, match='dict subclass'): + construct_class(name, dict, []) + + +def test_safe_mode_rejects_import_before_executing_module(monkeypatch): + monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '0') + with pytest.raises(ValueError, match='TWINKLE_TRUST_REMOTE_CODE'): + construct_class('does_not_exist:Component', dict, []) diff --git a/tests/utils/test_response_token_scores.py b/tests/utils/test_response_token_scores.py new file mode 100644 index 000000000..0cd8103ec --- /dev/null +++ b/tests/utils/test_response_token_scores.py @@ -0,0 +1,24 @@ +# Copyright (c) ModelScope Contributors. All rights reserved. +"""Response positions must survive variable-length Megatron microbatches.""" +import pytest +import torch + +from twinkle.signals.response import response_token_scores + + +@pytest.mark.parametrize('serialized', [False, True]) +def test_variable_length_microbatches_keep_feature_order(serialized): + batches = [torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[5., 6., 7.]])] + values = [batch.tolist() for batch in batches] if serialized else batches + features = [{'labels': [-100, 1]}, {'labels': [1, -100]}, {'labels': [-100, 1, 1]}] + assert response_token_scores({'logps': values}, features) == [[2.], [3.], [6., 7.]] + + +@pytest.mark.parametrize('values', [torch.tensor([[1., 2.], [3., 4.]]), [[1., 2.], [3., 4.]]]) +def test_dense_rows_remain_aligned(values): + assert response_token_scores({'logps': values}, [{'labels': [-100, 1]}] * 2) == [[2.], [4.]] + + +def test_microbatch_row_count_mismatch_fails(): + with pytest.raises(RuntimeError, match='per-row alignment'): + response_token_scores({'logps': [[[1., 2.], [3., 4.]]]}, [{'labels': [-100, 1]}]) From 42bdd0be892296d6b76428b72b51d6afb5f65e94 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 06:37:30 +0000 Subject: [PATCH 03/18] fix(server): close task checkpoint and model admission gaps Co-authored-by: Codex --- .../transformers/strategy/native_fsdp.py | 2 +- .../model/transformers/transformers.py | 2 +- src/twinkle/server/config/training.py | 14 ++++--- src/twinkle/server/model/app.py | 2 + tests/server/config/test_training_config.py | 13 ++++++ tests/server/model/test_task_head.py | 23 ++++++++++ .../transformers/test_native_fsdp_strategy.py | 18 ++++++++ .../test_qwen35_fla_bwd_precision.py | 42 +++++++++++++++++++ 8 files changed, 109 insertions(+), 7 deletions(-) diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 006d12ad8..4ca516134 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -324,7 +324,7 @@ def restore_independent_task_head_state(self, head, state) -> bool: tensors = {**dict(head.named_parameters()), **dict(head.named_buffers())} expected = {f'task_head.{name}' for name in tensors} - legacy = {f'task_head.{name}' for name in head.named_parameters()} + legacy = {f'task_head.{name}' for name, _ in head.named_parameters()} if set(state) not in (expected, legacy): raise ValueError( f'task_head checkpoint keys {sorted(state)} do not match the head it restores: ' diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index 5c75639a8..0f1122bd3 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -372,7 +372,6 @@ def __init__( # applying runtime overrides such as ``attn_implementation``. Keep Twinkle's # config reference aligned with the config that the model actually uses. self.hf_config = self.model.config - self._apply_deterministic_model_kernels() logger.info_once( f'[TransformersModel] attn_implementation: requested={kwargs.get("attn_implementation")!r}, ' f'resolved={getattr(self.model.config, "_attn_implementation", None)!r}') @@ -681,6 +680,7 @@ def _apply_deterministic_model_kernels(self): def _lazy_wrap_model(self): if not self._model_wrapped: + self._apply_deterministic_model_kernels() optimizer_groups = [og for og in self.optimizer_group.values() if og.optimizer is not None] self.strategy.capture_pre_ep_state_if_needed(self.model, enable_ep=self._enable_expert_parallel) self._maybe_apply_expert_parallel() diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index 171629e9b..067db0328 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -17,6 +17,13 @@ from .application_spec import ApplicationSpec +def validate_native_st_model(model_path: str, task_type: str | None, model_cls: str | None) -> None: + if task_type == 'embedding' and model_cls is None and (Path(model_path) / 'modules.json').is_file(): + raise ValueError( + 'native sentence-transformers checkpoint requires model_cls; ' + 'the generic embedding wrapper cannot preserve its module pipeline') + + class SpectralHybridArgs(BaseModel): """Strict server-owned Spectral Hybrid allocation and optimizer defaults.""" @@ -111,11 +118,7 @@ class ModelArgs(TrainingArguments, LoraArguments): @model_validator(mode="after") def _validate_spectral_backend(self): - if (self.task_type == 'embedding' and self.model_cls is None - and (Path(self.model_id) / 'modules.json').is_file()): - raise ValueError( - 'native sentence-transformers checkpoint requires model_cls; ' - 'the generic embedding wrapper cannot preserve its module pipeline') + validate_native_st_model(self.model_id, self.task_type, self.model_cls) if self.strategy is not None and self.backend != "transformers": raise ValueError("strategy is only supported by the transformers backend") if self.gradient_checkpointing is not None and self.backend != "transformers": @@ -204,6 +207,7 @@ def _instance_config(cls, value, info: ValidationInfo): @model_validator(mode="after") def _validate_backend(self): + validate_native_st_model(self.model_id, self.config.task_type, self.config.model_cls) if ( self.name.startswith("/") or any(part in ("", ".", "..") for part in self.name.split("/")) diff --git a/src/twinkle/server/model/app.py b/src/twinkle/server/model/app.py index 7e5c09fee..5d05f23fa 100644 --- a/src/twinkle/server/model/app.py +++ b/src/twinkle/server/model/app.py @@ -17,6 +17,7 @@ from twinkle.model.base import TrainableModel from twinkle.server.config.backend_dispatch import BackendSelector from twinkle.server.config.task_queue import TaskQueueConfig +from twinkle.server.config.training import validate_native_st_model from twinkle.server.deployment import bind_deployment, build_deployment_app from twinkle.server.runtime import init_twinkle_runtime from twinkle.server.state import ServerState, get_server_state @@ -65,6 +66,7 @@ def _validate_model_task(kw: dict[str, Any]) -> None: from twinkle.hub import HubOperation path = HubOperation.download_model(kw['model_id']) + validate_native_st_model(path, task_type, kw.get('model_cls')) loader = kw.get('model_loader') config = (loader.process_config(loader.build_config(path)) if loader is not None else AutoConfig.from_pretrained(path, trust_remote_code=trust_remote_code())) diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index 6b7b115f8..0de08c295 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -451,3 +451,16 @@ def test_native_backends_preserve_loader_and_precision_configuration(backend, mo assert model.backend == backend and model.mode == mode assert model.config.model_loader == 'worker_only.loader:load' assert model.config.mixed_precision == 'no' + + +@pytest.mark.parametrize('backend', ['transformers', 'megatron']) +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_resolved_native_st_without_model_cls_is_rejected(tmp_path, backend, mode): + from twinkle.server.config.training import TrainingModelConfig + + (tmp_path / 'modules.json').write_text('[]') + values = model_config(mode) + values.update(model_id=str(tmp_path), backend=backend) + values['config']['task_type'] = 'embedding' + with pytest.raises(ValidationError, match='native sentence-transformers checkpoint requires model_cls'): + TrainingModelConfig.model_validate(values) diff --git a/tests/server/model/test_task_head.py b/tests/server/model/test_task_head.py index 4ddafb580..66b6cb74f 100644 --- a/tests/server/model/test_task_head.py +++ b/tests/server/model/test_task_head.py @@ -273,3 +273,26 @@ def test_transformers_decision_uses_loader_architecture(monkeypatch, mode): assert 'task' not in received and 'task_head_loader' not in received else: assert received['task'] == 'decision' + + +@pytest.mark.parametrize('model_id', ['ms://sentence-transformers/LaBSE', + 'hf://sentence-transformers/LaBSE', 'configured-alias']) +@pytest.mark.parametrize('model_cls', [None, 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel']) +def test_native_st_guard_uses_resolved_directory(tmp_path, monkeypatch, model_id, model_cls): + from transformers import AutoConfig + from twinkle.hub import HubOperation + + (tmp_path / 'modules.json').write_text('[]') + monkeypatch.setattr(HubOperation, 'download_model', lambda path: str(tmp_path)) + config_reads = [] + monkeypatch.setattr(AutoConfig, 'from_pretrained', + lambda *a, **kw: config_reads.append(a) or SimpleNamespace(architectures=['BertModel'])) + kwargs = {'model_id': model_id, 'task_type': 'embedding'} + if model_cls is not None: + kwargs['model_cls'] = model_cls + _validate_model_task(kwargs) + assert config_reads + else: + with pytest.raises(ValueError, match='native sentence-transformers checkpoint requires model_cls'): + _validate_model_task(kwargs) + assert not config_reads diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 558a915d3..723716612 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -95,6 +95,7 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t _model_wrapped=False, _enable_expert_parallel=False, sp_strategy=None, _maybe_apply_expert_parallel=lambda: None, _ensure_sp_strategy=lambda: None, _ensure_gdn_padding_free_patch=lambda: None, + _apply_deterministic_model_kernels=lambda: None, _rebind_lr_scheduler=TransformersModel._rebind_lr_scheduler, register_mm_forward_hook=lambda group: None) with patch.object(original, 'to', return_value=replacement), patch( @@ -215,3 +216,20 @@ def test_independent_task_head_checkpoint_includes_nonpersistent_buffers(): set_state.assert_called_once() assert set_state.call_args.args[1].keys() == {'weight', 'bias'} torch.testing.assert_close(head.calibration, torch.tensor([3.0])) + + +@pytest.mark.parametrize('persistent', [False, True]) +def test_independent_task_head_loads_legacy_parameters_without_changing_calibration(persistent): + head = torch.nn.Linear(2, 1) + head.register_buffer('calibration', torch.tensor([7.0]), persistent=persistent) + head._twinkle_independent_fsdp = True + strategy = NativeFSDPStrategy(enable_ep=False) + legacy = {f'task_head.{name}': torch.full_like(param, 3.0) + for name, param in head.named_parameters()} + options = strategy._prepare_optimizer_state_dict_options(for_load=True) + options.broadcast_from_rank0 = False + with patch.object(strategy, '_prepare_optimizer_state_dict_options', return_value=options): + assert strategy.restore_independent_task_head_state(head, legacy) + for param in head.parameters(): + torch.testing.assert_close(param, torch.full_like(param, 3.0)) + torch.testing.assert_close(head.calibration, torch.tensor([7.0])) diff --git a/tests/transformers/test_qwen35_fla_bwd_precision.py b/tests/transformers/test_qwen35_fla_bwd_precision.py index 61d8b40e6..c5dd91d8b 100644 --- a/tests/transformers/test_qwen35_fla_bwd_precision.py +++ b/tests/transformers/test_qwen35_fla_bwd_precision.py @@ -329,3 +329,45 @@ def test_moe_linear_attention_fla_on_vs_off(self, seed): _compare_models('dense', ['linear_attention', 'linear_attention'], 2024, 256) _compare_models('moe', ['linear_attention', 'linear_attention'], 1234, 128) print('\nAll comparisons finished.') + + +@pytest.mark.parametrize('multi', [False, True]) +@pytest.mark.parametrize('deterministic', [False, True]) +def test_gdn_kernel_policy_applies_once_before_both_model_wrappers(tmp_path, monkeypatch, multi, deterministic): + from tokenizers import Tokenizer + from tokenizers.models import WordPiece + from transformers import AutoModelForCausalLM, PreTrainedTokenizerFast + from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule + from twinkle import Platform + import twinkle.infra as infra + from twinkle.model.transformers.transformers import TransformersModel + from twinkle.model.transformers.multi_lora_transformers import MultiLoraTransformersModel + + monkeypatch.setenv('ACCELERATE_USE_CPU', 'true') + monkeypatch.setattr(Platform, 'get_local_device', lambda *args, **kwargs: 'cpu') + monkeypatch.setattr(infra, '_mode', 'local') + monkeypatch.setattr(infra, '_device_group', None) + monkeypatch.setattr(infra, '_device_mesh', None) + config = Qwen3_5TextConfig(**_common_config_kwargs(['linear_attention', 'linear_attention'])) + Qwen3_5ForCausalLM(config).save_pretrained(tmp_path) + tokenizer = PreTrainedTokenizerFast( + tokenizer_object=Tokenizer(WordPiece({'[PAD]': 0, '[UNK]': 1}, unk_token='[UNK]')), + pad_token='[PAD]', unk_token='[UNK]') + tokenizer.save_pretrained(tmp_path) + cls = MultiLoraTransformersModel if multi else TransformersModel + wrapper = cls(model_id=str(tmp_path), model_cls=AutoModelForCausalLM, config=config, mixed_precision='no', + gradient_checkpointing=False, + **({'max_loras': 2, 'max_r': 4, 'target_modules': ['out_proj']} if multi else {})) + gdn = next(module for module in wrapper.model.modules() + if type(module).__name__ == 'Qwen3_5GatedDeltaNet') + fast_kernel = lambda *args, **kwargs: None + gdn.chunk_gated_delta_rule = fast_kernel + gdn.causal_conv1d_fn = fast_kernel + monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: deterministic) + wrapper._lazy_wrap_model() + assert gdn.chunk_gated_delta_rule is (torch_chunk_gated_delta_rule if deterministic else fast_kernel) + assert gdn.causal_conv1d_fn is (None if deterministic else fast_kernel) + # Re-entering an already wrapped model must not repeat the initialization policy. + gdn.chunk_gated_delta_rule = fast_kernel + wrapper._lazy_wrap_model() + assert gdn.chunk_gated_delta_rule is fast_kernel From 8179ab166f31e2ed46a7ed62152d0de3c6fe8065 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 07:08:01 +0000 Subject: [PATCH 04/18] fix(megatron): apply deterministic kernels before multi-LoRA wrapping Co-authored-by: Codex --- .../model/megatron/multi_lora_megatron.py | 1 + .../test_megatron_generative_reranker.py | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/twinkle/model/megatron/multi_lora_megatron.py b/src/twinkle/model/megatron/multi_lora_megatron.py index 9c7989af6..bedcadfff 100644 --- a/src/twinkle/model/megatron/multi_lora_megatron.py +++ b/src/twinkle/model/megatron/multi_lora_megatron.py @@ -170,6 +170,7 @@ def __init__( if self.task_head_slots is not None: self._validate_task_head_config() self._build_task_head_slots() + self._apply_deterministic_model_kernels() self.model = self.strategy.wrap_model(self.model) self.strategy.finish_param_config(self.model, None) self.multi_adapter.save_initial_weights() diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py index 9e597bf15..4dc06b36a 100644 --- a/tests/model/test_megatron_generative_reranker.py +++ b/tests/model/test_megatron_generative_reranker.py @@ -1,5 +1,7 @@ +import sys from types import SimpleNamespace +import pytest import torch from twinkle.model.megatron.megatron import _generative_reranker_score @@ -34,3 +36,45 @@ def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): wrapper._apply_deterministic_model_kernels() assert gdn.chunk_gated_delta_rule is torch_chunk_gated_delta_rule assert gdn.causal_conv1d_fn is None + + +@pytest.mark.parametrize('deterministic', [False, True]) +def test_multilora_selects_deterministic_kernels_before_eager_wrap(monkeypatch, deterministic): + from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule + from twinkle import initialize + import twinkle.model.megatron.multi_lora_megatron as module + + initialize(mode='local') + gdn = type('Qwen3NextGatedDeltaNet', (torch.nn.Module,), {})() + original = lambda *args: None + gdn.chunk_gated_delta_rule = original + gdn.causal_conv1d_fn = original + + class Wrapped(Exception): + pass + + class Strategy: + uses_fsdp = staticmethod(lambda _: False) + + def __init__(self, *args, **kwargs): + pass + + def create_megatron_model(self, load_weights): + return [gdn] + + def wrap_model(self, models): + assert gdn.chunk_gated_delta_rule is (torch_chunk_gated_delta_rule if deterministic else original) + assert gdn.causal_conv1d_fn is (None if deterministic else original) + raise Wrapped() + + monkeypatch.setattr(module, 'requires', lambda *args: None) + monkeypatch.setattr(module, 'ensure_mindspeed_adaptor_patched', lambda: None) + monkeypatch.setattr(module.HubOperation, 'download_model', lambda *args, **kwargs: 'local-model') + monkeypatch.setattr(module.torch_util, 'set_device', lambda: None) + monkeypatch.setattr(module.MultiLoraMegatronModel, '_try_init_process_group', lambda *args, **kwargs: None) + monkeypatch.setattr(module, 'MegatronStrategy', Strategy) + monkeypatch.setattr(module, 'MultiLora', lambda **kwargs: SimpleNamespace(patch=lambda models, **kw: models)) + monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: deterministic) + monkeypatch.setitem(sys.modules, 'twinkle.patch.megatron_peft', SimpleNamespace(MegatronPeft=lambda: lambda: None)) + with pytest.raises(Wrapped): + module.MultiLoraMegatronModel('local-model', config=SimpleNamespace()) From 3e1c34c3b6a0f05b8f4a9f75f69ec9689a5e430e Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 07:14:02 +0000 Subject: [PATCH 05/18] test(megatron): isolate eager-wrap constructor regression Co-authored-by: Codex --- tests/model/test_megatron_generative_reranker.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py index 4dc06b36a..90f43015a 100644 --- a/tests/model/test_megatron_generative_reranker.py +++ b/tests/model/test_megatron_generative_reranker.py @@ -41,10 +41,11 @@ def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): @pytest.mark.parametrize('deterministic', [False, True]) def test_multilora_selects_deterministic_kernels_before_eager_wrap(monkeypatch, deterministic): from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule - from twinkle import initialize + import twinkle.infra as infra import twinkle.model.megatron.multi_lora_megatron as module - initialize(mode='local') + monkeypatch.setattr(infra, '_mode', 'local') + monkeypatch.setattr(infra, '_device_group', None) gdn = type('Qwen3NextGatedDeltaNet', (torch.nn.Module,), {})() original = lambda *args: None gdn.chunk_gated_delta_rule = original From 4817a8cd64e36ad826fac30148cb022ca35389b2 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 16:04:37 +0800 Subject: [PATCH 06/18] fix(fsdp): wrap task head slots before tenants bind --- .../Usage Guide/Server and Client/Server.md | 2 + .../\346\234\215\345\212\241\347\253\257.md" | 2 + .../transformers/strategy/native_fsdp.py | 2 + .../transformers/test_native_fsdp_strategy.py | 22 +++++-- .../test_sentence_transformer_tenants.py | 57 ------------------- 5 files changed, 23 insertions(+), 62 deletions(-) diff --git a/docs/source_en/Usage Guide/Server and Client/Server.md b/docs/source_en/Usage Guide/Server and Client/Server.md index e19b18c03..94c6fe236 100644 --- a/docs/source_en/Usage Guide/Server and Client/Server.md +++ b/docs/source_en/Usage Guide/Server and Client/Server.md @@ -542,6 +542,8 @@ Attached-head checkpoints save parameters and calibration buffers together, incl For full-parameter Megatron decision training, optimizer checkpoints use MCore's native model-space format with CPU aggregation. Reserve host memory for the complete optimizer state; restore uses the format recorded in checkpoint metadata. Other tasks and Megatron FSDP retain their existing formats. +Real-device validation covers text-only StartLux 4B with Transformers LoRA and Megatron LoRA/full. Transformers full and multimodal training remain unvalidated. Native ST save/resume and family-loader compatibility for derived text-only Qwen3.5 checkpoints depend on separate Swift patches, currently pending and not delivered by this PR. The complete ST Megatron path is not connected yet. + ### Automatic sampling scaling Set instance counts and the scaling target in the `args` of your `import_path: sampler` entry. For example: diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" index 3f67daf7a..685e4f0e9 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" @@ -536,6 +536,8 @@ model.set_loss('swift.dev.loss.decision:ScoringLoss') Megatron 决策模型全参训练保存优化器状态时,使用 MCore 原生模型参数格式并在 CPU 上聚合,需要为完整优化器状态预留主机内存;恢复时读取 checkpoint 自带的格式 metadata。其他任务和 Megatron FSDP 继续使用原有格式。 +本轮实测覆盖 StartLux 4B 的文本输入:Transformers LoRA、Megatron LoRA/全参;Transformers 全参和多模态尚未完成验收。原生 ST 保存/续训及 Qwen3.5 纯文本派生模型的 family loader 兼容修复依赖独立 Swift 补丁,目前 pending,未随本 PR 交付;完整 ST 的 Megatron 路径仍未接通。 + ### 采样服务自动扩缩容 在 `import_path: sampler` 的 `args` 中设置实例数量和扩容目标。例如: diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 4ca516134..576113b2d 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -28,6 +28,8 @@ def _shard_builtin_task_heads(model, *, mesh, reshard_after_forward, mp_policy): # The head wrapper owns unsharding for the readout backward instead of the root wrapper. heads = [getattr(model, name, None) for name in ('score', 'classifier')] heads.extend((getattr(model, 'task_heads', None) or {}).values()) + # Future tenants can bind any preallocated slot after the root is already sharded. + heads.extend(module for name, module in model.named_children() if name.startswith('task_head_slot_')) seen = set() for head in heads: if (head is None or id(head) in seen or getattr(head, 'keep_fp32', False) diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 723716612..6d36d77d2 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -131,10 +131,18 @@ def test_builtin_score_head_is_sharded_before_the_root_model(): assert model_call.args[0] is model -def test_attached_standard_head_is_sharded_independently(): +@pytest.mark.parametrize('preallocated', [False, True]) +def test_attached_standard_head_is_sharded_independently(preallocated): model = torch.nn.Module() model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) - model.task_heads = {'': torch.nn.Linear(4, 1).to(torch.bfloat16)} + if preallocated: + from twinkle.model.multi_lora import MultiLoraTaskHeadSlots + + slots = MultiLoraTaskHeadSlots(2) + slots.build(model, lambda: torch.nn.Linear(4, 1).to(torch.bfloat16)) + slots.bind('a', model) + else: + model.task_heads = {'': torch.nn.Linear(4, 1).to(torch.bfloat16)} strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', @@ -142,9 +150,13 @@ def test_attached_standard_head_is_sharded_independently(): patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: strategy.wrap_model(model) - assert fully_shard.call_count == 2 - assert fully_shard.call_args_list[0].args[0] is model.task_heads[''] - assert model.task_heads['']._twinkle_independent_fsdp + assert fully_shard.call_count == (3 if preallocated else 2) + if preallocated: + slots.bind('b', model) + assert model.task_heads['b']._twinkle_independent_fsdp + assert fully_shard.call_args_list[1].args[0] is model.task_heads['b'] + assert fully_shard.call_args_list[0].args[0] is model.task_heads['a' if preallocated else ''] + assert all(head._twinkle_independent_fsdp for head in model.task_heads.values()) def test_optimizer_restore_accepts_lazy_state_for_unused_parameters(): diff --git a/tests/transformers/test_sentence_transformer_tenants.py b/tests/transformers/test_sentence_transformer_tenants.py index 4bdc2ca5b..d13cf6e99 100644 --- a/tests/transformers/test_sentence_transformer_tenants.py +++ b/tests/transformers/test_sentence_transformer_tenants.py @@ -106,60 +106,3 @@ def step(name): torch.testing.assert_close(reloaded, saved_vectors, rtol=0, atol=1e-6) model.remove_adapter('b') model.remove_adapter('c') - - -def test_st_full_checkpoint_restores_optimizer_and_continuation(tmp_path, monkeypatch): - from sentence_transformers.base.modules import Transformer, Normalize - from sentence_transformers.sentence_transformer.modules import Pooling - from tokenizers import Tokenizer - from tokenizers.models import WordPiece - from transformers import BertConfig, BertModel, PreTrainedTokenizerFast - from swift.dev.model.sentence_transformer_model import SentenceTransformerModel - from twinkle import Platform, initialize - - monkeypatch.setenv('ACCELERATE_USE_CPU', 'true') - monkeypatch.setattr(Platform, 'get_local_device', lambda *args, **kwargs: 'cpu') - initialize(mode='local') - backbone = tmp_path / 'bert' - BertModel(BertConfig(vocab_size=32, hidden_size=16, num_hidden_layers=1, num_attention_heads=2, - intermediate_size=32, hidden_dropout_prob=0, attention_probs_dropout_prob=0)).save_pretrained(backbone) - vocab = {'[PAD]': 0, '[UNK]': 1, '[CLS]': 2, '[SEP]': 3, '[MASK]': 4} - vocab.update({f't{i}': i for i in range(5, 32)}) - PreTrainedTokenizerFast(tokenizer_object=Tokenizer(WordPiece(vocab, unk_token='[UNK]')), - pad_token='[PAD]', unk_token='[UNK]').save_pretrained(backbone) - original = tmp_path / 'st' - st.SentenceTransformer(modules=[Transformer(str(backbone)), Pooling(16), Normalize()], device='cpu').save( - str(original)) - batch = [{'input_ids': [2, 5 + i, 3], 'attention_mask': [1] * 3, 'labels': [int(i % 3 == 0)]} - for i in range(6)] - - def build(): - model = SentenceTransformerModel(model_id=str(original), mixed_precision='no', - gradient_checkpointing=False) - model.set_processor('InputProcessor', padding_side='right') - model.set_loss('InfonceLoss', temperature=0.1) - model.set_optimizer('AdamW', lr=1e-3) - return model - - def vectors(model): - return model.forward_only(inputs=copy.deepcopy(batch))['embeddings'].cpu() - - def step(model): - model.forward_backward(inputs=copy.deepcopy(batch), task='embedding') - model.clip_grad_and_step() - - model = build() - vectors(model) - step(model) - checkpoint = model.save('trained', output_dir=str(tmp_path), save_optimizer=True, - consumed_train_samples=6) - restored_vectors = vectors(model) - step(model) - continued = vectors(model) - fresh = build() - progress = fresh.resume_from_checkpoint(checkpoint) - assert progress['consumed_train_samples'] == 6 and progress['cur_step'] == 1 - assert fresh.optimizer_group[''].optimizer.state - torch.testing.assert_close(vectors(fresh), restored_vectors, rtol=0, atol=0) - step(fresh) - torch.testing.assert_close(vectors(fresh), continued, rtol=0, atol=0) From 3711c872f1acd93d5df7fbc3465ce50645c8ec13 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 08:10:27 +0000 Subject: [PATCH 07/18] test: remove kernel helper checks covered by model construction --- .../model/test_megatron_generative_reranker.py | 16 ---------------- .../test_qwen35_fla_bwd_precision.py | 18 ------------------ 2 files changed, 34 deletions(-) diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py index 90f43015a..eb11cf3af 100644 --- a/tests/model/test_megatron_generative_reranker.py +++ b/tests/model/test_megatron_generative_reranker.py @@ -22,22 +22,6 @@ def test_generative_reranker_scores_yes_minus_no_and_backpropagates(): assert torch.count_nonzero(logits.grad[..., [0, 1, 2, 4, 5, 6, 8, 9, 10]]) == 0 -def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): - from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule - from twinkle.model.megatron.megatron import MegatronModel - - gdn_type = type('Qwen3NextGatedDeltaNet', (torch.nn.Module,), {}) - gdn = gdn_type() - gdn.chunk_gated_delta_rule = lambda *args: None - gdn.causal_conv1d_fn = lambda *args: None - wrapper = MegatronModel.__new__(MegatronModel) - wrapper.model = [gdn] - monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: True) - wrapper._apply_deterministic_model_kernels() - assert gdn.chunk_gated_delta_rule is torch_chunk_gated_delta_rule - assert gdn.causal_conv1d_fn is None - - @pytest.mark.parametrize('deterministic', [False, True]) def test_multilora_selects_deterministic_kernels_before_eager_wrap(monkeypatch, deterministic): from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule diff --git a/tests/transformers/test_qwen35_fla_bwd_precision.py b/tests/transformers/test_qwen35_fla_bwd_precision.py index c5dd91d8b..4591e32e2 100644 --- a/tests/transformers/test_qwen35_fla_bwd_precision.py +++ b/tests/transformers/test_qwen35_fla_bwd_precision.py @@ -124,24 +124,6 @@ def _force_fla_off(model: torch.nn.Module) -> None: delattr(la, '_twinkle_npu_patched') -def test_full_determinism_selects_torch_gdn_kernels(monkeypatch): - from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule - from twinkle.model.transformers.transformers import TransformersModel - - gdn_type = type('Qwen3_5GatedDeltaNet', (torch.nn.Module,), {}) - gdn = gdn_type() - gdn.chunk_gated_delta_rule = lambda *args: None - gdn.causal_conv1d_fn = lambda *args: None - wrapper = TransformersModel.__new__(TransformersModel) - torch.nn.Module.__init__(wrapper) - wrapper.model = torch.nn.Module() - wrapper.model.gdn = gdn - monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: True) - wrapper._apply_deterministic_model_kernels() - assert gdn.chunk_gated_delta_rule is torch_chunk_gated_delta_rule - assert gdn.causal_conv1d_fn is None - - def test_full_determinism_disables_nondeterministic_sdp(monkeypatch): from twinkle.utils.platforms.gpu import GPU From df75adf615bd80bf2b8c2cddb15c76e3c3b4f563 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 16:15:42 +0800 Subject: [PATCH 08/18] docs: keep training guides focused on user actions --- .../Usage Guide/Server and Client/Server.md | 20 +++---------------- .../Usage Guide/Train-as-a-Service.md | 2 +- .../\346\234\215\345\212\241\347\253\257.md" | 20 +++---------------- ...55\347\273\203\346\234\215\345\212\241.md" | 2 +- 4 files changed, 8 insertions(+), 36 deletions(-) diff --git a/docs/source_en/Usage Guide/Server and Client/Server.md b/docs/source_en/Usage Guide/Server and Client/Server.md index 94c6fe236..520280db4 100644 --- a/docs/source_en/Usage Guide/Server and Client/Server.md +++ b/docs/source_en/Usage Guide/Server and Client/Server.md @@ -513,7 +513,7 @@ Complete configurations are available in `cookbook/client/server/transformer/ser ### Swift decision models -Install the matching Swift dev version on the server and client. A decision family owns its loader, template, readout head and loss; Twinkle supplies training instances and tenant isolation. Add these fields to the `args` of a model application, keeping its device layout and instance limits: +Install a Swift version compatible with Twinkle on both the server and client. For example, to deploy StartLux for LoRA training, replace the corresponding fields in an existing model application's `args`, keeping its device layout and instance limits: ```yaml model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink @@ -526,23 +526,9 @@ env_vars: TWINKLE_TRUST_REMOTE_CODE: '1' ``` -The loader also supplies the multimodal module paths used by Transformers LoRA targeting. For Megatron, use `backend: megatron` and a supported model/bridge with a matching device group and parallelism layout. Loading through Swift does not add architectures missing from the Megatron bridge. Configure different tasks as separate model applications. +`TWINKLE_TRUST_REMOTE_CODE` allows model code to be loaded; enable it only for trusted models. For client-side training, use the model's corresponding Swift data processing, template and loss configuration. Configure different tasks as separate model applications. -Encode rows using Swift's `StartLuxPreprocessor` and `StartLuxTemplate`. Configure the existing processor and loss through explicit component imports, without a global registration hook: - -```python -model.set_template('swift.dev.template.Template', template_spec=template_spec) -model.set_processor('swift.dev.processor.server_plugin:DevInputProcessor', template_spec=template_spec) -model.set_loss('swift.dev.loss.decision:ScoringLoss') -``` - -`template_spec` is the JSON representation of the same Swift `TemplateSpec` used by the client encoder. `DevInputProcessor` carries `target_probs` to the decision collator, which creates floating-point loss targets. Token `labels` on the HTTP wire remain integers. Explicit `module:Class` component imports require the trust switch, as does the configured model loader. - -Attached-head checkpoints save parameters and calibration buffers together, including nonpersistent buffers. Removing a LoRA resets both before its head slot is reused. Older parameter-only head checkpoints remain loadable, but cannot recover calibration values that were never saved. - -For full-parameter Megatron decision training, optimizer checkpoints use MCore's native model-space format with CPU aggregation. Reserve host memory for the complete optimizer state; restore uses the format recorded in checkpoint metadata. Other tasks and Megatron FSDP retain their existing formats. - -Real-device validation covers text-only StartLux 4B with Transformers LoRA and Megatron LoRA/full. Transformers full and multimodal training remain unvalidated. Native ST save/resume and family-loader compatibility for derived text-only Qwen3.5 checkpoints depend on separate Swift patches, currently pending and not delivered by this PR. The complete ST Megatron path is not connected yet. +To use Megatron, set `backend: megatron` and configure its device group and parallelism. The model must be supported by the Megatron backend. When saving optimizer state for full-parameter Megatron decision training, reserve host memory for the complete optimizer state. ### Automatic sampling scaling diff --git a/docs/source_en/Usage Guide/Train-as-a-Service.md b/docs/source_en/Usage Guide/Train-as-a-Service.md index b9b8c510b..cb167b563 100644 --- a/docs/source_en/Usage Guide/Train-as-a-Service.md +++ b/docs/source_en/Usage Guide/Train-as-a-Service.md @@ -64,7 +64,7 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` -On the Transformers backend, resumable checkpoints (Twinkle's `save_optimizer=True` or Tinker's `save_state()`) require a complete gradient accumulation window, followed by the optimizer step and gradient clearing. Pending gradients are not included in checkpoints, so saving optimizer state before that point is rejected. Weights-only exports remain available. Older checkpoints saved during accumulation can only be used to restart training from model weights. +On the Transformers backend, finish the current gradient accumulation and optimizer update before saving a training checkpoint with Tinker's `save_state()` or Twinkle's `save(..., save_optimizer=True)`. Weights-only exports do not have this restriction. With the code above, you can train a self-cognition LoRA based on `Qwen/Qwen3.8-27B`. This LoRA will change the model's name and creator to the names specified during training. To perform inference using this LoRA: diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" index 685e4f0e9..8df068353 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" @@ -507,7 +507,7 @@ twinkle-server check-config -c server_config.yaml ### Swift 决策模型 -服务端和客户端需要安装匹配的 Swift dev 版本。决策模型的 loader、模板、读出头和 loss 复用 Swift 实现,Twinkle 负责训练实例与租户隔离。在 model application 的 `args` 中增加以下配置,并保留该实例的设备布局和数量限制: +部署 StartLux 等决策模型时,服务端和客户端均需安装与 Twinkle 匹配的 Swift 版本。以下以 StartLux 的 LoRA 训练为例,替换现有模型服务的 `args` 中对应字段,并保留设备布局和实例数量配置: ```yaml model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink @@ -520,23 +520,9 @@ env_vars: TWINKLE_TRUST_REMOTE_CODE: '1' ``` -loader 还会提供 Transformers LoRA 选择模块时使用的多模态架构信息。使用 Megatron 时设置 `backend: megatron`,配置匹配的设备组与并行布局,并确认模型架构受对应 bridge 支持;接入 Swift loader 不会补齐 bridge 尚不支持的架构。不同任务仍分别配置 model application。 +`TWINKLE_TRUST_REMOTE_CODE` 允许加载模型代码,请仅用于可信模型。客户端训练时,使用该模型对应的 Swift 数据处理、模板和损失函数配置。不同任务请分别配置模型服务。 -客户端通过 Swift 的 `StartLuxPreprocessor` 和 `StartLuxTemplate` 编码数据,再用明确的组件路径配置已有 processor 和 loss,无需全局注册钩子: - -```python -model.set_template('swift.dev.template.Template', template_spec=template_spec) -model.set_processor('swift.dev.processor.server_plugin:DevInputProcessor', template_spec=template_spec) -model.set_loss('swift.dev.loss.decision:ScoringLoss') -``` - -`template_spec` 是客户端编码所用 Swift `TemplateSpec` 的 JSON 表示。`DevInputProcessor` 将 `target_probs` 交给决策任务的 collator,由它构造浮点训练目标;HTTP 中的 token `labels` 仍保持整数约束。`模块:类名` 组件导入与配置中的 model loader 一样,需要开启信任开关。 - -附加任务头的 checkpoint 同时保存参数与校准 buffer,包括 nonpersistent buffer。移除 LoRA 后,两者都会重置,再将槽位交给新租户。旧的仅参数 checkpoint 仍可加载,但无法恢复旧文件从未保存过的校准值。 - -Megatron 决策模型全参训练保存优化器状态时,使用 MCore 原生模型参数格式并在 CPU 上聚合,需要为完整优化器状态预留主机内存;恢复时读取 checkpoint 自带的格式 metadata。其他任务和 Megatron FSDP 继续使用原有格式。 - -本轮实测覆盖 StartLux 4B 的文本输入:Transformers LoRA、Megatron LoRA/全参;Transformers 全参和多模态尚未完成验收。原生 ST 保存/续训及 Qwen3.5 纯文本派生模型的 family loader 兼容修复依赖独立 Swift 补丁,目前 pending,未随本 PR 交付;完整 ST 的 Megatron 路径仍未接通。 +使用 Megatron 时,将 `backend` 改为 `megatron`,并配置对应的设备组和并行方式;模型必须受 Megatron 后端支持。使用 Megatron 全参训练决策模型时,保存优化器状态需为完整优化器状态预留主机内存。 ### 采样服务自动扩缩容 diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" index a9a300955..3cf86160f 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" @@ -65,7 +65,7 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` -在 Transformers 后端,保存可继续训练的状态(Twinkle 的 `save_optimizer=True` 或 Tinker 的 `save_state()`)需要完成当前梯度累积窗口,并执行更新和清梯度。累积中的梯度不包含在 checkpoint 中,因此服务会拒绝此时保存优化器状态;仅导出权重不受此限制。旧的累积中途 checkpoint 只能按仅加载模型权重的方式重新开始训练。 +使用 Transformers 后端时,请先完成当前梯度累积和优化器更新,再调用 Tinker 的 `save_state()` 或 Twinkle 的 `save(..., save_optimizer=True)` 保存训练断点。仅导出模型权重不受此限制。 通过上述代码,你可以训练一个原模型为`Qwen/Qwen3.8-27B`的自我认知lora。这个lora会改变模型的名称和制造者为训练时指定的名称。使用这个lora进行推理: From 9b9a490b917dcbc592b7c0c0871c180e4429faa2 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 16:39:42 +0800 Subject: [PATCH 09/18] docs: remove incomplete decision model setup section --- .../Usage Guide/Server and Client/Server.md | 19 ------------------- .../\346\234\215\345\212\241\347\253\257.md" | 19 ------------------- 2 files changed, 38 deletions(-) diff --git a/docs/source_en/Usage Guide/Server and Client/Server.md b/docs/source_en/Usage Guide/Server and Client/Server.md index 520280db4..ecb5f1730 100644 --- a/docs/source_en/Usage Guide/Server and Client/Server.md +++ b/docs/source_en/Usage Guide/Server and Client/Server.md @@ -511,25 +511,6 @@ Release models when training finishes; Twinkle clients can call `remove_adapter( Complete configurations are available in `cookbook/client/server/transformer/server_config.yaml` and `cookbook/client/server/megatron/server_config.yaml`. -### Swift decision models - -Install a Swift version compatible with Twinkle on both the server and client. For example, to deploy StartLux for LoRA training, replace the corresponding fields in an existing model application's `args`, keeping its device layout and instance limits: - -```yaml -model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink -backend: transformers -train_mode: lora -task_type: decision -model_loader: swift.dev.model.loader:build_model_loader -max_loras: 2 -env_vars: - TWINKLE_TRUST_REMOTE_CODE: '1' -``` - -`TWINKLE_TRUST_REMOTE_CODE` allows model code to be loaded; enable it only for trusted models. For client-side training, use the model's corresponding Swift data processing, template and loss configuration. Configure different tasks as separate model applications. - -To use Megatron, set `backend: megatron` and configure its device group and parallelism. The model must be supported by the Megatron backend. When saving optimizer state for full-parameter Megatron decision training, reserve host memory for the complete optimizer state. - ### Automatic sampling scaling Set instance counts and the scaling target in the `args` of your `import_path: sampler` entry. For example: diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" index 8df068353..7b51eb47e 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\346\234\215\345\212\241\347\253\257\345\222\214\345\256\242\346\210\267\347\253\257/\346\234\215\345\212\241\347\253\257.md" @@ -505,25 +505,6 @@ twinkle-server check-config -c server_config.yaml 完整配置可参考仓库中的 `cookbook/client/server/transformer/server_config.yaml` 和 `cookbook/client/server/megatron/server_config.yaml`。 -### Swift 决策模型 - -部署 StartLux 等决策模型时,服务端和客户端均需安装与 Twinkle 匹配的 Swift 版本。以下以 StartLux 的 LoRA 训练为例,替换现有模型服务的 `args` 中对应字段,并保留设备布局和实例数量配置: - -```yaml -model_id: ms://StartLuxAI/StartLux-Decision-v1.1-4B-AutoThink -backend: transformers -train_mode: lora -task_type: decision -model_loader: swift.dev.model.loader:build_model_loader -max_loras: 2 -env_vars: - TWINKLE_TRUST_REMOTE_CODE: '1' -``` - -`TWINKLE_TRUST_REMOTE_CODE` 允许加载模型代码,请仅用于可信模型。客户端训练时,使用该模型对应的 Swift 数据处理、模板和损失函数配置。不同任务请分别配置模型服务。 - -使用 Megatron 时,将 `backend` 改为 `megatron`,并配置对应的设备组和并行方式;模型必须受 Megatron 后端支持。使用 Megatron 全参训练决策模型时,保存优化器状态需为完整优化器状态预留主机内存。 - ### 采样服务自动扩缩容 在 `import_path: sampler` 的 `args` 中设置实例数量和扩容目标。例如: From 87f7f2bb5843877ffacc48d188082b75a98bdc36 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Mon, 16 Nov 2026 13:29:36 +0000 Subject: [PATCH 10/18] fix(model): harden checkpoint resume contracts Preserve lazy optimizer state across cold resume, reject incomplete saves before weight collection, and align Megatron checkpoint calls with supported APIs. Remove unverified decision handling and unrelated loader changes while tightening torch configuration validation. --- .../Usage Guide/Train-as-a-Service.md | 2 +- ...55\347\273\203\346\234\215\345\212\241.md" | 2 +- src/twinkle/model/megatron/bridge/mcore.py | 8 +- src/twinkle/model/megatron/megatron.py | 22 ++--- .../transformers/strategy/native_fsdp.py | 25 ++++-- .../model/transformers/transformers.py | 12 ++- src/twinkle/server/config/training.py | 10 +++ src/twinkle/utils/loader.py | 13 +-- .../model/multi_lora_transformers.py | 2 +- tests/server/config/test_training_config.py | 10 ++- .../test_checkpoint_writer_rank.py | 16 +++- .../test_megatron_weight_only_resume.py | 84 +++++++++++-------- .../transformers/test_native_fsdp_strategy.py | 80 +++++++++++++----- tests/utils/test_component_imports.py | 26 ------ 14 files changed, 192 insertions(+), 120 deletions(-) delete mode 100644 tests/utils/test_component_imports.py diff --git a/docs/source_en/Usage Guide/Train-as-a-Service.md b/docs/source_en/Usage Guide/Train-as-a-Service.md index cb167b563..0425583f6 100644 --- a/docs/source_en/Usage Guide/Train-as-a-Service.md +++ b/docs/source_en/Usage Guide/Train-as-a-Service.md @@ -64,7 +64,7 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` -On the Transformers backend, finish the current gradient accumulation and optimizer update before saving a training checkpoint with Tinker's `save_state()` or Twinkle's `save(..., save_optimizer=True)`. Weights-only exports do not have this restriction. +On the Transformers backend, finish the current gradient accumulation, optimizer update, and gradient clearing (`zero_grad`) before saving a training checkpoint with Tinker's `save_state()` or Twinkle's `save(..., save_optimizer=True)`. Weights-only exports do not have this restriction. With the code above, you can train a self-cognition LoRA based on `Qwen/Qwen3.8-27B`. This LoRA will change the model's name and creator to the names specified during training. To perform inference using this LoRA: diff --git "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" index 3cf86160f..94eeb1def 100644 --- "a/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" +++ "b/docs/source_zh/\344\275\277\347\224\250\346\214\207\345\274\225/\350\256\255\347\273\203\346\234\215\345\212\241.md" @@ -65,7 +65,7 @@ for epoch in range(2): print(f'Saved checkpoint for epoch {epoch} to {result.path}') ``` -使用 Transformers 后端时,请先完成当前梯度累积和优化器更新,再调用 Tinker 的 `save_state()` 或 Twinkle 的 `save(..., save_optimizer=True)` 保存训练断点。仅导出模型权重不受此限制。 +使用 Transformers 后端时,请先完成当前梯度累积、优化器更新和梯度清理(`zero_grad`),再调用 Tinker 的 `save_state()` 或 Twinkle 的 `save(..., save_optimizer=True)` 保存训练断点。仅导出模型权重不受此限制。 通过上述代码,你可以训练一个原模型为`Qwen/Qwen3.8-27B`的自我认知lora。这个lora会改变模型的名称和制造者为训练时指定的名称。使用这个lora进行推理: diff --git a/src/twinkle/model/megatron/bridge/mcore.py b/src/twinkle/model/megatron/bridge/mcore.py index d8337f530..78f532534 100644 --- a/src/twinkle/model/megatron/bridge/mcore.py +++ b/src/twinkle/model/megatron/bridge/mcore.py @@ -30,10 +30,14 @@ def build_model_config(self, hf_config: Any, parallel_kwargs: Dict[str, Any], st is_seq_cls = any(name.endswith('ForSequenceClassification') for name in (getattr(hf_config, 'architectures', None) or [])) if is_seq_cls: - config_kwargs.update(task_type='seq_cls', num_labels=hf_config.num_labels) + if kwargs.get('task_type', 'seq_cls') != 'seq_cls': + raise ValueError('A sequence-classification checkpoint requires task_type=seq_cls.') + if kwargs.get('num_labels', hf_config.num_labels) != hf_config.num_labels: + raise ValueError('num_labels must match the persisted classification head.') config_kwargs.update(kwargs) if is_seq_cls: - config_kwargs['untie_embeddings_and_output_weights'] = True + config_kwargs.update(task_type='seq_cls', num_labels=hf_config.num_labels, + untie_embeddings_and_output_weights=True) # per-token-mean grad normalization (mcore default is False; twinkle forces True). if 'calculate_per_token_loss' not in config_kwargs: config_kwargs['calculate_per_token_loss'] = True diff --git a/src/twinkle/model/megatron/megatron.py b/src/twinkle/model/megatron/megatron.py index f12d0131b..09aafad98 100644 --- a/src/twinkle/model/megatron/megatron.py +++ b/src/twinkle/model/megatron/megatron.py @@ -257,7 +257,6 @@ def __init__( # ``_attach_task_head``. task_head_loader = kwargs.pop('task_head_loader', None) task = kwargs.pop('task', None) - self._head_task = task or ('decision' if task_head_loader is not None else None) num_labels = kwargs.pop('num_labels', None) if config is not None: self.hf_config = config @@ -1982,13 +1981,6 @@ def _save_mcore_optimizer( # The sharding type must match the data-parallel wrapper the strategy chose at wrap time # (Megatron-FSDP stores optimizer state as DTensors, plain DDP does not). sharded_sd_metadata = self.strategy.get_sharded_sd_metadata() - if (self._head_task == 'decision' - and sharded_sd_metadata.get('distrib_optim_sharding_type') == 'dp_reshardable'): - # A tiny FP32 calibration head can leave an empty local DP bucket. - # MCore's canonical model-space format supports that layout; gather - # on CPU so saving does not duplicate full optimizer state on device. - sharded_sd_metadata.update(distrib_optim_sharding_type='fully_reshardable', - distrib_optim_fully_reshardable_mem_efficient=True) no_save_rng = kwargs.pop('no_save_rng', False) rng_state = None if no_save_rng else self._get_rng_state() @@ -2022,18 +2014,20 @@ def _save_mcore_optimizer( # Async staging is only sound under an initialized process group (the metadata finalize # holds a barrier); a single-process run has no skew to hide anyway. async_save = kwargs.pop('async_save', False) and dist.is_initialized() - # Older supported MCore versions use the built-in queue without this option. - save_options = {'async_strategy': 'mcore'} if 'async_strategy' in inspect.signature( - dist_checkpointing.save).parameters else {} + save_parameters = inspect.signature(dist_checkpointing.save).parameters + save_options = {} + if 'async_strategy' in save_parameters: + # Avoid the optional nvidia-resiliency-ext queue on newer MCore. + save_options['async_strategy'] = 'mcore' + if 'content_metadata' in save_parameters: + save_options['content_metadata'] = sharded_sd_metadata async_request = dist_checkpointing.save( state_dict, iter_dir, save_strategy, async_sharded_save=async_save, - # Avoid the optional nvidia-resiliency-ext queue on newer MCore. - **save_options, validate_access_integrity=True, - content_metadata=sharded_sd_metadata, + **save_options, ) if async_request is not None: # The strategy already staged the tensors to host memory; the queue's worker performs diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 576113b2d..84b28abb1 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -24,10 +24,9 @@ PEFT_BASE_LAYER_SEGMENT = 'base_layer' -def _shard_builtin_task_heads(model, *, mesh, reshard_after_forward, mp_policy): +def _shard_attached_task_heads(model, *, mesh, reshard_after_forward, mp_policy): # The head wrapper owns unsharding for the readout backward instead of the root wrapper. - heads = [getattr(model, name, None) for name in ('score', 'classifier')] - heads.extend((getattr(model, 'task_heads', None) or {}).values()) + heads = list((getattr(model, 'task_heads', None) or {}).values()) # Future tenants can bind any preallocated slot after the root is already sharded. heads.extend(module for name, module in model.named_children() if name.startswith('task_head_slot_')) seen = set() @@ -224,7 +223,7 @@ def wrap_model(self, model, optimizer=None, *additional_optimizers): world_size = self.device_mesh.world_size ep_fsdp_mesh_1d = self.ep_fsdp_device_mesh['ep_fsdp'] if ep_enabled else None - _shard_builtin_task_heads( + _shard_attached_task_heads( model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) _shard_fp32_task_heads( model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) @@ -305,8 +304,7 @@ def _prepare_optimizer_state_dict_options(self, *, for_load: bool): full_state_dict=True, cpu_offload=not for_load, broadcast_from_rank0=for_load, - # Adam initializes state lazily, so unused trainable parameters have no entry to restore. - strict=not for_load, + strict=True, ) def get_independent_task_head_state(self, head): @@ -369,6 +367,10 @@ def save_optimizer_checkpoint(self, model, optimizer, output_path: str, *, param optimizer, options=self._prepare_optimizer_state_dict_options(for_load=False), ) + # DCP requires an entry for every trainable parameter; Adam leaves unused parameters uninitialized. + for group in optim_state['param_groups']: + for name in group['params']: + optim_state['state'].setdefault(name, {}) if Platform.is_master(): remap_optimizer_state_names(optim_state, param_name_mapping or {}) torch.save(optim_state, output_path) @@ -391,12 +393,23 @@ def load_optimizer_checkpoint(self, model, optimizer, input_path: str, *, param_ if Platform.is_master(): optim_state = torch.load(input_path, map_location='cpu', weights_only=True) remap_optimizer_state_names(optim_state, param_name_mapping or {}) + empty_states = None + if Platform.is_master(): + empty_states = {name for name, value in optim_state['state'].items() if not value} + if dist.is_initialized(): + payload = [empty_states] + dist.broadcast_object_list(payload, src=0) + empty_states = payload[0] set_optimizer_state_dict( model, optimizer, optim_state, options=self._prepare_optimizer_state_dict_options(for_load=True), ) + # DCP initializes every Adam slot before loading; explicitly empty snapshots remain lazy. + parameters = dict(model.named_parameters()) + for name in empty_states: + optimizer.state.pop(parameters[name], None) def get_ep_clip_kwargs(self, model) -> Dict[str, Any]: """Return EP-aware kwargs for normalize_and_clip_grad_norm.""" diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index 0f1122bd3..dabaa5736 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -2501,6 +2501,8 @@ def save(self, name: Optional[str] = None, output_dir: Optional[str] = None, int checkpoint_dir = os.path.join(output_dir, name) if optimizer_config.cur_step % interval != 0: return + if kwargs.get('save_optimizer', False): + self._validate_training_state_save(adapter_name) if isinstance(self.strategy, NativeFSDPStrategy) and self.strategy.use_rank0_pretrained_broadcast(): # Initial adapter snapshots precede the first forward. Materialize on every # rank before state-dict collectives: non-source ranks still hold meta tensors. @@ -2604,9 +2606,7 @@ def _save_optimizer(self, output_dir, **kwargs): if lr_scheduler is not None: torch.save(lr_scheduler.state_dict(), os.path.join(output_dir, 'scheduler.pt')) - def _save_training_state(self, output_dir, **kwargs): - adapter_name = kwargs.pop('adapter_name', _default_adapter_name) - defer_trainer_state = kwargs.pop('defer_trainer_state', False) + def _validate_training_state_save(self, adapter_name): optimizer_config = self.optimizer_group[adapter_name] optimizer_config.require_finalized('save training state') optimizer = optimizer_config.optimizer @@ -2616,6 +2616,12 @@ def _save_training_state(self, output_dir, **kwargs): raise ValueError( 'save_optimizer=True requires a complete gradient accumulation window; ' 'finish the pending backward/optimizer steps and zero_grad before saving, or save weights only.') + + def _save_training_state(self, output_dir, **kwargs): + adapter_name = kwargs.pop('adapter_name', _default_adapter_name) + defer_trainer_state = kwargs.pop('defer_trainer_state', False) + self._validate_training_state_save(adapter_name) + optimizer_config = self.optimizer_group[adapter_name] # The optimizer/scheduler files are written before _write_weights() gets a chance to # create the checkpoint dir via save_pretrained; torch.save then raises # "Parent directory ... does not exist". This lives HERE (not at the top of save()) so it diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index 067db0328..db0622b90 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -73,6 +73,16 @@ class TrainingArguments(BaseModel): num_labels: int | None = Field(default=None, gt=0, strict=True) language_model_only: bool | None = Field(default=None, strict=True) + @field_validator('extra_torch_kwargs') + @classmethod + def _validate_torch_kwargs(cls, value): + if value is not None: + allowed = {'seed', 'full_determinism', 'tf32'} + unknown = set(value) - allowed + if unknown: + raise ValueError(f'unsupported extra_torch_kwargs keys {sorted(unknown)}; expected {sorted(allowed)}') + return value + @model_validator(mode="after") def _validate_head(self): if self.task_type == "seq_cls" and self.num_labels is None: diff --git a/src/twinkle/utils/loader.py b/src/twinkle/utils/loader.py index 8e0008927..4228bdb1e 100644 --- a/src/twinkle/utils/loader.py +++ b/src/twinkle/utils/loader.py @@ -157,7 +157,7 @@ def construct_class( """Try to load a class. Args: - func: Class/instance, built-in name, plugin path, or trusted ``module:Class`` spec. + func: The input class or class name/plugin name to load instance from class_T: The base class of the instance module_T: The module of the class_T **init_args: The args to construct the instruct @@ -179,16 +179,7 @@ def construct_class( cls = getattr(module, func) break else: - module_name, separator, name = func.partition(':') - if (separator and all(part.isidentifier() for part in module_name.split('.')) - and name.isidentifier() and not Path(func).exists()): - if not trust_remote_code(): - raise ValueError('Component import specs require TWINKLE_TRUST_REMOTE_CODE != 0') - cls = getattr(importlib.import_module(module_name), name) - if not isinstance(cls, type) or not issubclass(cls, class_T): - raise TypeError(f'{func!r} must name a {class_T.__name__} subclass') - else: - cls = Plugin.load_plugin(func, class_T) + cls = Plugin.load_plugin(func, class_T) return cls(**init_args) else: # Do nothing by default diff --git a/src/twinkle_client/model/multi_lora_transformers.py b/src/twinkle_client/model/multi_lora_transformers.py index 96b407cc8..594fc7fa9 100644 --- a/src/twinkle_client/model/multi_lora_transformers.py +++ b/src/twinkle_client/model/multi_lora_transformers.py @@ -422,7 +422,7 @@ def calculate_metric(self, is_training: bool = True, **kwargs) -> model_types.Ca # ------------------------------------------------------------------ # def save(self, name: str, **kwargs) -> model_types.SaveResponse: - """Save a checkpoint; save_optimizer=True requires a complete accumulation window.""" + """Save a checkpoint; save_optimizer=True requires a completed update and cleared gradients.""" return self._submit('save', model_types.SaveRequest, model_types.SaveResponse, name=name, **kwargs) def load(self, name: str, **kwargs) -> None: diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index 0de08c295..9da879def 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -444,13 +444,21 @@ def test_transformers_still_accepts_single_device(): def test_native_backends_preserve_loader_and_precision_configuration(backend, mode): app = model_application() app['args'].update(backend=backend, train_mode=mode, mixed_precision='no', - model_loader='worker_only.loader:load') + model_loader='worker_only.loader:load', extra_torch_kwargs={'full_determinism': True}) config = ServerConfig(applications=[app]) restored = ServerConfig.model_validate(config.to_yaml_dict()) model = restored.training_config.models[0] assert model.backend == backend and model.mode == mode assert model.config.model_loader == 'worker_only.loader:load' assert model.config.mixed_precision == 'no' + assert model.config.extra_torch_kwargs == {'full_determinism': True} + + +def test_extra_torch_kwargs_rejects_unknown_keys(): + app = model_application() + app['args']['extra_torch_kwargs'] = {'full_determinism': True, 'deterministic': True} + with pytest.raises(ValidationError, match='unsupported extra_torch_kwargs'): + ServerConfig(applications=[app]) @pytest.mark.parametrize('backend', ['transformers', 'megatron']) diff --git a/tests/transformers/test_checkpoint_writer_rank.py b/tests/transformers/test_checkpoint_writer_rank.py index 5fe173140..ed62c10d4 100644 --- a/tests/transformers/test_checkpoint_writer_rank.py +++ b/tests/transformers/test_checkpoint_writer_rank.py @@ -20,7 +20,8 @@ def test_full_checkpoint_has_one_writer_after_collectives(self): wrapper.strategy = Mock() wrapper.strategy.unwrap_model.return_value = model wrapper.strategy.get_full_state_dict.return_value = state - wrapper.optimizer_group = {'': SimpleNamespace(cur_step=3)} + wrapper.optimizer_group = {'': SimpleNamespace( + cur_step=3, gradient_accumulation_steps=1, optimizer=None, require_finalized=Mock())} wrapper._save_training_state = Mock() wrapper._save_tokenizer = Mock() wrapper._save_task_head_checkpoint = Mock() @@ -48,6 +49,19 @@ def test_full_checkpoint_has_one_writer_after_collectives(self): else: model.save_pretrained.assert_not_called() + def test_partial_accumulation_is_rejected_before_weight_collection(self): + wrapper = object.__new__(TransformersModel) + wrapper.strategy = Mock() + wrapper.optimizer_group = {'': SimpleNamespace( + cur_step=3, gradient_accumulation_steps=2, optimizer=None, require_finalized=Mock())} + + with self.assertRaisesRegex(ValueError, 'complete gradient accumulation window'): + inspect.unwrap(TransformersModel.save)( + wrapper, name='partial', output_dir='/unused', save_optimizer=True) + + wrapper.strategy.get_full_state_dict.assert_not_called() + wrapper.strategy.unwrap_model.assert_not_called() + if __name__ == '__main__': unittest.main() diff --git a/tests/transformers/test_megatron_weight_only_resume.py b/tests/transformers/test_megatron_weight_only_resume.py index 4a2fe56db..8687bcf9f 100644 --- a/tests/transformers/test_megatron_weight_only_resume.py +++ b/tests/transformers/test_megatron_weight_only_resume.py @@ -1,3 +1,4 @@ +import sys from contextlib import nullcontext from copy import deepcopy from types import SimpleNamespace @@ -58,26 +59,34 @@ def load_weights(*args, **kwargs): assert other.master.item() == other.model_parameter.item() == 11. -@pytest.mark.parametrize('task,sharding', [(None, 'dp_reshardable'), ('decision', 'dp_reshardable'), - ('decision', 'fsdp_dtensor')]) -def test_mcore_optimizer_save_uses_versioned_torch_dist_strategy(tmp_path, monkeypatch, task, sharding): +@pytest.mark.parametrize('api', ['legacy', 'current']) +@pytest.mark.parametrize('sharding', ['dp_reshardable', 'fsdp_dtensor']) +def test_mcore_optimizer_save_uses_versioned_torch_dist_strategy(tmp_path, monkeypatch, api, sharding): pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') from megatron.core import dist_checkpointing from megatron.core import parallel_state as mpu captured = {} - def save(state_dict, checkpoint_dir, sharded_strategy=None, common_strategy=None, - validate_access_integrity=True, async_sharded_save=False, - preprocess_common_before_consistancy_check=None, content_metadata=None): - captured.update(strategy=sharded_strategy, async_save=async_sharded_save, - metadata=content_metadata) + def record(strategy, async_save, **kwargs): + captured.update(strategy=strategy, async_save=async_save, **kwargs) - monkeypatch.setattr(dist_checkpointing, 'save', save) + def legacy_save(state_dict, checkpoint_dir, sharded_strategy=None, common_strategy=None, + validate_access_integrity=True, async_sharded_save=False, + preprocess_common_before_consistancy_check=None): + record(sharded_strategy, async_sharded_save) + + def current_save(state_dict, checkpoint_dir, sharded_strategy=None, common_strategy=None, + validate_access_integrity=True, async_sharded_save=False, + preprocess_common_before_consistancy_check=None, content_metadata=None, + async_strategy='nvrx'): + record(sharded_strategy, async_sharded_save, metadata=content_metadata, async_strategy=async_strategy) + + monkeypatch.setattr(dist_checkpointing, 'save', legacy_save if api == 'legacy' else current_save) monkeypatch.setattr(mpu, 'get_data_parallel_world_size', lambda **kwargs: 1) metadata = {'sharded': True, 'distrib_optim_sharding_type': sharding} wrapper = SimpleNamespace( - model=object(), _head_task=task, + model=object(), strategy=SimpleNamespace(get_sharded_sd_metadata=lambda: metadata), _generate_state_dict=lambda **kwargs: {'optimizer': {}}, ) @@ -90,52 +99,50 @@ def save(state_dict, checkpoint_dir, sharded_strategy=None, common_strategy=None assert captured['strategy'].version == 1 assert captured['strategy'].thread_count == 3 assert captured['async_save'] is False - assert captured['metadata'] is metadata - expected = 'fully_reshardable' if task == 'decision' and sharding == 'dp_reshardable' else sharding - assert metadata['distrib_optim_sharding_type'] == expected - assert metadata.get('distrib_optim_fully_reshardable_mem_efficient', False) == (expected == 'fully_reshardable') + assert metadata == {'sharded': True, 'distrib_optim_sharding_type': sharding} + if api == 'current': + assert captured['metadata'] is metadata + assert captured['async_strategy'] == 'mcore' + else: + assert 'metadata' not in captured + assert 'async_strategy' not in captured assert (tmp_path / 'latest_checkpointed_iteration.txt').read_text() == '1' -def test_mixed_precision_optimizer_checkpoint_uses_the_actual_main_param_order(monkeypatch): +def test_mixed_precision_optimizer_checkpoint_uses_the_actual_main_param_order(): pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') - import torch - from megatron.core import optimizer as mcore_optimizer from megatron.core.optimizer.distrib_optimizer import DistributedOptimizer + from twinkle.model.megatron.megatron import _align_distributed_optimizer_param_order + low = torch.nn.Parameter(torch.ones(8, dtype=torch.bfloat16)) head = torch.nn.Parameter(torch.ones(3)) master = torch.nn.Parameter(low.float().clone()) distributed = object.__new__(DistributedOptimizer) distributed.model_float16_groups = [[low]] distributed.model_fp32_groups = [[head]] - # MCore builds these indices in gradient-buffer order, then reorders the - # inner optimizer's main parameters to FP32 first. A mixed group must agree. distributed.model_param_group_index_map = {low: (0, 0), head: (0, 1)} distributed.optimizer = torch.optim.Adam([head, master]) - distributed.config = SimpleNamespace(use_precision_aware_optimizer_no_fp8_or_ds_fp8=False) - chained = SimpleNamespace(chained_optimizers=[distributed]) - monkeypatch.setattr(mcore_optimizer, 'get_megatron_optimizer', lambda **kwargs: chained) - wrapper = SimpleNamespace(model=[SimpleNamespace(ddp_config=SimpleNamespace(use_distributed_optimizer=True))], - use_distributed_optimizer=True, mixed_precision='bf16') + distributed.ddp_config = SimpleNamespace(use_custom_fsdp=False) + distributed.config = SimpleNamespace( + use_precision_aware_optimizer=False, use_precision_aware_optimizer_no_fp8_or_ds_fp8=False) - MegatronModel._create_megatron_optimizer(wrapper) + _align_distributed_optimizer_param_order(SimpleNamespace(chained_optimizers=[distributed])) for param, expected in [(low, master), (head, head)]: assert distributed._get_main_param_and_optimizer_states(param)['param'] is expected -def test_mcore_persisted_classifier_uses_independent_output_head(monkeypatch): - mcore_bridge = pytest.importorskip('mcore_bridge', reason='requires the optional Megatron bridge') - import torch - from mcore_bridge.bridge.gpt_bridge import GPTBridge - +def test_mcore_persisted_classifier_config(monkeypatch): + pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') from twinkle import Platform from twinkle.model.megatron.bridge.mcore import MCoreBridgeBackend - monkeypatch.setattr(mcore_bridge, 'hf_to_mcore_config', - lambda config: {'untie_embeddings_and_output_weights': False}) - monkeypatch.setattr(mcore_bridge, 'ModelConfig', lambda **kwargs: kwargs) + bridge = SimpleNamespace( + ModelConfig=lambda **kwargs: kwargs, + hf_to_mcore_config=lambda config: {'untie_embeddings_and_output_weights': False}, + ) + monkeypatch.setitem(sys.modules, 'mcore_bridge', bridge) monkeypatch.setattr(Platform, 'get_platform', lambda: SimpleNamespace( configure_model_config=lambda config: None)) strategy = SimpleNamespace(params_type='float32', sequence_parallel=False, variable_seq_lengths=False) @@ -147,10 +154,19 @@ def test_mcore_persisted_classifier_uses_independent_output_head(monkeypatch): assert config['task_type'] == 'seq_cls' assert config['num_labels'] == 3 assert config['untie_embeddings_and_output_weights'] is True + with pytest.raises(ValueError, match='task_type'): + MCoreBridgeBackend().build_model_config(hf_config, {}, strategy, task_type='causal_lm') + with pytest.raises(ValueError, match='num_labels'): + MCoreBridgeBackend().build_model_config(hf_config, {}, strategy, num_labels=2) + + +def test_mcore_persisted_classifier_uses_independent_output_head(): + pytest.importorskip('mcore_bridge', reason='requires the optional Megatron bridge') + from mcore_bridge.bridge.gpt_bridge import GPTBridge bridge = object.__new__(GPTBridge) bridge.config = SimpleNamespace( - task_type=config['task_type'], untie_embeddings_and_output_weights=True, padded_vocab_size=13) + task_type='seq_cls', untie_embeddings_and_output_weights=True, padded_vocab_size=13) bridge.is_multimodal = False bridge.tp_size = bridge.etp_size = 1 bridge.tp_rank = bridge.etp_rank = 0 diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 6d36d77d2..cb9a0d79a 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -1,13 +1,14 @@ # Copyright (c) ModelScope Contributors. All rights reserved. -from unittest.mock import MagicMock, patch -from types import SimpleNamespace import os +from copy import deepcopy +from types import SimpleNamespace +from unittest.mock import MagicMock, patch import pytest import torch -from twinkle.model.transformers.strategy.native_fsdp import NativeFSDPStrategy from twinkle.model.transformers.strategy import native_fsdp +from twinkle.model.transformers.strategy.native_fsdp import NativeFSDPStrategy from twinkle.model.transformers.transformers import TransformersModel @@ -113,7 +114,7 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t torch.testing.assert_close(replacement['b'], torch.ones(2)) -def test_builtin_score_head_is_sharded_before_the_root_model(): +def test_builtin_score_head_remains_in_root_fsdp_group(): model = torch.nn.Module() model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) model.score = torch.nn.Linear(4, 2).to(torch.bfloat16) @@ -124,11 +125,10 @@ def test_builtin_score_head_is_sharded_before_the_root_model(): patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: strategy.wrap_model(model) - assert fully_shard.call_count == 2 - head_call, model_call = fully_shard.call_args_list - assert head_call.args[0] is model.score - assert head_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 - assert model_call.args[0] is model + fully_shard.assert_called_once() + assert fully_shard.call_args.args[0] is model + assert fully_shard.call_args.kwargs['mp_policy'].param_dtype is torch.bfloat16 + assert not getattr(model.score, '_twinkle_independent_fsdp', False) @pytest.mark.parametrize('preallocated', [False, True]) @@ -159,22 +159,64 @@ def test_attached_standard_head_is_sharded_independently(preallocated): assert all(head._twinkle_independent_fsdp for head in model.task_heads.values()) -def test_optimizer_restore_accepts_lazy_state_for_unused_parameters(): - from torch.distributed.checkpoint.state_dict import get_optimizer_state_dict, set_optimizer_state_dict - +def test_optimizer_restore_accepts_lazy_state_for_unused_parameters(tmp_path): strategy = NativeFSDPStrategy(device_mesh=MagicMock(), enable_ep=False) model = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 1)) optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) model[0](torch.ones(2, 3)).sum().backward() optimizer.step() optimizer.zero_grad() - state = get_optimizer_state_dict( - model, optimizer, options=strategy._prepare_optimizer_state_dict_options(for_load=False)) - load_options = strategy._prepare_optimizer_state_dict_options(for_load=True) - load_options.broadcast_from_rank0 = False - set_optimizer_state_dict(model, optimizer, state, options=load_options) - assert optimizer.state[model[0].weight]['step'].item() == 1 - assert len(optimizer.state[model[1].weight]) == 0 + with patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=True): + strategy.save_optimizer_checkpoint(model, optimizer, str(tmp_path / 'optimizer.pt')) + state = torch.load(tmp_path / 'optimizer.pt', weights_only=True) + assert state['state']['1.weight'] == {} + + cold = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 1)) + cold_optimizer = torch.optim.Adam(cold.parameters(), lr=1e-3) + prepare_options = strategy._prepare_optimizer_state_dict_options + + def cpu_options(*, for_load): + options = prepare_options(for_load=for_load) + options.broadcast_from_rank0 = False + return options + + with patch.object(strategy, '_prepare_optimizer_state_dict_options', side_effect=cpu_options), \ + patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=True): + strategy.load_optimizer_checkpoint(cold, cold_optimizer, str(tmp_path / 'optimizer.pt')) + assert cold_optimizer.state[cold[0].weight]['step'].item() == 1 + assert not cold_optimizer.state[cold[1].weight] + + corrupted = deepcopy(state) + del corrupted['state']['0.weight'] + torch.save(corrupted, tmp_path / 'corrupted.pt') + with patch.object(strategy, '_prepare_optimizer_state_dict_options', side_effect=cpu_options), \ + patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=True), \ + pytest.raises(KeyError): + strategy.load_optimizer_checkpoint(cold, cold_optimizer, str(tmp_path / 'corrupted.pt')) + + +def test_nonmaster_restores_lazy_slots_from_broadcast_metadata(): + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), enable_ep=False) + model = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 1)) + optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) + model(torch.ones(2, 3)).sum().backward() + optimizer.step() + optimizer.zero_grad() + + def broadcast_empty_states(payload, src): + assert src == 0 + payload[0] = {'1.weight'} + + with patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=False), \ + patch('twinkle.model.transformers.strategy.native_fsdp.dist.is_initialized', return_value=True), \ + patch('twinkle.model.transformers.strategy.native_fsdp.dist.broadcast_object_list', + side_effect=broadcast_empty_states), \ + patch('torch.distributed.checkpoint.state_dict.set_optimizer_state_dict') as set_state: + strategy.load_optimizer_checkpoint(model, optimizer, '/unused') + + set_state.assert_called_once() + assert optimizer.state[model[0].weight] + assert not optimizer.state[model[1].weight] def test_fp32_task_head_is_sharded_as_separate_precision_group(): diff --git a/tests/utils/test_component_imports.py b/tests/utils/test_component_imports.py deleted file mode 100644 index 2c96e61c1..000000000 --- a/tests/utils/test_component_imports.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (c) ModelScope Contributors. All rights reserved. -"""Explicit component imports preserve the base-class and trust contracts.""" -import pytest - -from twinkle.utils.loader import construct_class - - -def test_import_instantiates_the_requested_class(monkeypatch): - from collections import Counter - - monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '1') - result = construct_class('collections:Counter', dict, [], a=2) - assert type(result) is Counter and result == {'a': 2} - - -@pytest.mark.parametrize('name', ['collections:deque', 'collections:__name__']) -def test_import_rejects_other_component_types(monkeypatch, name): - monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '1') - with pytest.raises(TypeError, match='dict subclass'): - construct_class(name, dict, []) - - -def test_safe_mode_rejects_import_before_executing_module(monkeypatch): - monkeypatch.setenv('TWINKLE_TRUST_REMOTE_CODE', '0') - with pytest.raises(ValueError, match='TWINKLE_TRUST_REMOTE_CODE'): - construct_class('does_not_exist:Component', dict, []) From 2ab4552a5debf3bd9306aad6843c7d6a69f7c3c8 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 08:20:22 +0800 Subject: [PATCH 11/18] fix(fsdp): skip empty optimizer state on non-master ranks --- .../model/transformers/strategy/native_fsdp.py | 8 ++++---- tests/transformers/test_native_fsdp_strategy.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 84b28abb1..a9aeee88b 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -367,11 +367,11 @@ def save_optimizer_checkpoint(self, model, optimizer, output_path: str, *, param optimizer, options=self._prepare_optimizer_state_dict_options(for_load=False), ) - # DCP requires an entry for every trainable parameter; Adam leaves unused parameters uninitialized. - for group in optim_state['param_groups']: - for name in group['params']: - optim_state['state'].setdefault(name, {}) if Platform.is_master(): + # DCP requires an entry for every trainable parameter; Adam leaves unused parameters uninitialized. + for group in optim_state['param_groups']: + for name in group['params']: + optim_state['state'].setdefault(name, {}) remap_optimizer_state_names(optim_state, param_name_mapping or {}) torch.save(optim_state, output_path) diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index cb9a0d79a..f08ddb2cd 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -159,6 +159,20 @@ def test_attached_standard_head_is_sharded_independently(preallocated): assert all(head._twinkle_independent_fsdp for head in model.task_heads.values()) +def test_nonmaster_save_ignores_empty_gathered_optimizer_state(tmp_path): + strategy = NativeFSDPStrategy(device_mesh=MagicMock(), enable_ep=False) + model = torch.nn.Linear(3, 3) + optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) + + with patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=False), \ + patch('torch.distributed.checkpoint.state_dict.get_optimizer_state_dict', return_value={}) as get_state, \ + patch('torch.save') as save: + strategy.save_optimizer_checkpoint(model, optimizer, str(tmp_path / 'optimizer.pt')) + + get_state.assert_called_once() + save.assert_not_called() + + def test_optimizer_restore_accepts_lazy_state_for_unused_parameters(tmp_path): strategy = NativeFSDPStrategy(device_mesh=MagicMock(), enable_ep=False) model = torch.nn.Sequential(torch.nn.Linear(3, 3), torch.nn.Linear(3, 1)) @@ -191,7 +205,7 @@ def cpu_options(*, for_load): torch.save(corrupted, tmp_path / 'corrupted.pt') with patch.object(strategy, '_prepare_optimizer_state_dict_options', side_effect=cpu_options), \ patch('twinkle.model.transformers.strategy.native_fsdp.Platform.is_master', return_value=True), \ - pytest.raises(KeyError): + pytest.raises((KeyError, RuntimeError), match='0.weight'): strategy.load_optimizer_checkpoint(cold, cold_optimizer, str(tmp_path / 'corrupted.pt')) From 9ee344ca9632e513c6c978ee4de30011f7e87c8f Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 10:18:15 +0800 Subject: [PATCH 12/18] fix(lora): preserve multimodal targets across tenant slots --- src/twinkle/model/multi_lora.py | 31 +++---- .../transformers/multi_lora_transformers.py | 6 ++ tests/model/test_multimodal_lora_slots.py | 85 +++++++++++++++++++ 3 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 tests/model/test_multimodal_lora_slots.py diff --git a/src/twinkle/model/multi_lora.py b/src/twinkle/model/multi_lora.py index 0b6553a67..a787215a3 100644 --- a/src/twinkle/model/multi_lora.py +++ b/src/twinkle/model/multi_lora.py @@ -505,9 +505,9 @@ def match_target_modules( # Strip LoRA-specific suffixes (e.g. ".lora_A.default.weight") so that # a full parameter name like "model.layers.0.attn.proj.lora_A.default.weight" # can be matched against target_modules like ["attn.proj"]. - cleaned = module_name - if '.lora_' in module_name: - cleaned = re.sub(r'\.lora_\w+(\.[\w-]+)*$', '', module_name) + cleaned = module_name.removeprefix('base_model.model.') + if '.lora_' in cleaned: + cleaned = re.sub(r'\.lora_\w+(\.[\w-]+)*$', '', cleaned) if isinstance(target_modules, str): return re.fullmatch(target_modules, cleaned) is not None @@ -689,6 +689,14 @@ def patch(self, low_cpu_mem_usage = module_device.type == 'meta' base_config = kwargs.get('lora_config', None) + megatron_targets = None + if isinstance(module, list): + requested = base_config.target_modules if base_config is not None else target_modules + if requested: + from .megatron import MegatronModel + requested = [requested] if isinstance(requested, str) else list(requested) + # Resolve each pristine chunk once; adding a slot changes its module tree. + megatron_targets = [MegatronModel.get_target_modules(chunk, requested) for chunk in module] for i in range(self.max_loras): config = deepcopy(base_config) if base_config is not None else None if config is None: @@ -716,22 +724,14 @@ def _patch_peft(_module): self._patch_lora_forward(name, submodule) return _module - def _patch_megatron(_module): - # Expand target_modules (e.g., 'all-linear' -> actual module names) + def _patch_megatron(_module, targets): _config = deepcopy(config) + if targets is not None: + _config.target_modules = targets if isinstance(_module, PeftModel): _module.add_adapter(lora_tenant.adapter_name, _config, low_cpu_mem_usage=low_cpu_mem_usage) self._autocast_adapter_dtype(_module, lora_tenant.adapter_name) else: - # TODO first wrap needs parse target_modules, need to fix later - if _config.target_modules: - if isinstance(_config.target_modules, str): - target_modules = [_config.target_modules] - else: - target_modules = list(_config.target_modules) - - from .megatron import MegatronModel - _config.target_modules = MegatronModel.get_target_modules(_module, target_modules) _module = get_peft_model( _module, _config, lora_tenant.adapter_name, low_cpu_mem_usage=low_cpu_mem_usage) @@ -741,7 +741,8 @@ def _patch_megatron(_module): return _module if isinstance(module, list): - module = [_patch_megatron(_m) for _m in module] + module = [_patch_megatron(chunk, megatron_targets[index] if megatron_targets is not None else None) + for index, chunk in enumerate(module)] else: module = _patch_peft(module) diff --git a/src/twinkle/model/transformers/multi_lora_transformers.py b/src/twinkle/model/transformers/multi_lora_transformers.py index ecabb77a3..599575a11 100644 --- a/src/twinkle/model/transformers/multi_lora_transformers.py +++ b/src/twinkle/model/transformers/multi_lora_transformers.py @@ -359,6 +359,12 @@ def add_adapter_to_model(self, adapter_name: str, config_or_dir: Union[PeftConfi config = self._copy_lora_config(config_or_dir) if config.modules_to_save: raise ValueError('modules_to_save is not supported for a multi-tenant LoRA adapter.') + self._expand_multimodal_lora_target( + config, + self.strategy.unwrap_model(self.model), + freeze_llm=kwargs.pop('freeze_llm', False), + freeze_vit=kwargs.pop('freeze_vit', True), + freeze_aligner=kwargs.pop('freeze_aligner', True)) self.multi_adapter.validate_tenant_target_modules(config.target_modules, getattr(config, 'target_parameters', None)) self._register_adapter(adapter_name, config, **kwargs) diff --git a/tests/model/test_multimodal_lora_slots.py b/tests/model/test_multimodal_lora_slots.py new file mode 100644 index 000000000..7a621e308 --- /dev/null +++ b/tests/model/test_multimodal_lora_slots.py @@ -0,0 +1,85 @@ +# Copyright (c) ModelScope Contributors. All rights reserved. +import inspect +from copy import deepcopy +from types import SimpleNamespace + +import pytest +import torch +from peft import LoraConfig +from peft.tuners.lora import LoraLayer + +from twinkle.model.multi_lora import MultiLora +from twinkle.model.transformers.multi_lora_transformers import MultiLoraTransformersModel +from twinkle.model.transformers.transformers import TransformersModel + + +class _VisionLanguageModel(torch.nn.Module): + + def __init__(self): + super().__init__() + self.language = torch.nn.Sequential(torch.nn.Linear(4, 4)) + self.visual = torch.nn.Sequential(torch.nn.Linear(4, 4)) + self.aligner = torch.nn.Sequential(torch.nn.Linear(4, 4)) + + def forward(self, x): + return self.language(x) + self.aligner(self.visual(x)) + + +@pytest.mark.parametrize('vision_only', [False, True]) +def test_multimodal_tenant_updates_only_requested_towers(vision_only): + manager = MultiLora(max_loras=2, max_r=2) + module = manager.patch(_VisionLanguageModel(), lora_config=LoraConfig(r=2, target_modules='all-linear')) + manager.save_initial_weights() + manager.reset_adapter_status() + host = SimpleNamespace( + model=module, model_arch={'language_model': ['language'], 'vision_tower': ['visual'], 'aligner': ['aligner']}, + multi_adapter=manager, strategy=SimpleNamespace(unwrap_model=lambda model: model), + _copy_lora_config=MultiLoraTransformersModel._copy_lora_config, + _register_adapter=lambda name, config, **kwargs: manager.acquire_lora(name, config), + ) + host._expand_multimodal_lora_target = lambda *args, **kwargs: TransformersModel._expand_multimodal_lora_target( + host, *args, **kwargs) + config = LoraConfig(r=2, lora_alpha=2, target_modules='all-linear', lora_dropout=0.0) + original = deepcopy(config.to_dict()) + inspect.unwrap(MultiLoraTransformersModel.add_adapter_to_model)( + host, 'tenant', config, freeze_llm=vision_only, freeze_vit=not vision_only) + assert config.to_dict() == original + before = {name: value.detach().clone() for name, value in module.named_parameters()} + with manager.adapter('tenant'): + optimizer = torch.optim.SGD([p for p in module.parameters() if p.requires_grad], lr=0.1) + module(torch.ones(2, 4)).square().sum().backward() + optimizer.step() + changed = [name for name, value in module.named_parameters() if not torch.equal(value, before[name])] + assert changed + expected = '.visual.' if vision_only else '.language.' + assert all(expected in name and '.lora_' in name for name in changed), changed + + +def test_megatron_slots_share_each_chunks_original_target_modules(monkeypatch): + from twinkle.model.megatron.megatron import MegatronModel + + class Chunk(torch.nn.Module): + + def __init__(self, name): + super().__init__() + self.add_module(name, torch.nn.Linear(4, 4)) + self.visual = torch.nn.Linear(4, 4) + + chunks = [Chunk('first'), Chunk('second')] + calls = [] + + def targets(model, requested): + # Expansion must inspect each pristine chunk, before PEFT changes the module tree. + assert not any(isinstance(layer, LoraLayer) for layer in model.modules()) + assert requested == ['all-linear'] + calls.append(model) + return ['first' if model is chunks[0] else 'second'] + + monkeypatch.setattr(MegatronModel, 'get_target_modules', staticmethod(targets)) + manager = MultiLora(max_loras=2, max_r=2) + wrapped = manager.patch(chunks, lora_config=LoraConfig(r=2, target_modules='all-linear')) + assert calls == chunks + for chunk in wrapped: + adapted = [(name, layer) for name, layer in chunk.named_modules() if isinstance(layer, LoraLayer)] + assert len(adapted) == 1 and '.visual' not in adapted[0][0] + assert set(adapted[0][1].lora_A) == {'lora_0', 'lora_1'} From aa1c4968cd9c876085ca51232e7e68cb38f0956d Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 15:44:54 +0800 Subject: [PATCH 13/18] refactor: simplify checkpoint and response handling --- src/twinkle/model/megatron/bridge/mcore.py | 10 ++++----- .../transformers/strategy/native_fsdp.py | 3 +-- src/twinkle/signals/response.py | 15 ++++++------- .../transformers/test_native_fsdp_strategy.py | 22 +++++-------------- 4 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/twinkle/model/megatron/bridge/mcore.py b/src/twinkle/model/megatron/bridge/mcore.py index 78f532534..465f2b38a 100644 --- a/src/twinkle/model/megatron/bridge/mcore.py +++ b/src/twinkle/model/megatron/bridge/mcore.py @@ -27,17 +27,15 @@ def build_model_config(self, hf_config: Any, parallel_kwargs: Dict[str, Any], st config_kwargs = hf_to_mcore_config(hf_config) # Preserve a checkpoint's classification head instead of constructing a # vocabulary-sized language-model output layer and skipping score weights. - is_seq_cls = any(name.endswith('ForSequenceClassification') - for name in (getattr(hf_config, 'architectures', None) or [])) - if is_seq_cls: + if any(name.endswith('ForSequenceClassification') + for name in (getattr(hf_config, 'architectures', None) or [])): if kwargs.get('task_type', 'seq_cls') != 'seq_cls': raise ValueError('A sequence-classification checkpoint requires task_type=seq_cls.') if kwargs.get('num_labels', hf_config.num_labels) != hf_config.num_labels: raise ValueError('num_labels must match the persisted classification head.') + kwargs.update(task_type='seq_cls', num_labels=hf_config.num_labels, + untie_embeddings_and_output_weights=True) config_kwargs.update(kwargs) - if is_seq_cls: - config_kwargs.update(task_type='seq_cls', num_labels=hf_config.num_labels, - untie_embeddings_and_output_weights=True) # per-token-mean grad normalization (mcore default is False; twinkle forces True). if 'calculate_per_token_loss' not in config_kwargs: config_kwargs['calculate_per_token_loss'] = True diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index a9aeee88b..1d53353ef 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -390,11 +390,10 @@ def load_optimizer_checkpoint(self, model, optimizer, input_path: str, *, param_ from torch.distributed.checkpoint.state_dict import set_optimizer_state_dict optim_state = {} + empty_states = None if Platform.is_master(): optim_state = torch.load(input_path, map_location='cpu', weights_only=True) remap_optimizer_state_names(optim_state, param_name_mapping or {}) - empty_states = None - if Platform.is_master(): empty_states = {name for name, value in optim_state['state'].items() if not value} if dist.is_initialized(): payload = [empty_states] diff --git a/src/twinkle/signals/response.py b/src/twinkle/signals/response.py index 5996accf0..9eb322a6c 100644 --- a/src/twinkle/signals/response.py +++ b/src/twinkle/signals/response.py @@ -52,14 +52,13 @@ def response_token_scores(outputs: Any, features: List[Dict[str, Any]], *, outpu values = outputs.get(output_key) if isinstance(outputs, dict) else outputs if values is None: raise RuntimeError(f'the forward returned no {output_key!r} channel.') - if isinstance(values, (list, tuple)) and values and isinstance(values[0], (list, tuple, np.ndarray, torch.Tensor)): - rows = [] - for batch in values: - batch = torch.as_tensor(batch).detach().float().cpu() - rows.extend(batch.unbind(0) if batch.dim() > 1 else [batch]) - else: - values = torch.as_tensor(values).detach().float().cpu() - rows = list(values.unbind(0)) if values.dim() > 1 else [values] + if not (isinstance(values, (list, tuple)) and values + and isinstance(values[0], (list, tuple, np.ndarray, torch.Tensor))): + values = [values] + rows = [] + for batch in values: + batch = torch.as_tensor(batch).detach().float().cpu() + rows.extend(batch.unbind(0) if batch.dim() > 1 else [batch]) if len(rows) != len(features): raise RuntimeError(f'forward returned {len(rows)} rows for {len(features)} features; ' 'per-row alignment is impossible.') diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index f08ddb2cd..fb5a7fb17 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -234,24 +234,12 @@ def broadcast_empty_states(payload, src): def test_fp32_task_head_is_sharded_as_separate_precision_group(): - - class Head(torch.nn.Module): - keep_fp32 = True - - def __init__(self): - super().__init__() - self.bias = torch.nn.Parameter(torch.zeros(4, dtype=torch.float32)) - - class Model(torch.nn.Module): - - def __init__(self): - super().__init__() - self.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) - self.task_head = Head() - self.scoring_head = self.task_head - strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) - model = Model() + model = torch.nn.Module() + model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) + model.task_head = torch.nn.Linear(4, 1) + model.task_head.keep_fp32 = True + model.scoring_head = model.task_head with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', return_value=MagicMock()), \ patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: From c1e63aad91cdc3cfd51dc2cb66e78efdc4e22bdd Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 16:08:23 +0800 Subject: [PATCH 14/18] refactor: consolidate backend validation and task-head sharding --- .../transformers/strategy/native_fsdp.py | 31 ++--- src/twinkle/server/config/training.py | 51 ++++----- tests/server/config/test_training_config.py | 48 ++++---- tests/server/model/test_task_head.py | 108 +++++------------- .../test_megatron_weight_only_resume.py | 39 +------ .../transformers/test_native_fsdp_strategy.py | 46 ++++---- 6 files changed, 105 insertions(+), 218 deletions(-) diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 1d53353ef..82513abdb 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -24,36 +24,24 @@ PEFT_BASE_LAYER_SEGMENT = 'base_layer' -def _shard_attached_task_heads(model, *, mesh, reshard_after_forward, mp_policy): +def _shard_task_heads(model, *, mesh, reshard_after_forward, mp_policy): # The head wrapper owns unsharding for the readout backward instead of the root wrapper. heads = list((getattr(model, 'task_heads', None) or {}).values()) # Future tenants can bind any preallocated slot after the root is already sharded. heads.extend(module for name, module in model.named_children() if name.startswith('task_head_slot_')) + heads = [head for head in heads if not getattr(head, 'keep_fp32', False)] + heads.extend(module for module in model.modules() if getattr(module, 'keep_fp32', False) + and not getattr(module, '_twinkle_independent_fsdp', False)) seen = set() for head in heads: - if (head is None or id(head) in seen or getattr(head, 'keep_fp32', False) - or next(head.parameters(), None) is None): + if head is None or id(head) in seen or next(head.parameters(), None) is None: continue seen.add(id(head)) - fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) + policy = _build_mp_policy('no') if getattr(head, 'keep_fp32', False) else mp_policy + fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=policy) head._twinkle_independent_fsdp = True -def _shard_fp32_task_heads(model, *, mesh, reshard_after_forward): - # Independent FSDP wrappers unshard these externally-called heads and keep per-group dtypes uniform. - for module in model.modules(): - if (getattr(module, '_twinkle_independent_fsdp', False) - or not getattr(module, 'keep_fp32', False) or next(module.parameters(), None) is None): - continue - fully_shard( - module, - mesh=mesh, - reshard_after_forward=reshard_after_forward, - mp_policy=_build_mp_policy('no'), - ) - module._twinkle_independent_fsdp = True - - class NativeFSDPStrategy(TransformerStrategyBase): def __init__(self, @@ -223,10 +211,8 @@ def wrap_model(self, model, optimizer=None, *additional_optimizers): world_size = self.device_mesh.world_size ep_fsdp_mesh_1d = self.ep_fsdp_device_mesh['ep_fsdp'] if ep_enabled else None - _shard_attached_task_heads( + _shard_task_heads( model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) - _shard_fp32_task_heads( - model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) for layer_mod, experts_mod in layer_pairs: layer_mod._fsdp_modules = [] @@ -304,7 +290,6 @@ def _prepare_optimizer_state_dict_options(self, *, for_load: bool): full_state_dict=True, cpu_offload=not for_load, broadcast_from_rank0=for_load, - strict=True, ) def get_independent_task_head_state(self, head): diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index db0622b90..f05afdf0f 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -99,6 +99,24 @@ def _validate_replicas(self): raise ValueError("min_replicas must not exceed max_replicas") return self + def validate_backend(self, backend: str, *, mode: str, hybrid=None) -> None: + if self.strategy is not None and backend != "transformers": + raise ValueError("strategy requires the transformers backend") + if self.gradient_checkpointing is not None and backend != "transformers": + raise ValueError("gradient_checkpointing requires the transformers backend") + if self.mixed_precision is not None and backend == "mock": + raise ValueError("mixed_precision requires the transformers or megatron backend") + if hybrid is not None: + if backend != "transformers": + raise ValueError("hybrid requires the transformers backend") + if mode == "full": + raise ValueError('hybrid is not supported with train_mode="full"') + if self.model_cls is not None and (backend != "transformers" or mode not in ("lora", "full") + or hybrid is not None): + raise ValueError("model_cls requires a transformers LoRA/full deployment") + if self.model_loader is not None and backend == "mock": + raise ValueError("model_loader requires the transformers or megatron backend") + class LoraArguments(BaseModel): """LoRA parameters shared by YAML and LoRA instance configuration.""" @@ -129,23 +147,7 @@ class ModelArgs(TrainingArguments, LoraArguments): @model_validator(mode="after") def _validate_spectral_backend(self): validate_native_st_model(self.model_id, self.task_type, self.model_cls) - if self.strategy is not None and self.backend != "transformers": - raise ValueError("strategy is only supported by the transformers backend") - if self.gradient_checkpointing is not None and self.backend != "transformers": - raise ValueError("gradient_checkpointing requires the transformers backend") - if self.mixed_precision is not None and self.backend == "mock": - raise ValueError("mixed_precision requires the transformers or megatron backend") - if self.hybrid is not None: - if self.backend != "transformers": - raise ValueError("hybrid is only supported by the transformers backend") - if self.train_mode == "full": - raise ValueError('hybrid is not supported with train_mode="full"') - if self.model_cls is not None: - if (self.backend != "transformers" or self.train_mode not in ("lora", "full") - or self.hybrid is not None): - raise ValueError("model_cls requires a transformers LoRA/full deployment") - if self.model_loader is not None and self.backend == "mock": - raise ValueError("model_loader requires the transformers or megatron backend") + self.validate_backend(self.backend, mode=self.train_mode, hybrid=self.hybrid) if self.language_model_only is not None and self.backend != "megatron": raise ValueError("language_model_only requires the megatron backend") return self @@ -230,20 +232,7 @@ def _validate_backend(self): raise ValueError("megatron training instances require at least 2 devices") if self.config.device_mesh.device_type not in (None, self.device_type): raise ValueError("device_mesh.device_type must match device_type") - if self.config.strategy is not None and self.backend != "transformers": - raise ValueError("strategy requires the transformers backend") - if self.config.gradient_checkpointing is not None and self.backend != "transformers": - raise ValueError("gradient_checkpointing requires the transformers backend") - if self.config.mixed_precision is not None and self.backend == "mock": - raise ValueError("mixed_precision requires the transformers or megatron backend") - if isinstance(self.config, LoraTrainingConfig) and self.config.hybrid and self.backend != "transformers": - raise ValueError("hybrid requires the transformers backend") - if self.config.model_cls is not None: - if (self.backend != "transformers" or self.mode not in ("lora", "full") - or getattr(self.config, "hybrid", None) is not None): - raise ValueError("model_cls requires a transformers LoRA/full deployment") - if self.config.model_loader is not None and self.backend == "mock": - raise ValueError("model_loader requires the transformers or megatron backend") + self.config.validate_backend(self.backend, mode=self.mode, hybrid=getattr(self.config, "hybrid", None)) return self @property diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index 9da879def..37bd88f85 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -31,7 +31,8 @@ def model_application(): @pytest.mark.asyncio -async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construction(monkeypatch): +@pytest.mark.parametrize('mode', ['lora', 'full']) +async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construction(monkeypatch, mode): from types import SimpleNamespace from unittest.mock import AsyncMock, Mock from twinkle.server.config.training import TrainingModelConfig @@ -42,7 +43,7 @@ async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construct app = model_application() # These modules deliberately do not exist on the gateway. Resolution belongs to workers. app['args'].update(model_cls='worker_only.encoder:Model', model_loader='worker_only.loader:load', - gradient_checkpointing=False, mixed_precision='no') + gradient_checkpointing=False, mixed_precision='no', train_mode=mode) catalog = ServerConfig(applications=[app]).training_config spec = TrainingModelConfig.model_validate_json(catalog.models[0].model_dump_json()) captured = Mock(return_value=object()) @@ -55,7 +56,7 @@ async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construct pool=SimpleNamespace(name=lambda *args: 'owned-instance'), deployment_options=lambda *args: {}, deploy=AsyncMock()) await driver.deploy(InstanceRecord(instance_id='instance', controller_epoch='epoch', - base_model=spec.name, mode='lora', startup_deadline=0), spec) + base_model=spec.name, mode=mode, startup_deadline=0), spec) assert captured.call_args.kwargs['model_cls'] == app['args']['model_cls'] assert captured.call_args.kwargs['model_loader'] == app['args']['model_loader'] assert captured.call_args.kwargs['gradient_checkpointing'] is False @@ -63,10 +64,11 @@ async def test_custom_model_specs_survive_catalog_roundtrip_and_worker_construct driver.runtime.deploy.assert_awaited_once() -@pytest.mark.parametrize('mutation', ['hybrid', 'megatron']) -def test_custom_model_class_rejects_incompatible_training_modes(mutation): +@pytest.mark.parametrize('mutation,mode', [('hybrid', 'lora'), ('megatron', 'lora'), ('megatron', 'full')]) +def test_custom_model_class_rejects_incompatible_training_modes(mutation, mode): app = model_application() app['args']['model_cls'] = 'worker_only.encoder:Model' + app['args']['train_mode'] = mode if mutation == 'hybrid': app['args']['hybrid'] = {'allocation_path': '/tmp/allocation.json'} else: @@ -75,18 +77,21 @@ def test_custom_model_class_rejects_incompatible_training_modes(mutation): ServerConfig(applications=[app]) -@pytest.mark.parametrize('backend,train_mode', [ - ('transformers', 'lora'), - ('transformers', 'full'), - ('megatron', 'lora'), - ('megatron', 'full'), -]) -def test_native_st_without_model_cls_is_rejected_before_runtime(tmp_path, backend, train_mode): +@pytest.mark.parametrize('resolved', [False, True]) +def test_native_st_without_model_cls_is_rejected_before_runtime(tmp_path, resolved): + from twinkle.server.config.training import TrainingModelConfig + (tmp_path / 'modules.json').write_text('[]') - app = model_application() - app['args'].update(model_id=str(tmp_path), task_type='embedding', backend=backend, train_mode=train_mode) with pytest.raises(ValidationError, match='native sentence-transformers checkpoint requires model_cls'): - ServerConfig(applications=[app]) + if resolved: + values = model_config() + values['model_id'] = str(tmp_path) + values['config']['task_type'] = 'embedding' + TrainingModelConfig.model_validate(values) + else: + app = model_application() + app['args'].update(model_id=str(tmp_path), task_type='embedding') + ServerConfig(applications=[app]) def test_native_st_with_model_cls_keeps_the_supported_lora_entry(tmp_path): @@ -459,16 +464,3 @@ def test_extra_torch_kwargs_rejects_unknown_keys(): app['args']['extra_torch_kwargs'] = {'full_determinism': True, 'deterministic': True} with pytest.raises(ValidationError, match='unsupported extra_torch_kwargs'): ServerConfig(applications=[app]) - - -@pytest.mark.parametrize('backend', ['transformers', 'megatron']) -@pytest.mark.parametrize('mode', ['lora', 'full']) -def test_resolved_native_st_without_model_cls_is_rejected(tmp_path, backend, mode): - from twinkle.server.config.training import TrainingModelConfig - - (tmp_path / 'modules.json').write_text('[]') - values = model_config(mode) - values.update(model_id=str(tmp_path), backend=backend) - values['config']['task_type'] = 'embedding' - with pytest.raises(ValidationError, match='native sentence-transformers checkpoint requires model_cls'): - TrainingModelConfig.model_validate(values) diff --git a/tests/server/model/test_task_head.py b/tests/server/model/test_task_head.py index 66b6cb74f..cfc1469d8 100644 --- a/tests/server/model/test_task_head.py +++ b/tests/server/model/test_task_head.py @@ -104,7 +104,8 @@ def tokenizer(text, **kwargs): @pytest.mark.parametrize('task,width', [('seq_cls', 3), ('reranker', 1), ('value', 1)]) -def test_transformers_backend_preserves_validated_head_contract(monkeypatch, task, width): +@pytest.mark.parametrize('mode', ['lora', 'full']) +def test_transformers_backend_preserves_validated_head_contract(monkeypatch, task, width, mode): from transformers import AutoConfig from twinkle.hub import HubOperation from twinkle.server.model import app @@ -114,15 +115,15 @@ def test_transformers_backend_preserves_validated_head_contract(monkeypatch, tas monkeypatch.setattr(AutoConfig, 'from_pretrained', lambda *a, **kw: SimpleNamespace( architectures=['Qwen2ForCausalLM'])) received = {} - monkeypatch.setattr(transformers_model, 'TwinkleCompatTransformersModel', lambda **kwargs: received.update(kwargs)) - kwargs = {'model_id': 'unused', 'task_type': task, 'num_labels': width} + model_class = 'TwinkleCompatFullTransformersModel' if mode == 'full' else 'TwinkleCompatTransformersModel' + monkeypatch.setattr(transformers_model, model_class, lambda **kwargs: received.update(kwargs)) + kwargs = {'model_id': 'unused', 'task_type': task, 'num_labels': width, 'train_mode': mode} app._validate_model_task(kwargs) app._make_transformers_model(kwargs) assert received == {'model_id': 'unused', 'task': task, 'num_labels': width} -@pytest.mark.parametrize('task,width', [('seq_cls', 3), ('reranker', 1)]) -def test_full_persisted_head_does_not_forward_num_labels_to_hf(monkeypatch, task, width): +def test_full_persisted_head_does_not_forward_num_labels_to_hf(monkeypatch): from twinkle.server.model import app from twinkle.server.model.backends import transformers_model @@ -130,33 +131,27 @@ def test_full_persisted_head_does_not_forward_num_labels_to_hf(monkeypatch, task monkeypatch.setattr(transformers_model, 'TwinkleCompatFullTransformersModel', lambda **kwargs: received.update(kwargs)) app._make_transformers_model({'model_id': 'persisted-classifier', 'train_mode': 'full', - 'num_labels': width, 'max_loras': 2}) + 'num_labels': 3, 'max_loras': 2}) assert received == {'model_id': 'persisted-classifier'} -@pytest.mark.parametrize('task,width', [('seq_cls', 3), ('reranker', 1)]) -def test_full_attached_head_reaches_full_model_constructor(monkeypatch, task, width): - from twinkle.server.model import app +def test_full_model_attaches_head_and_preserves_device_mesh(monkeypatch): + from twinkle.infra.dispatch import _get_device_mesh_param_name from twinkle.server.model.backends import transformers_model + mesh = SimpleNamespace(fsdp_size=2) received = {} - monkeypatch.setattr(transformers_model, 'TwinkleCompatFullTransformersModel', - lambda **kwargs: received.update(kwargs)) - app._make_transformers_model({'model_id': 'causal-backbone', 'train_mode': 'full', - 'task': task, 'num_labels': width}) - assert received == {'model_id': 'causal-backbone', 'task': task, 'num_labels': width} - - -def test_full_model_attaches_head_to_default_optimizer_group(monkeypatch): - from twinkle.server.model.backends import transformers_model - - monkeypatch.setattr(transformers_model.TransformersModel, '__init__', lambda self, **kwargs: None) + monkeypatch.setattr(transformers_model.TransformersModel, '__init__', + lambda self, **kwargs: received.update(kwargs)) attached = [] monkeypatch.setattr(transformers_model.TransformersModel, '_add_task_head', lambda self, *args: attached.append(args)) transformers_model.TwinkleCompatFullTransformersModel( - model_id='causal-backbone', task='seq_cls', num_labels=3) + model_id='causal-backbone', device_mesh=mesh, task='seq_cls', num_labels=3) assert attached == [('', 'seq_cls', None, 3)] + assert received['device_mesh'] is mesh + assert _get_device_mesh_param_name( + transformers_model.TwinkleCompatFullTransformersModel.__init__) == 'device_mesh' def test_server_forwards_full_determinism_to_workers(monkeypatch): @@ -174,57 +169,20 @@ def test_server_forwards_full_determinism_to_workers(monkeypatch): assert received['extra_torch_kwargs'] == {'full_determinism': True} -def test_full_custom_st_class_uses_compat_dispatch(monkeypatch): +def test_full_custom_model_class_uses_compat_dispatch(monkeypatch): from twinkle.server.model import app - marker = type('SentenceTransformerModel', (), {}) + marker = type('CustomModel', (), {}) received = {} monkeypatch.setattr(app, '_resolve_model_cls', lambda spec: marker) monkeypatch.setattr(app, '_make_model_class', lambda base: lambda **kwargs: received.update(base=base, kwargs=kwargs)) app._make_transformers_model({'model_id': 'st-model', 'train_mode': 'full', - 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'}) + 'model_cls': 'worker_only.encoder:Model'}) assert received['base'] is marker assert received['kwargs'] == {'model_id': 'st-model'} -@pytest.mark.parametrize('mode', ['lora', 'full']) -def test_native_st_model_cls_is_accepted_for_transformers_only(mode): - from twinkle.server.config.training import ModelArgs - - kwargs = {'model_id': 'st-model', 'backend': 'transformers', 'train_mode': mode, - 'device_group': {'name': 'model', 'ranks': 1}, 'device_mesh': {'dp_size': 1}, - 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'} - ModelArgs(**kwargs) - kwargs['backend'] = 'megatron' - with pytest.raises(ValidationError, match='model_cls'): - ModelArgs(**kwargs) - - -def test_full_native_st_model_cls_survives_resolved_config(): - from twinkle.server.config.training import TrainingModelConfig - - config = TrainingModelConfig( - name='st', model_id='st-model', backend='transformers', device_type='cuda', mode='full', - config={'num_devices': 1, 'device_mesh': {'dp_size': 1}, - 'model_cls': 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel'}) - assert config.config.model_cls.endswith('SentenceTransformerModel') - - -def test_full_remote_class_preserves_device_mesh_contract(monkeypatch): - from twinkle.infra.dispatch import _get_device_mesh_param_name - from twinkle.server.model.backends import transformers_model - - mesh = SimpleNamespace(fsdp_size=2) - received = {} - monkeypatch.setattr(transformers_model.TransformersModel, '__init__', - lambda self, **kwargs: received.update(kwargs)) - transformers_model.TwinkleCompatFullTransformersModel(model_id='causal-backbone', device_mesh=mesh) - assert received['device_mesh'] is mesh - assert _get_device_mesh_param_name( - transformers_model.TwinkleCompatFullTransformersModel.__init__) == 'device_mesh' - - def test_embedding_deployment_accepts_explicit_task(monkeypatch): from transformers import AutoConfig @@ -239,15 +197,7 @@ def test_embedding_deployment_accepts_explicit_task(monkeypatch): assert kwargs == {'model_id': 'causal-backbone'} -@pytest.mark.parametrize('mode', ['lora', 'full']) -def test_decision_family_supplies_worker_head_factory(mode): - loader = SimpleNamespace(task_type='decision', task_head_loader=lambda: ('family', 'Loader')) - args = TrainingArguments(task_type='decision', model_loader='family:build') - kwargs = {'model_id': 'decision-backbone', 'task_type': args.task_type, - 'model_loader': loader, 'train_mode': mode} - _validate_model_task(kwargs) - assert kwargs['task'] == 'decision' - assert kwargs['task_head_loader'] == ('family', 'Loader') +def test_decision_requires_a_family_head_factory(): with pytest.raises(ValueError, match='family model_loader'): _validate_model_task({'task_type': 'decision', 'model_loader': SimpleNamespace()}) with pytest.raises(ValidationError, match='model_loader'): @@ -262,23 +212,25 @@ def test_transformers_decision_uses_loader_architecture(monkeypatch, mode): received = {} for name in ('TwinkleCompatFullTransformersModel', 'TwinkleCompatTransformersModel'): monkeypatch.setattr(transformers_model, name, lambda **kw: received.update(kw)) - loader = SimpleNamespace(task_type='decision', model_arch=SimpleNamespace( + loader = SimpleNamespace(task_type='decision', task_head_loader=lambda: ('family', 'Loader'), + model_arch=SimpleNamespace( language_model=['model.language_model'], vision_tower=['model.visual'], aligner=['model.visual.merger'], lm_head='lm_head')) - app._make_transformers_model({'model_id': 'decision-backbone', 'train_mode': mode, - 'task': 'decision', 'task_head_loader': ('family', 'Loader'), - 'model_loader': loader}) + args = TrainingArguments(task_type='decision', model_loader='family:build') + kwargs = {'model_id': 'decision-backbone', 'train_mode': mode, + 'task_type': args.task_type, 'model_loader': loader} + app._validate_model_task(kwargs) + app._make_transformers_model(kwargs) assert received['model_arch']['vision_tower'] == ['model.visual'] if mode == 'full': assert 'task' not in received and 'task_head_loader' not in received else: assert received['task'] == 'decision' + assert received['task_head_loader'] == ('family', 'Loader') -@pytest.mark.parametrize('model_id', ['ms://sentence-transformers/LaBSE', - 'hf://sentence-transformers/LaBSE', 'configured-alias']) @pytest.mark.parametrize('model_cls', [None, 'swift.dev.model.sentence_transformer_model:SentenceTransformerModel']) -def test_native_st_guard_uses_resolved_directory(tmp_path, monkeypatch, model_id, model_cls): +def test_native_st_guard_uses_resolved_directory(tmp_path, monkeypatch, model_cls): from transformers import AutoConfig from twinkle.hub import HubOperation @@ -287,7 +239,7 @@ def test_native_st_guard_uses_resolved_directory(tmp_path, monkeypatch, model_id config_reads = [] monkeypatch.setattr(AutoConfig, 'from_pretrained', lambda *a, **kw: config_reads.append(a) or SimpleNamespace(architectures=['BertModel'])) - kwargs = {'model_id': model_id, 'task_type': 'embedding'} + kwargs = {'model_id': 'ms://sentence-transformers/LaBSE', 'task_type': 'embedding'} if model_cls is not None: kwargs['model_cls'] = model_cls _validate_model_task(kwargs) diff --git a/tests/transformers/test_megatron_weight_only_resume.py b/tests/transformers/test_megatron_weight_only_resume.py index 8687bcf9f..16cc9299c 100644 --- a/tests/transformers/test_megatron_weight_only_resume.py +++ b/tests/transformers/test_megatron_weight_only_resume.py @@ -60,8 +60,7 @@ def load_weights(*args, **kwargs): @pytest.mark.parametrize('api', ['legacy', 'current']) -@pytest.mark.parametrize('sharding', ['dp_reshardable', 'fsdp_dtensor']) -def test_mcore_optimizer_save_uses_versioned_torch_dist_strategy(tmp_path, monkeypatch, api, sharding): +def test_mcore_optimizer_save_uses_versioned_torch_dist_strategy(tmp_path, monkeypatch, api): pytest.importorskip('megatron.core', reason='requires the optional Megatron backend') from megatron.core import dist_checkpointing from megatron.core import parallel_state as mpu @@ -84,7 +83,7 @@ def current_save(state_dict, checkpoint_dir, sharded_strategy=None, common_strat monkeypatch.setattr(dist_checkpointing, 'save', legacy_save if api == 'legacy' else current_save) monkeypatch.setattr(mpu, 'get_data_parallel_world_size', lambda **kwargs: 1) - metadata = {'sharded': True, 'distrib_optim_sharding_type': sharding} + metadata = {'sharded': True, 'distrib_optim_sharding_type': 'dp_reshardable'} wrapper = SimpleNamespace( model=object(), strategy=SimpleNamespace(get_sharded_sd_metadata=lambda: metadata), @@ -99,9 +98,8 @@ def current_save(state_dict, checkpoint_dir, sharded_strategy=None, common_strat assert captured['strategy'].version == 1 assert captured['strategy'].thread_count == 3 assert captured['async_save'] is False - assert metadata == {'sharded': True, 'distrib_optim_sharding_type': sharding} if api == 'current': - assert captured['metadata'] is metadata + assert captured['metadata'] == {'sharded': True, 'distrib_optim_sharding_type': 'dp_reshardable'} assert captured['async_strategy'] == 'mcore' else: assert 'metadata' not in captured @@ -158,34 +156,3 @@ def test_mcore_persisted_classifier_config(monkeypatch): MCoreBridgeBackend().build_model_config(hf_config, {}, strategy, task_type='causal_lm') with pytest.raises(ValueError, match='num_labels'): MCoreBridgeBackend().build_model_config(hf_config, {}, strategy, num_labels=2) - - -def test_mcore_persisted_classifier_uses_independent_output_head(): - pytest.importorskip('mcore_bridge', reason='requires the optional Megatron bridge') - from mcore_bridge.bridge.gpt_bridge import GPTBridge - - bridge = object.__new__(GPTBridge) - bridge.config = SimpleNamespace( - task_type='seq_cls', untie_embeddings_and_output_weights=True, padded_vocab_size=13) - bridge.is_multimodal = False - bridge.tp_size = bridge.etp_size = 1 - bridge.tp_rank = bridge.etp_rank = 0 - bridge._peft_format = False - mg_model = SimpleNamespace( - output_layer=SimpleNamespace(weight=torch.nn.Parameter(torch.zeros(1, 8))), - decoder=SimpleNamespace(final_layernorm=SimpleNamespace(weight=torch.nn.Parameter(torch.zeros(8)))), - ) - score = torch.arange(8, dtype=torch.float32).reshape(1, 8) - embedding = score.repeat(13, 1) + 100 - norm = torch.ones(8) - def lazy(value): - return SimpleNamespace(load=lambda: value) - - bridge._convert_post_process(mg_model, { - 'score.weight': lazy(score), - 'model.embed_tokens.weight': lazy(embedding), - 'model.norm.weight': lazy(norm), - }, '', True) - - torch.testing.assert_close(mg_model.output_layer.weight, score) - torch.testing.assert_close(mg_model.decoder.final_layernorm.weight, norm) diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index fb5a7fb17..f59523094 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -114,16 +114,21 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t torch.testing.assert_close(replacement['b'], torch.ones(2)) -def test_builtin_score_head_remains_in_root_fsdp_group(): - model = torch.nn.Module() - model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) - model.score = torch.nn.Linear(4, 2).to(torch.bfloat16) +@pytest.fixture +def head_sharding(): strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) - with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', return_value=MagicMock()), \ patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: - strategy.wrap_model(model) + yield strategy, fully_shard + + +def test_builtin_score_head_remains_in_root_fsdp_group(head_sharding): + model = torch.nn.Module() + model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) + model.score = torch.nn.Linear(4, 2).to(torch.bfloat16) + strategy, fully_shard = head_sharding + strategy.wrap_model(model) fully_shard.assert_called_once() assert fully_shard.call_args.args[0] is model @@ -132,7 +137,7 @@ def test_builtin_score_head_remains_in_root_fsdp_group(): @pytest.mark.parametrize('preallocated', [False, True]) -def test_attached_standard_head_is_sharded_independently(preallocated): +def test_attached_standard_head_is_sharded_independently(preallocated, head_sharding): model = torch.nn.Module() model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) if preallocated: @@ -143,12 +148,8 @@ def test_attached_standard_head_is_sharded_independently(preallocated): slots.bind('a', model) else: model.task_heads = {'': torch.nn.Linear(4, 1).to(torch.bfloat16)} - strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) - - with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', - return_value=MagicMock()), \ - patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: - strategy.wrap_model(model) + strategy, fully_shard = head_sharding + strategy.wrap_model(model) assert fully_shard.call_count == (3 if preallocated else 2) if preallocated: @@ -233,20 +234,21 @@ def broadcast_empty_states(payload, src): assert not optimizer.state[model[1].weight] -def test_fp32_task_head_is_sharded_as_separate_precision_group(): - strategy = NativeFSDPStrategy(device_mesh=MagicMock(), mixed_precision='bf16', enable_ep=False) +def test_fp32_task_head_is_sharded_as_separate_precision_group(head_sharding): model = torch.nn.Module() model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) model.task_head = torch.nn.Linear(4, 1) model.task_head.keep_fp32 = True model.scoring_head = model.task_head - with patch('twinkle.model.transformers.strategy.native_fsdp._build_fsdp_mesh', - return_value=MagicMock()), \ - patch('twinkle.model.transformers.strategy.native_fsdp.fully_shard') as fully_shard: - strategy.wrap_model(model) - - assert fully_shard.call_count == 2 - head_call, model_call = fully_shard.call_args_list + model.standard_head = torch.nn.Linear(4, 1).to(torch.bfloat16) + model.task_heads = {'': model.standard_head} + strategy, fully_shard = head_sharding + strategy.wrap_model(model) + + assert fully_shard.call_count == 3 + standard_call, head_call, model_call = fully_shard.call_args_list + assert standard_call.args[0] is model.standard_head + assert standard_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 assert head_call.args[0] is model.task_head assert head_call.kwargs['mp_policy'].param_dtype is None assert head_call.kwargs['mp_policy'].reduce_dtype is None From 83a9ee8a74eb3f45fce5d3f51618c4321e42e08c Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 17:23:49 +0800 Subject: [PATCH 15/18] fix(fsdp): restore sharded task heads without extra BF16 groups --- src/twinkle/model/megatron/megatron.py | 9 +- src/twinkle/model/tasks.py | 6 ++ .../transformers/strategy/native_fsdp.py | 37 ++++---- .../model/transformers/transformers.py | 4 +- .../model/multi_lora_transformers.py | 2 +- .../test_megatron_generative_reranker.py | 2 +- .../transformers/test_native_fsdp_strategy.py | 86 ++++++++----------- 7 files changed, 64 insertions(+), 82 deletions(-) diff --git a/src/twinkle/model/megatron/megatron.py b/src/twinkle/model/megatron/megatron.py index 09aafad98..2b50ecfd2 100644 --- a/src/twinkle/model/megatron/megatron.py +++ b/src/twinkle/model/megatron/megatron.py @@ -36,7 +36,7 @@ from twinkle.model.micro_batch import (MicroBatchConfig, agree_micro_batch_count, plan_micro_batches_contiguous, sequence_length) from twinkle.model.optimizer_group import BaseOptimizerGroup, TrainStatus -from twinkle.model.tasks import get_task_spec_obj +from twinkle.model.tasks import _generative_reranker_score, get_task_spec_obj from twinkle.patch import Patch, apply_context, apply_patch from twinkle.perf import timed from twinkle.processor import InputProcessor @@ -93,13 +93,6 @@ def _gather_vocab_parallel(tensor): return gather_from_tensor_model_parallel_region(tensor) -def _generative_reranker_score(logits, template): - from twinkle.model.tasks import _resolve_generative_reranker_token_ids - - positive, negative = _resolve_generative_reranker_token_ids(template) - return logits[..., positive:positive + 1] - logits[..., negative:negative + 1] - - def _last_valid_positions(processor, batch, output_tensor): """CP-reassembled output plus each record's last-valid-token position (shared machinery for the seq_cls pool and the decision readout). diff --git a/src/twinkle/model/tasks.py b/src/twinkle/model/tasks.py index 4a9fa1408..f60d58e65 100644 --- a/src/twinkle/model/tasks.py +++ b/src/twinkle/model/tasks.py @@ -320,6 +320,12 @@ class Decision(TaskSpec): megatron_context = _no_patch +def _generative_reranker_score(logits, template): + """Return the positive-minus-negative token logit for a generative reranker.""" + positive, negative = _resolve_generative_reranker_token_ids(template) + return logits[..., positive:positive + 1] - logits[..., negative:negative + 1] + + def _resolve_generative_reranker_token_ids(template): """Resolve the positive/negative token ids for a generative reranker from the template tokenizer. diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 82513abdb..35ad95256 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -6,6 +6,7 @@ from torch import nn from torch.distributed.device_mesh import DeviceMesh as TorchDeviceMesh from torch.distributed.fsdp import FSDPModule, fully_shard +from torch.distributed.tensor import DTensor from typing import TYPE_CHECKING, Any, Dict, List, Literal, Mapping, Optional, Set from twinkle.model.state_dict_utils import collect_local_state, is_lora_state_key, is_saveable_adapter_key @@ -24,22 +25,13 @@ PEFT_BASE_LAYER_SEGMENT = 'base_layer' -def _shard_task_heads(model, *, mesh, reshard_after_forward, mp_policy): - # The head wrapper owns unsharding for the readout backward instead of the root wrapper. - heads = list((getattr(model, 'task_heads', None) or {}).values()) - # Future tenants can bind any preallocated slot after the root is already sharded. - heads.extend(module for name, module in model.named_children() if name.startswith('task_head_slot_')) - heads = [head for head in heads if not getattr(head, 'keep_fp32', False)] - heads.extend(module for module in model.modules() if getattr(module, 'keep_fp32', False) - and not getattr(module, '_twinkle_independent_fsdp', False)) - seen = set() - for head in heads: - if head is None or id(head) in seen or next(head.parameters(), None) is None: +def _shard_fp32_task_heads(model, *, mesh, reshard_after_forward): + # FSDP parameter groups require one storage dtype; FP32 heads cannot share the BF16 backbone group. + for head in model.modules(): + if (not getattr(head, 'keep_fp32', False) or isinstance(head, FSDPModule) + or next(head.parameters(), None) is None): continue - seen.add(id(head)) - policy = _build_mp_policy('no') if getattr(head, 'keep_fp32', False) else mp_policy - fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=policy) - head._twinkle_independent_fsdp = True + fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=_build_mp_policy('no')) class NativeFSDPStrategy(TransformerStrategyBase): @@ -211,8 +203,7 @@ def wrap_model(self, model, optimizer=None, *additional_optimizers): world_size = self.device_mesh.world_size ep_fsdp_mesh_1d = self.ep_fsdp_device_mesh['ep_fsdp'] if ep_enabled else None - _shard_task_heads( - model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward, mp_policy=mp_policy) + _shard_fp32_task_heads(model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) for layer_mod, experts_mod in layer_pairs: layer_mod._fsdp_modules = [] @@ -292,8 +283,8 @@ def _prepare_optimizer_state_dict_options(self, *, for_load: bool): broadcast_from_rank0=for_load, ) - def get_independent_task_head_state(self, head): - if not getattr(head, '_twinkle_independent_fsdp', False): + def get_task_head_state(self, head): + if not any(isinstance(parameter, DTensor) for parameter in head.parameters()): return None from torch.distributed.checkpoint.state_dict import get_model_state_dict @@ -302,8 +293,8 @@ def get_independent_task_head_state(self, head): state.setdefault(name, buffer) return {f'task_head.{name}': tensor.detach().clone().cpu() for name, tensor in state.items()} - def restore_independent_task_head_state(self, head, state) -> bool: - if not getattr(head, '_twinkle_independent_fsdp', False): + def restore_task_head_state(self, head, state) -> bool: + if not any(isinstance(parameter, DTensor) for parameter in head.parameters()): return False from torch.distributed.checkpoint.state_dict import set_model_state_dict @@ -321,6 +312,10 @@ def restore_independent_task_head_state(self, head, state) -> bool: persistent = set(head.state_dict()) model_state = {key[len('task_head.'):]: value for key, value in state.items() if key[len('task_head.'):] in persistent} + # Parameter-only checkpoints keep the head's current calibration; strict loading still needs its keys. + for name, buffer in head.named_buffers(): + if name in persistent: + model_state.setdefault(name, buffer) set_model_state_dict( head, model_state, options=self._prepare_optimizer_state_dict_options(for_load=True)) with torch.no_grad(): diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index dabaa5736..132ff1c12 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -3049,7 +3049,7 @@ def _task_head_is_master(self) -> bool: def _task_head_state(self, adapter_name): head = self._attached_head(adapter_name) - getter = getattr(self.strategy, 'get_independent_task_head_state', None) + getter = getattr(self.strategy, 'get_task_head_state', None) if head is not None and getter is not None: state = getter(head) if state is not None: @@ -3058,7 +3058,7 @@ def _task_head_state(self, adapter_name): def _restore_task_head_state(self, adapter_name, state) -> None: head = self._attached_head(adapter_name) - restorer = getattr(self.strategy, 'restore_independent_task_head_state', None) + restorer = getattr(self.strategy, 'restore_task_head_state', None) if head is not None and restorer is not None and restorer(head, state): return super()._restore_task_head_state(adapter_name, state) diff --git a/src/twinkle_client/model/multi_lora_transformers.py b/src/twinkle_client/model/multi_lora_transformers.py index 594fc7fa9..487af1b49 100644 --- a/src/twinkle_client/model/multi_lora_transformers.py +++ b/src/twinkle_client/model/multi_lora_transformers.py @@ -422,7 +422,7 @@ def calculate_metric(self, is_training: bool = True, **kwargs) -> model_types.Ca # ------------------------------------------------------------------ # def save(self, name: str, **kwargs) -> model_types.SaveResponse: - """Save a checkpoint; save_optimizer=True requires a completed update and cleared gradients.""" + """Save model checkpoint.""" return self._submit('save', model_types.SaveRequest, model_types.SaveResponse, name=name, **kwargs) def load(self, name: str, **kwargs) -> None: diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py index eb11cf3af..27aaff0d6 100644 --- a/tests/model/test_megatron_generative_reranker.py +++ b/tests/model/test_megatron_generative_reranker.py @@ -4,7 +4,7 @@ import pytest import torch -from twinkle.model.megatron.megatron import _generative_reranker_score +from twinkle.model.tasks import _generative_reranker_score def test_generative_reranker_scores_yes_minus_no_and_backpropagates(): diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index f59523094..27a954efc 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -133,31 +133,6 @@ def test_builtin_score_head_remains_in_root_fsdp_group(head_sharding): fully_shard.assert_called_once() assert fully_shard.call_args.args[0] is model assert fully_shard.call_args.kwargs['mp_policy'].param_dtype is torch.bfloat16 - assert not getattr(model.score, '_twinkle_independent_fsdp', False) - - -@pytest.mark.parametrize('preallocated', [False, True]) -def test_attached_standard_head_is_sharded_independently(preallocated, head_sharding): - model = torch.nn.Module() - model.backbone = torch.nn.Linear(4, 4).to(torch.bfloat16) - if preallocated: - from twinkle.model.multi_lora import MultiLoraTaskHeadSlots - - slots = MultiLoraTaskHeadSlots(2) - slots.build(model, lambda: torch.nn.Linear(4, 1).to(torch.bfloat16)) - slots.bind('a', model) - else: - model.task_heads = {'': torch.nn.Linear(4, 1).to(torch.bfloat16)} - strategy, fully_shard = head_sharding - strategy.wrap_model(model) - - assert fully_shard.call_count == (3 if preallocated else 2) - if preallocated: - slots.bind('b', model) - assert model.task_heads['b']._twinkle_independent_fsdp - assert fully_shard.call_args_list[1].args[0] is model.task_heads['b'] - assert fully_shard.call_args_list[0].args[0] is model.task_heads['a' if preallocated else ''] - assert all(head._twinkle_independent_fsdp for head in model.task_heads.values()) def test_nonmaster_save_ignores_empty_gathered_optimizer_state(tmp_path): @@ -245,49 +220,62 @@ def test_fp32_task_head_is_sharded_as_separate_precision_group(head_sharding): strategy, fully_shard = head_sharding strategy.wrap_model(model) - assert fully_shard.call_count == 3 - standard_call, head_call, model_call = fully_shard.call_args_list - assert standard_call.args[0] is model.standard_head - assert standard_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 + assert fully_shard.call_count == 2 + head_call, model_call = fully_shard.call_args_list assert head_call.args[0] is model.task_head assert head_call.kwargs['mp_policy'].param_dtype is None assert head_call.kwargs['mp_policy'].reduce_dtype is None assert model_call.args[0] is model assert model_call.kwargs['mp_policy'].param_dtype is torch.bfloat16 assert model.task_head.bias.dtype is torch.float32 - assert model.task_head._twinkle_independent_fsdp + assert model.standard_head.weight.dtype is torch.bfloat16 -def test_independent_task_head_checkpoint_includes_nonpersistent_buffers(): - head = torch.nn.Linear(2, 1) +@pytest.fixture +def sharded_head(tmp_path): + from torch.distributed.device_mesh import init_device_mesh + from torch.distributed.tensor import Shard, distribute_tensor + + torch.distributed.init_process_group('gloo', init_method=f'file://{tmp_path / "head-pg"}', rank=0, world_size=1) + try: + mesh = init_device_mesh('cpu', (1,)) + head = torch.nn.Linear(2, 1) + for name, parameter in list(head.named_parameters()): + setattr(head, name, torch.nn.Parameter(distribute_tensor(parameter.detach(), mesh, [Shard(0)]))) + yield head + finally: + torch.distributed.destroy_process_group() + + +def test_sharded_task_head_checkpoint_includes_nonpersistent_buffers(sharded_head): + head = sharded_head head.register_buffer('calibration', torch.tensor([1.0]), persistent=False) - head._twinkle_independent_fsdp = True strategy = NativeFSDPStrategy(enable_ep=False) - with patch('torch.distributed.checkpoint.state_dict.get_model_state_dict', - return_value={'weight': head.weight.detach(), 'bias': head.bias.detach()}): - state = strategy.get_independent_task_head_state(head) + state = strategy.get_task_head_state(head) assert set(state) == {'task_head.weight', 'task_head.bias', 'task_head.calibration'} restored = {key: torch.full_like(value, 3.0) for key, value in state.items()} - with patch('torch.distributed.checkpoint.state_dict.set_model_state_dict') as set_state: - assert strategy.restore_independent_task_head_state(head, restored) - set_state.assert_called_once() - assert set_state.call_args.args[1].keys() == {'weight', 'bias'} + assert strategy.restore_task_head_state(head, restored) + for parameter in head.parameters(): + torch.testing.assert_close(parameter.full_tensor(), torch.full_like(parameter.full_tensor(), 3.0)) torch.testing.assert_close(head.calibration, torch.tensor([3.0])) @pytest.mark.parametrize('persistent', [False, True]) -def test_independent_task_head_loads_legacy_parameters_without_changing_calibration(persistent): - head = torch.nn.Linear(2, 1) +def test_sharded_task_head_loads_legacy_parameters_without_changing_calibration(persistent, sharded_head): + head = sharded_head head.register_buffer('calibration', torch.tensor([7.0]), persistent=persistent) - head._twinkle_independent_fsdp = True strategy = NativeFSDPStrategy(enable_ep=False) - legacy = {f'task_head.{name}': torch.full_like(param, 3.0) + legacy = {f'task_head.{name}': torch.full_like(param.full_tensor(), 3.0) for name, param in head.named_parameters()} - options = strategy._prepare_optimizer_state_dict_options(for_load=True) - options.broadcast_from_rank0 = False - with patch.object(strategy, '_prepare_optimizer_state_dict_options', return_value=options): - assert strategy.restore_independent_task_head_state(head, legacy) + assert strategy.restore_task_head_state(head, legacy) for param in head.parameters(): - torch.testing.assert_close(param, torch.full_like(param, 3.0)) + torch.testing.assert_close(param.full_tensor(), torch.full_like(param.full_tensor(), 3.0)) torch.testing.assert_close(head.calibration, torch.tensor([7.0])) + + +def test_unsharded_head_keeps_the_plain_tensor_checkpoint_path(): + head = torch.nn.Linear(2, 1) + strategy = NativeFSDPStrategy(enable_ep=False) + assert strategy.get_task_head_state(head) is None + assert strategy.restore_task_head_state(head, {}) is False From 4b25dc4892a5bf053b17934ea59a6b053c7fd5a2 Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 18:00:52 +0800 Subject: [PATCH 16/18] refactor: remove experiment-only deterministic kernel overrides --- src/twinkle/model/megatron/megatron.py | 12 ---- .../model/megatron/multi_lora_megatron.py | 1 - .../model/transformers/transformers.py | 11 ---- src/twinkle/server/config/training.py | 11 ---- src/twinkle/server/model/app.py | 1 - src/twinkle/server/runtime.py | 2 - src/twinkle/utils/platforms/gpu.py | 1 - .../test_megatron_generative_reranker.py | 45 --------------- tests/server/config/test_training_config.py | 10 +--- tests/server/model/test_task_head.py | 15 ----- .../test_qwen35_fla_bwd_precision.py | 55 ------------------- 11 files changed, 1 insertion(+), 163 deletions(-) diff --git a/src/twinkle/model/megatron/megatron.py b/src/twinkle/model/megatron/megatron.py index 2b50ecfd2..e18889f41 100644 --- a/src/twinkle/model/megatron/megatron.py +++ b/src/twinkle/model/megatron/megatron.py @@ -448,20 +448,8 @@ def _construct_default_optimizer_group(self): _device_mesh=self.device_mesh, ) - def _apply_deterministic_model_kernels(self): - if not torch.are_deterministic_algorithms_enabled(): - return - from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule - - for model in self.model: - for module in model.modules(): - if type(module).__name__ == 'Qwen3NextGatedDeltaNet': - module.chunk_gated_delta_rule = torch_chunk_gated_delta_rule - module.causal_conv1d_fn = None - def _lazy_wrap_model(self): if not self._model_wrapped: - self._apply_deterministic_model_kernels() self.model = self.strategy.wrap_model(self.model) self._model_wrapped = True diff --git a/src/twinkle/model/megatron/multi_lora_megatron.py b/src/twinkle/model/megatron/multi_lora_megatron.py index bedcadfff..9c7989af6 100644 --- a/src/twinkle/model/megatron/multi_lora_megatron.py +++ b/src/twinkle/model/megatron/multi_lora_megatron.py @@ -170,7 +170,6 @@ def __init__( if self.task_head_slots is not None: self._validate_task_head_config() self._build_task_head_slots() - self._apply_deterministic_model_kernels() self.model = self.strategy.wrap_model(self.model) self.strategy.finish_param_config(self.model, None) self.multi_adapter.save_initial_weights() diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index 132ff1c12..57745d305 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -668,19 +668,8 @@ def _fill_deepspeed_auto(self, kwargs: Dict[str, Any]) -> None: break fill(int(micro_batch_size), int(ga or 1)) - def _apply_deterministic_model_kernels(self): - if not torch.are_deterministic_algorithms_enabled(): - return - from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule - - for module in self.model.modules(): - if type(module).__name__ == 'Qwen3_5GatedDeltaNet': - module.chunk_gated_delta_rule = torch_chunk_gated_delta_rule - module.causal_conv1d_fn = None - def _lazy_wrap_model(self): if not self._model_wrapped: - self._apply_deterministic_model_kernels() optimizer_groups = [og for og in self.optimizer_group.values() if og.optimizer is not None] self.strategy.capture_pre_ep_state_if_needed(self.model, enable_ep=self._enable_expert_parallel) self._maybe_apply_expert_parallel() diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index f05afdf0f..7821d1bb7 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -66,23 +66,12 @@ class TrainingArguments(BaseModel): model_loader: str | None = None gradient_checkpointing: bool | None = Field(default=None, strict=True) mixed_precision: Literal["no", "fp8", "fp16", "bf16"] | None = None - extra_torch_kwargs: dict[str, Any] | None = None # The readout family names the head task this deployment serves (see # ``twinkle/model/tasks.py``); ``causal_lm`` carries no head. task_type: Literal["causal_lm", "embedding", "seq_cls", "reranker", "value", "decision"] | None = None num_labels: int | None = Field(default=None, gt=0, strict=True) language_model_only: bool | None = Field(default=None, strict=True) - @field_validator('extra_torch_kwargs') - @classmethod - def _validate_torch_kwargs(cls, value): - if value is not None: - allowed = {'seed', 'full_determinism', 'tf32'} - unknown = set(value) - allowed - if unknown: - raise ValueError(f'unsupported extra_torch_kwargs keys {sorted(unknown)}; expected {sorted(allowed)}') - return value - @model_validator(mode="after") def _validate_head(self): if self.task_type == "seq_cls" and self.num_labels is None: diff --git a/src/twinkle/server/model/app.py b/src/twinkle/server/model/app.py index 5d05f23fa..1882e385e 100644 --- a/src/twinkle/server/model/app.py +++ b/src/twinkle/server/model/app.py @@ -266,7 +266,6 @@ async def __init__(self, nproc_per_node=nproc_per_node, device_group=self.device_group, device_mesh_dict=device_mesh, - extra_torch_kwargs=kwargs.pop('extra_torch_kwargs', None), ) self.replica_id = self.training_context.instance_id self.max_loras = kwargs.get('max_loras', 5) diff --git a/src/twinkle/server/runtime.py b/src/twinkle/server/runtime.py index 4d9df81e3..1a6d8e131 100644 --- a/src/twinkle/server/runtime.py +++ b/src/twinkle/server/runtime.py @@ -19,7 +19,6 @@ def init_twinkle_runtime( device_mesh_dict: dict[str, Any], *, ncpu_proc_per_node: int | None = None, - extra_torch_kwargs: dict[str, Any] | None = None, ) -> Any | None: """Initialize the Twinkle distributed runtime and build a DeviceMesh. @@ -44,7 +43,6 @@ def init_twinkle_runtime( 'nproc_per_node': nproc_per_node, 'groups': [device_group], 'lazy_collect': False, - 'extra_torch_kwargs': extra_torch_kwargs, } if ncpu_proc_per_node is not None: init_kwargs['ncpu_proc_per_node'] = ncpu_proc_per_node diff --git a/src/twinkle/utils/platforms/gpu.py b/src/twinkle/utils/platforms/gpu.py index f6f26b1ba..a038a635b 100644 --- a/src/twinkle/utils/platforms/gpu.py +++ b/src/twinkle/utils/platforms/gpu.py @@ -56,6 +56,5 @@ def apply_determinism() -> None: os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':16:8' os.environ['FLASH_ATTENTION_DETERMINISTIC'] = '1' import torch - torch.backends.cuda.enable_mem_efficient_sdp(False) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False diff --git a/tests/model/test_megatron_generative_reranker.py b/tests/model/test_megatron_generative_reranker.py index 27aaff0d6..a13b85f97 100644 --- a/tests/model/test_megatron_generative_reranker.py +++ b/tests/model/test_megatron_generative_reranker.py @@ -1,7 +1,5 @@ -import sys from types import SimpleNamespace -import pytest import torch from twinkle.model.tasks import _generative_reranker_score @@ -20,46 +18,3 @@ def test_generative_reranker_scores_yes_minus_no_and_backpropagates(): assert torch.count_nonzero(logits.grad[..., 3]) == 10 assert torch.count_nonzero(logits.grad[..., 7]) == 10 assert torch.count_nonzero(logits.grad[..., [0, 1, 2, 4, 5, 6, 8, 9, 10]]) == 0 - - -@pytest.mark.parametrize('deterministic', [False, True]) -def test_multilora_selects_deterministic_kernels_before_eager_wrap(monkeypatch, deterministic): - from transformers.models.qwen3_next.modeling_qwen3_next import torch_chunk_gated_delta_rule - import twinkle.infra as infra - import twinkle.model.megatron.multi_lora_megatron as module - - monkeypatch.setattr(infra, '_mode', 'local') - monkeypatch.setattr(infra, '_device_group', None) - gdn = type('Qwen3NextGatedDeltaNet', (torch.nn.Module,), {})() - original = lambda *args: None - gdn.chunk_gated_delta_rule = original - gdn.causal_conv1d_fn = original - - class Wrapped(Exception): - pass - - class Strategy: - uses_fsdp = staticmethod(lambda _: False) - - def __init__(self, *args, **kwargs): - pass - - def create_megatron_model(self, load_weights): - return [gdn] - - def wrap_model(self, models): - assert gdn.chunk_gated_delta_rule is (torch_chunk_gated_delta_rule if deterministic else original) - assert gdn.causal_conv1d_fn is (None if deterministic else original) - raise Wrapped() - - monkeypatch.setattr(module, 'requires', lambda *args: None) - monkeypatch.setattr(module, 'ensure_mindspeed_adaptor_patched', lambda: None) - monkeypatch.setattr(module.HubOperation, 'download_model', lambda *args, **kwargs: 'local-model') - monkeypatch.setattr(module.torch_util, 'set_device', lambda: None) - monkeypatch.setattr(module.MultiLoraMegatronModel, '_try_init_process_group', lambda *args, **kwargs: None) - monkeypatch.setattr(module, 'MegatronStrategy', Strategy) - monkeypatch.setattr(module, 'MultiLora', lambda **kwargs: SimpleNamespace(patch=lambda models, **kw: models)) - monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: deterministic) - monkeypatch.setitem(sys.modules, 'twinkle.patch.megatron_peft', SimpleNamespace(MegatronPeft=lambda: lambda: None)) - with pytest.raises(Wrapped): - module.MultiLoraMegatronModel('local-model', config=SimpleNamespace()) diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index 37bd88f85..b426a5855 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -449,18 +449,10 @@ def test_transformers_still_accepts_single_device(): def test_native_backends_preserve_loader_and_precision_configuration(backend, mode): app = model_application() app['args'].update(backend=backend, train_mode=mode, mixed_precision='no', - model_loader='worker_only.loader:load', extra_torch_kwargs={'full_determinism': True}) + model_loader='worker_only.loader:load') config = ServerConfig(applications=[app]) restored = ServerConfig.model_validate(config.to_yaml_dict()) model = restored.training_config.models[0] assert model.backend == backend and model.mode == mode assert model.config.model_loader == 'worker_only.loader:load' assert model.config.mixed_precision == 'no' - assert model.config.extra_torch_kwargs == {'full_determinism': True} - - -def test_extra_torch_kwargs_rejects_unknown_keys(): - app = model_application() - app['args']['extra_torch_kwargs'] = {'full_determinism': True, 'deterministic': True} - with pytest.raises(ValidationError, match='unsupported extra_torch_kwargs'): - ServerConfig(applications=[app]) diff --git a/tests/server/model/test_task_head.py b/tests/server/model/test_task_head.py index cfc1469d8..38b50382d 100644 --- a/tests/server/model/test_task_head.py +++ b/tests/server/model/test_task_head.py @@ -154,21 +154,6 @@ def test_full_model_attaches_head_and_preserves_device_mesh(monkeypatch): transformers_model.TwinkleCompatFullTransformersModel.__init__) == 'device_mesh' -def test_server_forwards_full_determinism_to_workers(monkeypatch): - import twinkle - from twinkle.server.runtime import init_twinkle_runtime - - received = {} - monkeypatch.setattr(twinkle, 'initialize', lambda **kwargs: received.update(kwargs)) - mesh = SimpleNamespace(world_size=2) - monkeypatch.setattr(twinkle.DeviceMesh, 'from_sizes', lambda **kwargs: mesh) - result = init_twinkle_runtime( - is_mock=False, nproc_per_node=2, device_group=SimpleNamespace(name='model'), - device_mesh_dict={'fsdp_size': 2}, extra_torch_kwargs={'full_determinism': True}) - assert result is mesh - assert received['extra_torch_kwargs'] == {'full_determinism': True} - - def test_full_custom_model_class_uses_compat_dispatch(monkeypatch): from twinkle.server.model import app diff --git a/tests/transformers/test_qwen35_fla_bwd_precision.py b/tests/transformers/test_qwen35_fla_bwd_precision.py index 4591e32e2..d0fbd5775 100644 --- a/tests/transformers/test_qwen35_fla_bwd_precision.py +++ b/tests/transformers/test_qwen35_fla_bwd_precision.py @@ -124,19 +124,6 @@ def _force_fla_off(model: torch.nn.Module) -> None: delattr(la, '_twinkle_npu_patched') -def test_full_determinism_disables_nondeterministic_sdp(monkeypatch): - from twinkle.utils.platforms.gpu import GPU - - calls = [] - monkeypatch.setattr(torch.backends.cuda, 'enable_mem_efficient_sdp', calls.append) - monkeypatch.setattr(torch.backends.cudnn, 'deterministic', False) - monkeypatch.setattr(torch.backends.cudnn, 'benchmark', True) - GPU.apply_determinism() - assert calls == [False] - assert torch.backends.cudnn.deterministic - assert not torch.backends.cudnn.benchmark - - def _force_fla_on(model: torch.nn.Module) -> int: import importlib import twinkle.kernel.ops.fla.npu as fla_mod @@ -311,45 +298,3 @@ def test_moe_linear_attention_fla_on_vs_off(self, seed): _compare_models('dense', ['linear_attention', 'linear_attention'], 2024, 256) _compare_models('moe', ['linear_attention', 'linear_attention'], 1234, 128) print('\nAll comparisons finished.') - - -@pytest.mark.parametrize('multi', [False, True]) -@pytest.mark.parametrize('deterministic', [False, True]) -def test_gdn_kernel_policy_applies_once_before_both_model_wrappers(tmp_path, monkeypatch, multi, deterministic): - from tokenizers import Tokenizer - from tokenizers.models import WordPiece - from transformers import AutoModelForCausalLM, PreTrainedTokenizerFast - from transformers.models.qwen3_5.modeling_qwen3_5 import torch_chunk_gated_delta_rule - from twinkle import Platform - import twinkle.infra as infra - from twinkle.model.transformers.transformers import TransformersModel - from twinkle.model.transformers.multi_lora_transformers import MultiLoraTransformersModel - - monkeypatch.setenv('ACCELERATE_USE_CPU', 'true') - monkeypatch.setattr(Platform, 'get_local_device', lambda *args, **kwargs: 'cpu') - monkeypatch.setattr(infra, '_mode', 'local') - monkeypatch.setattr(infra, '_device_group', None) - monkeypatch.setattr(infra, '_device_mesh', None) - config = Qwen3_5TextConfig(**_common_config_kwargs(['linear_attention', 'linear_attention'])) - Qwen3_5ForCausalLM(config).save_pretrained(tmp_path) - tokenizer = PreTrainedTokenizerFast( - tokenizer_object=Tokenizer(WordPiece({'[PAD]': 0, '[UNK]': 1}, unk_token='[UNK]')), - pad_token='[PAD]', unk_token='[UNK]') - tokenizer.save_pretrained(tmp_path) - cls = MultiLoraTransformersModel if multi else TransformersModel - wrapper = cls(model_id=str(tmp_path), model_cls=AutoModelForCausalLM, config=config, mixed_precision='no', - gradient_checkpointing=False, - **({'max_loras': 2, 'max_r': 4, 'target_modules': ['out_proj']} if multi else {})) - gdn = next(module for module in wrapper.model.modules() - if type(module).__name__ == 'Qwen3_5GatedDeltaNet') - fast_kernel = lambda *args, **kwargs: None - gdn.chunk_gated_delta_rule = fast_kernel - gdn.causal_conv1d_fn = fast_kernel - monkeypatch.setattr(torch, 'are_deterministic_algorithms_enabled', lambda: deterministic) - wrapper._lazy_wrap_model() - assert gdn.chunk_gated_delta_rule is (torch_chunk_gated_delta_rule if deterministic else fast_kernel) - assert gdn.causal_conv1d_fn is (None if deterministic else fast_kernel) - # Re-entering an already wrapped model must not repeat the initialization policy. - gdn.chunk_gated_delta_rule = fast_kernel - wrapper._lazy_wrap_model() - assert gdn.chunk_gated_delta_rule is fast_kernel From 1cccc5786d500e39c78d6c47d9192dc24d0c435f Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 19:29:18 +0800 Subject: [PATCH 17/18] refactor: share sharded tensor copies for task checkpoints --- src/twinkle/model/base.py | 12 +-- src/twinkle/model/multi_lora.py | 31 +------- .../transformers/strategy/native_fsdp.py | 58 ++------------ .../model/transformers/transformers.py | 16 ---- src/twinkle/utils/framework.py | 29 +++++++ .../transformers/test_native_fsdp_strategy.py | 49 ------------ tests/transformers/test_task_head_buffers.py | 79 +++++++++++++++++++ 7 files changed, 121 insertions(+), 153 deletions(-) diff --git a/src/twinkle/model/base.py b/src/twinkle/model/base.py index 739fc2ed9..0ef972996 100644 --- a/src/twinkle/model/base.py +++ b/src/twinkle/model/base.py @@ -7,7 +7,7 @@ from itertools import chain from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Protocol, Type, Union -from twinkle import Platform, remote_function +from twinkle import Platform, remote_function, torch_util from twinkle.data_format import InputFeature, ModelOutput from twinkle.hub import HubOperation from twinkle.loss.base import Loss @@ -397,7 +397,7 @@ def _task_head_state(self, adapter_name: Optional[str]) -> Dict[str, Any]: head = self._attached_head(adapter_name) if head is None: return {} - return {f'task_head.{name}': tensor.detach().clone().cpu() + return {f'task_head.{name}': torch_util.to_local_tensor(tensor).detach().clone().cpu() for name, tensor in chain(head.named_parameters(), head.named_buffers())} def _task_head_carries_on_this_rank(self) -> bool: @@ -411,7 +411,6 @@ def _restore_task_head_state(self, adapter_name: Optional[str], state: Dict[str, this with their slot-pool import. An empty snapshot passes -- every host checks the OTHER side's emptiness, never its own (a rank without slots skips, a checkpoint written head-less stays head-less).""" - import torch head = self._attached_head(adapter_name) if head is None: if state and self._task_head_carries_on_this_rank(): @@ -431,11 +430,8 @@ def _restore_task_head_state(self, adapter_name: Optional[str], state: Dict[str, tensor = tensors[key[len('task_head.'):]] if value.shape != tensor.shape: raise ValueError(f'task-head shape mismatch for {key}: {value.shape} != {tensor.shape}') - with torch.no_grad(): - for key, value in state.items(): - param = tensors[key[len('task_head.'):]] - value = value.to(device=param.device, dtype=param.dtype) - param.data.copy_(value) + for key, value in state.items(): + torch_util.copy_tensor_(tensors[key[len('task_head.'):]], value) def _save_task_head_checkpoint(self, checkpoint_dir: str, adapter_name: Optional[str]) -> None: """Write the adapter's attached head to its own task_head.safetensors. diff --git a/src/twinkle/model/multi_lora.py b/src/twinkle/model/multi_lora.py index a787215a3..dd08902f5 100644 --- a/src/twinkle/model/multi_lora.py +++ b/src/twinkle/model/multi_lora.py @@ -6,7 +6,6 @@ from itertools import chain from peft import LoraConfig, PeftModel, get_peft_model from peft.tuners.lora import Embedding, Linear, LoraLayer -from torch.distributed.tensor import distribute_tensor from types import MethodType from typing import Any, Callable, Dict, List, Optional, Union @@ -189,39 +188,13 @@ def _get_available_lora(self) -> Optional[LoraTenant]: def _read_param_tensor(self, parameter): return torch_util.to_local_tensor(parameter) - @staticmethod - def _is_distributed_param(parameter): - return hasattr(parameter, 'device_mesh') and hasattr(parameter, 'placements') - @staticmethod def _is_target_parameter_lora_name(name: str) -> bool: return '._twinkle_lora_' in name def _write_param_tensor(self, parameter, value): - if value is None: - return - value = value.detach().to(dtype=parameter.dtype) - if self._is_distributed_param(parameter): - if self._is_distributed_param(value): - parameter.data.copy_(value.to(parameter.device)) - return - - local_param = parameter.to_local() if hasattr(parameter, 'to_local') else None - parameter_shape = tuple(parameter.shape) - value_shape = tuple(value.shape) - if local_param is not None: - local_shape = tuple(local_param.shape) - if value_shape == local_shape and value_shape != parameter_shape: - local_param.copy_(value.to(local_param.device)) - return - if value_shape != parameter_shape: - raise ValueError( - f'Cannot write tensor with shape {value_shape} to distributed parameter with global shape ' - f'{parameter_shape} and local shape {local_shape}') - value = distribute_tensor(value.to(parameter.device), parameter.device_mesh, parameter.placements) - else: - value = value.to(parameter.device) - parameter.data.copy_(value) + if value is not None: + torch_util.copy_tensor_(parameter, value) @staticmethod def _slice_rank_tensor(name: str, tensor, rank: int): diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index 35ad95256..b0f28bc5a 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -6,7 +6,6 @@ from torch import nn from torch.distributed.device_mesh import DeviceMesh as TorchDeviceMesh from torch.distributed.fsdp import FSDPModule, fully_shard -from torch.distributed.tensor import DTensor from typing import TYPE_CHECKING, Any, Dict, List, Literal, Mapping, Optional, Set from twinkle.model.state_dict_utils import collect_local_state, is_lora_state_key, is_saveable_adapter_key @@ -25,13 +24,13 @@ PEFT_BASE_LAYER_SEGMENT = 'base_layer' -def _shard_fp32_task_heads(model, *, mesh, reshard_after_forward): - # FSDP parameter groups require one storage dtype; FP32 heads cannot share the BF16 backbone group. - for head in model.modules(): - if (not getattr(head, 'keep_fp32', False) or isinstance(head, FSDPModule) - or next(head.parameters(), None) is None): +def _shard_fp32_modules(model, *, mesh, reshard_after_forward): + # FSDP parameter groups require one storage dtype; keep FP32 modules separate from the BF16 group. + for module in model.modules(): + if (not getattr(module, 'keep_fp32', False) or isinstance(module, FSDPModule) + or next(module.parameters(), None) is None): continue - fully_shard(head, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=_build_mp_policy('no')) + fully_shard(module, mesh=mesh, reshard_after_forward=reshard_after_forward, mp_policy=_build_mp_policy('no')) class NativeFSDPStrategy(TransformerStrategyBase): @@ -203,7 +202,7 @@ def wrap_model(self, model, optimizer=None, *additional_optimizers): world_size = self.device_mesh.world_size ep_fsdp_mesh_1d = self.ep_fsdp_device_mesh['ep_fsdp'] if ep_enabled else None - _shard_fp32_task_heads(model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) + _shard_fp32_modules(model, mesh=fsdp_mesh, reshard_after_forward=reshard_after_forward) for layer_mod, experts_mod in layer_pairs: layer_mod._fsdp_modules = [] @@ -283,49 +282,6 @@ def _prepare_optimizer_state_dict_options(self, *, for_load: bool): broadcast_from_rank0=for_load, ) - def get_task_head_state(self, head): - if not any(isinstance(parameter, DTensor) for parameter in head.parameters()): - return None - from torch.distributed.checkpoint.state_dict import get_model_state_dict - - state = get_model_state_dict(head, options=self._prepare_optimizer_state_dict_options(for_load=False)) - for name, buffer in head.named_buffers(): - state.setdefault(name, buffer) - return {f'task_head.{name}': tensor.detach().clone().cpu() for name, tensor in state.items()} - - def restore_task_head_state(self, head, state) -> bool: - if not any(isinstance(parameter, DTensor) for parameter in head.parameters()): - return False - from torch.distributed.checkpoint.state_dict import set_model_state_dict - - tensors = {**dict(head.named_parameters()), **dict(head.named_buffers())} - expected = {f'task_head.{name}' for name in tensors} - legacy = {f'task_head.{name}' for name, _ in head.named_parameters()} - if set(state) not in (expected, legacy): - raise ValueError( - f'task_head checkpoint keys {sorted(state)} do not match the head it restores: ' - f'expected {sorted(expected)}.') - for key, value in state.items(): - tensor = tensors[key[len('task_head.'):]] - if value.shape != tensor.shape: - raise ValueError(f'task-head shape mismatch for {key}: {value.shape} != {tensor.shape}') - persistent = set(head.state_dict()) - model_state = {key[len('task_head.'):]: value for key, value in state.items() - if key[len('task_head.'):] in persistent} - # Parameter-only checkpoints keep the head's current calibration; strict loading still needs its keys. - for name, buffer in head.named_buffers(): - if name in persistent: - model_state.setdefault(name, buffer) - set_model_state_dict( - head, model_state, options=self._prepare_optimizer_state_dict_options(for_load=True)) - with torch.no_grad(): - for key, value in state.items(): - name = key[len('task_head.'):] - if name not in persistent: - tensor = tensors[name] - tensor.copy_(value.to(device=tensor.device, dtype=tensor.dtype)) - return True - def needs_wrapped_optimizer_state(self) -> bool: return self.device_mesh is not None diff --git a/src/twinkle/model/transformers/transformers.py b/src/twinkle/model/transformers/transformers.py index 57745d305..cdb0717f0 100644 --- a/src/twinkle/model/transformers/transformers.py +++ b/src/twinkle/model/transformers/transformers.py @@ -3036,22 +3036,6 @@ def _task_head_host(self): def _task_head_is_master(self) -> bool: return Platform.is_master() - def _task_head_state(self, adapter_name): - head = self._attached_head(adapter_name) - getter = getattr(self.strategy, 'get_task_head_state', None) - if head is not None and getter is not None: - state = getter(head) - if state is not None: - return state - return super()._task_head_state(adapter_name) - - def _restore_task_head_state(self, adapter_name, state) -> None: - head = self._attached_head(adapter_name) - restorer = getattr(self.strategy, 'restore_task_head_state', None) - if head is not None and restorer is not None and restorer(head, state): - return - super()._restore_task_head_state(adapter_name, state) - def _add_task_head(self, adapter_name: str, task, task_head_loader=None, num_labels=None) -> None: """Build one task head worker-side and register it per adapter. diff --git a/src/twinkle/utils/framework.py b/src/twinkle/utils/framework.py index f72c523e3..11c32e478 100644 --- a/src/twinkle/utils/framework.py +++ b/src/twinkle/utils/framework.py @@ -178,6 +178,35 @@ def to_local_tensor(tensor: 'torch.Tensor') -> 'torch.Tensor': return tensor.to_local() return tensor + @staticmethod + def copy_tensor_(tensor: 'torch.Tensor', value: 'torch.Tensor') -> None: + """Copy a full tensor or a saved local shard in place, preserving the target layout.""" + import torch + + with torch.no_grad(): + value = value.detach().to(dtype=tensor.dtype) + if hasattr(tensor, 'device_mesh') and hasattr(tensor, 'placements'): + if hasattr(value, 'device_mesh') and hasattr(value, 'placements'): + tensor.copy_(value.to(tensor.device)) + return + local = tensor.to_local() if hasattr(tensor, 'to_local') else None + global_shape, value_shape = tuple(tensor.shape), tuple(value.shape) + if local is not None: + local_shape = tuple(local.shape) + if value_shape == local_shape and value_shape != global_shape: + local.copy_(value.to(local.device)) + return + if value_shape != global_shape: + raise ValueError( + f'Cannot write tensor with shape {value_shape} to distributed parameter with global shape ' + f'{global_shape} and local shape {local_shape}') + from torch.distributed.tensor import distribute_tensor + + value = distribute_tensor(value.to(tensor.device), tensor.device_mesh, tensor.placements) + else: + value = value.to(tensor.device) + tensor.copy_(value) + @staticmethod def synchronize(device=None): """Wait for all queued work on ``device``, defaulting to the LOCAL_RANK device. diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 27a954efc..7701ea528 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -230,52 +230,3 @@ def test_fp32_task_head_is_sharded_as_separate_precision_group(head_sharding): assert model.task_head.bias.dtype is torch.float32 assert model.standard_head.weight.dtype is torch.bfloat16 - -@pytest.fixture -def sharded_head(tmp_path): - from torch.distributed.device_mesh import init_device_mesh - from torch.distributed.tensor import Shard, distribute_tensor - - torch.distributed.init_process_group('gloo', init_method=f'file://{tmp_path / "head-pg"}', rank=0, world_size=1) - try: - mesh = init_device_mesh('cpu', (1,)) - head = torch.nn.Linear(2, 1) - for name, parameter in list(head.named_parameters()): - setattr(head, name, torch.nn.Parameter(distribute_tensor(parameter.detach(), mesh, [Shard(0)]))) - yield head - finally: - torch.distributed.destroy_process_group() - - -def test_sharded_task_head_checkpoint_includes_nonpersistent_buffers(sharded_head): - head = sharded_head - head.register_buffer('calibration', torch.tensor([1.0]), persistent=False) - strategy = NativeFSDPStrategy(enable_ep=False) - state = strategy.get_task_head_state(head) - assert set(state) == {'task_head.weight', 'task_head.bias', 'task_head.calibration'} - - restored = {key: torch.full_like(value, 3.0) for key, value in state.items()} - assert strategy.restore_task_head_state(head, restored) - for parameter in head.parameters(): - torch.testing.assert_close(parameter.full_tensor(), torch.full_like(parameter.full_tensor(), 3.0)) - torch.testing.assert_close(head.calibration, torch.tensor([3.0])) - - -@pytest.mark.parametrize('persistent', [False, True]) -def test_sharded_task_head_loads_legacy_parameters_without_changing_calibration(persistent, sharded_head): - head = sharded_head - head.register_buffer('calibration', torch.tensor([7.0]), persistent=persistent) - strategy = NativeFSDPStrategy(enable_ep=False) - legacy = {f'task_head.{name}': torch.full_like(param.full_tensor(), 3.0) - for name, param in head.named_parameters()} - assert strategy.restore_task_head_state(head, legacy) - for param in head.parameters(): - torch.testing.assert_close(param.full_tensor(), torch.full_like(param.full_tensor(), 3.0)) - torch.testing.assert_close(head.calibration, torch.tensor([7.0])) - - -def test_unsharded_head_keeps_the_plain_tensor_checkpoint_path(): - head = torch.nn.Linear(2, 1) - strategy = NativeFSDPStrategy(enable_ep=False) - assert strategy.get_task_head_state(head) is None - assert strategy.restore_task_head_state(head, {}) is False diff --git a/tests/transformers/test_task_head_buffers.py b/tests/transformers/test_task_head_buffers.py index a8fcca9df..b414aed01 100644 --- a/tests/transformers/test_task_head_buffers.py +++ b/tests/transformers/test_task_head_buffers.py @@ -75,3 +75,82 @@ def test_bad_buffer_shape_rejected_before_any_tensor_changes(): with pytest.raises(ValueError, match='shape'): slots.import_state('a', host, state, _write) torch.testing.assert_close(head.weight, before) + + +@pytest.fixture(params=[False, True], ids=['plain', 'sharded']) +def head_model(tmp_path, request): + from torch.distributed.device_mesh import init_device_mesh + from torch.distributed.tensor import Shard, distribute_tensor + from twinkle.model.transformers.strategy.native_fsdp import NativeFSDPStrategy + from twinkle.model.transformers.transformers import TransformersModel + + sharded = request.param + if sharded: + torch.distributed.init_process_group( + 'gloo', init_method=f'file://{tmp_path / "head-pg"}', rank=0, world_size=1) + try: + mesh = init_device_mesh('cpu', (1,)) if sharded else None + head = nn.Linear(2, 1) + if sharded: + for name, parameter in list(head.named_parameters()): + setattr(head, name, nn.Parameter(distribute_tensor(parameter.detach(), mesh, [Shard(0)]))) + host = nn.Module() + host.task_head = head + model = object.__new__(TransformersModel) + model.__dict__['model'] = host + model.strategy = NativeFSDPStrategy(device_mesh=mesh, enable_ep=False) + yield model, head + finally: + if sharded: + torch.distributed.destroy_process_group() + + +@pytest.mark.parametrize('persistent', [False, True]) +def test_head_checkpoint_restores_buffers_and_keeps_parameters(head_model, persistent): + from torch.distributed.tensor import DTensor + + model, head = head_model + head.register_buffer('calibration', torch.tensor([7.0]), persistent=persistent) + parameters = dict(head.named_parameters()) + state = model._task_head_state('') + assert set(state) == {'task_head.weight', 'task_head.bias', 'task_head.calibration'} + assert all(not isinstance(value, DTensor) and value.device.type == 'cpu' for value in state.values()) + with torch.no_grad(): + for parameter in head.parameters(): + parameter.zero_() + head.calibration.zero_() + model._restore_task_head_state('', state) + for name, parameter in head.named_parameters(): + assert parameter is parameters[name] + value = parameter.full_tensor() if isinstance(parameter, DTensor) else parameter + torch.testing.assert_close(value, state[f'task_head.{name}']) + torch.testing.assert_close(head.calibration, torch.tensor([7.0])) + + +@pytest.mark.parametrize('persistent', [False, True]) +def test_head_loads_legacy_parameters_without_changing_calibration(head_model, persistent): + from torch.distributed.tensor import DTensor + + model, head = head_model + head.register_buffer('calibration', torch.tensor([7.0]), persistent=persistent) + legacy = {key: torch.full_like(value, 3.0) for key, value in model._task_head_state('').items() + if key != 'task_head.calibration'} + model._restore_task_head_state('', legacy) + for parameter in head.parameters(): + value = parameter.full_tensor() if isinstance(parameter, DTensor) else parameter + torch.testing.assert_close(value, torch.full_like(value, 3.0)) + torch.testing.assert_close(head.calibration, torch.tensor([7.0])) + + +def test_head_rejects_bad_buffer_shape_before_loading_parameters(head_model): + model, head = head_model + head.register_buffer('calibration', torch.tensor([7.0]), persistent=False) + before = model._task_head_state('') + state = {key: value.clone() for key, value in before.items()} + state['task_head.weight'].fill_(99) + state['task_head.calibration'] = torch.ones(3) + with pytest.raises(ValueError, match='shape'): + model._restore_task_head_state('', state) + after = model._task_head_state('') + for key, value in before.items(): + torch.testing.assert_close(after[key], value) From bdfdfba3a5975d41ba78e692eeb4800ef5422fdc Mon Sep 17 00:00:00 2001 From: Yunnglin Date: Tue, 17 Nov 2026 21:09:18 +0800 Subject: [PATCH 18/18] refactor(server): keep model file validation at worker boundary --- .../transformers/strategy/native_fsdp.py | 2 +- src/twinkle/server/config/training.py | 10 ------- src/twinkle/server/model/app.py | 19 ++++++------ tests/server/config/test_training_config.py | 30 +------------------ tests/server/model/test_task_head.py | 10 +++++-- .../transformers/test_native_fsdp_strategy.py | 1 - 6 files changed, 18 insertions(+), 54 deletions(-) diff --git a/src/twinkle/model/transformers/strategy/native_fsdp.py b/src/twinkle/model/transformers/strategy/native_fsdp.py index b0f28bc5a..3229fb1ef 100644 --- a/src/twinkle/model/transformers/strategy/native_fsdp.py +++ b/src/twinkle/model/transformers/strategy/native_fsdp.py @@ -304,7 +304,7 @@ def save_optimizer_checkpoint(self, model, optimizer, output_path: str, *, param options=self._prepare_optimizer_state_dict_options(for_load=False), ) if Platform.is_master(): - # DCP requires an entry for every trainable parameter; Adam leaves unused parameters uninitialized. + # DCP requires an entry for every optimizer parameter; Adam leaves unused parameters uninitialized. for group in optim_state['param_groups']: for name in group['params']: optim_state['state'].setdefault(name, {}) diff --git a/src/twinkle/server/config/training.py b/src/twinkle/server/config/training.py index 7821d1bb7..fa7262a5c 100644 --- a/src/twinkle/server/config/training.py +++ b/src/twinkle/server/config/training.py @@ -3,7 +3,6 @@ from __future__ import annotations import math -from pathlib import Path from typing import TYPE_CHECKING, Any, Literal from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, ValidationInfo, field_validator, model_validator @@ -17,13 +16,6 @@ from .application_spec import ApplicationSpec -def validate_native_st_model(model_path: str, task_type: str | None, model_cls: str | None) -> None: - if task_type == 'embedding' and model_cls is None and (Path(model_path) / 'modules.json').is_file(): - raise ValueError( - 'native sentence-transformers checkpoint requires model_cls; ' - 'the generic embedding wrapper cannot preserve its module pipeline') - - class SpectralHybridArgs(BaseModel): """Strict server-owned Spectral Hybrid allocation and optimizer defaults.""" @@ -135,7 +127,6 @@ class ModelArgs(TrainingArguments, LoraArguments): @model_validator(mode="after") def _validate_spectral_backend(self): - validate_native_st_model(self.model_id, self.task_type, self.model_cls) self.validate_backend(self.backend, mode=self.train_mode, hybrid=self.hybrid) if self.language_model_only is not None and self.backend != "megatron": raise ValueError("language_model_only requires the megatron backend") @@ -208,7 +199,6 @@ def _instance_config(cls, value, info: ValidationInfo): @model_validator(mode="after") def _validate_backend(self): - validate_native_st_model(self.model_id, self.config.task_type, self.config.model_cls) if ( self.name.startswith("/") or any(part in ("", ".", "..") for part in self.name.split("/")) diff --git a/src/twinkle/server/model/app.py b/src/twinkle/server/model/app.py index 1882e385e..6dddf291f 100644 --- a/src/twinkle/server/model/app.py +++ b/src/twinkle/server/model/app.py @@ -17,7 +17,6 @@ from twinkle.model.base import TrainableModel from twinkle.server.config.backend_dispatch import BackendSelector from twinkle.server.config.task_queue import TaskQueueConfig -from twinkle.server.config.training import validate_native_st_model from twinkle.server.deployment import bind_deployment, build_deployment_app from twinkle.server.runtime import init_twinkle_runtime from twinkle.server.state import ServerState, get_server_state @@ -62,11 +61,15 @@ def _validate_model_task(kw: dict[str, Any]) -> None: kw['task'] = 'decision' kw['task_head_loader'] = head_factory() return + from pathlib import Path from transformers import AutoConfig from twinkle.hub import HubOperation path = HubOperation.download_model(kw['model_id']) - validate_native_st_model(path, task_type, kw.get('model_cls')) + if task_type == 'embedding' and kw.get('model_cls') is None and (Path(path) / 'modules.json').is_file(): + raise ValueError( + 'native sentence-transformers checkpoint requires model_cls; ' + 'the generic embedding wrapper cannot preserve its module pipeline') loader = kw.get('model_loader') config = (loader.process_config(loader.build_config(path)) if loader is not None else AutoConfig.from_pretrained(path, trust_remote_code=trust_remote_code())) @@ -77,7 +80,6 @@ def _validate_model_task(kw: dict[str, Any]) -> None: if classification: if num_labels is None or getattr(config, 'num_labels', None) != num_labels: raise ValueError('num_labels does not match the persisted classification head') - from pathlib import Path from safetensors import safe_open head_found = False for file in Path(path).glob('model*.safetensors'): @@ -138,15 +140,12 @@ def _make_transformers_model(kw: dict[str, Any]) -> Any: from .backends.transformers_model import TwinkleCompatFullTransformersModel for key in _LORA_ONLY_CTOR_KWARGS: kw.pop(key, None) - num_labels = kw.pop('num_labels', None) - task = kw.pop('task', None) - if task == 'decision' and getattr(kw.get('model_loader'), 'task_type', None) == 'decision': + if kw.get('task') == 'decision' and getattr(kw.get('model_loader'), 'task_type', None) == 'decision': # The family loader attaches its single head while constructing the full model. + kw.pop('task') kw.pop('task_head_loader', None) - elif task is not None: - kw['task'] = task - if num_labels is not None: - kw['num_labels'] = num_labels + if kw.get('task') is None: + kw.pop('num_labels', None) if model_cls_spec is not None: return _make_model_class(_resolve_model_cls(model_cls_spec))(**kw) return TwinkleCompatFullTransformersModel(**kw) diff --git a/tests/server/config/test_training_config.py b/tests/server/config/test_training_config.py index b426a5855..61a7686a9 100644 --- a/tests/server/config/test_training_config.py +++ b/tests/server/config/test_training_config.py @@ -77,34 +77,6 @@ def test_custom_model_class_rejects_incompatible_training_modes(mutation, mode): ServerConfig(applications=[app]) -@pytest.mark.parametrize('resolved', [False, True]) -def test_native_st_without_model_cls_is_rejected_before_runtime(tmp_path, resolved): - from twinkle.server.config.training import TrainingModelConfig - - (tmp_path / 'modules.json').write_text('[]') - with pytest.raises(ValidationError, match='native sentence-transformers checkpoint requires model_cls'): - if resolved: - values = model_config() - values['model_id'] = str(tmp_path) - values['config']['task_type'] = 'embedding' - TrainingModelConfig.model_validate(values) - else: - app = model_application() - app['args'].update(model_id=str(tmp_path), task_type='embedding') - ServerConfig(applications=[app]) - - -def test_native_st_with_model_cls_keeps_the_supported_lora_entry(tmp_path): - (tmp_path / 'modules.json').write_text('[]') - app = model_application() - app['args'].update( - model_id=str(tmp_path), - task_type='embedding', - model_cls='swift.dev.model.multi_lora_sentence_transformer_model:MultiLoraSentenceTransformerModel', - ) - ServerConfig(applications=[app]) - - def test_st_example_is_a_cold_on_demand_custom_model(): config = ServerConfig.from_yaml('cookbook/client/swift_model/sentence_transformer_config.yaml') model = config.training_config.models[0] @@ -135,7 +107,7 @@ def test_existing_yaml_uses_explicit_instance_limits_and_business_idle(): for path in paths: config = ServerConfig.from_yaml(path) applications = [app for app in config.applications if app.import_path == 'model'] - for app, model in zip(applications, config.training_config.models, strict=True): + for app, model in zip(applications, config.training_config.models): assert model.config.idle_timeout_s == app.args.idle_timeout_s assert model.config.min_replicas == app.args.min_replicas assert model.config.max_replicas == app.args.max_replicas diff --git a/tests/server/model/test_task_head.py b/tests/server/model/test_task_head.py index 38b50382d..8c8d8cc5e 100644 --- a/tests/server/model/test_task_head.py +++ b/tests/server/model/test_task_head.py @@ -136,18 +136,22 @@ def test_full_persisted_head_does_not_forward_num_labels_to_hf(monkeypatch): def test_full_model_attaches_head_and_preserves_device_mesh(monkeypatch): + import inspect + + from twinkle import DeviceMesh from twinkle.infra.dispatch import _get_device_mesh_param_name from twinkle.server.model.backends import transformers_model - mesh = SimpleNamespace(fsdp_size=2) + mesh = DeviceMesh.from_sizes(dp_size=2, device_type='cpu') received = {} monkeypatch.setattr(transformers_model.TransformersModel, '__init__', lambda self, **kwargs: received.update(kwargs)) attached = [] monkeypatch.setattr(transformers_model.TransformersModel, '_add_task_head', lambda self, *args: attached.append(args)) - transformers_model.TwinkleCompatFullTransformersModel( - model_id='causal-backbone', device_mesh=mesh, task='seq_cls', num_labels=3) + model_class = transformers_model.TwinkleCompatFullTransformersModel + inspect.unwrap(model_class.__init__)( + object.__new__(model_class), model_id='causal-backbone', device_mesh=mesh, task='seq_cls', num_labels=3) assert attached == [('', 'seq_cls', None, 3)] assert received['device_mesh'] is mesh assert _get_device_mesh_param_name( diff --git a/tests/transformers/test_native_fsdp_strategy.py b/tests/transformers/test_native_fsdp_strategy.py index 7701ea528..68fa04447 100644 --- a/tests/transformers/test_native_fsdp_strategy.py +++ b/tests/transformers/test_native_fsdp_strategy.py @@ -96,7 +96,6 @@ def test_lazy_wrapping_rebinds_each_tenants_optimizer_without_other_parameters(t _model_wrapped=False, _enable_expert_parallel=False, sp_strategy=None, _maybe_apply_expert_parallel=lambda: None, _ensure_sp_strategy=lambda: None, _ensure_gdn_padding_free_patch=lambda: None, - _apply_deterministic_model_kernels=lambda: None, _rebind_lr_scheduler=TransformersModel._rebind_lr_scheduler, register_mm_forward_hook=lambda group: None) with patch.object(original, 'to', return_value=replacement), patch(