[CPU] support the case where num_attention_heads or intermediate_size is not divisible by the TP size (#6771)

This commit is contained in:
Chunyuan WU
2025-07-04 00:51:38 +08:00
committed by GitHub
parent 9fcc9a80e7
commit 1dce6c480f
11 changed files with 399 additions and 40 deletions

View File

@@ -961,3 +961,57 @@ def kv_cache_scales_loader(
tp_rank,
)
return []
def get_actual_shard_size(shard_size, weight_start, weight_end):
if weight_end < weight_start:
return 0
return min(shard_size, weight_end - weight_start)
def reset_param_data_if_needed(param_data, dim, start, length):
if length == 0:
return
assert length > 0, f"Length should be positive, but got {length}"
param_data.narrow(dim, start, length).zero_()
return
def narrow_padded_param_and_loaded_weight(
param_data,
loaded_weight,
param_data_start,
weight_start,
dim,
shard_size,
narrow_weight=True,
):
actual_shard_size = get_actual_shard_size(
shard_size, weight_start, loaded_weight.size(dim)
)
if narrow_weight:
if actual_shard_size > 0:
loaded_weight = loaded_weight.narrow(dim, weight_start, actual_shard_size)
else:
# No real data to load; create a dummy tensor filled with zeros
loaded_weight = torch.zeros_like(
param_data.narrow(dim, param_data_start, actual_shard_size)
)
# [Note] Reset padded weights to zero.
# If the actual shard size is less than the shard size, we need to reset
# the padded param_data to zero and then copy the loaded_weight into it.
reset_param_data_if_needed(
param_data,
dim,
param_data_start + actual_shard_size,
shard_size - actual_shard_size,
)
param_data = param_data.narrow(dim, param_data_start, actual_shard_size)
return param_data, loaded_weight