"backends/v2/src/client/sharded_client.rs" did not exist on "beb552127a1547081a24263d110ba6f47c6ac72a"
mlp_decomp_autocast.py 5.16 KB
Newer Older
Mitchell Wortsman's avatar
Mitchell Wortsman committed
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

import torch
import json
from bitsandbytes.nn.triton_based_modules import SwitchBackGlobalMLP, SwitchBackGlobalLinear, MyLinear
import time

if __name__ == '__main__':
    
    print('Startin')


    for dim in [1024, 1280, 1408, 1664, 2048]:
        for batch in [2**14, 2**15, 2**16, 2**17]:

            if dim != 4096 or batch != 2**17:
                continue
        
            
            x1 = torch.randn(batch, dim).cuda().requires_grad_(True)
            d = 2

            standard = torch.nn.Sequential(
                torch.nn.Linear(dim, 4 * dim),
                torch.nn.GELU(),
                torch.nn.Linear(4 * dim, dim),
            ).cuda()

            my_standard = torch.nn.Sequential(
                MyLinear(dim, 4 * dim),
                torch.nn.GELU(),
                MyLinear(4 * dim, dim),
            ).cuda()

            fused_mlp = SwitchBackGlobalMLP(dim, 4 * dim).cuda()

            sb = torch.nn.Sequential(
                SwitchBackGlobalLinear(dim, 4 * dim),
                torch.nn.GELU(),
                SwitchBackGlobalLinear(4 * dim, dim),
            ).cuda()
            
            standard_compiled = torch.compile(standard)

            print('Model part 2')

            repeat = 32
            

            info = {'repeat' : repeat, 'batch_size' : batch, 'dim' : dim}

            # k = 'standard'
            # for _ in range(repeat // 2):
            #     with torch.cuda.amp.autocast():
            #         out_standard = standard(x1)
            #     ((2 ** 16) * out_standard).abs().mean().backward()

            # torch.cuda.synchronize()
            # start = time.time()
            # for _ in range(repeat):
            #     with torch.cuda.amp.autocast():
            #         out_standard = standard(x1)
            #     ((2 ** 16) * out_standard).abs().mean().backward()

            # torch.cuda.synchronize()
            # end = time.time()
            # ms = (end - start) / repeat * 1000
            # print(f"time {k}: {ms:.3f} ms")
            # info[k] = ms


            # x1.grad.zero_()
            
            # k = 'my_standard'
            # for _ in range(repeat // 2):
            #     with torch.cuda.amp.autocast():
            #         out_my_standard = my_standard(x1)
            #     ((2 ** 16) * out_my_standard).abs().mean().backward()

            # torch.cuda.synchronize()
            # start = time.time()
            # for _ in range(repeat):
            #     with torch.cuda.amp.autocast():
            #         out_my_standard = my_standard(x1)
            #     ((2 ** 16) * out_my_standard).abs().mean().backward()

            # torch.cuda.synchronize()
            # end = time.time()
            # ms = (end - start) / repeat * 1000
            # print(f"time {k}: {ms:.3f} ms")
            # info[k] = ms

            # x1.grad.zero_()

            # k = 'standard_compiled'
            # for _ in range(repeat // 2):
            #     with torch.cuda.amp.autocast():
            #         out_standard_compiled = standard_compiled(x1)
            #     ((2 ** 16) * out_standard_compiled).abs().mean().backward()

            # torch.cuda.synchronize()
            # start = time.time()
            # for _ in range(repeat):
            #     with torch.cuda.amp.autocast():
            #         out_standard_compiled = standard_compiled(x1)
            #     ((2 ** 16) * out_standard_compiled).abs().mean().backward()

            # torch.cuda.synchronize()
            # end = time.time()
            # ms = (end - start) / repeat * 1000
            # print(f"time {k}: {ms:.3f} ms")
            # info[k] = ms

            # x1.grad.zero_()

            k = 'sb'
            for _ in range(repeat // 2):
                with torch.cuda.amp.autocast():
                    out_sb = sb(x1)
                ((2 ** 16) * out_sb).abs().mean().backward()

            torch.cuda.synchronize()
            start = time.time()
            for _ in range(repeat):
                with torch.cuda.amp.autocast():
                    out_sb = sb(x1)
                ((2 ** 16) * out_sb).abs().mean().backward()

            torch.cuda.synchronize()
            end = time.time()
            ms = (end - start) / repeat * 1000
            print(f"time {k}: {ms:.3f} ms")
            info[k] = ms


            info_json = json.dumps(info)


            with open("tests/triton_tests/info_mlp_autocast.jsonl", "a") as file:
                file.write(info_json + "\n")


        #exit()

    # err_fused = (out_standard - out_fused).abs().mean()
    # err_sb = (out_standard - out_sb).abs().mean()
    # print('OUT', err_fused, err_sb)

    # err_fused = (standard[d].weight.grad - fused_mlp.linear2.weight.grad).abs().mean()
    # err_sb = (standard[d].weight.grad - sb[d].weight.grad).abs().mean()

    # print('GW2', err_fused, err_sb)

    # err_fused = (standard[0].weight.grad - fused_mlp.linear1.weight.grad).abs().mean()
    # err_sb = (standard[0].weight.grad - sb[0].weight.grad).abs().mean()

    # print('GW1', err_fused, err_sb)

    # err_fused = (x1.grad - x2.grad).abs().mean()
    # err_sb = (x1.grad - x3.grad).abs().mean()

    # print('GX1', err_fused, err_sb)

    # import pdb; pdb.set_trace()


    # # NO GELU, ST GRADIENTS, EVERYTHING FINE.