test_cast_float8blockwise.cu 26.1 KB
Newer Older
1
2
3
4
5
6
/*************************************************************************
 * Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 *
 * See LICENSE for license information.
 ************************************************************************/

wenjh's avatar
wenjh committed
7
8
9
#ifdef __HIP_PLATFORM_AMD__
#include <hip/hip_runtime.h>
#endif
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cuda_bf16.h>
#include <cuda_fp8.h>
#include <cuda_runtime.h>
#include <gtest/gtest.h>
#include <transformer_engine/activation.h>
#include <transformer_engine/cast.h>

#include "../test_common.h"
#include "transformer_engine/transformer_engine.h"

using namespace transformer_engine;
using namespace test;

namespace {

25
26
27
28
29
30
struct QuantizationOptions {
  bool force_pow_2_scales = false;
  float amax_epsilon = 0.0;
  size_t block_scaling_dim = 2u;
};

31
32
33
#ifdef __HIP_PLATFORM_AMD__
size_t kBlockLen = static_cast<size_t>(blockwise_fp8_block_len());
#else
34
constexpr size_t kBlockLen = 128;
35
#endif
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

enum ProcessingMethod {
  CAST_ONLY,
  // CAST_DBIAS,
  // CAST_DBIAS_DACT,
  // CAST_DACT,
  // CAST_ACT
};

enum ActivationType {
  Identity,
  // GeLU,
  // SiLU,
  // ReLU,
  // QGeLU,
  // SReLU
};

template <typename InputType, typename OutputType>
void scales_from_amax(float amax, const QuantizationOptions& opts, float* qscale_out,
                      float* qscale_inv_out) {
  float input_type_max_val = Quantized_Limits<InputType>::max();
  float quant_type_max_val = Quantized_Limits<OutputType>::max();
  float eps = opts.amax_epsilon;
wenjh's avatar
wenjh committed
60
61
62
#ifdef __HIP_PLATFORM_AMD__
  amax = amax > eps? amax: eps;
#else
63
  amax = std::max(amax, eps);
wenjh's avatar
wenjh committed
64
#endif
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
  float qscale = quant_type_max_val / amax;
  if (std::isinf(qscale)) {
    qscale = input_type_max_val;
  }
  if (std::isnan(qscale) || amax == 0) {
    qscale = 1.0;
  }

  if (opts.force_pow_2_scales && qscale != 0.0) {
    uint32_t scale_bits = *reinterpret_cast<uint32_t*>(&qscale);
    // Scale must be positive, shift it
    uint8_t exp = scale_bits >> 23;
    ASSERT_FALSE(exp == 0) << "Subnormals in this path is a logic error.";
    qscale = ldexpf(1.0f, static_cast<int32_t>(exp) - 127);
  }

  float qscale_inv = 1.0 / qscale;
  *qscale_out = qscale;
  *qscale_inv_out = qscale_inv;
}

template <typename InputType, typename OutputType>
void ref_quantize(const ProcessingMethod processing_method, const InputType* input,
                  const std::pair<size_t, size_t>& input_hw, OutputType* output, float* scale_inv,
                  OutputType* output_t, float* scale_inv_t, const QuantizationOptions& opts) {
90
91
92
93
#ifdef __HIP_PLATFORM_AMD__
  size_t kBlockLenX = kBlockLen;
  size_t kBlockLenY = kBlockLen;
#else
94
95
  constexpr size_t kBlockLenX = kBlockLen;
  constexpr size_t kBlockLenY = kBlockLen;
96
#endif
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

  auto quantize_element = [](InputType element, float qscale) -> OutputType {
    // Scale in FP32 and cast result to nearest FP8.
    return static_cast<OutputType>(float(element) * qscale);
  };

  size_t height = input_hw.first;
  size_t width = input_hw.second;
  size_t blocks_x = (width + kBlockLenX - 1) / kBlockLenX;
  size_t blocks_y = (height + kBlockLenY - 1) / kBlockLenY;
  // Find the absolute maximum value in the block
  for (size_t block_x = 0; block_x < blocks_x; ++block_x) {
    for (size_t block_y = 0; block_y < blocks_y; ++block_y) {
      float amax = 0.0f;
      // Calculate amax for a tile.
      for (size_t i = 0; i < kBlockLenX; ++i) {
        for (size_t j = 0; j < kBlockLenY; ++j) {
          size_t x_pos = i + block_x * kBlockLenX;
          size_t y_pos = j + block_y * kBlockLenY;
          if (y_pos >= height || x_pos >= width) {
            continue;
          }
          float val = static_cast<float>(input[y_pos * width + x_pos]);
wenjh's avatar
wenjh committed
120
121
122
          #ifdef __HIP_PLATFORM_AMD__
          amax = amax > std::abs(val)? amax: std::abs(val);
          #else
123
          amax = std::max(amax, std::abs(val));
wenjh's avatar
wenjh committed
124
          #endif
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
        }
      }

      // We've calculated amax for a tile. Calculate scale and
      // scale_inv and populate outputs.
      float qscale, qscale_inv;
      scales_from_amax<InputType, OutputType>(amax, opts, &qscale, &qscale_inv);

      // NOTE: This reference function outputs contigous scale tensors.
      // It calculates a naive scale data format. Strides are handled
      // in comparison.
      if (scale_inv != nullptr) {
        scale_inv[block_y * blocks_x + block_x] = qscale_inv;
      }
      if (scale_inv_t != nullptr) {
        scale_inv_t[block_x * blocks_y + block_y] = qscale_inv;
      }

      for (size_t i = 0; i < kBlockLenX; ++i) {
        for (size_t j = 0; j < kBlockLenY; ++j) {
          size_t x_pos = i + block_x * kBlockLenX;
          size_t y_pos = j + block_y * kBlockLenY;
          if (y_pos >= height || x_pos >= width) {
            continue;
          }
          if (output != nullptr) {
            output[y_pos * width + x_pos] = quantize_element(input[y_pos * width + x_pos], qscale);
          }
          if (output_t != nullptr) {
            output_t[x_pos * height + y_pos] =
                quantize_element(input[y_pos * width + x_pos], qscale);
          }
        }
      }
    }
  }
}

template <typename InputType, typename OutputType>
void ref_quantize_onedimensional_blocks(const ProcessingMethod processing_method,
                                        const InputType* input,
                                        const std::pair<size_t, size_t>& input_hw,
                                        OutputType* output, float* scale_inv, OutputType* output_t,
                                        float* scale_inv_t, const QuantizationOptions& opts) {
  float input_type_max_val = Quantized_Limits<InputType>::max();
  float quant_type_max_val = Quantized_Limits<OutputType>::max();

172
173
174
#ifdef __HIP_PLATFORM_AMD__
  size_t kBlockLenX = kBlockLen;
#else
175
  constexpr size_t kBlockLenX = kBlockLen;
176
#endif
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

  auto quantize_element = [](InputType element, float qscale) -> OutputType {
    // Scale in FP32 and cast result to nearest FP8.
    return static_cast<OutputType>(float(element) * qscale);
  };

  size_t height = input_hw.first;
  size_t width = input_hw.second;
  size_t blocks_x = (width + kBlockLenX - 1) / kBlockLenX;
  size_t blocks_x_t = (height + kBlockLenX - 1) / kBlockLenX;
  if (output != nullptr && scale_inv != nullptr) {
    // Find the absolute maximum value in the block
    for (size_t block_x = 0; block_x < blocks_x; ++block_x) {
      for (size_t y = 0; y < height; ++y) {
        float amax = 0.0f;
        // Calculate amax for a tile.
        for (size_t i = 0; i < kBlockLenX; ++i) {
          size_t x_pos = i + block_x * kBlockLenX;
          if (x_pos >= width) {
            continue;
          }
          float val = static_cast<float>(input[y * width + x_pos]);
wenjh's avatar
wenjh committed
199
200
201
          #ifdef __HIP_PLATFORM_AMD__
          amax = amax > std::abs(val)? amax: std::abs(val);
          #else
202
          amax = std::max(amax, std::abs(val));
wenjh's avatar
wenjh committed
203
          #endif
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
232
233
234
        }

        // We've calculated amax for a tile. Calculate scale and
        // scale_inv and populate outputs.
        float qscale, qscale_inv;
        scales_from_amax<InputType, OutputType>(amax, opts, &qscale, &qscale_inv);

        scale_inv[y + height * block_x] = qscale_inv;

        for (size_t i = 0; i < kBlockLenX; ++i) {
          size_t x_pos = i + block_x * kBlockLenX;
          if (x_pos >= width) {
            continue;
          }
          output[y * width + x_pos] = quantize_element(input[y * width + x_pos], qscale);
        }
      }
    }
  }
  if (output_t != nullptr && scale_inv_t != nullptr) {
    // Find the absolute maximum value in the block
    for (size_t block_x_t = 0; block_x_t < blocks_x_t; ++block_x_t) {
      for (size_t x = 0; x < width; ++x) {
        float amax = 0.0f;
        // Calculate amax for a tile.
        for (size_t i = 0; i < kBlockLenX; ++i) {
          size_t y_pos = i + block_x_t * kBlockLenX;
          if (y_pos >= height) {
            continue;
          }
          float val = static_cast<float>(input[x + y_pos * width]);
wenjh's avatar
wenjh committed
235
236
237
          #ifdef __HIP_PLATFORM_AMD__
          amax = amax > std::abs(val)? amax: std::abs(val);
          #else
238
          amax = std::max(amax, std::abs(val));
wenjh's avatar
wenjh committed
239
          #endif
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        }

        // We've calculated amax for a tile. Calculate scale and
        // scale_inv and populate outputs.
        float qscale, qscale_inv;
        scales_from_amax<InputType, OutputType>(amax, opts, &qscale, &qscale_inv);

        scale_inv_t[x + width * block_x_t] = qscale_inv;

        for (size_t i = 0; i < kBlockLenX; ++i) {
          size_t y_pos = i + block_x_t * kBlockLenX;
          if (y_pos >= height) {
            continue;
          }
          output_t[x * height + y_pos] = quantize_element(input[y_pos * width + x], qscale);
        }
      }
    }
  }
}

inline size_t scale_align_stride(size_t inner_elements) {
  return ((inner_elements + 4u - 1u) / 4u) * 4u;
};

void compare_scaling_factors(const std::string& name, const float* test, const float* ref,
                             const size_t row_blocks, const size_t col_blocks,
                             const size_t test_stride, const size_t ref_stride) {
  for (int i = 0; i < row_blocks; ++i) {
    for (int j = 0; j < col_blocks; ++j) {
      const int test_idx = i * test_stride + j;
      const int ref_idx = i * ref_stride + j;
      ASSERT_FALSE(test[test_idx] != ref[ref_idx])
          << "Error in " << name << std::endl
          << "Mismatch: " << test[test_idx] << " vs " << ref[ref_idx] << " at index " << test_idx
          << "," << ref_idx;
    }
  }
}

void compare_scaling_factors_one_dimensional_blocks(const std::string& name, const float* test,
                                                    const float* ref, const size_t rows,
wenjh's avatar
wenjh committed
282
                                                    const size_t col_blocks) {
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  const size_t test_stride = scale_align_stride(rows);
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < col_blocks; ++j) {
      const int test_idx = i + test_stride * j;
      const int ref_idx = i + rows * j;
      ASSERT_FALSE(test[test_idx] != ref[ref_idx])
          << "Error in " << name << std::endl
          << "Mismatch: " << test[test_idx] << " vs " << ref[ref_idx] << " at index " << test_idx
          << "," << ref_idx;
    }
  }
}

