test_qdq.cu 5.45 KB
Newer Older
Przemek Tredak's avatar
Przemek Tredak committed
1
/*************************************************************************
2
 * Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Przemek Tredak's avatar
Przemek Tredak committed
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 *
 * See LICENSE for license information.
 ************************************************************************/

#include "gtest/gtest.h"
#include <transformer_engine/cast.h>
#include <transformer_engine/logging.h>
#include <gtest/gtest.h>
#include <cuda_runtime.h>
#include <cuda_bf16.h>
#include <memory>
#include <iostream>
#include <iomanip>
#include <random>
#include <cstring>
#include "../test_common.h"
#include "transformer_engine/transformer_engine.h"

using namespace transformer_engine;

namespace {

template <typename InputType, typename OutputType>
void compute_ref_q(const InputType *data, OutputType *output,
                   const size_t N,
                   float *amax, float scale) {
    using compute_t = float;
    compute_t current_max = -1e100;
    for (size_t i = 0; i < N; ++i) {
      compute_t current = static_cast<compute_t>(data[i]);
      current_max = fmaxf(current_max, fabsf(current));
      if (std::is_same<OutputType, test::fp8e4m3>::value ||
          std::is_same<OutputType, test::fp8e5m2>::value) {
        output[i] = OutputType(scale * current);
      } else {
        output[i] = OutputType(current);
      }
    }
    *amax = current_max;
}

template <typename InputType, typename OutputType>
void compute_ref_dq(const InputType *data, OutputType *output,
                    const size_t N, float scale_inv) {
    using compute_t = float;
    for (size_t i = 0; i < N; ++i) {
      compute_t current = static_cast<compute_t>(data[i]);
      output[i] = OutputType(scale_inv * current);
    }
}

template <typename InputType, typename OutputType>
void performTestQ(const size_t N) {
  using namespace test;

  DType itype = TypeInfo<InputType>::dtype;
  DType otype = TypeInfo<OutputType>::dtype;

  Tensor input({ N }, itype);
  Tensor output({ N }, otype);

  std::unique_ptr<OutputType[]> ref_output = std::make_unique<OutputType[]>(N);

66
67
  fillUniform(&input);
  setRandomScale(&output);
Przemek Tredak's avatar
Przemek Tredak committed
68

69
  nvte_fp8_quantize(input.data(), output.data(), 0);
Przemek Tredak's avatar
Przemek Tredak committed
70
71
72

  float ref_amax;
  compute_ref_q<InputType, OutputType>(input.cpu_dptr<InputType>(), ref_output.get(),
73
                                       N, &ref_amax, output.scale());
Przemek Tredak's avatar
Przemek Tredak committed
74
75
76
77
78
79

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

  auto [atol_amax, rtol_amax] = getTolerances(DType::kFloat32);
80
  compareResults("amax", output.amax(), ref_amax, atol_amax, rtol_amax);
Przemek Tredak's avatar
Przemek Tredak committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  auto [atol, rtol] = getTolerances(otype);
  compareResults("output_q", output, ref_output.get(), atol, rtol);
}

template <typename InputType, typename OutputType>
void performTestDQ(const size_t N) {
  using namespace test;

  DType itype = TypeInfo<InputType>::dtype;
  DType otype = TypeInfo<OutputType>::dtype;

  Tensor input({ N }, itype);
  Tensor output({ N }, otype);

  std::unique_ptr<OutputType[]> ref_output = std::make_unique<OutputType[]>(N);

97
  fillUniform(&input);
Przemek Tredak's avatar
Przemek Tredak committed
98

99
  nvte_fp8_dequantize(input.data(), output.data(), 0);
Przemek Tredak's avatar
Przemek Tredak committed
100
101

  compute_ref_dq<InputType, OutputType>(input.cpu_dptr<InputType>(), ref_output.get(),
102
                                        N, input.scale_inv());
Przemek Tredak's avatar
Przemek Tredak committed
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

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

  auto [atol, rtol] = getTolerances(otype);
  compareResults("output_dq", output, ref_output.get(), atol, rtol);
}

std::vector<size_t> qdq_test_cases = {2048* 12288,
                                      768 * 1024,
                                      256 * 65536,
                                      65536 * 128,
                                      257 * 259,
                                      128*128+1};

} //namespace

class QDQTestSuite : public ::testing::TestWithParam<std::tuple<transformer_engine::DType,
                                                                transformer_engine::DType,
                                                                size_t>> {};

TEST_P(QDQTestSuite, TestQ) {
    using namespace transformer_engine;
    using namespace test;

    const DType input_type = std::get<0>(GetParam());
    const DType output_type = std::get<1>(GetParam());
    const size_t N = std::get<2>(GetParam());

    TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(input_type, InputType,
      TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(output_type, OutputType,
        performTestQ<InputType, OutputType>(N);
      );
    );
}

TEST_P(QDQTestSuite, TestDQ) {
    using namespace transformer_engine;
    using namespace test;

    const DType input_type = std::get<0>(GetParam());
    const DType output_type = std::get<1>(GetParam());
    const size_t N = std::get<2>(GetParam());

    TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(input_type, InputType,
      TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(output_type, OutputType,
        performTestDQ<OutputType, InputType>(N);
      );
    );
}

INSTANTIATE_TEST_SUITE_P(
    OperatorTest,
    QDQTestSuite,
    ::testing::Combine(
        ::testing::Values(DType::kFloat32, DType::kBFloat16, DType::kFloat16),
        ::testing::Values(DType::kFloat8E4M3, DType::kFloat8E5M2),
        ::testing::ValuesIn(qdq_test_cases)),
    [](const testing::TestParamInfo<QDQTestSuite::ParamType>& info) {
      std::string name = test::typeName(std::get<0>(info.param)) + "X" +
                         test::typeName(std::get<1>(info.param)) + "X" +
                         std::to_string(std::get<2>(info.param));
      return name;
    });