Skip to content
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

[TOSA] TOSA 1.0 updates for LLVM hash 64edde66 #3978

Open
wants to merge 3 commits 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
11 changes: 11 additions & 0 deletions include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ void CreateReplaceOpAndInfer(PatternRewriter &rewriter, Operation *op,
LogicalResult getAvgPool2dAccType(PatternRewriter &rewriter, Value input,
TypeAttr &accType);

// Get accumulator type for TOSA convolution ops
LogicalResult getConvOpsAccType(PatternRewriter &rewriter,
RankedTensorType inputTy,
RankedTensorType weightTy,
RankedTensorType outputTy, TypeAttr &accType);

// Temporary function to get TOSA const shape
// TODO: Remove this function when getTosaConstShape is available in
// externals/llvm-project/mlir/include/mlir/Dialect/Tosa/Utils/ConversionUtils.h
Value getTosaConstShape(PatternRewriter &rewriter, Location loc,
llvm::ArrayRef<int64_t> shape);
} // namespace tosa
} // namespace mlir

Expand Down
65 changes: 44 additions & 21 deletions lib/Conversion/TorchToTosa/TorchToTosa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/Dialect/Tosa/Utils/ConversionUtils.h"
#include "mlir/IR/Matchers.h"
#include "mlir/Transforms/DialectConversion.h"
#include "torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h"
Expand Down Expand Up @@ -2252,6 +2253,12 @@ LogicalResult ConvertAtenOp<AtenConvolutionOp>::matchAndRewrite(
return rewriter.notifyMatchFailure(op,
"non-const dilation list unsupported");

TypeAttr accType;
if (failed(tosa::getConvOpsAccType(rewriter, inputTy, weightTy, outputTy,
accType)))
return rewriter.notifyMatchFailure(
op, "failed to get accumulator type for convolution ops");

// TOSA works in NHWC and takes OHWI (conv) / HWIM (depthwise conv) weights.
// Perform the necessary transformations.
std::optional<Value> nchwToNhwcTransposeConst =
Expand Down Expand Up @@ -2365,12 +2372,12 @@ LogicalResult ConvertAtenOp<AtenConvolutionOp>::matchAndRewrite(
// full convolution
convOpResult =
rewriter
.create<tosa::Conv2DOp>(op->getLoc(),
getTypeConverter()->convertType(convOpTy),
transposedInput, transformedWeight, bias,
rewriter.getDenseI64ArrayAttr(padding),
rewriter.getDenseI64ArrayAttr(stride),
rewriter.getDenseI64ArrayAttr(dilation))
.create<tosa::Conv2DOp>(
op->getLoc(), getTypeConverter()->convertType(convOpTy),
transposedInput, transformedWeight, bias,
rewriter.getDenseI64ArrayAttr(padding),
rewriter.getDenseI64ArrayAttr(stride),
rewriter.getDenseI64ArrayAttr(dilation), accType)
.getResult();
} else if (weightShape[1] == 1) {
// depthwise convolution
Expand All @@ -2381,7 +2388,7 @@ LogicalResult ConvertAtenOp<AtenConvolutionOp>::matchAndRewrite(
transposedInput, transformedWeight, bias,
rewriter.getDenseI64ArrayAttr(padding),
rewriter.getDenseI64ArrayAttr(stride),
rewriter.getDenseI64ArrayAttr(dilation))
rewriter.getDenseI64ArrayAttr(dilation), accType)
.getResult();
} else {
llvm_unreachable("Unhandled convolution type");
Expand Down Expand Up @@ -3909,9 +3916,11 @@ LogicalResult ConvertAtenOp<AtenBroadcastToOp>::matchAndRewrite(
}
}

auto result = rewriter.create<tosa::TileOp>(
op->getLoc(), resultType, reshapedInput,
rewriter.getDenseI64ArrayAttr(tileOpShape));
auto tileOpMultiples =
tosa::getTosaConstShape(rewriter, op->getLoc(), tileOpShape);

auto result = rewriter.create<tosa::TileOp>(op->getLoc(), resultType,
reshapedInput, tileOpMultiples);

rewriter.replaceOp(op, {result.getResult()});
}
Expand Down Expand Up @@ -4104,9 +4113,11 @@ LogicalResult ConvertAtenOp<AtenIndexSelectOp>::matchAndRewrite(
RankedTensorType::get(makeShapeLLVMCompatible(expandedIndicesShape),
rewriter.getIntegerType(32));

auto tileOpMultiples =
tosa::getTosaConstShape(rewriter, op->getLoc(), tileShape);

auto expandedIndices = rewriter.create<tosa::TileOp>(
op->getLoc(), tileType, reshapedIndices.getResult(),
rewriter.getDenseI64ArrayAttr(tileShape));
op->getLoc(), tileType, reshapedIndices.getResult(), tileOpMultiples);

