config.cpp 8.79 KB
Newer Older
1
/*************************************************************************
2
 * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
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
 *
 * See LICENSE for license information.
 ************************************************************************/

#include "./config.h"

#include <transformer_engine/gemm.h>
#include <transformer_engine/transformer_engine.h>

#include <cstring>

#include "../util/logging.h"

NVTEMatmulConfig nvte_create_matmul_config() { return new transformer_engine::MatmulConfig; }

void nvte_get_matmul_config_attribute(NVTEMatmulConfig config, NVTEMatmulConfigAttribute attr,
                                      void *buf, size_t size_in_bytes, size_t *size_written) {
  // Write attribute size
  NVTE_CHECK(attr < kNVTEMatmulConfigNumAttributes, "Invalid NVTEMatmulConfigAttribute (got ",
             static_cast<int>(attr), ")");
  NVTE_CHECK(size_written != nullptr, "Invalid size_written (got NULL)");
  const auto &attr_size = transformer_engine::MatmulConfig::attr_sizes[attr];
  *size_written = attr_size;

  // Return immediately if buffer is not provided
  if (buf == nullptr) {
    return;
  }

  // Check buffer size
  NVTE_CHECK(size_in_bytes >= attr_size,
             "Buffer is too small for matmul config attribute "
             "(attribute ",
             static_cast<int>(attr), " needs ", attr_size, " bytes, but buffer has ", size_in_bytes,
             " bytes)");

39
40
41
42
43
44
  // bool size is implementation-dependent, so we explicitly specify
  // uint8_t in the user-facing API.
  auto bool_to_uint8 = [](bool in, void *out) {
    *reinterpret_cast<uint8_t *>(out) = static_cast<uint8_t>(in);
  };

45
46
47
48
49
50
51
52
53
54
55
  // Write to buffer
  NVTE_CHECK(config != nullptr, "Invalid NVTEMatmulConfig (got NULL)");
  const auto &config_ = *reinterpret_cast<const transformer_engine::MatmulConfig *>(config);
  switch (attr) {
    case kNVTEMatmulConfigBiasTensor:
      std::memcpy(buf, &config_.bias_tensor, attr_size);
      break;
    case kNVTEMatmulConfigDBiasTensor:
      std::memcpy(buf, &config_.dbias_tensor, attr_size);
      break;
    case kNVTEMatmulConfigWithGELUEpilogue:
56
      bool_to_uint8(config_.with_gelu_epilogue, buf);
57
58
      break;
    case kNVTEMatmulConfigWithDGELUEpilogue:
59
      bool_to_uint8(config_.with_dgelu_epilogue, buf);
60
61
62
63
64
      break;
    case kNVTEMatmulConfigEpilogueAuxTensor:
      std::memcpy(buf, &config_.epilogue_aux_tensor, attr_size);
      break;
    case kNVTEMatmulConfigUseSplitAccumulator:
65
      bool_to_uint8(config_.use_split_accumulator, buf);
66
67
      break;
    case kNVTEMatmulConfigSMCount:
68
      *reinterpret_cast<int32_t *>(buf) = static_cast<int32_t>(config_.sm_count);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
      break;
    default:
      NVTE_ERROR("Unsupported NVTEMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  }
}

void nvte_set_matmul_config_attribute(NVTEMatmulConfig config, NVTEMatmulConfigAttribute attr,
                                      const void *buf, size_t size_in_bytes) {
  // Check attribute and buffer
  NVTE_CHECK(attr < kNVTEMatmulConfigNumAttributes, "Invalid NVTEMatmulConfigAttribute (got ",
             static_cast<int>(attr), ")");
  const auto &attr_size = transformer_engine::MatmulConfig::attr_sizes[attr];
  NVTE_CHECK(size_in_bytes >= attr_size,
             "Buffer is too small for matmul config attribute "
             "(attribute ",
             static_cast<int>(attr), " needs ", attr_size, " bytes, but buffer has ", size_in_bytes,
             " bytes)");
  NVTE_CHECK(buf != nullptr, "Invalid buffer (got NULL)");

88
89
90
91
92
93
  // bool size is implementation-dependent, so we explicitly specify
  // uint8_t in the user-facing API.
  auto uint8_to_bool = [](const void *in, bool &out) {
    out = static_cast<bool>(*reinterpret_cast<const uint8_t *>(in));
  };

94
95
96
97
98
99
100
101
102
103
104
  // Read from buffer
  NVTE_CHECK(config != nullptr, "Invalid NVTEMatmulConfig (got NULL)");
  auto &config_ = *reinterpret_cast<transformer_engine::MatmulConfig *>(config);
  switch (attr) {
    case kNVTEMatmulConfigBiasTensor:
      std::memcpy(&config_.bias_tensor, buf, attr_size);
      break;
    case kNVTEMatmulConfigDBiasTensor:
      std::memcpy(&config_.dbias_tensor, buf, attr_size);
      break;
    case kNVTEMatmulConfigWithGELUEpilogue:
105
      uint8_to_bool(buf, config_.with_gelu_epilogue);
106
107
      break;
    case kNVTEMatmulConfigWithDGELUEpilogue:
108
      uint8_to_bool(buf, config_.with_dgelu_epilogue);
109
110
111
112
113
      break;
    case kNVTEMatmulConfigEpilogueAuxTensor:
      std::memcpy(&config_.epilogue_aux_tensor, buf, attr_size);
      break;
    case kNVTEMatmulConfigUseSplitAccumulator:
114
      uint8_to_bool(buf, config_.use_split_accumulator);
115
116
      break;
    case kNVTEMatmulConfigSMCount:
117
      config_.sm_count = static_cast<int>(*reinterpret_cast<const int32_t *>(buf));
118
119
120
121
122
123
124
125
126
127
128
      break;
    default:
      NVTE_ERROR("Unsupported NVTEMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  }
}

void nvte_destroy_matmul_config(NVTEMatmulConfig config) {
  if (config != nullptr) {
    delete reinterpret_cast<transformer_engine::MatmulConfig *>(config);
  }
}
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

NVTEGroupedMatmulConfig nvte_create_grouped_matmul_config() {
  return new transformer_engine::GroupedMatmulConfig;
}

void nvte_get_grouped_matmul_config_attribute(NVTEGroupedMatmulConfig config,
                                              NVTEGroupedMatmulConfigAttribute attr, void *buf,
                                              size_t size_in_bytes, size_t *size_written) {
  // Write attribute size
  NVTE_CHECK(attr < kNVTEGroupedMatmulConfigNumAttributes,
             "Invalid NVTEGroupedMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  NVTE_CHECK(size_written != nullptr, "Invalid size_written (got NULL)");
  const auto &attr_size = transformer_engine::GroupedMatmulConfig::attr_sizes[attr];
  *size_written = attr_size;

  // Return immediately if buffer is not provided
  if (buf == nullptr) {
    return;
  }

  // Check buffer size
  NVTE_CHECK(size_in_bytes >= attr_size,
             "Buffer is too small for grouped matmul config attribute "
             "(attribute ",
             static_cast<int>(attr), " needs ", attr_size, " bytes, but buffer has ", size_in_bytes,
             " bytes)");

  // Write to buffer
  NVTE_CHECK(config != nullptr, "Invalid NVTEGroupedMatmulConfig (got NULL)");
  const auto &config_ = *reinterpret_cast<const transformer_engine::GroupedMatmulConfig *>(config);
  switch (attr) {
    case kNVTEGroupedMatmulConfigAvgM: {
      int64_t val = config_.avg_m.value_or(0);
      std::memcpy(buf, &val, attr_size);
      break;
    }
    case kNVTEGroupedMatmulConfigAvgN: {
      int64_t val = config_.avg_n.value_or(0);
      std::memcpy(buf, &val, attr_size);
      break;
    }
    case kNVTEGroupedMatmulConfigAvgK: {
      int64_t val = config_.avg_k.value_or(0);
      std::memcpy(buf, &val, attr_size);
      break;
    }
    case kNVTEGroupedMatmulConfigSMCount:
      std::memcpy(buf, &config_.sm_count, attr_size);
      break;
    default:
      NVTE_ERROR("Unsupported NVTEGroupedMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  }
}

void nvte_set_grouped_matmul_config_attribute(NVTEGroupedMatmulConfig config,
                                              NVTEGroupedMatmulConfigAttribute attr,
                                              const void *buf, size_t size_in_bytes) {
  // Check attribute and buffer
  NVTE_CHECK(attr < kNVTEGroupedMatmulConfigNumAttributes,
             "Invalid NVTEGroupedMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  const auto &attr_size = transformer_engine::GroupedMatmulConfig::attr_sizes[attr];
  NVTE_CHECK(size_in_bytes >= attr_size,
             "Buffer is too small for grouped matmul config attribute "
             "(attribute ",
             static_cast<int>(attr), " needs ", attr_size, " bytes, but buffer has ", size_in_bytes,
             " bytes)");
  NVTE_CHECK(buf != nullptr, "Invalid buffer (got NULL)");

  // Read from buffer
  NVTE_CHECK(config != nullptr, "Invalid NVTEGroupedMatmulConfig (got NULL)");
  auto &config_ = *reinterpret_cast<transformer_engine::GroupedMatmulConfig *>(config);
  switch (attr) {
    case kNVTEGroupedMatmulConfigAvgM: {
      int64_t val;
      std::memcpy(&val, buf, attr_size);
      config_.avg_m = val;
      break;
    }
    case kNVTEGroupedMatmulConfigAvgN: {
      int64_t val;
      std::memcpy(&val, buf, attr_size);
      config_.avg_n = val;
      break;
    }
    case kNVTEGroupedMatmulConfigAvgK: {
      int64_t val;
      std::memcpy(&val, buf, attr_size);
      config_.avg_k = val;
      break;
    }
    case kNVTEGroupedMatmulConfigSMCount:
      std::memcpy(&config_.sm_count, buf, attr_size);
      break;
    default:
      NVTE_ERROR("Unsupported NVTEGroupedMatmulConfigAttribute (got ", static_cast<int>(attr), ")");
  }
}

void nvte_destroy_grouped_matmul_config(NVTEGroupedMatmulConfig config) {
  if (config != nullptr) {
    delete reinterpret_cast<transformer_engine::GroupedMatmulConfig *>(config);
  }
}