[CPU] Fix TP padding issue on Phi-4 (#8289)

This commit is contained in:
blzheng
2025-08-18 07:25:26 +08:00
committed by GitHub
parent b341b7dbce
commit ebbb75e917
5 changed files with 93 additions and 27 deletions

View File

@@ -110,6 +110,20 @@ def adjust_scalar_to_fused_array(param, loaded_weight, shard_id):
return param[shard_id], loaded_weight
def adjust_shard_offsets(shard_offsets, loaded_weight, dim):
actual_weight_size = loaded_weight.size(dim)
target_weight_size = shard_offsets[-1][-1] + shard_offsets[-1][-2]
if actual_weight_size != target_weight_size:
new_shard_offsets = []
new_offset = 0
for shard_id, shard_offset, shard_size in shard_offsets:
actual_shard_size = actual_weight_size * shard_size // target_weight_size
new_shard_offsets.append((shard_id, new_offset, actual_shard_size))
new_offset += actual_shard_size
return new_shard_offsets
return shard_offsets
class LinearBase(torch.nn.Module):
"""Base linear layer.
@@ -535,6 +549,11 @@ class MergedColumnParallelLinear(ColumnParallelLinear):
packed_dim = getattr(param, "packed_dim", None)
use_bitsandbytes_4bit = getattr(param, "use_bitsandbytes_4bit", False)
if _is_cpu:
shard_offsets = adjust_shard_offsets(
shard_offsets, loaded_weight, output_dim
)
for shard_id, shard_offset, shard_size in shard_offsets:
# Special case for Quantization.
# If quantized, we need to adjust the offset and size to account
@@ -977,6 +996,11 @@ class QKVParallelLinear(ColumnParallelLinear):
use_bitsandbytes_4bit = getattr(param, "use_bitsandbytes_4bit", False)
packed_dim = getattr(param, "packed_dim", None)
if _is_cpu:
shard_offsets = adjust_shard_offsets(
shard_offsets, loaded_weight, output_dim
)
for shard_id, shard_offset, shard_size in shard_offsets:
# Special case for Quantized Weights.
# If quantized, we need to adjust the offset and size to account