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

Method shard_weights

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

Source from the content-addressed store, hash-verified

1802
1803 # Split along the input/output channel depending on whether it is the last conv layer.
1804 def shard_weights(self, conv):
1805 if self.shard_by_oc:
1806 total_size = conv.weight.shape[0]
1807 else:
1808 total_size = conv.weight.shape[1]
1809 bias_data = None
1810 cols_per_rank = [0]
1811 for i in range(self.world_size - 1, -1, -1):
1812 cols = total_size // self.world_size
1813 if i < total_size % self.world_size:
1814 cols += 1
1815 cols_per_rank.append(cols_per_rank[-1] + cols)
1816 weight_data = conv.weight.data
1817 if self.shard_by_oc:
1818 # not last conv layer, split output channel
1819 weight_data = weight_data[cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1820 if conv.bias is not None:
1821 bias_data = conv.bias.data[cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1822 else:
1823 # last conv layer, split input channel
1824 weight_data = weight_data[:, cols_per_rank[self.rank]:cols_per_rank[self.rank + 1]]
1825 if conv.bias is not None:
1826 bias_data = conv.bias.data / float(self.world_size)
1827 self.conv = nn.Conv2d(weight_data.shape[1], weight_data.shape[0], conv.kernel_size, conv.stride, conv.padding,
1828 conv.dilation, conv.groups, conv.bias is not None, conv.padding_mode)
1829 self.conv.weight = torch.nn.Parameter(weight_data)
1830 if conv.bias is not None:
1831 self.conv.bias = torch.nn.Parameter(bias_data)
1832 del conv
1833
1834 def forward(self, input: torch.Tensor) -> torch.Tensor:
1835 return self.conv(input)

Callers 1

__init__Method · 0.95

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected