File size: 7,635 Bytes
f2c15d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from typing import Optional

import pytest
import torch
import torch.nn as nn

import act_mem
import layers

BATCH_SIZES = (1, 2)
D_MODELS = (128, 256)
SEQ_LENS = (64, 128)
N_HEADS = (2, 4)


DEVICES = ["cpu"]
if torch.cuda.is_available():
    DEVICES.append("cuda")


ZERO_MEM_ACT_FNS = [
    nn.ReLU(),
    nn.Sigmoid(),
    nn.Tanh(),
    nn.LeakyReLU(inplace=True),
    nn.Sigmoid(),
]
ALL_ACT_FNS = ZERO_MEM_ACT_FNS + [
    nn.ELU(),
    nn.GELU(),
    nn.Hardshrink(),
    nn.Hardsigmoid(),
    nn.Hardswish(),
    nn.Hardtanh(),
    nn.LeakyReLU(),
    nn.SELU(),
    nn.SiLU(),
]


class TestSavedTensorContext:
    @pytest.mark.parametrize("device", DEVICES)
    @pytest.mark.parametrize("d_model", D_MODELS)
    @pytest.mark.parametrize("batch_size", BATCH_SIZES)
    def test_linear(self, device: str, d_model: int, batch_size: int) -> None:
        """
        Test a simple linear layer. The inputs should be saved for backwards
        """
        inputs = torch.randn(batch_size, d_model, requires_grad=True, device=device)
        lin = nn.Linear(d_model, d_model, device=device)
        with act_mem.SavedTensorContext(ignored_tensors=lin.parameters()) as saved:
            _ = lin(inputs)
        assert saved.saved_tensor_mem == inputs.numel() * inputs.element_size()

    @pytest.mark.parametrize("device", DEVICES)
    @pytest.mark.parametrize("d_model", D_MODELS)
    @pytest.mark.parametrize("batch_size", BATCH_SIZES)
    def test_linear_amp(self, device: str, d_model: int, batch_size: int) -> None:
        """
        Test a linear layer with AMP. The saved tensors should now be a low-precision version of the
        inputs and the low-precision version of the weights version of the weights
        """
        inputs = torch.randn(batch_size, d_model, requires_grad=True, device=device)
        lin = nn.Linear(d_model, d_model, device=device)
        dtype = torch.bfloat16
        with torch.autocast(device_type=device, dtype=dtype):
            with act_mem.SavedTensorContext(ignored_tensors=lin.parameters()) as saved:
                out = lin(inputs)
        assert (
            saved.saved_tensor_mem
            == out.numel() * out.element_size() + lin.weight.numel() * dtype.itemsize
        )

    @pytest.mark.parametrize("act_fn", ALL_ACT_FNS)
    @pytest.mark.parametrize("dropout_prob", (None, 0.5))
    @pytest.mark.parametrize("device", DEVICES)
    @pytest.mark.parametrize("d_model", D_MODELS)
    @pytest.mark.parametrize("batch_size", BATCH_SIZES)
    @pytest.mark.parametrize("seq_len", SEQ_LENS)
    def test_mlp(
        self,
        act_fn: nn.Module,
        dropout_prob: Optional[float],
        device: str,
        d_model: int,
        batch_size: int,
        seq_len: int,
    ) -> None:
        """
        For the transformer MLP layer with a ReLU non-linearity, the initial inputs and the inputs
        to the final linear layer (which are four times as large) must always be saved. If the
        derivative of the activation function cannot be expressed in terms of the activation
        function's *outputs*, then the activation inputs must also be saved (which are again four
        times as large as the MLP's inputs). The MLP activation memory can be nearly halved by a
        choice of activation function.
        """
        inputs = torch.randn(
            batch_size, seq_len, d_model, requires_grad=True, device=device
        )
        expansion_factor = 4
        mlp = layers.MLP(
            d_model=d_model, act_fn=act_fn, dropout_prob=dropout_prob, device=device
        )
        with act_mem.SavedTensorContext(ignored_tensors=mlp.parameters()) as saved:
            _ = mlp(inputs)

        # Compare measured memory against expected
        first_lin_input_mem = act_mem.get_tensor_bytes(inputs)
        second_lin_input_mem = expansion_factor * first_lin_input_mem
        # Only some activations require additional activation memory
        activation_input_mem = 0 if act_fn in ZERO_MEM_ACT_FNS else second_lin_input_mem
        dropout_act_mem = (
            0 if not dropout_prob else inputs.numel() * (4 if device == "cpu" else 1)
        )

        expected_mem = (
            first_lin_input_mem
            + second_lin_input_mem
            + activation_input_mem
            + dropout_act_mem
        )
        assert saved.saved_tensor_mem == expected_mem

    @pytest.mark.parametrize("act_fn", ALL_ACT_FNS)
    @pytest.mark.parametrize("dropout_prob", (None, 0.5))
    @pytest.mark.parametrize("device", DEVICES)
    @pytest.mark.parametrize("d_model", D_MODELS)
    @pytest.mark.parametrize("batch_size", BATCH_SIZES)
    @pytest.mark.parametrize("seq_len", SEQ_LENS)
    def test_mlp_amp(
        self,
        act_fn: nn.Module,
        dropout_prob: Optional[float],
        device: str,
        d_model: int,
        batch_size: int,
        seq_len: int,
    ) -> None:
        """
        Similar story with AMP. The only changes come from the modified dtypes and needing to also
        save references to the low-precision weights in the Linear layers.
        """
        inputs = torch.randn(
            batch_size, seq_len, d_model, requires_grad=True, device=device
        )
        expansion_factor = 4
        mlp = layers.MLP(
            d_model=d_model, act_fn=act_fn, dropout_prob=dropout_prob, device=device
        )
        dtype = torch.bfloat16
        with torch.autocast(device_type=device, dtype=dtype):
            with act_mem.SavedTensorContext(ignored_tensors=mlp.parameters()) as saved:
                _ = mlp(inputs)

        # Compare measured memory against expected
        amp_weight_mem = 2 * expansion_factor * d_model**2 * dtype.itemsize
        first_lin_input_mem = inputs.numel() * dtype.itemsize
        second_lin_input_mem = expansion_factor * inputs.numel() * dtype.itemsize
        # Only some activations require additional activation memory
        activation_input_mem = 0 if act_fn in ZERO_MEM_ACT_FNS else second_lin_input_mem
        dropout_act_mem = (
            0
            if not dropout_prob
            else inputs.numel() * (dtype.itemsize if device == "cpu" else 1)
        )

        expected_mem = (
            amp_weight_mem
            + first_lin_input_mem
            + second_lin_input_mem
            + activation_input_mem
            + dropout_act_mem
        )
        assert (
            saved.saved_tensor_mem == expected_mem
        ), f"Failed on {act_fn=}, {dropout_prob=}"


@pytest.mark.skipif(not torch.cuda.is_available(), reason="cuda not available")
class TestCUDAMemReadings:
    @pytest.mark.parametrize("d_model", D_MODELS)
    @pytest.mark.parametrize("batch_size", BATCH_SIZES)
    @pytest.mark.parametrize("seq_len", SEQ_LENS)
    @pytest.mark.parametrize("act_fn", ALL_ACT_FNS)
    def test_mlp(
        self, d_model: int, batch_size: int, seq_len: int, act_fn: nn.Module
    ) -> None:
        """
        Track saved tensors and allocated memory and verify they agree.
        """

        inputs = torch.randn(batch_size, seq_len, d_model, device="cuda")
        mlp = layers.MLP(d_model=d_model, act_fn=act_fn, device="cuda")

        with act_mem.AllocatedMemContext() as mem, act_mem.SavedTensorContext(
            ignored_tensors=mlp.parameters()
        ) as saved:
            outputs = mlp(inputs)

        # AllocatedMemContext captures the outputs, but not inputs, while SavedTensorContext
        # captures inputs and not outputs. Nevertheless, the readings agree because inputs and
        # outputs are tensors of the same size and `dtype`.
        assert mem.delta["current"] == saved.saved_tensor_mem