9 #ifndef MLIR_FXPMATH_UNIFORM_KERNEL_UTILS_H_ 10 #define MLIR_FXPMATH_UNIFORM_KERNEL_UTILS_H_ 31 for (
unsigned checkWidth : checkWidths) {
41 const F xLog2 = std::log(x) * (1.0 / std::log(2.0));
42 const F xLog2Rounded = std::round(xLog2);
43 const F xLog2Frac = xLog2 - xLog2Rounded;
44 log2Result =
static_cast<int>(xLog2Rounded);
47 return std::abs(xLog2Frac) < 1e-6;
55 : op(op), lhs(lhs), rhs(rhs), clampMin(clampMin), clampMax(clampMax),
59 lhsStorageType(quant::QuantizedType::castToStorageType(lhs->getType())),
60 rhsStorageType(quant::QuantizedType::castToStorageType(rhs->getType())),
62 quant::QuantizedType::castToStorageType(*op->result_type_begin())) {
83 int &resultLog2Scale)
const {
101 int64_t typeMin =
resultType.getStorageTypeMin();
102 int64_t typeMax =
resultType.getStorageTypeMax();
142 assert(realMultiplier < 1.0);
143 assert(realMultiplier > 0.0);
145 const double q = std::frexp(realMultiplier, &exponent);
146 auto qFixed =
static_cast<int64_t
>(std::round(q * (1ll << 31)));
147 assert(qFixed <= (1ll << 31));
148 if (qFixed == (1ll << 31)) {
152 assert(qFixed <= std::numeric_limits<int32_t>::max());
153 multiplier =
static_cast<int32_t
>(qFixed);
163 switch (st.getKind()) {
176 return newElementType;
189 assert(t.
isa<
IntegerType>() &&
"integer broadcast must be of integer type");
198 APFloat::rmNearestTiesToEven, &losesInfo);
200 assert((status & (APFloat::opDivByZero | APFloat::opInvalidOp)) == 0 &&
201 "could not convert to float const");
210 assert(floatElementType &&
211 "float broadcast element type must be float like");
217 assert(floatType &&
"float broadcast must be of float type");
227 #endif // MLIR_FXPMATH_UNIFORM_KERNEL_UTILS_H_ Definition: InferTypeOpInterface.cpp:20
unsigned getIntOrFloatBitWidth()
Definition: StandardTypes.cpp:103
bool integralLog2(F x, int &log2Result)
Definition: UniformKernelUtils.h:40
Type getStorageType() const
Definition: QuantTypes.cpp:58
Definition: Operation.h:27
APFloat convertFloatToType(FloatType ft, APFloat value)
Definition: UniformKernelUtils.h:195
Integer types can have arbitrary bitwidth up to a large fixed limit.
Definition: StandardTypes.h:82
QuantizedMultiplierSmallerThanOneExp(double realMultiplier)
Definition: UniformKernelUtils.h:141
Definition: StandardTypes.h:59
static UnrankedTensorType get(Type elementType)
Definition: StandardTypes.cpp:279
static QuantizedType getQuantizedElementType(Type primitiveOrContainerType)
Definition: QuantTypes.cpp:89
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Definition: Attributes.cpp:572
static MemRefType get(ArrayRef< int64_t > shape, Type elementType, ArrayRef< AffineMap > affineMapComposition={}, unsigned memorySpace=0)
Definition: StandardTypes.cpp:303
Definition: StandardTypes.h:113
bool isIntOrFloat()
Return true of this is an integer or a float type.
Definition: StandardTypes.cpp:45
bool hasStorageBitWidth(quant::QuantizedType t, ArrayRef< unsigned > checkWidths)
Definition: UniformKernelUtils.h:28
Definition: StandardTypes.h:178
Attribute broadcastScalarConstIntValue(Type t, int64_t value)
Definition: UniformKernelUtils.h:181
const llvm::fltSemantics & getFloatSemantics()
Return the floating semantics of this float type.
Definition: StandardTypes.cpp:86
Definition: StandardTypes.h:56
Type castElementType(Type t, Type newElementType)
Casts an integer or floating point based shaped type to a new element type.
Definition: UniformKernelUtils.h:161
Attribute broadcastScalarConstFloatValue(Type t, APFloat value)
Definition: UniformKernelUtils.h:207
U dyn_cast_or_null() const
Definition: Types.h:261
Definition: StandardTypes.h:58
U dyn_cast() const
Definition: Types.h:258
Definition: UniformKernelUtils.h:140
Definition: Attributes.h:53
int32_t multiplier
Definition: UniformKernelUtils.h:156
static IntegerAttr get(Type type, int64_t value)
Definition: Attributes.cpp:271
Definition: StandardTypes.h:390
int exponent
Definition: UniformKernelUtils.h:157
static RankedTensorType get(ArrayRef< int64_t > shape, Type elementType)
Definition: StandardTypes.cpp:248
static VectorType get(ArrayRef< int64_t > shape, Type elementType)
Definition: StandardTypes.cpp:199
Definition: StandardTypes.h:57
result_type_iterator result_type_begin()
Definition: Operation.h:262
Definition: QuantTypes.h:60
static FloatAttr get(Type type, double value)
Definition: Attributes.cpp:175
bool isa() const
Definition: Types.h:254
quant::UniformQuantizedType getUniformElementType(Type t)
Definition: UniformKernelUtils.h:23
U cast() const
Definition: Types.h:264