|
| 1 | +import torch |
| 2 | +import shark_turbine.kernel as tk |
| 3 | +import shark_turbine.kernel.lang as tkl |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +FLOAT_DTYPES = [tkl.f16, tkl.f32, tkl.f64] |
| 8 | +INT_DTYPES = [ |
| 9 | + tkl.bool, |
| 10 | + tkl.i8, |
| 11 | + tkl.i16, |
| 12 | + tkl.i32, |
| 13 | + tkl.i64, |
| 14 | +] |
| 15 | + |
| 16 | + |
| 17 | +def rms_norm_krnl(dtype, input, weight, output): |
| 18 | + M = tkl.sym.M |
| 19 | + K = tkl.sym.K |
| 20 | + |
| 21 | + @tk.gen.thread(M) |
| 22 | + def rms_norm_kernel( |
| 23 | + input: tkl.OutputBuffer[M, K, dtype], |
| 24 | + weight: tk.lang.InputBuffer[M, K, dtype], |
| 25 | + output: tk.lang.OutputBuffer[M, K, dtype], |
| 26 | + ): |
| 27 | + row_index = tk.lang.program_id(0) |
| 28 | + eps = tkl.constant((1,), dtype, 0.00001) |
| 29 | + zero = tkl.constant((1,), dtype, 0.0) |
| 30 | + input_row = input[row_index, :] |
| 31 | + sq_inp = input_row * input_row |
| 32 | + sq_inp_red = tkl.sum(sq_inp) |
| 33 | + # TODO: The input_row * zero is just dummy computation to pass in the right shapes, |
| 34 | + # otherwise it leads to 'error: unknown: 'math.exp2' op operand #0 must be floating-point-like, but got 'vector<f16>' |
| 35 | + denom = tkl.rsqrt(input_row * zero + sq_inp_red) |
| 36 | + denom_eta = denom + eps |
| 37 | + output[row_index, :] = denom_eta * input_row * weight[row_index, :] |
| 38 | + |
| 39 | + with tk.gen.TestLaunchContext(): |
| 40 | + rms_norm_kernel(input, weight, output) |
| 41 | + |
| 42 | + |
| 43 | +def iota_krnl(dtype, input): |
| 44 | + M = tkl.sym.M |
| 45 | + |
| 46 | + @tk.gen.thread(M) |
| 47 | + def iota_kernel(out: tkl.OutputBuffer[M, dtype]): |
| 48 | + a = ( |
| 49 | + tkl.constant((17, 37, 19), dtype, 5) |
| 50 | + if dtype in INT_DTYPES |
| 51 | + else tkl.constant((17, 37, 19), dtype, 5.0) |
| 52 | + ) |
| 53 | + b = ( |
| 54 | + tkl.constant((17, 37, 19), dtype, 10) |
| 55 | + if dtype in INT_DTYPES |
| 56 | + else tkl.constant((17, 37, 19), dtype, 10.0) |
| 57 | + ) |
| 58 | + c = ( |
| 59 | + tkl.constant((17, 37, 19), dtype, 2) |
| 60 | + if dtype in INT_DTYPES |
| 61 | + else tkl.constant((17, 37, 19), dtype, 2.0) |
| 62 | + ) |
| 63 | + if dtype in INT_DTYPES: |
| 64 | + c = (a * b) // c |
| 65 | + else: |
| 66 | + c = (a * b) / c |
| 67 | + c = c + a - b |
| 68 | + |
| 69 | + with tk.gen.TestLaunchContext(): |
| 70 | + iota_kernel(input) |
| 71 | + |
| 72 | + |
| 73 | +def softmax_krnl(dtype, input, output): |
| 74 | + M = tkl.sym.M |
| 75 | + K = tkl.sym.K |
| 76 | + |
| 77 | + @tk.gen.thread(M) |
| 78 | + def softmax_kernel( |
| 79 | + input: tk.lang.InputBuffer[M, K, dtype], |
| 80 | + output: tk.lang.OutputBuffer[M, K, dtype], |
| 81 | + ): |
| 82 | + row_index = tk.lang.program_id(0) |
| 83 | + input_row = input[row_index, :] |
| 84 | + numerator = tkl.exp2(input_row - tkl.max(input_row)) |
| 85 | + if dtype in INT_DTYPES: |
| 86 | + output_row = numerator // tkl.sum(numerator) |
| 87 | + else: |
| 88 | + output_row = numerator / tkl.sum(numerator) |
| 89 | + output[row_index, :] = output_row |
| 90 | + |
| 91 | + with tk.gen.TestLaunchContext(): |
| 92 | + softmax_kernel(input, output) |
| 93 | + |
| 94 | + |
| 95 | +def gemm_fx_kernel(dtype, A, B, output): |
| 96 | + N = tkl.sym.N |
| 97 | + M = tkl.sym.M |
| 98 | + K = tkl.sym.K |
| 99 | + BLOCK_SIZE = tkl.sym.BLOCK_SIZE |
| 100 | + |
| 101 | + @tk.gen.thread(N // BLOCK_SIZE, M // BLOCK_SIZE) |
| 102 | + def gemm_kernel( |
| 103 | + A: tkl.InputBuffer[N, K, dtype], |
| 104 | + B: tkl.InputBuffer[K, M, dtype], |
| 105 | + output: tkl.OutputBuffer[N, M, dtype], |
| 106 | + ): |
| 107 | + grid_n = tkl.program_id(0) |
| 108 | + grid_m = tkl.program_id(1) |
| 109 | + |
| 110 | + acc = None |
| 111 | + # TODO: Only considering the float and integer cases. |
| 112 | + if dtype in INT_DTYPES: |
| 113 | + acc = tkl.constant((BLOCK_SIZE, BLOCK_SIZE), dtype, 0) |
| 114 | + else: |
| 115 | + acc = tkl.constant((BLOCK_SIZE, BLOCK_SIZE), dtype, 0.0) |
| 116 | + |
| 117 | + @tkl.for_loop(0, K // BLOCK_SIZE, init_args=[acc]) |
| 118 | + def body(i, c): |
| 119 | + a = tkl.load(A, (grid_n, i * BLOCK_SIZE), (BLOCK_SIZE, BLOCK_SIZE)) |
| 120 | + b = tkl.load(B, (i * BLOCK_SIZE, grid_m), (BLOCK_SIZE, BLOCK_SIZE)) |
| 121 | + return (tkl.dot(a, b, c),) |
| 122 | + |
| 123 | + tkl.store(output, (grid_n, grid_m), body[0]) |
| 124 | + |
| 125 | + with tk.gen.TestLaunchContext({BLOCK_SIZE: 32}): |
| 126 | + gemm_kernel(A, B, output) |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize( |
| 130 | + ("dtype",), |
| 131 | + [(x,) for x in FLOAT_DTYPES + INT_DTYPES], |
| 132 | +) |
| 133 | +def test_iota_krnl(dtype): |
| 134 | + input = torch.zeros(17) |
| 135 | + iota_krnl(dtype, input) |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize( |
| 139 | + ("dtype",), |
| 140 | + [(x,) for x in FLOAT_DTYPES], |
| 141 | +) |
| 142 | +def test_rms_norm_krnl(dtype): |
| 143 | + input = torch.randn(128, 64).to(dtype.to_torch_type()) |
| 144 | + weight = torch.randn(128, 64).to(dtype.to_torch_type()) |
| 145 | + output = torch.randn(128, 64).to(dtype.to_torch_type()) |
| 146 | + rms_norm_krnl(dtype, input, weight, output) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize( |
| 150 | + ("dtype",), |
| 151 | + [(x,) for x in FLOAT_DTYPES], |
| 152 | +) |
| 153 | +def test_softmax_krnl(dtype): |
| 154 | + input = torch.randn(128, 64).to(dtype.to_torch_type()) |
| 155 | + output = torch.randn(128, 64).to(dtype.to_torch_type()) |
| 156 | + softmax_krnl(dtype, input, output) |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.parametrize( |
| 160 | + ("dtype",), |
| 161 | + [(x,) for x in FLOAT_DTYPES + INT_DTYPES], |
| 162 | +) |
| 163 | +def test_gemm_krnl(dtype): |
| 164 | + A = torch.randn(512, 1024).to(dtype.to_torch_type()) |
| 165 | + B = torch.randn(1024, 2048).to(dtype.to_torch_type()) |
| 166 | + output = torch.zeros(512, 2048).to(dtype.to_torch_type()) |
| 167 | + gemm_fx_kernel(dtype, A, B, output) |
0 commit comments