template <typename InputType, typename OutputType>
void runTestCase(const ProcessingMethod processing_method, const std::vector<size_t>& shape,
                 const bool rowwise, const bool colwise, InputsFillCase fill_case,
                 const QuantizationOptions& opts) {
  using namespace test;
  using EncodingType = fp32;
  DType itype = TypeInfo<InputType>::dtype;
  DType otype = TypeInfo<OutputType>::dtype;

  const size_t rows = first_dimension(shape);
  const size_t cols = last_dimension(shape);

  size_t blocks_x = (cols + kBlockLen - 1) / kBlockLen;
  size_t blocks_y = (rows + kBlockLen - 1) / kBlockLen;

  Tensor input("input", shape, itype);
  Tensor grad("grad", shape, itype);
  Tensor output_c("output_c", shape, otype, rowwise, colwise,
314
                  opts.block_scaling_dim == 2 ? NVTE_BLOCK_SCALING_2D : NVTE_BLOCK_SCALING_1D);
315
  Tensor output_dbias("output_dbias", std::vector<size_t>{cols}, itype);
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

  std::unique_ptr<OutputType[]> ref_output = std::make_unique<OutputType[]>(rows * cols);
  std::unique_ptr<OutputType[]> ref_output_t = std::make_unique<OutputType[]>(rows * cols);
  std::unique_ptr<float[]> ref_scale_inv = std::make_unique<float[]>(blocks_y * blocks_x);
  std::unique_ptr<float[]> ref_scale_inv_t = std::make_unique<float[]>(blocks_y * blocks_x);

  if (!rowwise) {
    ref_output = nullptr;
    ref_scale_inv = nullptr;
  }
  if (!colwise) {
    ref_output_t = nullptr;
    ref_scale_inv_t = nullptr;
  }

  fillCase<EncodingType>(&input, fill_case);
  fillUniform(&grad);

334
335
336
  QuantizationConfigWrapper quant_config;
  quant_config.set_force_pow_2_scales(opts.force_pow_2_scales);
  quant_config.set_amax_epsilon(opts.amax_epsilon);
337
338
339
  Tensor workspace;
  switch (processing_method) {
    case ProcessingMethod::CAST_ONLY: {
340
      nvte_quantize_v2(input.data(), output_c.data(), quant_config, nullptr);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
      break;
    }
  }

  cudaDeviceSynchronize();
  auto err = cudaGetLastError();
  ASSERT_EQ(err, cudaSuccess) << cudaGetErrorString(err);

  ref_quantize<InputType, OutputType>(processing_method, input.rowwise_cpu_dptr<InputType>(),
                                      {rows, cols}, ref_output.get(), ref_scale_inv.get(),
                                      ref_output_t.get(), ref_scale_inv_t.get(), opts);

  float atol = 0.0;
  float rtol = 0.0;

  if (rowwise) {
    compareResults("output_c", output_c, ref_output.get(), true, atol, rtol);
    compare_scaling_factors("scale_inv", output_c.rowwise_cpu_scale_inv_ptr<float>(),
                            ref_scale_inv.get(), blocks_y, blocks_x, scale_align_stride(blocks_x),
                            blocks_x);
  }
  if (colwise) {
    compareResults("output_c_t", output_c, ref_output_t.get(), false, atol, rtol);
    compare_scaling_factors("scale_inv_t", output_c.columnwise_cpu_scale_inv_ptr<float>(),
                            ref_scale_inv_t.get(), blocks_x, blocks_y, scale_align_stride(blocks_y),
                            blocks_y);
  }
}

