materialize
Materialize a pruned variant from the sorted teacher — slice/merge, no realized checkpoints.
This is the realize-side counterpart of sorted_teacher.sort_state_dict() and the static
twin of the dynamic prune (dynamic_block_prune): given the sorted teacher’s state dict and a
per-layer target (ffn K / attn (q, kv)), it produces the physically smaller weights for the
chosen variant by prefix-slicing (removal) or merging (q-preserving KV reduction). It backs the
block library (a variant = a slice/merge spec into the sorted teacher, not realized weight_paths)
and the bypass / final-realize steps.
Because the sorted teacher orders channels/heads by importance, slicing [:K] (and the blocked
attention keep-set) yields exactly the importance-pruned weights. Reuses the model-agnostic
primitives in attention_ffn_surgery; the descriptor supplies the per-layer keys/geometry via
sorted_teacher.build_layer_layouts().
Classes
Target dims for one layer's replaced subblock (None = unchanged for that axis). |
Functions
Return a new state dict with each targeted layer's block sliced/merged to its variant. |
|
Convert replacement-library |
|
Build a pruned model by materializing |
|
Stream one sorted-teacher shard at a time into a realized HF checkpoint. |
|
Physically realize only the descriptor-owned residual width. |
- class BlockTarget
Bases:
objectTarget dims for one layer’s replaced subblock (None = unchanged for that axis).
- __init__(target_intermediate=None, target_num_q=None, target_num_kv=None, target_mla_heads=None, target_q_lora_rank=None, target_kv_lora_rank=None, target_num_experts=None, expert_keep_indices=None, target_expert_intermediate=None, target_shared_expert_intermediate=None, target_latent_dim=None, target_mamba_heads=None, target_mamba_groups=None, target_mamba_head_dim=None, target_mamba_state_dim=None, remove_ffn=False, remove_attention=False, remove_mamba=False, remove_moe=False)
- Parameters:
target_intermediate (int | None)
target_num_q (int | None)
target_num_kv (int | None)
target_mla_heads (int | None)
target_q_lora_rank (int | None)
target_kv_lora_rank (int | None)
target_num_experts (int | None)
expert_keep_indices (tuple[int, ...] | None)
target_expert_intermediate (int | None)
target_shared_expert_intermediate (int | None)
target_latent_dim (int | None)
target_mamba_heads (int | None)
target_mamba_groups (int | None)
target_mamba_head_dim (int | None)
target_mamba_state_dim (int | None)
remove_ffn (bool)
remove_attention (bool)
remove_mamba (bool)
remove_moe (bool)
- Return type:
None
- expert_keep_indices: tuple[int, ...] | None = None
- remove_attention: bool = False
- remove_ffn: bool = False
- remove_mamba: bool = False
- remove_moe: bool = False
- target_expert_intermediate: int | None = None
- target_intermediate: int | None = None
- target_kv_lora_rank: int | None = None
- target_latent_dim: int | None = None
- target_mamba_groups: int | None = None
- target_mamba_head_dim: int | None = None
- target_mamba_heads: int | None = None
- target_mamba_state_dim: int | None = None
- target_mla_heads: int | None = None
- target_num_experts: int | None = None
- target_num_kv: int | None = None
- target_num_q: int | None = None
- target_q_lora_rank: int | None = None
- block_targets_from_replacements(layer_replacements, teacher_block_configs, num_attention_heads)
Convert replacement-library
layer_replacements->{layer_idx: BlockTarget}.Each replacement pairs
parent_layer_indiceswithchild_block_configs(parsedBlockConfigobjects). The child’s FFNintermediate_size/ attentionnum_query_heads+num_kv_headsbecome the per-layer target dims thatmaterialize_solution_state_dict()slices. If a child only reducesnum_kv_heads, materialization also reducesnum_query_headsby removing the corresponding sorted query-head groups.- Parameters:
num_attention_heads (int)
- Return type:
dict[int, BlockTarget]
- materialize_checkpoint_from_sorted(sorted_dir, layer_replacements, descriptor, child_model_config, output_dir, *, overwrite=False, solution_identity=None)
Stream one sorted-teacher shard at a time into a realized HF checkpoint.
- Parameters:
overwrite (bool)
solution_identity (str | None)
- Return type:
Path
Physically realize only the descriptor-owned residual width.
This creates the width-specific parent used by nested replacement scoring: block configurations remain identical to the sorted teacher while every language residual tensor, projector output, LM-head input, and MTP residual axis is sliced together.
- Parameters:
hidden_width (int)
alignment (int)
overwrite (bool)
- Return type:
Path
- materialize_model_from_sorted(sorted_dir, layer_replacements, descriptor, child_model_config)
Build a pruned model by materializing
layer_replacementsfrom the sorted teacher.The realize/library counterpart of the dynamic prune: load the sorted teacher’s weights, slice/ merge the replaced blocks to their target dims, then load the result into a model built from
child_model_config(the solution’s heterogeneous block configs). No realized intermediate checkpoint. For large models this materializes densely — the sharded path writes the result via the normal save and reloads withload_and_shard_model(an in-container follow-up).
- materialize_solution_state_dict(state_dict, layouts, targets)
Return a new state dict with each targeted layer’s block sliced/merged to its variant.
state_dictis the (sorted) teacher’s;targetsmapslayer_idx -> BlockTarget. Untargeted layers are copied unchanged. The result has smaller tensors for materialized blocks, so it loads into a model configured with the corresponding (heterogeneous) block configs.- Parameters:
targets (dict[int, BlockTarget])