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

[Torch] emit AtenPixelUnshuffleOp and add decomposition #3252

Open
wants to merge 12 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
24 changes: 24 additions & 0 deletions include/torch-mlir/Dialect/Torch/IR/GeneratedTorchOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -7502,6 +7502,30 @@ def Torch_AtenPixelShuffleOp : Torch_Op<"aten.pixel_shuffle", [
}];
}

def Torch_AtenPixelUnshuffleOp : Torch_Op<"aten.pixel_unshuffle", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::pixel_unshuffle : (Tensor, int) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self,
Torch_IntType:$downscale_factor
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenPixelUnshuffleOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 2, 1);
}
void AtenPixelUnshuffleOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 2, 1);
}
}];
}

def Torch_AtenPermuteOp : Torch_Op<"aten.permute", [
AllowsTypeRefinement,
ReadOnly
Expand Down
54 changes: 54 additions & 0 deletions lib/Dialect/Torch/Transforms/AbstractInterpLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7242,6 +7242,56 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %15 = torch.aten.append.t %6, %14 : !torch.list<int>, !torch.int -> !torch.list<int>\n"
" return %6 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.pixel_unshuffle\"(%arg0: !torch.list<int>, %arg1: !torch.int) -> !torch.list<int> {\n"
" %int1 = torch.constant.int 1\n"
" %int-3 = torch.constant.int -3\n"
" %str = torch.constant.str \"AssertionError: number of input height must be divisible by downscale_factor in pixel_unshuffle\"\n"
" %int-2 = torch.constant.int -2\n"
" %str_0 = torch.constant.str \"AssertionError: number of input width must be divisible by downscale_factor in pixel_unshuffle\"\n"
" %int-1 = torch.constant.int -1\n"
" %none = torch.constant.none\n"
" %str_1 = torch.constant.str \"AssertionError: input must be at least rank-3 in pixel_unshuffle\"\n"
" %int3 = torch.constant.int 3\n"
" %int0 = torch.constant.int 0\n"
" %0 = torch.aten.len.t %arg0 : !torch.list<int> -> !torch.int\n"
" %1 = torch.aten.ge.int %0, %int3 : !torch.int, !torch.int -> !torch.bool\n"
" torch.prim.If %1 -> () {\n"
" torch.prim.If.yield\n"
" } else {\n"
" torch.prim.RaiseException %str_1, %none : !torch.str, !torch.none\n"
" torch.prim.If.yield\n"
" }\n"
" %2 = torch.aten.__getitem__.t %arg0, %int-1 : !torch.list<int>, !torch.int -> !torch.int\n"
" %3 = torch.aten.remainder.int %2, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %4 = torch.aten.eq.int %3, %int0 : !torch.int, !torch.int -> !torch.bool\n"
" torch.prim.If %4 -> () {\n"
" torch.prim.If.yield\n"
" } else {\n"
" torch.prim.RaiseException %str_0, %none : !torch.str, !torch.none\n"
" torch.prim.If.yield\n"
" }\n"
" %5 = torch.aten.__getitem__.t %arg0, %int-2 : !torch.list<int>, !torch.int -> !torch.int\n"
" %6 = torch.aten.remainder.int %5, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %7 = torch.aten.eq.int %6, %int0 : !torch.int, !torch.int -> !torch.bool\n"
" torch.prim.If %7 -> () {\n"
" torch.prim.If.yield\n"
" } else {\n"
" torch.prim.RaiseException %str, %none : !torch.str, !torch.none\n"
" torch.prim.If.yield\n"
" }\n"
" %8 = torch.aten.slice.t %arg0, %int0, %int-3, %int1 : !torch.list<int>, !torch.int, !torch.int, !torch.int -> !torch.list<int>\n"
" %9 = torch.aten.__getitem__.t %arg0, %int-3 : !torch.list<int>, !torch.int -> !torch.int\n"
" %10 = torch.aten.mul.int %9, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %11 = torch.aten.mul.int %10, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %12 = torch.aten.append.t %8, %11 : !torch.list<int>, !torch.int -> !torch.list<int>\n"
" %13 = torch.aten.__getitem__.t %arg0, %int-2 : !torch.list<int>, !torch.int -> !torch.int\n"
" %14 = torch.aten.floordiv.int %13, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %15 = torch.aten.append.t %8, %14 : !torch.list<int>, !torch.int -> !torch.list<int>\n"
" %16 = torch.aten.__getitem__.t %arg0, %int-1 : !torch.list<int>, !torch.int -> !torch.int\n"
" %17 = torch.aten.floordiv.int %16, %arg1 : !torch.int, !torch.int -> !torch.int\n"
" %18 = torch.aten.append.t %8, %17 : !torch.list<int>, !torch.int -> !torch.list<int>\n"
" return %8 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.permute\"(%arg0: !torch.list<int>, %arg1: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.permute(%arg0, %arg1) : (!torch.list<int>, !torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
Expand Down Expand Up @@ -9921,6 +9971,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.pixel_unshuffle\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.int) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.avg_pool1d\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.list<int>, %arg2: !torch.list<int>, %arg3: !torch.list<int>, %arg4: !torch.bool, %arg5: !torch.bool) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n"
Expand Down
196 changes: 152 additions & 44 deletions lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,36 @@ class DecomposeAtenLinalgCrossOp : public OpRewritePattern<AtenLinalgCrossOp> {
};
} // namespace

namespace {
static ValueTensorType getTypeFromShape(Type dtype, ArrayRef<Value> vals) {
// Get a vector of integers from a vector of Values.
auto getIntShape = [](auto &&vals) {
SmallVector<int64_t> shape;
shape.reserve(vals.size());
for (auto v : vals) {
int64_t cst_val;
if (matchPattern(v, m_TorchConstantInt(&cst_val))) {
shape.push_back(cst_val);
} else {
shape.push_back(kUnknownSize);
}
}
return shape;
};
auto intShape = getIntShape(vals);
return ValueTensorType::get(vals[0].getContext(), intShape, dtype);
}

// Get the size of the dimension 'i'. Note the use of 'createOrFold' instead
// of 'create': if the dimension size is known, then the AtenSizeIntOp is
// folded to a ConstantOp.
static Value getDimSize(PatternRewriter &rewriter, Value tensor, int64_t dim) {
auto loc = tensor.getLoc();
auto dimVal =
rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(dim));
return rewriter.createOrFold<AtenSizeIntOp>(loc, tensor, dimVal);
}

// Decompose aten.pixel_shuffle into: prims.split_dim, aten.permute, and
// prims.collapse operations.
//
Expand All @@ -2092,7 +2122,6 @@ class DecomposeAtenLinalgCrossOp : public OpRewritePattern<AtenLinalgCrossOp> {
// X = X.collapse(...) # shape (*leading_dims, C, r*H, r*W)
//
// 'r' above is referred to as the 'upscale factor' or just 'factor' below.
namespace {
class DecomposeAtenPixelShuffleOp
: public OpRewritePattern<AtenPixelShuffleOp> {
public:
Expand Down Expand Up @@ -2122,41 +2151,10 @@ class DecomposeAtenPixelShuffleOp

const auto inOptionalDType = inType.getOptionalDtype();

auto getTypeFromShape = [inOptionalDType](auto &&vals) {
// Get a vector of integers from a vector of Values.
auto getIntShape = [](auto &&vals) {
SmallVector<int64_t> shape;
shape.reserve(vals.size());
for (auto v : vals) {
int64_t cst_val;
if (matchPattern(v, m_TorchConstantInt(&cst_val))) {
shape.push_back(cst_val);
} else {
shape.push_back(kUnknownSize);
}
}
return shape;
};

const auto intShape = getIntShape(vals);
return ValueTensorType::get(vals[0].getContext(),
llvm::ArrayRef(intShape), inOptionalDType);
};

auto nLeadingDims = inRank - 3;

// Get the size of the dimension 'i'. Note the use of 'createOrFold' instead
// of 'create': if the dimension size is known, then the AtenSizeIntOp is
// folded to a ConstantOp.
auto getDimSize = [&](uint64_t i) -> Value {
Value dim =
rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(i));
return rewriter.createOrFold<AtenSizeIntOp>(loc, inValue, dim);
};

auto inC = getDimSize(inRank - 3);
auto inH = getDimSize(inRank - 2);
auto inW = getDimSize(inRank - 1);
auto inC = getDimSize(rewriter, inValue, inRank - 3);
auto inH = getDimSize(rewriter, inValue, inRank - 2);
auto inW = getDimSize(rewriter, inValue, inRank - 1);

auto factor = op.getUpscaleFactor();

Expand Down Expand Up @@ -2214,24 +2212,24 @@ class DecomposeAtenPixelShuffleOp
auto partiallyExpanded =
rewriter
.create<PrimsSplitDimOp>(
loc, getTypeFromShape(partiallyExpandedShape), inValue,
dimensionConstants[nLeadingDims], outC)
loc, getTypeFromShape(inOptionalDType, partiallyExpandedShape),
inValue, dimensionConstants[nLeadingDims], outC)
.getResult();

// Split new dimension factorSquared -> (factor, factor)
auto fullyExpanded = rewriter.create<PrimsSplitDimOp>(
loc, getTypeFromShape(prePermuteShape), partiallyExpanded,
dimensionConstants[nLeadingDims + 1], factor);
loc, getTypeFromShape(inOptionalDType, prePermuteShape),
partiallyExpanded, dimensionConstants[nLeadingDims + 1], factor);

