MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / shard_weights

Method shard_weights

deepspeed/module_inject/layers.py:1886–1914  ·  view source on GitHub ↗
(self, conv)

Source from the content-addressed store, hash-verified

1884
1885 # Split along the input/output channel depending on whether it is the last conv layer.
1886 def shard_weights(self, conv):
1887 if self.shard_by_oc:
1888 total_size = conv.weight.shape[0]
1889 else:
1890 total_size = conv.weight.shape[1]
1891 bias_data = None
1892 cols_per_rank = [0]
1893 for i in range(self.world_size - 1, -1, -1):
1894 cols = total_size // self.world_size
1895 if i < total_size % self.world_size:
1896 cols += 1
1897 cols_per_rank.append(cols_per_rank[-1] + cols)
1898 weight_data = conv.weight.data
1899 if self.shard_by_oc:
1900 # not last conv layer, split output channel
1901 weight_data = weight_data[cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1902 if conv.bias is not None:
1903 bias_data = conv.bias.data[cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1904 else:
1905 # last conv layer, split input channel
1906 weight_data = weight_data[:, cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1907 if conv.bias is not None:
1908 bias_data = conv.bias.data / float(self.world_size)
1909 self.conv = nn.Conv2d(weight_data.shape[1], weight_data.shape[0], conv.kernel_size, conv.stride, conv.padding,
1910 conv.dilation, conv.groups, conv.bias is not None, conv.padding_mode)
1911 self.conv.weight = torch.nn.Parameter(weight_data)
1912 if conv.bias is not None:
1913 self.conv.bias = torch.nn.Parameter(bias_data)
1914 del conv
1915
1916 def forward(self, input: torch.Tensor) -> torch.Tensor:
1917 return self.conv(input)

Callers 1

__init__Method · 0.95

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected