"ts/webui/src/static/style/common/trialStatus.css" did not exist on "b2b4f4581deafebdd172c18e91ece060a6d62e47"
tcgen5_meta.h 5.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef TVM_TL_OP_TCGEN5_META_H_
#define TVM_TL_OP_TCGEN5_META_H_

#include <cstdint>
#include <tvm/runtime/data_type.h>
#include <tvm/runtime/logging.h>

#include <utility>
#include <vector>

namespace tvm {
namespace tl {

using runtime::DataType;

struct TCGEN5MMAMeta {
  int atom_m, atom_n, atom_k;
18
  bool enable_ws, enable_2cta;
19
20
21
22
23
24
25
};

inline std::pair<bool, TCGEN5MMAMeta>
GetTCGEN5MMAMeta(int M, int N, int K, DataType ab_dtype, DataType c_dtype) {
// TODO (lei) Currently not all shapes / dtypes are supported for TCGEN5MMA.
#define FAIL                                                                   \
  return {                                                                     \
26
27
28
29
30
    false, TCGEN5MMAMeta { 0, 0, 0, false, false }                             \
  }
#define SUCCESS(atom_m, atom_n, atom_k, use_ws, use_2cta)                      \
  return {                                                                     \
    true, TCGEN5MMAMeta { atom_m, atom_n, atom_k, use_ws, use_2cta }           \
31
32
33
34
35
36
37
38
39
  }
  std::vector<int> ws_valid_atom_ns = {256, 128, 64};
  if ((ab_dtype.is_bfloat16() || ab_dtype.is_float16()) &&
      (c_dtype.is_float() && c_dtype.bits() == 32)) {
    if (K % 16 != 0)
      FAIL;
    if (M % 128 == 0) {
      for (int atom_n = 256; atom_n >= 16; atom_n -= 16)
        if (N % atom_n == 0)
40
          SUCCESS(128, atom_n, 16, false, false);
41
42
43
44
      FAIL;
    } else if (M % 64 == 0) {
      for (int atom_n : ws_valid_atom_ns)
        if (N % atom_n == 0)
45
          SUCCESS(64, atom_n, 16, true, false);
46
47
48
49
      FAIL;
    } else if (M % 32 == 0) {
      for (int atom_n : ws_valid_atom_ns)
        if (N % atom_n == 0)
50
          SUCCESS(32, atom_n, 16, true, false);
51
52
53
54
      FAIL;
    } else {
      FAIL;
    }
55
56
57
58
59
60
  } else if ((ab_dtype.is_float8_e4m3fn() || ab_dtype.is_float8_e4m3() ||
              ab_dtype.is_float8_e5m2() || ab_dtype.is_float8_e5m2fnuz() ||
              ab_dtype.is_float6_e2m3fn() || ab_dtype.is_float6_e3m2fn() ||
              ab_dtype.is_float4_e2m1fn()) &&
             ((c_dtype.is_float() && c_dtype.bits() == 32) ||
              (c_dtype.is_float16() && c_dtype.bits() == 16))) {
61
62
63
    if (K % 32 != 0)
      FAIL;
    if (M % 128 == 0) {
64
65
66
      for (int atom_n : ws_valid_atom_ns)
        if (N % atom_n == 0)
          SUCCESS(128, atom_n, 32, true, false);
67
68
      for (int atom_n = 256; atom_n >= 16; atom_n -= 16)
        if (N % atom_n == 0)
69
70
71
72
          SUCCESS(128, atom_n, 32, false, true);
      for (int atom_n = 256; atom_n >= 8; atom_n -= 8)
        if (N % atom_n == 0)
          SUCCESS(128, atom_n, 32, false, false);
73
74
75
76
      FAIL;
    } else if (M % 64 == 0) {
      for (int atom_n : ws_valid_atom_ns)
        if (N % atom_n == 0)
77
78
79
80
          SUCCESS(64, atom_n, 32, true, false);
      for (int atom_n = 256; atom_n >= 8; atom_n -= 8)
        if (N % atom_n == 0)
          SUCCESS(64, atom_n, 32, false, false);
81
82
83
84
      FAIL;
    } else if (M % 32 == 0) {
      for (int atom_n : ws_valid_atom_ns)
        if (N % atom_n == 0)
85
          SUCCESS(32, atom_n, 32, true, false);
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
      FAIL;
    } else {
      FAIL;
    }
  }
  FAIL;
#undef FAIL
#undef SUCCESS
}

inline uint32_t GetTCGEN5InstrDesc(int atom_m, int atom_n, int atom_k,
                                   DataType ab_dtype, DataType c_dtype,
                                   bool a_is_k_major, bool b_is_k_major,
                                   int scale_in_a, int scale_in_b) {
  ICHECK(atom_m % 16 == 0) << "atom_m must be divisible by 16";
  ICHECK(atom_n % 8 == 0) << "atom_n must be divisible by 8";
  ICHECK(atom_k == 16 || atom_k == 32)
      << "Unsupported atom_k for TCGEN5MMA descriptor: " << atom_k;
  ICHECK(scale_in_a == 1 || scale_in_a == -1)
      << "scale_in_a must be +/-1 for TCGEN5MMA";
  ICHECK(scale_in_b == 1 || scale_in_b == -1)
      << "scale_in_b must be +/-1 for TCGEN5MMA";

  auto encode_dtype = [&](DataType dtype) -> uint32_t {
    if (dtype.is_float16()) {
      return static_cast<uint32_t>(0);
    } else if (dtype.is_bfloat16()) {
      return static_cast<uint32_t>(1);
    } else if (dtype.is_float8_e4m3fn() || dtype.is_float8_e4m3fnuz() ||
               dtype.is_float8_e4m3()) {
      return static_cast<uint32_t>(0);
    } else if (dtype.is_float8_e5m2fnuz() || dtype.is_float8_e5m2()) {
      return static_cast<uint32_t>(1);
    }
    LOG(FATAL) << "Unsupported dtype for TCGEN5MMA descriptor: " << dtype;
    return 0u;
  };

  uint32_t a_format = encode_dtype(ab_dtype);
  uint32_t b_format = a_format;

  uint32_t c_format = 0;
  if (c_dtype.is_float16()) {
    c_format = 0;
  } else if (c_dtype.is_float()) {
    c_format = 1;
  } else if (c_dtype.is_int()) {
    c_format = 2;
  } else {
    LOG(FATAL) << "Unsupported accumulator dtype for TCGEN5MMA descriptor: "
               << c_dtype;
  }

  auto set_bits = [](uint32_t value, int start, int width) -> uint32_t {
    uint32_t mask = (width == 32) ? 0xFFFFFFFFu : ((1u << width) - 1);
    return (value & mask) << start;
  };

  uint32_t desc = 0;
  desc |= set_bits(0, 0, 2); // sparse_id2
  desc |= set_bits(0, 2, 1); // sparse_flag
  desc |= set_bits(0, 3, 1); // saturate
  desc |= set_bits(c_format, 4, 2);

  desc |= set_bits(a_format, 7, 3);
  desc |= set_bits(b_format, 10, 3);

  uint32_t a_neg = (scale_in_a == -1) ? 1u : 0u;
  uint32_t b_neg = (scale_in_b == -1) ? 1u : 0u;
  desc |= set_bits(a_neg, 13, 1);
  desc |= set_bits(b_neg, 14, 1);

  uint32_t a_major = a_is_k_major ? 0u : 1u;
  uint32_t b_major = b_is_k_major ? 0u : 1u;
  desc |= set_bits(a_major, 15, 1);
  desc |= set_bits(b_major, 16, 1);

  uint32_t n_dim = static_cast<uint32_t>(atom_n >> 3);
  uint32_t m_dim = static_cast<uint32_t>(atom_m >> 4);
  desc |= set_bits(n_dim, 17, 6);
  desc |= set_bits(0, 23, 1);
  desc |= set_bits(m_dim, 24, 5);
  desc |= set_bits(0, 29, 1);

  uint32_t max_shift = 0u;
  desc |= set_bits(max_shift, 30, 2);

  return desc;
}

} // namespace tl
} // namespace tvm

#endif // TVM_TL_OP_TCGEN5_META_H_