// Perform the permutation
auto permuted =
rewriter.create<AtenPermuteOp>(loc, getTypeFromShape(postPermuteShape),
fullyExpanded, permuteDimsOrder);
auto permuted = rewriter.create<AtenPermuteOp>(
loc, getTypeFromShape(inOptionalDType, postPermuteShape), fullyExpanded,
permuteDimsOrder);

// Collapse final 2 dimension
auto partiallyCollapsed = rewriter.create<PrimsCollapseOp>(
loc, getTypeFromShape(partiallyCollapsedShape), permuted,
dimensionConstants[nLeadingDims + 3],
loc, getTypeFromShape(inOptionalDType, partiallyCollapsedShape),
permuted, dimensionConstants[nLeadingDims + 3],
dimensionConstants[nLeadingDims + 4]);

// Collapse back to original rank
Expand All @@ -2243,6 +2241,115 @@ class DecomposeAtenPixelShuffleOp
return success();
}
};

// Decompose aten.pixel_unshuffle into: prims.split_dim, aten.permute, and
// prims.collapse operations.
//
// If input is a tensor of shape
// (*leading_dims, C, H*r, W*r),
//
// where leading_dims is of size N, then
// X = pixel_unshuffle(input, downscale_factor)
//
// gets replaced with
// X = input.split_dim(...) # shape (*leading_dims, C, H*r, W, r)
// X = X.split_dim(...) # shape (*leading_dims, C, H, r, W, r)
// X = X.permute(0, ..., N, N+2, N+4, N+1, N+3)
// # shape (*leading_dims, C, r, r, H, W)
// X = X.collapse(...) # shape (*leading_dims, C, r*H, r*W)
class DecomposeAtenPixelUnshuffleOp
: public OpRewritePattern<AtenPixelUnshuffleOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(AtenPixelUnshuffleOp op,
PatternRewriter &rewriter) const override {

Location loc = op.getLoc();
Value inValue = op.getSelf();
auto inType = cast<BaseTensorType>(inValue.getType());
auto maybeSizes = inType.getOptionalSizes();
if (!maybeSizes) {
return rewriter.notifyMatchFailure(
op, "Expected input tensor to have known rank.");
}
auto inShape = maybeSizes.value();
auto inRank = inShape.size();

const auto inOptionalDType = inType.getOptionalDtype();

auto nLeadingDims = inRank - 3;
auto inC = getDimSize(rewriter, inValue, inRank - 3);
auto inH = getDimSize(rewriter, inValue, inRank - 2);
auto inW = getDimSize(rewriter, inValue, inRank - 1);

auto factor = op.getDownscaleFactor();

Value factorSquared =
rewriter.createOrFold<AtenMulIntOp>(loc, factor, factor);

Value outC = rewriter.createOrFold<AtenMulIntOp>(loc, inC, factorSquared);

Value outH = rewriter.createOrFold<AtenFloordivIntOp>(loc, inH, factor);
Value outW = rewriter.createOrFold<AtenFloordivIntOp>(loc, inW, factor);

SmallVector<Value> dimensionConstants;
dimensionConstants.reserve(inRank + 2);
for (unsigned i = 0; i < inRank + 2; ++i) {
dimensionConstants.push_back(
rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(i)));
}