template <typename InputType, typename OutputType>
void runTestCaseOneDimensionalBlocks(const ProcessingMethod processing_method,
                                     const std::vector<size_t>& shape, const bool rowwise,
                                     const bool colwise, InputsFillCase fill_case,
                                     const QuantizationOptions& opts) {
  using namespace test;
  using EncodingType = fp32;
  DType itype = TypeInfo<InputType>::dtype;
  DType otype = TypeInfo<OutputType>::dtype;

  const size_t rows = first_dimension(shape);
  const size_t cols = last_dimension(shape);

  size_t blocks_x = (cols + kBlockLen - 1) / kBlockLen;
  size_t blocks_x_t = (rows + kBlockLen - 1) / kBlockLen;

  Tensor input("input", shape, itype);
  Tensor grad("grad", shape, itype);
  Tensor output_c("output_c", shape, otype, rowwise, colwise,
389
                  opts.block_scaling_dim == 2 ? NVTE_BLOCK_SCALING_2D : NVTE_BLOCK_SCALING_1D);
390
  Tensor output_dbias("output_dbias", std::vector<size_t>{cols}, itype);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

  std::unique_ptr<OutputType[]> ref_output = std::make_unique<OutputType[]>(rows * cols);
  std::unique_ptr<OutputType[]> ref_output_t = std::make_unique<OutputType[]>(rows * cols);
  std::unique_ptr<float[]> ref_scale_inv = std::make_unique<float[]>(rows * blocks_x);
  std::unique_ptr<float[]> ref_scale_inv_t = std::make_unique<float[]>(cols * blocks_x_t);

  if (!rowwise) {
    ref_output = nullptr;
    ref_scale_inv = nullptr;
  }
  if (!colwise) {
    ref_output_t = nullptr;
    ref_scale_inv_t = nullptr;
  }

  fillCase<EncodingType>(&input, fill_case);
  fillUniform(&grad);

  Tensor workspace;
410
411
412
  QuantizationConfigWrapper quant_config;
  quant_config.set_force_pow_2_scales(opts.force_pow_2_scales);
  quant_config.set_amax_epsilon(opts.amax_epsilon);
413
414
  switch (processing_method) {
    case ProcessingMethod::CAST_ONLY: {
415
      nvte_quantize_v2(input.data(), output_c.data(), quant_config, nullptr);
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
      break;
    }
  }

  cudaDeviceSynchronize();
  auto err = cudaGetLastError();
  ASSERT_EQ(err, cudaSuccess) << cudaGetErrorString(err);

  ref_quantize_onedimensional_blocks<InputType, OutputType>(
      processing_method, input.rowwise_cpu_dptr<InputType>(), {rows, cols}, ref_output.get(),
      ref_scale_inv.get(), ref_output_t.get(), ref_scale_inv_t.get(), opts);

  float atol = 0.0;
  float rtol = 0.0;
  if (rowwise) {
    compareResults("output_c", output_c, ref_output.get(), true, atol, rtol);
    compare_scaling_factors_one_dimensional_blocks("scale_inv",
                                                   output_c.rowwise_cpu_scale_inv_ptr<float>(),
wenjh's avatar
wenjh committed
434
                                                   ref_scale_inv.get(), rows, blocks_x);
435
436
437
438
439
  }
  if (colwise) {
    compareResults("output_c_t", output_c, ref_output_t.get(), false, atol, rtol);
    compare_scaling_factors_one_dimensional_blocks("scale_inv_t",
                                                   output_c.columnwise_cpu_scale_inv_ptr<float>(),
wenjh's avatar
wenjh committed
440
                                                   ref_scale_inv_t.get(), cols, blocks_x_t);
441
442
443
444
  }
}

