test_fused_dense.py 6.16 KB
Newer Older
1
import math
2
from functools import partial
3

Tri Dao's avatar
Tri Dao committed
4
import pytest
5
6
7
import torch
import torch.nn.functional as F
from einops import rearrange
8
from flash_attn.ops.fused_dense import FusedDense, FusedMLP
9
10


Tri Dao's avatar
Tri Dao committed
11
12
13
14
15
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
@pytest.mark.parametrize("return_residual", [False, True])
@pytest.mark.parametrize("has_bias", [True, False])
@pytest.mark.parametrize("out_features", [1024, 4096])
@pytest.mark.parametrize("in_features", [1024, 4096])
Tri Dao's avatar
Tri Dao committed
16
def test_fused_linear_bias(in_features, out_features, has_bias, return_residual, dtype):
Tri Dao's avatar
Tri Dao committed
17
    device = "cuda"
18
19
20
21
22
    rtol, atol = (3e-3, 1e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3)
    # set seed
    torch.random.manual_seed(0)
    batch_size = 8
    seqlen = 512
Tri Dao's avatar
Tri Dao committed
23
24
25
    x_pt = torch.randn(
        batch_size, seqlen, in_features, device=device, dtype=dtype, requires_grad=True
    )
26
    x = x_pt.detach().clone().requires_grad_()
Tri Dao's avatar
Tri Dao committed
27
    model_pt = torch.nn.Linear(in_features, out_features, bias=has_bias, device=device, dtype=dtype)
Tri Dao's avatar
Tri Dao committed
28
29
30
31
32
33
34
35
    model = FusedDense(
        in_features,
        out_features,
        bias=has_bias,
        return_residual=return_residual,
        device=device,
        dtype=dtype,
    )
36
37
    with torch.no_grad():
        model.weight.copy_(model_pt.weight)
Tri Dao's avatar
Tri Dao committed
38
39
        if has_bias:
            model.bias.copy_(model_pt.bias)
40
    out_pt = model_pt(x_pt)
Tri Dao's avatar
Tri Dao committed
41
42
43
44
    if not return_residual:
        out = model(x)
    else:
        out, x_copy = model(x)
Tri Dao's avatar
Tri Dao committed
45
46
47
48
49
50
51
52
53
54
        x_copy = (
            x_copy[..., :out_features]
            if out_features < in_features
            else F.pad(x_copy, (0, out_features - in_features))
        )
        x_pt_copy = (
            x_pt[..., :out_features]
            if out_features < in_features
            else F.pad(x_pt, (0, out_features - in_features))
        )
Tri Dao's avatar
Tri Dao committed
55
56
57
58
        # Just add some random function of the residual
        out_pt = out_pt + F.gelu(x_pt_copy)
        out = out + F.gelu(x_copy)

59
60
61
62
63
64
65
66
67
68
69
    # with torch.no_grad():
    #     out_fl = F.linear(x_pt.float(), model.weight.float(), model.bias.float()).half()
    assert torch.allclose(out, out_pt, rtol=rtol, atol=atol)

    # If we don't divide by batch_size, the gradient gets a bit too large.
    g = torch.randn_like(out) / 32
    out_pt.backward(g)
    out.backward(g)
    assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)
    # The error for d_weight and d_bias is quite a bit higher
    assert torch.allclose(model.weight.grad, model_pt.weight.grad, rtol=rtol, atol=atol * 10)
Tri Dao's avatar
Tri Dao committed
70
71
    if has_bias:
        assert torch.allclose(model.bias.grad, model_pt.bias.grad, rtol=rtol, atol=atol * 5)
72
73


Tri Dao's avatar
Tri Dao committed
74
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
75
# @pytest.mark.parametrize('dtype', [torch.float16])
Tri Dao's avatar
Tri Dao committed
76
@pytest.mark.parametrize("heuristic", ["auto", -1])
77
# @pytest.mark.parametrize('heuristic', ['auto'])
Tri Dao's avatar
Tri Dao committed
78
@pytest.mark.parametrize("checkpoint_lvl", [0, 1, 2])
79
# @pytest.mark.parametrize('checkpoint_lvl', [1])
Tri Dao's avatar
Tri Dao committed
80
@pytest.mark.parametrize("return_residual", [False, True])
81
# @pytest.mark.parametrize('return_residual', [False])
Tri Dao's avatar
Tri Dao committed
82
83
@pytest.mark.parametrize("has_bias2", [True, False])
@pytest.mark.parametrize("has_bias1", [True, False])
84
85
# @pytest.mark.parametrize('has_bias2', [True])
# @pytest.mark.parametrize('has_bias1', [True])
Tri Dao's avatar
Tri Dao committed
86
@pytest.mark.parametrize("activation", ["gelu_approx", "relu"])
87
# @pytest.mark.parametrize('activation', ['relu'])
Tri Dao's avatar
Tri Dao committed
88
89
@pytest.mark.parametrize("out_features", [1024, 4096])
@pytest.mark.parametrize("in_features", [1024, 4096])
90
91
# @pytest.mark.parametrize('out_features', [4096])
# @pytest.mark.parametrize('in_features', [1024])
Tri Dao's avatar
Tri Dao committed
92
93
94
95
96
97
98
99
100
101
102
103
def test_fused_mlp(
    in_features,
    out_features,
    activation,
    has_bias1,
    has_bias2,
    return_residual,
    checkpoint_lvl,
    heuristic,
    dtype,
):
    device = "cuda"
