A replacement for ``torch.nn.Linear`` that works with ZeRO-3 to reduce memory requirements via tiling. TiledLinear breaks the input and output dimensions of a linear layer into tiles that are processed in sequence. This class enables huge linear layers when combined
(self,
in_features,
out_features,
bias=True,
in_splits=1,
out_splits=1,
input_is_already_split=False,
combine_out_splits=True,
linear_cls=torch.nn.Linear,
init_linear=None,
**kwargs)
| 32 | class TiledLinear(torch.nn.Module): |
| 33 | |
| 34 | def __init__(self, |
| 35 | in_features, |
| 36 | out_features, |
| 37 | bias=True, |
| 38 | in_splits=1, |
| 39 | out_splits=1, |
| 40 | input_is_already_split=False, |
| 41 | combine_out_splits=True, |
| 42 | linear_cls=torch.nn.Linear, |
| 43 | init_linear=None, |
| 44 | **kwargs): |
| 45 | """A replacement for ``torch.nn.Linear`` that works with ZeRO-3 to reduce |
| 46 | memory requirements via tiling. |
| 47 | |
| 48 | TiledLinear breaks the input and output dimensions of a linear layer |
| 49 | into tiles that are processed in sequence. This class enables huge |
| 50 | linear layers when combined with ZeRO-3 because inactive tiles can be |
| 51 | partitioned and offloaded. |
| 52 | |
| 53 | .. note:: |
| 54 | We recommend using as few tiles as necessary. Tiling |
| 55 | significantly reduces memory usage, but can reduce throughput |
| 56 | for inexpensive layers. This due to the smaller kernels having |
| 57 | less parallelism and lower arithmetic intensity, while |
| 58 | introducing more frequent synchronization and communication. |
| 59 | |
| 60 | Args: |
| 61 | in_features (int): See ``torch.nn.Linear`` |
| 62 | out_features (int): See ``torch.nn.Linear`` |
| 63 | bias (bool, optional): See ``torch.nn.Linear`` |
| 64 | in_splits (int, optional): The number of tiles along the input dimension. Defaults to 1. |
| 65 | out_splits (int, optional): The number of tiles along the output dimension. Defaults to 1. |
| 66 | input_is_already_split (bool, optional): If set to ``True``, assume that the ``input_`` in |
| 67 | to ``forward()`` is already split into ``in_splits`` chunks. Defaults to ``False``. |
| 68 | combine_out_splits (bool, optional): If set to ``False``, do not combine the ``out_splits`` outputs |
| 69 | into a single tensor. Defaults to ``True``. |
| 70 | linear_cls (class, optional): The underlying class to build individual tiles. |
| 71 | Defaults to ``torch.nn.Linear``. |
| 72 | init_linear (``torch.nn.Linear``, optional): If set, copy the parameters of |
| 73 | ``init_linear``. Useful for debugging. Defaults to ``None``. |
| 74 | kwargs (dict, optional): additional keyword arguments to provide to ``linear_cls()``. |
| 75 | |
| 76 | Raises: |
| 77 | RuntimeError: ``in_splits`` must be within the range [1, in_features). |
| 78 | RuntimeError: ``out_splits`` must be within the range of [1, out_features). |
| 79 | """ |
| 80 | |
| 81 | super().__init__() |
| 82 | |
| 83 | if (in_splits < 1) or (in_splits > in_features): |
| 84 | raise RuntimeError('in splits must be in range [1, in_features].') |
| 85 | if (out_splits < 1) or (out_splits > out_features): |
| 86 | raise RuntimeError('out splits must be in range [1, out_features].') |
| 87 | |
| 88 | # global, not necessarily local |
| 89 | self.in_features = in_features |
| 90 | self.out_features = out_features |
| 91 | self.use_bias = bias |
nothing calls this directly
no test coverage detected