std::vector<std::vector<size_t>> matrix_sizes = {
445
446
447
    {1, 16}, {65, 96}, {256, 256}, {993, 512},
    {256, 65536}, {4096, 1632}, {1024, 1},
    {16, 512}, {1024}, {8, 32, 1024}, {16, 8, 4, 512},
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
};

std::vector<InputsFillCase> input_scenarios = {
    InputsFillCase::uniform,
};

std::vector<ProcessingMethod> processing_methods = {
    ProcessingMethod::CAST_ONLY,
    // ProcessingMethod::CAST_DBIAS,
    // ProcessingMethod::CAST_DBIAS_DACT,
    // ProcessingMethod::CAST_DACT,
    // ProcessingMethod::CAST_ACT,
};

// Only GeLU activation tests are supported
std::vector<ActivationType> Activation_types = {
    ActivationType::Identity,
    // ActivationType::GeLU,
    // ActivationType::SiLU,
    // ActivationType::ReLU,
    // ActivationType::QGeLU,
    // ActivationType::SReLU,
};


std::vector<float> amax_epsilons = {
    0.0f,
475
476
    1.0f, // Make large to be observable.

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
};

}  // namespace

class FusedCastFloat8BlockwiseTestSuite
    : public ::testing::TestWithParam<std::tuple<
          ProcessingMethod, ActivationType, std::vector<size_t>, transformer_engine::DType,
          transformer_engine::DType, InputsFillCase, bool, float, bool>> {};

