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

Add flow.tensor.bitcast for torch view as complex/real #18705

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace mlir::iree_compiler::TorchInput {

#define GEN_PASS_DEF_BITCASTQUANTTENSORPASS
#define GEN_PASS_DEF_BITCASTTENSORPASS
#include "compiler/plugins/input/Torch/InputConversion/Passes.h.inc"

namespace {
Expand Down Expand Up @@ -62,8 +62,44 @@ class BitCastViewDtype
}
};

class BitCastQuantizedMatmul
: public OpRewritePattern<torch::Torch::OperatorOp> {
template <typename BitcastOp>
class BitCastViewComplex : public OpRewritePattern<BitcastOp> {
public:
using OpRewritePattern<BitcastOp>::OpRewritePattern;
LogicalResult matchAndRewrite(BitcastOp op,
PatternRewriter &rewriter) const override {

Value in = op.getSelf();
auto loc = op.getLoc();
auto inType = cast<torch::Torch::ValueTensorType>(in.getType());
auto resultType = cast<torch::Torch::ValueTensorType>(op.getType());
auto bType = inType.toBuiltinTensor();

Value builtinCast =
rewriter.create<torch::TorchConversion::ToBuiltinTensorOp>(loc, bType,
in);
auto rType = resultType.toBuiltinTensor();

// Cast to the builtin tensor type.
llvm::SmallVector<Value> dynDims;
for (int i = 0, s = bType.getRank(); i < s; ++i) {
if (bType.isDynamicDim(i)) {
dynDims.push_back(rewriter.create<tensor::DimOp>(loc, builtinCast, i));
}
}

Value flowBitcast = rewriter.create<IREE::Flow::TensorBitCastOp>(
loc, rType, builtinCast, dynDims, dynDims);

auto torchCast =
rewriter.create<torch::TorchConversion::FromBuiltinTensorOp>(
loc, resultType, flowBitcast);
rewriter.replaceOp(op, torchCast);
return success();
}
};

class BitCastizedMatmul : public OpRewritePattern<torch::Torch::OperatorOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(torch::Torch::OperatorOp op,
Expand Down Expand Up @@ -148,8 +184,8 @@ class BitCastQuantizedMatmul
} // namespace

namespace {
class BitCastQuantTensorPass final
: public impl::BitCastQuantTensorPassBase<BitCastQuantTensorPass> {
class BitCastTensorPass final
: public impl::BitCastTensorPassBase<BitCastTensorPass> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<IREE::Flow::FlowDialect>();
registry.insert<torch::Torch::TorchDialect>();
Expand All @@ -159,7 +195,9 @@ class BitCastQuantTensorPass final
void runOnOperation() override {
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
patterns.add<BitCastQuantizedMatmul, BitCastViewDtype>(context);
patterns.add<BitCastizedMatmul, BitCastViewDtype,
BitCastViewComplex<torch::Torch::AtenViewAsComplexOp>,
BitCastViewComplex<torch::Torch::AtenViewAsRealOp>>(context);
if (failed(
applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
signalPassFailure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ iree_cc_library(
"Passes.h"
SRCS
"BindSymbolicShapes.cpp"
"BitCastQuantTensor.cpp"
"BitCastTensor.cpp"
"ConvertTMTensorToLinalgExt.cpp"
"FuncConversion.cpp"
"SetStrictSymbolicShapes.cpp"
Expand Down
2 changes: 1 addition & 1 deletion compiler/plugins/input/Torch/InputConversion/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void createTorchToIREEPipeline(
// now be simplified.
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
}
pm.addNestedPass<func::FuncOp>(createBitCastQuantTensorPass());
pm.addNestedPass<func::FuncOp>(createBitCastTensorPass());
pm.addNestedPass<func::FuncOp>(
torch::Torch::createReduceOpVariantsPass(llvm::StringRef()));
pm.addNestedPass<func::FuncOp>(
Expand Down
4 changes: 2 additions & 2 deletions compiler/plugins/input/Torch/InputConversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def BindSymbolicShapesPass :
let summary = "Process torch dynamic shape bindings into IREE analyzable forms";
}

def BitCastQuantTensorPass :
InterfacePass<"torch-iree-bitcast-quant-tensor", "mlir::FunctionOpInterface"> {
def BitCastTensorPass :
InterfacePass<"torch-iree-bitcast-tensor", "mlir::FunctionOpInterface"> {
let summary = "Bitcasts i8 packed tensors of sub-byte types to the actual bit width";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ iree_lit_test_suite(
"auto_input_conversion.mlir"
"attention.mlir"
"bind_symbolic_shapes.mlir"
"bitcast_quant_tensor.mlir"
"bitcast_tensor.mlir"
"func_conversion.mlir"
"func_conversion_invalid.mlir"
"scan.mlir"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: iree-opt %s --pass-pipeline="builtin.module(func.func(torch-iree-bitcast-quant-tensor))" -split-input-file -verify-diagnostics | FileCheck %s
// RUN: iree-opt %s --pass-pipeline="builtin.module(func.func(torch-iree-bitcast-tensor))" -split-input-file -verify-diagnostics | FileCheck %s

// CHECK-LABEL: func @forward
func.func @forward(%arg0: !torch.vtensor<[1,1,8],f16>) -> !torch.vtensor<[1,1,8],f16> {
Expand All @@ -24,3 +24,22 @@ func.func @view_type(%arg0 : !torch.vtensor<[295501824],ui8>) -> !torch.vtensor<
%0 = torch.aten.view.dtype %arg0, %int4 : !torch.vtensor<[295501824],ui8>, !torch.int -> !torch.vtensor<[147750912],si16>
return %0 : !torch.vtensor<[147750912],si16>
}


// -----

// CHECK-LABEL: @view_as_complex
func.func @view_as_complex(%arg0 : !torch.vtensor<[128,2],f32>) -> !torch.vtensor<[128], complex<f32>> {
// CHECK: flow.tensor.bitcast %[[IN:.+]] : tensor<128x2xf32> -> tensor<128xcomplex<f32>>
%0 = torch.aten.view_as_complex %arg0 : !torch.vtensor<[128,2],f32> -> !torch.vtensor<[128],complex<f32>>
return %0 : !torch.vtensor<[128],complex<f32>>
}

// -----

// CHECK-LABEL: @view_as_real
func.func @view_as_real(%arg0 : !torch.vtensor<[128], complex<f32>>) -> !torch.vtensor<[128,2],f32> {
// CHECK: flow.tensor.bitcast %[[IN:.+]] : tensor<128xcomplex<f32>> -> tensor<128x2xf32>
%0 = torch.aten.view_as_real %arg0 : !torch.vtensor<[128],complex<f32>> -> !torch.vtensor<[128,2],f32>
return %0 : !torch.vtensor<[128,2],f32>
}
Loading