// convert torch style index and dim into tf style indices
// tensor<[1,4,2],si64> -> tensor<[1,4,2,3],si64>
Expand Down Expand Up @@ -4445,17 +4456,23 @@ LogicalResult ConvertAtenOp<AtenIndexTensorHackedTwinOp>::matchAndRewrite(
if (needsTiling) {
auto idxType =
dyn_cast<RankedTensorType>(indicesTfConcatTensors[i].getType());

// indicesTfConcatTensors has a trailing [1] dim for the final concat.
auto maxRankMaxDimShapeTf(maxRankMaxDimShape);
maxRankMaxDimShapeTf.push_back(1);

auto tileOpShapeTf(tileOpShape);
tileOpShapeTf.push_back(1);

auto tileOutputTy = RankedTensorType::get(maxRankMaxDimShapeTf,
idxType.getElementType());
auto reshapedIdxTensor = indicesTfConcatTensors[i];

auto tileOpMultiples =
tosa::getTosaConstShape(rewriter, op->getLoc(), tileOpShapeTf);

indicesTfConcatTensors[i] = rewriter.create<tosa::TileOp>(
op->getLoc(), tileOutputTy, reshapedIdxTensor,
rewriter.getDenseI64ArrayAttr(tileOpShapeTf));
op->getLoc(), tileOutputTy, reshapedIdxTensor, tileOpMultiples);
}

// Every index tensor now has the same rank and shape
Expand Down Expand Up @@ -6023,12 +6040,14 @@ class ConvertAtenFillOp : public OpConversionPattern<AtenOpT> {
op->getLoc(), fillValueMatchedInputRankType, fillValue,
rewriter.getDenseI64ArrayAttr(fillValueMatchedInputRankShape));

auto tileOpMultiples =
tosa::getTosaConstShape(rewriter, op->getLoc(), outType.getShape());

