MLIR 22.0.0git
TypeDetail.h
Go to the documentation of this file.
1//===- TypeDetail.h - QuantOps Type detail ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef TYPE_DETAIL_H_
10#define TYPE_DETAIL_H_
11
14#include "mlir/IR/TypeSupport.h"
15#include "mlir/IR/Types.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/Hashing.h"
18#include "llvm/ADT/bit.h"
19
20namespace mlir {
21namespace quant {
22namespace detail {
23
29
30 /// Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
31 unsigned flags;
32
33 // Integral type for the storage point representation.
35
36 // Floating point type that the quantized type approximates.
38
39 // The minimum value storageType can take.
41
42 // The maximum value storageType can take.
44};
45
47 struct KeyTy {
52 unsigned flags;
57
58 // Check for equality of two structures that share KeyTy data members
59 // (by name).
60 template <typename T, typename U>
61 static bool genericIsEqual(const T &lhs, const U &rhs) {
62 return lhs.flags == rhs.flags && lhs.storageType == rhs.storageType &&
63 lhs.expressedType == rhs.expressedType &&
64 lhs.storageTypeMin == rhs.storageTypeMin &&
65 lhs.storageTypeMax == rhs.storageTypeMax;
66 }
67
68 bool operator==(const KeyTy &other) const {
69 return genericIsEqual(*this, other);
70 }
71
72 unsigned getHashValue() const {
73 return llvm::hash_combine(flags, storageType, expressedType,
75 }
76 };
77
81
82 bool operator==(const KeyTy &key) const {
83 return KeyTy::genericIsEqual(*this, key);
84 }
85
86 /// Construction.
88 const KeyTy &key) {
89 return new (allocator.allocate<AnyQuantizedTypeStorage>())
91 }
92
93 static unsigned hashKey(const KeyTy &key) { return key.getHashValue(); }
94};
95
97 struct KeyTy {
103 /// Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
104 unsigned flags;
105
106 // Integral type for the storage point representation.
108
109 // Floating point type that the quantized type approximates.
111
112 double scale;
116
117 // Check for equality of two structures that share KeyTy data members
118 // (by name).
119 template <typename T, typename U>
120 static bool genericIsEqual(const T &lhs, const U &rhs) {
121 return lhs.flags == rhs.flags && lhs.storageType == rhs.storageType &&
122 lhs.expressedType == rhs.expressedType && lhs.scale == rhs.scale &&
123 lhs.zeroPoint == rhs.zeroPoint &&
124 lhs.storageTypeMin == rhs.storageTypeMin &&
125 lhs.storageTypeMax == rhs.storageTypeMax;
126 }
127
128 bool operator==(const KeyTy &other) const {
129 return genericIsEqual(*this, other);
130 }
131
132 unsigned getHashValue() const {
133 int64_t scaleBits = llvm::bit_cast<int64_t>(scale);
134 return llvm::hash_combine(flags, storageType, expressedType, scaleBits,
136 }
137 };
138
143
144 bool operator==(const KeyTy &key) const {
145 return KeyTy::genericIsEqual(*this, key);
146 }
147
148 /// Construction.
150 const KeyTy &key) {
151 return new (allocator.allocate<UniformQuantizedTypeStorage>())
153 }
154
155 static unsigned hashKey(const KeyTy &key) { return key.getHashValue(); }
156
157 double scale;
159};
160
162 struct KeyTy {
171 /// Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
172 unsigned flags;
173
174 // Integral type for the storage point representation.
176
177 // Floating point type that the quantized type approximates.
179
185
187
189
190 // Check for equality of two structures that share KeyTy data members
191 // (by name).
192 template <typename T, typename U>
193 static bool genericIsEqual(const T &lhs, const U &rhs) {
194 return lhs.flags == rhs.flags && lhs.storageType == rhs.storageType &&
195 lhs.expressedType == rhs.expressedType &&
196 lhs.getScales() == rhs.getScales() &&
197 lhs.getZeroPoints() == rhs.getZeroPoints() &&
198 lhs.quantizedDimension == rhs.quantizedDimension &&
199 lhs.storageTypeMin == rhs.storageTypeMin &&
200 lhs.storageTypeMax == rhs.storageTypeMax;
201 }
202
203 bool operator==(const KeyTy &other) const {
204 return genericIsEqual(*this, other);
205 }
206
207 unsigned getHashValue() const {
208 int64_t *scalesCast = llvm::bit_cast<int64_t *>(scales.data());
209 ArrayRef<int64_t> scalesBits(scalesCast, scales.size());
210 return llvm::hash_combine(flags, storageType, expressedType,
211 llvm::hash_combine_range(scalesBits),
212 llvm::hash_combine_range(zeroPoints),
214 }
215 };
216
217 // We pass scales and zeroPoints in directly rather than relying on KeyTy
218 // because we have to create new reallocated versions in `construct` below.
220 ArrayRef<int64_t> zeroPoints)
223 scaleElements(scales.data()), zeroPointElements(zeroPoints.data()),
224 quantParamsSize(scales.size()),
226
227 bool operator==(const KeyTy &key) const {
228 return KeyTy::genericIsEqual(*this, key);
229 }
230
231 /// Construction.
233 construct(TypeStorageAllocator &allocator, const KeyTy &key) {
234 ArrayRef<double> scales = allocator.copyInto(key.scales);
235 ArrayRef<int64_t> zeroPoints = allocator.copyInto(key.zeroPoints);
236 return new (allocator.allocate<UniformQuantizedPerAxisTypeStorage>())
237 UniformQuantizedPerAxisTypeStorage(key, scales, zeroPoints);
238 }
239
240 static unsigned hashKey(const KeyTy &key) { return key.getHashValue(); }
241
245
249
250 const double *scaleElements;
254};
255
257 struct KeyTy {
266 /// Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
267 unsigned flags;
268
269 // Integral type for the storage point representation.
271
272 // Floating point type that the quantized type approximates.
274
281
283
285
286 // Check for equality of two structures that share KeyTy data members
287 // (by name).
288 template <typename T, typename U>
289 static bool genericIsEqual(const T &lhs, const U &rhs) {
290 return lhs.flags == rhs.flags && lhs.storageType == rhs.storageType &&
291 lhs.expressedType == rhs.expressedType &&
292 lhs.scales == rhs.scales && lhs.zeroPoints == rhs.zeroPoints &&
293 lhs.quantizedDimensions == rhs.quantizedDimensions &&
294 lhs.blockSizes == rhs.blockSizes &&
295 lhs.storageTypeMin == rhs.storageTypeMin &&
296 lhs.storageTypeMax == rhs.storageTypeMax;
297 }
298
299 bool operator==(const KeyTy &other) const {
300 return genericIsEqual(*this, other);
301 }
302
303 unsigned getHashValue() const {
304 // Hash the scalar attributes.
305 unsigned hash = llvm::hash_combine(flags, storageType, expressedType,
307
308 // Hash the scales.
309 for (auto scaleAttr : scales.getValues<APFloat>()) {
310 hash = llvm::hash_combine(
311 hash, llvm::bit_cast<int64_t>(scaleAttr.convertToDouble()));
312 }
313
314 // Hash the zero points. (Assumed to be integers, adjust if needed).
315 for (auto zeroPointAttr : zeroPoints.getValues<APInt>()) {
316 hash = llvm::hash_combine(hash, zeroPointAttr.getSExtValue());
317 }
318
319 // Hash the quantized dimensions and block sizes.
320 hash = llvm::hash_combine(hash,
321 llvm::hash_combine_range(quantizedDimensions),
322 llvm::hash_combine_range(blockSizes));
323
324 return hash;
325 }
326 };
327
328 // We pass scales and zeroPoints in directly rather than relying on KeyTy
329 // because we have to create new reallocated versions in `construct` below.
339
340 bool operator==(const KeyTy &key) const {
341 return KeyTy::genericIsEqual(*this, key);
342 }
343
344 /// Construction.
356
357 static unsigned hashKey(const KeyTy &key) { return key.getHashValue(); }
358
360
362
366
368
373};
374
376 struct KeyTy {
379 // Floating point type that the quantized type approximates.
381
382 double min;
383 double max;
384
385 // Check for equality of two structures that share KeyTy data members
386 // (by name).
387 template <typename T, typename U>
388 static bool genericIsEqual(const T &lhs, const U &rhs) {
389 return lhs.expressedType == rhs.expressedType && lhs.min == rhs.min &&
390 lhs.max == rhs.max;
391 }
392
393 bool operator==(const KeyTy &other) const {
394 return genericIsEqual(*this, other);
395 }
396
397 unsigned getHashValue() const {
398 int64_t minBits = llvm::bit_cast<double>(min);
399 int64_t maxBits = llvm::bit_cast<double>(max);
400 return llvm::hash_combine(expressedType, minBits, maxBits);
401 }
402 };
403
405 : QuantizedTypeStorage(0, NoneType(), key.expressedType, 0, 0),
406 min(key.min), max(key.max) {}
407
408 bool operator==(const KeyTy &key) const {
409 return KeyTy::genericIsEqual(*this, key);
410 }
411
412 /// Construction.
414 construct(TypeStorageAllocator &allocator, const KeyTy &key) {
415 return new (allocator.allocate<CalibratedQuantizedTypeStorage>())
417 }
418
419 static unsigned hashKey(const KeyTy &key) { return key.getHashValue(); }
420
421 double min;
422 double max;
423};
424
425} // namespace detail
426} // namespace quant
427} // namespace mlir
428
429#endif // TYPE_DETAIL_H_
lhs
An attribute that represents a reference to a dense vector or tensor object.
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
T * allocate()
Allocate an instance of the provided type.
Base storage class appearing in a Type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Include the generated interface declarations.
StorageUniquer::StorageAllocator TypeStorageAllocator
This is a utility allocator used to allocate memory for instances of derived Types.
bool operator==(const KeyTy &other) const
Definition TypeDetail.h:68
static bool genericIsEqual(const T &lhs, const U &rhs)
Definition TypeDetail.h:61
KeyTy(unsigned flags, Type storageType, Type expressedType, int64_t storageTypeMin, int64_t storageTypeMax)
Definition TypeDetail.h:48
static AnyQuantizedTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition TypeDetail.h:87
static unsigned hashKey(const KeyTy &key)
Definition TypeDetail.h:93
bool operator==(const KeyTy &key) const
Definition TypeDetail.h:82
static bool genericIsEqual(const T &lhs, const U &rhs)
Definition TypeDetail.h:388
KeyTy(Type expressedType, double min, double max)
Definition TypeDetail.h:377
static unsigned hashKey(const KeyTy &key)
Definition TypeDetail.h:419
static CalibratedQuantizedTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition TypeDetail.h:414
QuantizedTypeStorage(unsigned flags, Type storageType, Type expressedType, int64_t storageTypeMin, int64_t storageTypeMax)
Definition TypeDetail.h:25
unsigned flags
Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
Definition TypeDetail.h:31
unsigned flags
Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
Definition TypeDetail.h:172
static bool genericIsEqual(const T &lhs, const U &rhs)
Definition TypeDetail.h:193
KeyTy(unsigned flags, Type storageType, Type expressedType, ArrayRef< double > scales, ArrayRef< int64_t > zeroPoints, int32_t quantizedDimension, int64_t storageTypeMin, int64_t storageTypeMax)
Definition TypeDetail.h:163
static UniformQuantizedPerAxisTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition TypeDetail.h:233
UniformQuantizedPerAxisTypeStorage(const KeyTy &key, ArrayRef< double > scales, ArrayRef< int64_t > zeroPoints)
Definition TypeDetail.h:219
KeyTy(unsigned flags, Type storageType, Type expressedType, DenseElementsAttr scales, DenseElementsAttr zeroPoints, ArrayRef< int32_t > quantizedDimensions, ArrayRef< int64_t > blockSizes, int64_t storageTypeMin, int64_t storageTypeMax)
Definition TypeDetail.h:258
static bool genericIsEqual(const T &lhs, const U &rhs)
Definition TypeDetail.h:289
unsigned flags
Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
Definition TypeDetail.h:267
static UniformQuantizedSubChannelTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition TypeDetail.h:346
UniformQuantizedSubChannelTypeStorage(const KeyTy &key, DenseElementsAttr scales, DenseElementsAttr zeroPoints, ArrayRef< int32_t > quantizedDimensions, ArrayRef< int64_t > blockSizes)
Definition TypeDetail.h:330
unsigned flags
Flags corresponding to the bitmapped enum QuantizationFlags::FlagValue.
Definition TypeDetail.h:104
static bool genericIsEqual(const T &lhs, const U &rhs)
Definition TypeDetail.h:120
KeyTy(unsigned flags, Type storageType, Type expressedType, double scale, int64_t zeroPoint, int64_t storageTypeMin, int64_t storageTypeMax)
Definition TypeDetail.h:98
static unsigned hashKey(const KeyTy &key)
Definition TypeDetail.h:155
static UniformQuantizedTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition TypeDetail.h:149