@@ -25,6 +25,8 @@ struct QuantizedTypeStorage;
2525struct AnyQuantizedTypeStorage ;
2626struct UniformQuantizedTypeStorage ;
2727struct UniformQuantizedPerAxisTypeStorage ;
28+ struct QuantileQuantizedTypeStorage ;
29+ struct QuantileQuantizedPerAxisTypeStorage ;
2830struct CalibratedQuantizedTypeStorage ;
2931
3032} // namespace detail
@@ -394,6 +396,128 @@ class UniformQuantizedPerAxisType
394396 }
395397};
396398
399+ // / QuantileQuantizedType derives from UniformQuantizedType and adds to it a
400+ // / look up table array of quantile values.
401+ // /
402+ // / Syntax synopsis:
403+ // / Per-layer, all parameters expressed:
404+ // / !quant<quantile[StorageType:ExpressedType]{Quantiles}:{Scale:ZeroPoint}>
405+ // / Per-layer, optional parameters omitted:
406+ // / !quant<quantile[StorageType]{Quantiles}:{Scale}>
407+ // /
408+ // / StorageType: 'i'|'u' NumBits
409+ // / ExpressedType: 'f16', 'f32', 'bf16', 'f64'
410+ // / Quantiles: Quantile+
411+ // / Quantile: A legal double value
412+ // / Scale: A legal double value
413+ // / ZeroPoint: An integer value
414+ class QuantileQuantizedType
415+ : public Type::TypeBase<QuantileQuantizedType, UniformQuantizedType,
416+ detail::QuantileQuantizedTypeStorage> {
417+ public:
418+ using Base::Base;
419+ using Base::getChecked;
420+
421+ static constexpr StringLiteral name = " quant.quantile" ;
422+
423+ // / Gets an instance of the type with all parameters specified but not
424+ // / checked.
425+ static QuantileQuantizedType get (unsigned flags, Type storageType,
426+ Type expressedType,
427+ ArrayRef<double > quantiles, double scale,
428+ int64_t zeroPoint, int64_t storageTypeMin,
429+ int64_t storageTypeMax);
430+
431+ static QuantileQuantizedType
432+ getChecked (function_ref<InFlightDiagnostic()> emitError, unsigned flags,
433+ Type storageType, Type expressedType, ArrayRef<double > quantiles,
434+ double scale, int64_t zeroPoint, int64_t storageTypeMin,
435+ int64_t storageTypeMax);
436+
437+ // / Verifies construction invariants and issues errors/warnings.
438+ static LogicalResult verifyInvariants (function_ref<InFlightDiagnostic()> emitError,
439+ unsigned flags, Type storageType,
440+ Type expressedType, ArrayRef<double> quantiles,
441+ double scale, int64_t zeroPoint,
442+ int64_t storageTypeMin, int64_t storageTypeMax);
443+
444+ // / Gets the quantile values
445+ ArrayRef<double > getQuantiles () const ;
446+
447+ // Fixed point values are real numbers divided by a scale.
448+ // Currently, only signed storage types are treated as fixed point.
449+ // A fixed point value can be obtained from an affine value by subtracting
450+ // the zeroPoint.
451+ // In the future, this may be explicit versus implied by type and zeroPoint.
452+ bool isFixedPoint () const { return isSigned () && getZeroPoint () == 0 ; }
453+ };
454+
455+ // / Represents per-axis QuantileQuantizedType (also known as per-channel
456+ // / quantization).
457+ // /
458+ // / Syntax synopsis:
459+ // / Per-axis, all parameters expressed:
460+ // / !quant<quantile[StorageType:ExpressedType:QuantizedDim]{Quantiles}:{QuantParams}>
461+ // / Per-axis, optional parameters omitted:
462+ // / !quant<quantile[StorageType]{Quantiles}:{Scale}>
463+ // /
464+ // / StorageType: 'i'|'u' NumBits
465+ // / ExpressedType: 'f16', 'f32', 'bf16', 'f64'
466+ // / QuantizedDim: An integer value
467+ // / Quantiles: Quantile+
468+ // / Quantile: A legal double value
469+ // / QuantParams: (Scale ':' ZeroPoint)+
470+ // / Scale: A legal double value
471+ // / ZeroPoint: An integer value
472+ class QuantileQuantizedPerAxisType
473+ : public Type::TypeBase<QuantileQuantizedPerAxisType,
474+ UniformQuantizedPerAxisType,
475+ detail::QuantileQuantizedPerAxisTypeStorage> {
476+ public:
477+ using Base::Base;
478+ using Base::getChecked;
479+
480+ static constexpr StringLiteral name = " quant.quantile_per_axis" ;
481+
482+ // / Gets an instance of the type with all parameters specified but not
483+ // / checked.
484+ static QuantileQuantizedPerAxisType
485+ get (unsigned flags, Type storageType, Type expressedType,
486+ ArrayRef<double > quantiles, ArrayRef<double > scales,
487+ ArrayRef<int64_t > zeroPoints, int32_t quantizedDimension,
488+ int64_t storageTypeMin, int64_t storageTypeMax);
489+
490+ // / Gets an instance of the type with all specified parameters checked.
491+ // / Returns a nullptr convertible type on failure.
492+ static QuantileQuantizedPerAxisType
493+ getChecked (function_ref<InFlightDiagnostic()> emitError, unsigned flags,
494+ Type storageType, Type expressedType, ArrayRef<double > quantiles,
495+ ArrayRef<double > scales, ArrayRef<int64_t > zeroPoints,
496+ int32_t quantizedDimension, int64_t storageTypeMin,
497+ int64_t storageTypeMax);
498+
499+ // / Verifies construction invariants and issues errors/warnings.
500+ static LogicalResult verifyInvariants (function_ref<InFlightDiagnostic()> emitError,
501+ unsigned flags, Type storageType,
502+ Type expressedType, ArrayRef<double> quantiles,
503+ ArrayRef<double> scales,
504+ ArrayRef<int64_t> zeroPoints,
505+ int32_t quantizedDimension,
506+ int64_t storageTypeMin, int64_t storageTypeMax);
507+
508+ // / Gets the quantile values
509+ ArrayRef<double > getQuantiles () const ;
510+
511+ // / Fixed point values are real numbers divided by a scale.
512+ // / Currently, only signed storage types are treated as fixed point.
513+ // / A fixed point value can be obtained from an affine value by subtracting
514+ // / the zeroPoint.
515+ // / In the future, this may be explicit versus implied by type and zeroPoint.
516+ bool isFixedPoint () const {
517+ return isSigned () && !llvm::is_contained (getZeroPoints (), 0 );
518+ }
519+ };
520+
397521// / A quantized type that infers its range from given min/max values.
398522// /
399523// / Typical syntax:
0 commit comments