|
| 1 | +import functools |
| 2 | + |
| 3 | +import ninetoothed.language as ntl |
| 4 | +from ninetoothed import Symbol, Tensor |
| 5 | + |
| 6 | +import ntops.kernels.mm as mm |
| 7 | + |
| 8 | +STRIDE_H = Symbol("stride_h", constexpr=True) |
| 9 | +STRIDE_W = Symbol("stride_w", constexpr=True) |
| 10 | +DILATION_H = Symbol("dilation_h", constexpr=True) |
| 11 | +DILATION_W = Symbol("dilation_w", constexpr=True) |
| 12 | + |
| 13 | + |
| 14 | +def arrangement( |
| 15 | + input, |
| 16 | + weight, |
| 17 | + bias, |
| 18 | + output, |
| 19 | + stride_h=None, |
| 20 | + stride_w=None, |
| 21 | + dilation_h=None, |
| 22 | + dilation_w=None, |
| 23 | + block_size_m=None, |
| 24 | + block_size_n=None, |
| 25 | + block_size_k=None, |
| 26 | +): |
| 27 | + if stride_h is None: |
| 28 | + stride_h = STRIDE_H |
| 29 | + |
| 30 | + if stride_w is None: |
| 31 | + stride_w = STRIDE_W |
| 32 | + |
| 33 | + if dilation_h is None: |
| 34 | + dilation_h = DILATION_H |
| 35 | + |
| 36 | + if dilation_w is None: |
| 37 | + dilation_w = DILATION_W |
| 38 | + |
| 39 | + if block_size_m is None: |
| 40 | + block_size_m = mm.BLOCK_SIZE_M |
| 41 | + |
| 42 | + if block_size_n is None: |
| 43 | + block_size_n = mm.BLOCK_SIZE_N |
| 44 | + |
| 45 | + if block_size_k is None: |
| 46 | + block_size_k = mm.BLOCK_SIZE_K |
| 47 | + |
| 48 | + mm_arrangement = functools.partial( |
| 49 | + mm.arrangement, |
| 50 | + block_size_m=block_size_m, |
| 51 | + block_size_n=block_size_n, |
| 52 | + block_size_k=block_size_k, |
| 53 | + ) |
| 54 | + |
| 55 | + input_arranged = input.tile( |
| 56 | + (1, *weight.shape[1:]), |
| 57 | + strides=(-1, -1, stride_h, stride_w), |
| 58 | + dilation=(1, 1, dilation_h, dilation_w), |
| 59 | + floor_mode=True, |
| 60 | + ) |
| 61 | + input_arranged = input_arranged.squeeze(1) |
| 62 | + input_arranged.dtype = input_arranged.dtype.squeeze(0) |
| 63 | + input_arranged = input_arranged.ravel() |
| 64 | + input_arranged = input_arranged.flatten(end_dim=3).flatten(start_dim=1) |
| 65 | + |
| 66 | + weight_arranged = weight.flatten(start_dim=1) |
| 67 | + weight_arranged = weight_arranged.permute((1, 0)) |
| 68 | + |
| 69 | + bias_arranged = bias.permute((0, 2, 3, 1)).flatten(end_dim=3) |
| 70 | + |
| 71 | + _, _, bias_arranged = mm_arrangement(input_arranged, weight_arranged, bias_arranged) |
| 72 | + |
| 73 | + output_arranged = output.permute((0, 2, 3, 1)).flatten(end_dim=3) |
| 74 | + |
| 75 | + input_arranged, weight_arranged, output_arranged = mm_arrangement( |
| 76 | + input_arranged, weight_arranged, output_arranged |
| 77 | + ) |
| 78 | + |
| 79 | + return input_arranged, weight_arranged, bias_arranged, output_arranged |
| 80 | + |
| 81 | + |
| 82 | +def application(input, weight, bias, output): |
| 83 | + mm_output = ntl.zeros(output.shape, dtype=ntl.float32) |
| 84 | + mm.application(input, weight, mm_output) |
| 85 | + output = mm_output + bias |
| 86 | + |
| 87 | + |
| 88 | +def premake( |
| 89 | + stride_h=None, |
| 90 | + stride_w=None, |
| 91 | + dilation_h=None, |
| 92 | + dilation_w=None, |
| 93 | + dtype=None, |
| 94 | + block_size_m=None, |
| 95 | + block_size_n=None, |
| 96 | + block_size_k=None, |
| 97 | +): |
| 98 | + arrangement_ = functools.partial( |
| 99 | + arrangement, |
| 100 | + stride_h=stride_h, |
| 101 | + stride_w=stride_w, |
| 102 | + dilation_h=dilation_h, |
| 103 | + dilation_w=dilation_w, |
| 104 | + block_size_m=block_size_m, |
| 105 | + block_size_n=block_size_n, |
| 106 | + block_size_k=block_size_k, |
| 107 | + ) |
| 108 | + |
| 109 | + tensors = tuple(Tensor(4, dtype=dtype) for _ in range(4)) |
| 110 | + |
| 111 | + return arrangement_, application, tensors |
0 commit comments