(self)
| 240 | return output |
| 241 | |
| 242 | def repartition(self): |
| 243 | assert self.output_size_per_partition == self.output_size |
| 244 | self.output_size_per_partition = divide(self.output_size, get_model_parallel_world_size()) |
| 245 | mp_rank = get_model_parallel_rank() |
| 246 | mp_size = get_model_parallel_world_size() |
| 247 | self.original_weight = self.weight |
| 248 | # weight is arranged as [stride0...stride1...stride2] * [input_size], extract non-contiguous parts |
| 249 | strides = [1]*self.stride if isinstance(self.stride, int) else self.stride # int means equal number of qkv, or ratios |
| 250 | assert self.weight.shape[0] % sum(strides) == 0, 'cannot divide weight evenly' |
| 251 | factor = self.weight.shape[0] // sum(strides) |
| 252 | # decompose weight according to strides |
| 253 | strided_weights, _acm = [], 0 |
| 254 | for i in range(len(strides)): |
| 255 | strided_weights.append(self.weight[_acm:_acm+factor*strides[i], :].detach()) |
| 256 | _acm += factor*strides[i] |
| 257 | new_weight = torch.cat([ |
| 258 | strided_weight[ |
| 259 | (strided_weight.shape[0]//mp_size)*mp_rank: |
| 260 | (strided_weight.shape[0]//mp_size)*(mp_rank+1) |
| 261 | ] |
| 262 | for strided_weight in strided_weights |
| 263 | ], dim=0).contiguous().view(self.output_size_per_partition, self.input_size) |
| 264 | self.weight = torch.nn.Parameter(new_weight) |
| 265 | del self.original_weight |
| 266 | if self.bias is not None and self.bias.numel() != 0: |
| 267 | self.original_bias = self.bias |
| 268 | # decompose bias according to strides |
| 269 | strided_biases, _acm = [], 0 |
| 270 | for i in range(len(strides)): |
| 271 | strided_biases.append(self.bias[_acm:_acm+factor*strides[i]].detach()) |
| 272 | _acm += factor*strides[i] |
| 273 | new_bias = torch.cat([ |
| 274 | strided_bias[ |
| 275 | (strided_bias.shape[0]//mp_size)*mp_rank: |
| 276 | (strided_bias.shape[0]//mp_size)*(mp_rank+1) |
| 277 | ] |
| 278 | for strided_bias in strided_biases |
| 279 | ], dim=0).contiguous().view(self.output_size_per_partition) |
| 280 | self.bias = torch.nn.Parameter(new_bias) |
| 281 | del self.original_bias |
| 282 | |
| 283 | def partition(self, new_model_parallel_size=None, full_weight=None): |
| 284 | assert self.output_size_per_partition == self.output_size or full_weight is not None |
nothing calls this directly
no test coverage detected