def _attention_leaf_spec(fqn, module, leaf, named_modules) -> Optional[ModuleShardingSpec]:
"""Build the boundary spec for one attention-leaf FQN (None if unhandled)."""
if leaf in {"linear_qb", "index_linear_qb", "linear_merge_weight"}:
return _head_sharded_projection_spec(fqn, module, leaf)
if leaf in {"linear_qkv", "index_linear_k"}:
return _linear(
module, param=Replicate(), in_src=Shard(1), in_dst=Shard(1),
out_src=Shard(1), out_dst=Shard(1))
if leaf == "linear_kvb":
return _linear(
module, param=Shard(0), in_src=Replicate(), in_dst=Replicate(),
out_src=Shard(-1), out_dst=Shard(-1))
if leaf == "linear_proj":
return _output_projection_spec(fqn, module, named_modules)
if leaf in {"q_layernorm", "k_layernorm", "index_k_layernorm"}:
return _layernorm_spec(module)
if leaf in {"rotary_emb", "gather_rotary_emb"}:
return _rotary_spec()
if leaf == "sparse_lightning_indexer_kllloss":
return _sparse_indexer_spec()
return None
def build_dsa_specs(model) -> Dict[str, ModuleShardingSpec]:
"""Materialize DSA leaf-boundary specs for a structurally matched model.