Tri Dao's avatar
Tri Dao committed
104
    rtol, atol = (3e-3, 3e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3)
105
106
107
108
    # set seed
    torch.random.manual_seed(0)
    batch_size = 8
    seqlen = 512
Tri Dao's avatar
Tri Dao committed
109
110
111
    x_pt = torch.randn(
        batch_size, seqlen, in_features, device=device, dtype=dtype, requires_grad=True
    )
112
    x = x_pt.detach().clone().requires_grad_()
Tri Dao's avatar
Tri Dao committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    model_pt_fc1 = torch.nn.Linear(
        in_features, out_features, bias=has_bias1, device=device, dtype=dtype
    )
    model_pt_fc2 = torch.nn.Linear(
        out_features, in_features, bias=has_bias2, device=device, dtype=dtype
    )
    model = FusedMLP(
        in_features,
        out_features,
        in_features,
        activation=activation,
        bias1=has_bias1,
        bias2=has_bias2,
        return_residual=return_residual,
        checkpoint_lvl=checkpoint_lvl,
        heuristic=heuristic,
        device=device,
        dtype=dtype,
    )
132
133
    with torch.no_grad():
        model.fc1.weight.copy_(model_pt_fc1.weight)
Tri Dao's avatar
Tri Dao committed
134
135
        if has_bias1:
            model.fc1.bias.copy_(model_pt_fc1.bias)
136
        model.fc2.weight.copy_(model_pt_fc2.weight)
Tri Dao's avatar
Tri Dao committed
137
138
        if has_bias2:
            model.fc2.bias.copy_(model_pt_fc2.bias)
Tri Dao's avatar
Tri Dao committed
139
140
141
142
143
    activation_fn = (
        partial(F.gelu, approximate="tanh")
        if activation == "gelu_approx"
        else partial(F.relu, inplace=True)
    )
144
    out_pt = model_pt_fc2(activation_fn(model_pt_fc1(x_pt)))
Tri Dao's avatar
Tri Dao committed
145
146
147
148
149
150
151
    if not return_residual:
        out = model(x)
    else:
        out, x_copy = model(x)
        # Just add some random function of the residual
        out_pt = out_pt + F.gelu(x_pt)
        out = out + F.gelu(x_copy)
152
153
154
155
156
157
    assert torch.allclose(out, out_pt, rtol=rtol, atol=atol)

    # If we don't divide by batch_size, the gradient gets a bit too large.
    g = torch.randn_like(out) / 32
    out_pt.backward(g)
    out.backward(g)
158
    # The error for relu is higher still
Tri Dao's avatar
Tri Dao committed
159
    if activation == "relu":
160
        atol = 1e-1 if dtype == torch.bfloat16 else 5e-2
161
162
    assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)
    # The error for d_weight and d_bias is quite a bit higher
Tri Dao's avatar
Tri Dao committed
163
164
165
    assert torch.allclose(
        model.fc1.weight.grad, model_pt_fc1.weight.grad, rtol=rtol, atol=atol * 10
    )
Tri Dao's avatar
Tri Dao committed
166
167
    if has_bias1:
        assert torch.allclose(model.fc1.bias.grad, model_pt_fc1.bias.grad, rtol=rtol, atol=atol * 5)
Tri Dao's avatar
Tri Dao committed
168
169
170
    assert torch.allclose(
        model.fc2.weight.grad, model_pt_fc2.weight.grad, rtol=rtol, atol=atol * 10
    )
Tri Dao's avatar
Tri Dao committed
171
172
    if has_bias2:
        assert torch.allclose(model.fc2.bias.grad, model_pt_fc2.bias.grad, rtol=rtol, atol=atol * 5)