MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / _initialize_affine_weight_cpu

Function _initialize_affine_weight_cpu

codegeex/megatron/mpu/layers.py:98–138  ·  view source on GitHub ↗

Initialize affine weight for model parallel. Build the master weight on all processes and scatter the relevant chunk.

(
    weight,
    output_size,
    input_size,
    per_partition_size,
    partition_dim,
    init_method,
    stride=1,
    return_master_weight=False,
)

Source from the content-addressed store, hash-verified

96
97
98def _initialize_affine_weight_cpu(
99 weight,
100 output_size,
101 input_size,
102 per_partition_size,
103 partition_dim,
104 init_method,
105 stride=1,
106 return_master_weight=False,
107):
108 """Initialize affine weight for model parallel.
109
110 Build the master weight on all processes and scatter
111 the relevant chunk."""
112
113 set_tensor_model_parallel_attributes(
114 tensor=weight, is_parallel=True, dim=partition_dim, stride=stride
115 )
116
117 # Initialize master weight
118 master_weight = torch.empty(
119 output_size, input_size, dtype=torch.float, requires_grad=False
120 )
121 init_method(master_weight)
122 args = get_args()
123 master_weight = master_weight.to(dtype=args.params_dtype)
124
125 # Split and copy
126 per_partition_per_stride_size = divide(per_partition_size, stride)
127 weight_list = torch.split(
128 master_weight, per_partition_per_stride_size, dim=partition_dim
129 )
130 rank = get_tensor_model_parallel_rank()
131 world_size = get_tensor_model_parallel_world_size()
132 my_weight_list = weight_list[rank::world_size]
133
134 with torch.no_grad():
135 torch.cat(my_weight_list, dim=partition_dim, out=weight)
136 if return_master_weight:
137 return master_weight
138 return None
139
140
141class VocabParallelEmbedding(torch.nn.Module):

Callers 3

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls 5

get_argsFunction · 0.90
divideFunction · 0.85

Tested by

no test coverage detected