class FusedCastFloat8VectorwiseTestSuite
    : public ::testing::TestWithParam<std::tuple<
          ProcessingMethod, ActivationType, std::vector<size_t>, transformer_engine::DType,
          transformer_engine::DType, InputsFillCase, bool, float, bool>> {};

#define DACT_FUNC_SWITCH(OP_FUNC_TYPE, OP, ...) \
  switch (OP_FUNC_TYPE) {                       \
    case ActivationType::Identity: {            \
      constexpr auto OP = &identity;            \
      {                                         \
        __VA_ARGS__                             \
      }                                         \
    } break;                                    \
  }

#define ACT_FUNC_SWITCH(OP_FUNC_TYPE, OP, ...) \
  switch (OP_FUNC_TYPE) {                      \
    case ActivationType::Identity: {           \
      constexpr auto OP = &identity;           \
      {                                        \
        __VA_ARGS__                            \
      }                                        \
    } break;                                   \
  }

TEST_P(FusedCastFloat8BlockwiseTestSuite, TestFusedCastFloat8Blockwise) {
  if (getDeviceComputeCapability() < hopperComputeCapability) {
    GTEST_SKIP();
  }

  using namespace transformer_engine;
  using namespace test;

  const ProcessingMethod processing_method = std::get<0>(GetParam());
  const ActivationType Act_type = std::get<1>(GetParam());
  const auto matrix_size = std::get<2>(GetParam());
  const DType input_type = std::get<3>(GetParam());
  const DType output_type = std::get<4>(GetParam());
  const InputsFillCase fill_case = std::get<5>(GetParam());
  const bool colwise = std::get<6>(GetParam());
  const bool rowwise = true;
  const float eps = std::get<7>(GetParam());
  const bool force_pow_2 = std::get<8>(GetParam());

  QuantizationOptions q_opts;
  q_opts.force_pow_2_scales = force_pow_2;
  q_opts.amax_epsilon = eps;
  q_opts.block_scaling_dim = 2u;

535
536
537
538
539
540
  // On Blackwell and newer, the FP8 block scaling recipe is emulated with MXFP8,
  // which requires using power of two scaling factors. Skip unsupported tests.
  if (getDeviceComputeCapability() >= blackwellComputeCapability && !force_pow_2) {
    GTEST_SKIP();
  }

541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  if (colwise && matrix_size.size() < 2) {
    // test_common Tensor initialization code does not
    // handle this case.
    GTEST_SKIP();
  }
  // Skips non Act tests if the Activation type is not an identity
  if (  // (processing_method == ProcessingMethod::CAST_ONLY || processing_method == ProcessingMethod::CAST_DBIAS)
      (processing_method == ProcessingMethod::CAST_ONLY) && Act_type != ActivationType::Identity) {
    GTEST_SKIP();
  }
  // Skips Act tests if the Activation is an identity
  // if ((processing_method == ProcessingMethod::CAST_DBIAS_DACT
  //     || processing_method == ProcessingMethod::CAST_DACT
  //     || processing_method == ProcessingMethod::CAST_ACT) && (Act_type == ActivationType::Identity)) {
  //     GTEST_SKIP();
  // }

  DACT_FUNC_SWITCH(
      Act_type, OP,
      TRANSFORMER_ENGINE_TYPE_SWITCH_FP16_FP32_ONLY(
          input_type, InputType,
          TRANSFORMER_ENGINE_TYPE_SWITCH_FP8_ONLY(
              output_type, OutputType,
              runTestCase<InputType, OutputType>(processing_method, matrix_size, rowwise, colwise,
                                                 fill_case, q_opts););););
}

