Skip to content

[TOSA] Add F16 and BF16 support for tosa.clamp #4281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/torch-mlir/Conversion/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ FailureOr<Value> unsqueezeTensor(PatternRewriter &rewriter, Operation *op,
// Returns the squeezed tensor or failure.
FailureOr<Value> squeezeTensor(PatternRewriter &rewriter, Operation *op,
Value input, int64_t dim);

// Float 16 limits
constexpr float Float16Max = 65504.0f;
constexpr float Float16Lowest = -65504.0f;

// BFloat 16 limits
constexpr float BFloat16Max = 3.38953139e38f;
constexpr float BFloat16Lowest = -3.38953139e38f;
} // namespace Torch
} // namespace torch
} // namespace mlir
Expand Down
75 changes: 64 additions & 11 deletions lib/Conversion/TorchToTosa/TorchToTosa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,11 @@ LogicalResult ConvertAtenOp<AtenReluOp>::matchAndRewrite(
ConversionPatternRewriter &rewriter) const {
Value self = adaptor.getSelf();
auto selfTy = cast<TensorType>(self.getType());
auto outTy =
dyn_cast<TensorType>(getTypeConverter()->convertType(op.getType()));
auto outElemTy = outTy.getElementType();

if (!selfTy) {
if (!selfTy || !outTy) {
return rewriter.notifyMatchFailure(op,
"Only Tensor types supported in TOSA");
}
Expand All @@ -883,12 +886,27 @@ LogicalResult ConvertAtenOp<AtenReluOp>::matchAndRewrite(
op, "Only floating-point datatype legalization currently supported");
}

FloatAttr minFloatAttr, maxFloatAttr;
if (outElemTy.isF16()) {
minFloatAttr = rewriter.getF16FloatAttr(0.0f);
maxFloatAttr = rewriter.getF16FloatAttr(Float16Max);
} else if (outElemTy.isBF16()) {
minFloatAttr = rewriter.getFloatAttr(rewriter.getBF16Type(), 0.0f);
maxFloatAttr = rewriter.getFloatAttr(rewriter.getBF16Type(), BFloat16Max);
} else if (outElemTy.isF32()) {
minFloatAttr = rewriter.getF32FloatAttr(0.0f);
maxFloatAttr = rewriter.getF32FloatAttr(std::numeric_limits<float>::max());
} else if (outElemTy.isF64()) {
minFloatAttr = rewriter.getF64FloatAttr(0.0f);
maxFloatAttr = rewriter.getF64FloatAttr(std::numeric_limits<double>::max());
} else {
return rewriter.notifyMatchFailure(op, "Unsupported floating-point type");
}

// Maps to tosa.clamp
// Use default NaN Propagation mode "PROPAGATE" for tosa.clamp
rewriter.replaceOpWithNewOp<tosa::ClampOp>(
op, getTypeConverter()->convertType(op.getType()), self,
rewriter.getF32FloatAttr(0.0f),
rewriter.getF32FloatAttr(std::numeric_limits<float>::max()),
op, outTy, self, minFloatAttr, maxFloatAttr,
/*nan_mode=*/rewriter.getStringAttr("PROPAGATE"));
return success();
}
Expand Down Expand Up @@ -5186,10 +5204,30 @@ LogicalResult ConvertAtenOp<AtenClampOp>::matchAndRewrite(
op, outType, adaptor.getSelf(), minIntAttr, maxIntAttr,
/*nan_mode=*/rewriter.getStringAttr("PROPAGATE"));
} else {
FloatAttr minFloatAttr = rewriter.getF32FloatAttr(
isMinNotNone ? minFloat : std::numeric_limits<float>::lowest());
FloatAttr maxFloatAttr = rewriter.getF32FloatAttr(
isMaxNotNone ? maxFloat : std::numeric_limits<float>::max());
FloatAttr minFloatAttr, maxFloatAttr;
if (outElemTy.isF16()) {
minFloatAttr =
rewriter.getF16FloatAttr(isMinNotNone ? minFloat : Float16Lowest);
maxFloatAttr =
rewriter.getF16FloatAttr(isMaxNotNone ? maxFloat : Float16Max);
} else if (outElemTy.isBF16()) {
minFloatAttr = rewriter.getFloatAttr(
rewriter.getBF16Type(), isMinNotNone ? minFloat : BFloat16Lowest);
maxFloatAttr = rewriter.getFloatAttr(
rewriter.getBF16Type(), isMaxNotNone ? maxFloat : BFloat16Max);
} else if (outElemTy.isF32()) {
minFloatAttr = rewriter.getF32FloatAttr(
isMinNotNone ? minFloat : std::numeric_limits<float>::lowest());
maxFloatAttr = rewriter.getF32FloatAttr(
isMaxNotNone ? maxFloat : std::numeric_limits<float>::max());
} else if (outElemTy.isF64()) {
minFloatAttr = rewriter.getF64FloatAttr(
isMinNotNone ? minFloat : std::numeric_limits<double>::lowest());
maxFloatAttr = rewriter.getF64FloatAttr(
isMaxNotNone ? maxFloat : std::numeric_limits<double>::max());
} else {
return rewriter.notifyMatchFailure(op, "Unsupported floating-point type");
}

rewriter.replaceOpWithNewOp<tosa::ClampOp>(
op, outType, adaptor.getSelf(), minFloatAttr, maxFloatAttr,
Expand Down Expand Up @@ -8547,14 +8585,29 @@ LogicalResult ConvertAtenOp<AtenLogitOp>::matchAndRewrite(

auto zi = self;

FloatAttr minFloatAttr, maxFloatAttr;
if (resultElemTy.isF16()) {
minFloatAttr = rewriter.getF16FloatAttr(eps);
maxFloatAttr = rewriter.getF16FloatAttr(1 - eps);
} else if (resultElemTy.isBF16()) {
minFloatAttr = rewriter.getFloatAttr(rewriter.getBF16Type(), eps);
maxFloatAttr = rewriter.getFloatAttr(rewriter.getBF16Type(), 1 - eps);
} else if (resultElemTy.isF32()) {
minFloatAttr = rewriter.getF32FloatAttr(eps);
maxFloatAttr = rewriter.getF32FloatAttr(1 - eps);
} else if (resultElemTy.isF64()) {
minFloatAttr = rewriter.getF64FloatAttr(eps);
maxFloatAttr = rewriter.getF64FloatAttr(1 - eps);
} else {
return rewriter.notifyMatchFailure(op, "Unsupported floating-point type");
}

// Clamp input to [eps, 1 - eps] when eps is not None
// Use default NaN Propagation mode "PROPAGATE" for tosa.clamp
if (!isEpsNone) {
zi = rewriter
.create<tosa::ClampOp>(
op->getLoc(), resultType, self,
rewriter.getF32FloatAttr(static_cast<float>(eps)),
rewriter.getF32FloatAttr(static_cast<float>(1 - eps)),
op->getLoc(), resultType, self, minFloatAttr, maxFloatAttr,
/*nan_mode=*/rewriter.getStringAttr("PROPAGATE"))
.getResult();
}
Expand Down
20 changes: 20 additions & 0 deletions projects/pt1/e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@
"ReflectionPad3dModuleBack_basic",
# RuntimeError: Unknown function SliceOutOfLowerBoundEndIndexModule
"NativeGroupNormModule_basic",
# error: argument must be a memref of f32, f64, i32, i64, i8, i1, c32, c64, but got 'memref<3x5xbf16>'
"ElementwiseClampMaxModule_bfloat16",
"ElementwiseClampMinModule_bfloat16",
"ElementwiseClampModule_bfloat16",
"ElementwiseReluModule_bfloat16",
}

FX_IMPORTER_CRASHING_SET = LINALG_CRASHING_SET | {
Expand Down Expand Up @@ -988,6 +993,11 @@
"NativeGroupNormModule_basic",
"AvgPool2dCeilModeFullDimIndivisibleByStrideModule_basic",
"MaxPool2dCeilModeFullDimIndivisibleByStrideModule_basic",
# error: argument must be a memref of f32, f64, i32, i64, i8, i1, c32, c64, but got 'memref<3x5xbf16>'
"ElementwiseClampMaxModule_bfloat16",
"ElementwiseClampMinModule_bfloat16",
"ElementwiseClampModule_bfloat16",
"ElementwiseReluModule_bfloat16",
}

FX_IMPORTER_STABLEHLO_CRASHING_SET = {
Expand Down Expand Up @@ -3392,6 +3402,11 @@
# RuntimeError: Given input size: (1x1x1). Calculated output size: (1x0x0). Output size is too small
"AvgPool2dWithoutPadFullDimIndivisibleByStrideModule_basic",
"MaxPool2dWithoutPadFullDimIndivisibleByStrideModule_basic",
# error: argument must be a memref of f32, f64, i32, i64, i8, i1, c32, c64, but got 'memref<?x?xbf16>'
"ElementwiseClampMaxModule_bfloat16",
"ElementwiseClampMinModule_bfloat16",
"ElementwiseClampModule_bfloat16",
"ElementwiseReluModule_bfloat16",
}

if torch_version_for_comparison() < version.parse("2.3.0.dev"):
Expand Down Expand Up @@ -3958,6 +3973,11 @@
"ReplicationPad1dModule_3DInput_basic",
"ReplicationPad3dModule_basic",
"ReplicationPad3dModuleSingleIntPad_basic",
# error: argument must be a memref of f32, f64, i32, i64, i8, i1, c32, c64, but got 'memref<3x5xbf16>'
"ElementwiseClampMaxModule_bfloat16",
"ElementwiseClampMinModule_bfloat16",
"ElementwiseClampModule_bfloat16",
"ElementwiseReluModule_bfloat16",
}

ONNX_TOSA_CRASHING_SET = {
Expand Down
Loading
Loading