huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
1
2
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "annotator/quantization.h"
 
#include <vector>
 
#include "gmock/gmock.h"
#include "gtest/gtest.h"
 
using testing::ElementsAreArray;
using testing::FloatEq;
using testing::Matcher;
 
namespace libtextclassifier3 {
namespace {
 
Matcher<std::vector<float>> ElementsAreFloat(const std::vector<float>& values) {
  std::vector<Matcher<float>> matchers;
  for (const float value : values) {
    matchers.push_back(FloatEq(value));
  }
  return ElementsAreArray(matchers);
}
 
TEST(QuantizationTest, DequantizeAdd8bit) {
  std::vector<float> scales{{0.1, 9.0, -7.0}};
  std::vector<uint8> embeddings{{/*0: */ 0x00, 0xFF, 0x09, 0x00,
                                 /*1: */ 0xFF, 0x09, 0x00, 0xFF,
                                 /*2: */ 0x09, 0x00, 0xFF, 0x09}};
 
  const int quantization_bits = 8;
  const int bytes_per_embedding = 4;
  const int num_sparse_features = 7;
  {
    const int bucket_id = 0;
    std::vector<float> dest(4, 0.0);
    DequantizeAdd(scales.data(), embeddings.data(), bytes_per_embedding,
                  num_sparse_features, quantization_bits, bucket_id,
                  dest.data(), dest.size());
 
    EXPECT_THAT(dest,
                ElementsAreFloat(std::vector<float>{
                    // clang-format off
                    {1.0 / 7 * 0.1 * (0x00 - 128),
                     1.0 / 7 * 0.1 * (0xFF - 128),
                     1.0 / 7 * 0.1 * (0x09 - 128),
                     1.0 / 7 * 0.1 * (0x00 - 128)}
                    // clang-format on
                }));
  }
 
  {
    const int bucket_id = 1;
    std::vector<float> dest(4, 0.0);
    DequantizeAdd(scales.data(), embeddings.data(), bytes_per_embedding,
                  num_sparse_features, quantization_bits, bucket_id,
                  dest.data(), dest.size());
 
    EXPECT_THAT(dest,
                ElementsAreFloat(std::vector<float>{
                    // clang-format off
                    {1.0 / 7 * 9.0 * (0xFF - 128),
                     1.0 / 7 * 9.0 * (0x09 - 128),
                     1.0 / 7 * 9.0 * (0x00 - 128),
                     1.0 / 7 * 9.0 * (0xFF - 128)}
                    // clang-format on
                }));
  }
}
 
TEST(QuantizationTest, DequantizeAdd1bitZeros) {
  const int bytes_per_embedding = 4;
  const int num_buckets = 3;
  const int num_sparse_features = 7;
  const int quantization_bits = 1;
  const int bucket_id = 1;
 
  std::vector<float> scales(num_buckets);
  std::vector<uint8> embeddings(bytes_per_embedding * num_buckets);
  std::fill(scales.begin(), scales.end(), 1);
  std::fill(embeddings.begin(), embeddings.end(), 0);
 
  std::vector<float> dest(32);
  DequantizeAdd(scales.data(), embeddings.data(), bytes_per_embedding,
                num_sparse_features, quantization_bits, bucket_id, dest.data(),
                dest.size());
 
  std::vector<float> expected(32);
  std::fill(expected.begin(), expected.end(),
            1.0 / num_sparse_features * (0 - 1));
  EXPECT_THAT(dest, ElementsAreFloat(expected));
}
 
TEST(QuantizationTest, DequantizeAdd1bitOnes) {
  const int bytes_per_embedding = 4;
  const int num_buckets = 3;
  const int num_sparse_features = 7;
  const int quantization_bits = 1;
  const int bucket_id = 1;
 
  std::vector<float> scales(num_buckets, 1.0);
  std::vector<uint8> embeddings(bytes_per_embedding * num_buckets, 0xFF);
 
  std::vector<float> dest(32);
  DequantizeAdd(scales.data(), embeddings.data(), bytes_per_embedding,
                num_sparse_features, quantization_bits, bucket_id, dest.data(),
                dest.size());
  std::vector<float> expected(32);
  std::fill(expected.begin(), expected.end(),
            1.0 / num_sparse_features * (1 - 1));
  EXPECT_THAT(dest, ElementsAreFloat(expected));
}
 
TEST(QuantizationTest, DequantizeAdd3bit) {
  const int bytes_per_embedding = 4;
  const int num_buckets = 3;
  const int num_sparse_features = 7;
  const int quantization_bits = 3;
  const int bucket_id = 1;
 
  std::vector<float> scales(num_buckets, 1.0);
  scales[1] = 9.0;
  std::vector<uint8> embeddings(bytes_per_embedding * num_buckets, 0);
  // For bucket_id=1, the embedding has values 0..9 for indices 0..9:
  embeddings[4] = (1 << 7) | (1 << 6) | (1 << 4) | 1;
  embeddings[5] = (1 << 6) | (1 << 4) | (1 << 3);
  embeddings[6] = (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | 1;
 
  std::vector<float> dest(10);
  DequantizeAdd(scales.data(), embeddings.data(), bytes_per_embedding,
                num_sparse_features, quantization_bits, bucket_id, dest.data(),
                dest.size());
 
  std::vector<float> expected;
  expected.push_back(1.0 / num_sparse_features * (1 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (2 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (3 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (4 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (5 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (6 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (7 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (0 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (0 - 4) * scales[bucket_id]);
  expected.push_back(1.0 / num_sparse_features * (0 - 4) * scales[bucket_id]);
  EXPECT_THAT(dest, ElementsAreFloat(expected));
}
 
}  // namespace
}  // namespace libtextclassifier3