TEST_P(FusedCastFloat8VectorwiseTestSuite, TestFusedCastFloat8Vectorwise) {
  if (getDeviceComputeCapability() < hopperComputeCapability) {
    GTEST_SKIP();
  }

  using namespace transformer_engine;
  using namespace test;

  const ProcessingMethod processing_method = std::get<0>(GetParam());
  const ActivationType Act_type = std::get<1>(GetParam());
  const auto matrix_size = std::get<2>(GetParam());
  const DType input_type = std::get<3>(GetParam());
  const DType output_type = std::get<4>(GetParam());
  const InputsFillCase fill_case = std::get<5>(GetParam());
  const bool colwise = std::get<6>(GetParam());
  const bool rowwise = true;
  const float eps = std::get<7>(GetParam());
  const bool force_pow_2 = std::get<8>(GetParam());

  QuantizationOptions q_opts;
  q_opts.force_pow_2_scales = force_pow_2;
  q_opts.amax_epsilon = eps;
  q_opts.block_scaling_dim = 1u;

592
593
594
595
596
597
  // On Blackwell and newer, the FP8 block scaling recipe is emulated with MXFP8,
  // which requires using power of two scaling factors. Skip unsupported tests.
  if (getDeviceComputeCapability() >= blackwellComputeCapability && !force_pow_2) {
    GTEST_SKIP();
  }

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  if (colwise && matrix_size.size() < 2) {
    // test_common Tensor initialization code does not
    // handle this case.
    GTEST_SKIP();
  }
  // Skips non Act tests if the Activation type is not an identity
  if (  // (processing_method == ProcessingMethod::CAST_ONLY || processing_method == ProcessingMethod::CAST_DBIAS)
      (processing_method == ProcessingMethod::CAST_ONLY) && Act_type != ActivationType::Identity) {
    GTEST_SKIP();
  }
  // Skips Act tests if the Activation is an identity
  // if ((processing_method == ProcessingMethod::CAST_DBIAS_DACT
  //     || processing_method == ProcessingMethod::CAST_DACT
  //     || processing_method == ProcessingMethod::CAST_ACT) && (Act_type == ActivationType::Identity)) {
  //     GTEST_SKIP();
  // }

  DACT_FUNC_SWITCH(
      Act_type, OP,
      TRANSFORMER_ENGINE_TYPE_SWITCH_FP16_FP32_ONLY(
          input_type, InputType,
          TRANSFORMER_ENGINE_TYPE_SWITCH_FP8_ONLY(
              output_type, OutputType,
              runTestCaseOneDimensionalBlocks<InputType, OutputType>(
                  processing_method, matrix_size, rowwise, colwise, fill_case, q_opts););););
}