SmallVector<Value> leadingDims;
leadingDims.reserve(nLeadingDims);
for (unsigned i = 0; i < nLeadingDims; ++i) {
Value leadingDimSize = rewriter.createOrFold<AtenSizeIntOp>(
loc, inValue, dimensionConstants[i]);
leadingDims.push_back(leadingDimSize);
}

SmallVector<Value> partiallyExpandedShape0 = leadingDims;
partiallyExpandedShape0.append({inC, inH, outW, factor});

SmallVector<Value> partiallyExpandedShape1 = leadingDims;
partiallyExpandedShape1.append({inC, outH, factor, outW, factor});

SmallVector<Value> postPermuteShape = leadingDims;
postPermuteShape.append({inC, factor, factor, outH, outW});

SmallVector<Value> partiallyCollapsedShape = leadingDims;
partiallyCollapsedShape.append({outC, outH, outW});

SmallVector<Value> outShape = leadingDims;
outShape.append({outC, outH, outW});

SmallVector<Value> permutation{dimensionConstants.begin(),
dimensionConstants.begin() + nLeadingDims};
SmallVector<uint64_t> permutationTail{0, 2, 4, 1, 3};
for (uint64_t d : permutationTail) {
permutation.push_back(dimensionConstants[nLeadingDims + d]);
}

Value partiallyExpanded = rewriter.create<PrimsSplitDimOp>(
loc, getTypeFromShape(inOptionalDType, partiallyExpandedShape0),
inValue, dimensionConstants[nLeadingDims + 2], outW);

Value fullyExpanded = rewriter.create<PrimsSplitDimOp>(
loc, getTypeFromShape(inOptionalDType, partiallyExpandedShape1),
partiallyExpanded, dimensionConstants[nLeadingDims + 1], outH);

Value permuted = rewriter.create<AtenPermuteOp>(
loc, getTypeFromShape(inOptionalDType, postPermuteShape), fullyExpanded,
rewriter.create<PrimListConstructOp>(
loc, Torch::ListType::get(Torch::IntType::get(op->getContext())),
permutation));

rewriter.replaceOpWithNewOp<PrimsCollapseOp>(
op, op.getType(), permuted, dimensionConstants[nLeadingDims + 0],
dimensionConstants[nLeadingDims + 2]);

return success();
}
};
} // namespace

// ReLU6(x) = min(max(0, x), 6) = min(Relu(x), 6)
Expand Down Expand Up @@ -7697,6 +7804,7 @@ class DecomposeComplexOpsPass
addPatternIfTargetOpIsIllegal<DecomposeAtenMvOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenLinalgCrossOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenPixelShuffleOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenPixelUnshuffleOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenTOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAten_LogSoftmaxBackwardDataOp>(
patterns);
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ static void markDecomposedOpsAsIllegal(MLIRContext *context,
target.addIllegalOp<AtenMvOp>();
target.addIllegalOp<AtenLinalgCrossOp>();
target.addIllegalOp<AtenPixelShuffleOp>();
target.addIllegalOp<AtenPixelUnshuffleOp>();
target.addIllegalOp<AtenTOp>();
target.addIllegalOp<Aten_LogSoftmaxBackwardDataOp>();
target.addDynamicallyLegalOp<AtenMatmulOp>([](AtenMatmulOp op) {
Expand Down
5 changes: 5 additions & 0 deletions projects/pt1/e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,8 @@
"PermuteNegativeIndexModule_basic",
"PixelShuffleModuleStaticRank3Int64_basic",
"PixelShuffleModuleStaticRank4Float32_basic",
"PixelUnshuffleModuleStaticRank3int64_basic",
"PixelUnshuffleModuleStaticRank4Float32_basic",
"PowIntFloatModule_basic",
"PrimListUnpackNumMismatchModule_basic",
"PrimMaxIntModule_basic",
Expand Down Expand Up @@ -2651,6 +2653,9 @@
"MaskedFillTensorFloatValueModule_basic",
"NativeDropoutTrainModule_basic",
"NativeDropoutTrainStaticShapeModule_basic",
"PixelUnshuffleModuleFullDynamic_basic",
"PixelUnshuffleModuleSpatiallyDynamic_basic",
"PixelUnshuffleModuleStaticRank3int64_basic",
"ReduceAnyFloatModule_basic",
"ReduceMaxAlongDimUnsignedInt_basic",
"ReduceMinAlongDimUnsignedInt_basic",
Expand Down
Loading
Loading