fillValueTargetTensor = rewriter.create<tosa::TileOp>(
op->getLoc(),
RankedTensorType::get(makeShapeTorchCompatible(outType.getShape()),
fillValueElemTy),
fillValueMatchedInputRankTensor.getResult(),
makeShapeTorchCompatible(outType.getShape()));
fillValueMatchedInputRankTensor.getResult(), tileOpMultiples);
} else {
if (failed(torchScalarToTosaTensor(
rewriter, op, op.getValue(), fillValueTargetTensor, outElemTy,
Expand Down Expand Up @@ -6179,7 +6198,7 @@ LogicalResult ConvertAtenOp<AtenConstantPadNdOp>::matchAndRewrite(
}

DenseElementsAttr paddingAttr = DenseIntElementsAttr::get(
RankedTensorType::get({rank, 2}, rewriter.getI64Type()),
RankedTensorType::get({2 * rank}, rewriter.getI64Type()),
translatePadsList);

Value padsList1 = rewriter.create<mlir::tosa::ConstOp>(
Expand Down Expand Up @@ -7836,9 +7855,11 @@ LogicalResult ConvertAtenOp<AtenOuterOp>::matchAndRewrite(
resultType.getElementType()),
self, rewriter.getDenseI64ArrayAttr(resultShapeIndex1Replaced));

auto selfTileOpMultiples = tosa::getTosaConstShape(rewriter, op->getLoc(),
resultShapeIndex0Replaced);

auto selfTiled = rewriter.create<tosa::TileOp>(
op->getLoc(), resultType, selfReshaped.getResult(),
rewriter.getDenseI64ArrayAttr(resultShapeIndex0Replaced));
op->getLoc(), resultType, selfReshaped.getResult(), selfTileOpMultiples);

// Reshape and tile vec2 to shape {resultShape[0], vec2Shape[0]}
auto vec2Reshaped = rewriter.create<tosa::ReshapeOp>(
Expand All @@ -7847,9 +7868,11 @@ LogicalResult ConvertAtenOp<AtenOuterOp>::matchAndRewrite(
resultType.getElementType()),
vec2, rewriter.getDenseI64ArrayAttr(resultShapeIndex0Replaced));

auto vec2TileOpMultiples = tosa::getTosaConstShape(rewriter, op->getLoc(),
resultShapeIndex1Replaced);

auto vec2Tiled = rewriter.create<tosa::TileOp>(
op->getLoc(), resultType, vec2Reshaped.getResult(),
rewriter.getDenseI64ArrayAttr(resultShapeIndex1Replaced));
op->getLoc(), resultType, vec2Reshaped.getResult(), vec2TileOpMultiples);

auto result =
tosa::createMulOpAndCast(rewriter, op, resultType, selfTiled.getResult(),
Expand Down
6 changes: 4 additions & 2 deletions lib/Conversion/TorchToTosa/TosaLegalizeCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//

#include "torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h"
#include "mlir/Dialect/Tosa/Utils/ConversionUtils.h"
#include "torch-mlir/Conversion/Utils/Utils.h"
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"

Expand Down Expand Up @@ -566,11 +567,12 @@ std::optional<Value> convertScatterNdOp(PatternRewriter &rewriter,

// [0] -> [0,0,0]
SmallVector<int64_t, 1> tileShape({W}); // {3}
auto tileOpMultiples =
tosa::getTosaConstShape(rewriter, op->getLoc(), tileShape);
auto tosaFillValuesTileOp = tosa::CreateOpAndInfer<tosa::TileOp>(
rewriter, op->getLoc(),
GetTypeFromTensorShape(tileShape, fillValuesType.getElementType()),
tosaFillValuesOneReshapeOp.getResult(),
rewriter.getDenseI64ArrayAttr(tileShape));
tosaFillValuesOneReshapeOp.getResult(), tileOpMultiples);

// [0,0,0] -> [[0,0,0]]
SmallVector<int64_t, 2> newTosaFillValuesShape({N, W}); // {1,3}
Expand Down
58 changes: 58 additions & 0 deletions lib/Conversion/TorchToTosa/TosaLegalizeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,63 @@ LogicalResult getAvgPool2dAccType(PatternRewriter &rewriter, Value input,
return success();
}

// Get accumulator type for TOSA convolution ops
LogicalResult getConvOpsAccType(PatternRewriter &rewriter,
RankedTensorType inputTy,
RankedTensorType weightTy,
RankedTensorType outputTy, TypeAttr &accType) {
auto inputElemTy = inputTy.getElementType();
auto weightElemTy = weightTy.getElementType();
auto outputElemTy = outputTy.getElementType();

auto quantTy = dyn_cast<quant::QuantizedType>(inputElemTy);
if (quantTy)
inputElemTy = quantTy.getStorageType();

// Get TOSA conv ops acc type based on input, weight, and output types
// according to the spec:
// https://www.mlplatform.org/tosa/tosa_spec.html#_conv2d
// https://www.mlplatform.org/tosa/tosa_spec.html#_depthwise_conv2d
// https://www.mlplatform.org/tosa/tosa_spec.html#_conv3d
//
// For undefined dtypes in TOSA like I64 and F64, acc_type will be set to the
// output type but does not offer any guarantee on the numerical precision
// since such cases will fail TOSA validation.
if ((inputElemTy.isF32() && weightElemTy.isF32() && outputElemTy.isF32()) ||
(inputElemTy.isF16() && weightElemTy.isF16() && outputElemTy.isF16()) ||
(inputElemTy.isBF16() && weightElemTy.isBF16() &&
outputElemTy.isBF16())) {
accType = mlir::TypeAttr::get(rewriter.getF32Type());
} else if (inputElemTy.isInteger(8) &&
(weightElemTy.isInteger(8) || weightElemTy.isInteger(4)) &&
outputElemTy.isInteger(32)) {
accType = mlir::TypeAttr::get(rewriter.getIntegerType(32));
} else if (inputElemTy.isInteger(16) && weightElemTy.isInteger(8) &&
outputElemTy.isInteger(48)) {
accType = mlir::TypeAttr::get(rewriter.getIntegerType(48));
} else if ((inputElemTy.isFloat8E4M3() && weightElemTy.isFloat8E4M3() &&
outputElemTy.isF16()) ||
(inputElemTy.isFloat8E5M2() && weightElemTy.isFloat8E5M2() &&
outputElemTy.isF16())) {
accType = mlir::TypeAttr::get(rewriter.getF16Type());
} else {
accType = mlir::TypeAttr::get(outputElemTy);
}

return success();
}

// Temporary function to get TOSA const shape
// TODO: Remove this function when getTosaConstShape is available in
// externals/llvm-project/mlir/include/mlir/Dialect/Tosa/Utils/ConversionUtils.h
Value getTosaConstShape(PatternRewriter &rewriter, Location loc,
llvm::ArrayRef<int64_t> shape) {
auto attr = rewriter.getIndexTensorAttr(shape);
auto type = mlir::tosa::shapeType::get(rewriter.getContext(), shape.size());
mlir::Operation *mlir_op =
rewriter.create<tosa::ConstShapeOp>(loc, type, attr);
return mlir_op->getResult(0);
}

} // namespace tosa
} // namespace mlir
Loading
Loading