std::string to_string(const ProcessingMethod method) {
  switch (method) {
    case ProcessingMethod::CAST_ONLY:
      return "CAST_ONLY";
    // case ProcessingMethod::CAST_DBIAS:      return "CAST_DBIAS";
    // case ProcessingMethod::CAST_DBIAS_DACT: return "CAST_DBIAS_DACT";
    // case ProcessingMethod::CAST_DACT:       return "CAST_DACT";
    // case ProcessingMethod::CAST_ACT:        return "CAST_ACT";
    default:
      return "";
  }
}

std::string to_string(const ActivationType Act_type) {
  switch (Act_type) {
    case ActivationType::Identity:
      return "Identity";
    // case ActivationType::GeLU:      return "GeLU";
    // case ActivationType::SiLU:      return "SiLU";
    // case ActivationType::ReLU:      return "ReLU";
    // case ActivationType::QGeLU:     return "QGeLU";
    // case ActivationType::SReLU:     return "SReLU";
    default:
      return "";
  }
}

INSTANTIATE_TEST_SUITE_P(
    OperatorTest, FusedCastFloat8BlockwiseTestSuite,
    ::testing::Combine(::testing::ValuesIn(processing_methods),
                       ::testing::ValuesIn(Activation_types), ::testing::ValuesIn(matrix_sizes),
                       ::testing::Values(DType::kFloat32, DType::kBFloat16, DType::kFloat16),
                       ::testing::Values(DType::kFloat8E4M3, DType::kFloat8E5M2),
                       ::testing::ValuesIn(input_scenarios), ::testing::Values(true, false),
659
                       ::testing::ValuesIn(amax_epsilons), ::testing::Values(true, false)),
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
    [](const testing::TestParamInfo<FusedCastFloat8BlockwiseTestSuite::ParamType>& info) {
      std::string name =
          to_string(std::get<0>(info.param)) + "X" + to_string(std::get<1>(info.param));
      const auto& shape = std::get<2>(info.param);
      for (const auto& s : shape) {
        name += "X" + std::to_string(s);
      }
      name += "X" + test::typeName(std::get<3>(info.param)) + "X" +
              test::typeName(std::get<4>(info.param)) + "X" +
              test::caseName(std::get<5>(info.param)) + "X" +
              std::to_string(std::get<6>(info.param)) + "X" +
              std::to_string(std::get<7>(info.param) != 0.0f) + "X" +
              std::to_string(std::get<8>(info.param));
      return name;
    });

INSTANTIATE_TEST_SUITE_P(
    OperatorTest, FusedCastFloat8VectorwiseTestSuite,
    ::testing::Combine(::testing::ValuesIn(processing_methods),
                       ::testing::ValuesIn(Activation_types), ::testing::ValuesIn(matrix_sizes),
                       ::testing::Values(DType::kFloat32, DType::kBFloat16, DType::kFloat16),
                       ::testing::Values(DType::kFloat8E4M3, DType::kFloat8E5M2),
                       ::testing::ValuesIn(input_scenarios), ::testing::Values(true, false),
683
                       ::testing::ValuesIn(amax_epsilons), ::testing::Values(true, false)),
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    [](const testing::TestParamInfo<FusedCastFloat8VectorwiseTestSuite::ParamType>& info) {
      std::string name =
          to_string(std::get<0>(info.param)) + "X" + to_string(std::get<1>(info.param));
      const auto& shape = std::get<2>(info.param);
      for (const auto& s : shape) {
        name += "X" + std::to_string(s);
      }
      name += "X" + test::typeName(std::get<3>(info.param)) + "X" +
              test::typeName(std::get<4>(info.param)) + "X" +
              test::caseName(std::get<5>(info.param)) + "X" +
              std::to_string(std::get<6>(info.param)) + "X" +
              std::to_string(std::get<7>(info.param) != 0.0f) + "X" +
              std::to_string(std::get<8>(info.param));
      return name;
    });