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

tcp.custom_op support for torch.aten.arange with dynamic input #100

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 47 additions & 1 deletion lib/Conversion/TorchToTcp/TcpCustomOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,50 @@ class ConvertAtenSliceScatterOp
}
};

class ConvertAtenArangeStartStepOp
: public OpConversionPattern<AtenArangeStartStepOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(AtenArangeStartStepOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {

// At this point all tensors should have value semantics, and hence the
// `layout` check can be ignored.

// The pin_memory should be either `False` or `none`.
bool pinMemory;
if (!isa<Torch::NoneType>(op.getPinMemory().getType()) &&
(!matchPattern(op.getPinMemory(), m_TorchConstantBool(&pinMemory)) ||
pinMemory)) {
return rewriter.notifyMatchFailure(
op, "unimplemented: pin_memory must be either None or false");
}

torch_to_tcp::TorchToTcpCustomOpConversionHelper helper{op, rewriter,
getTypeConverter()};
bool allStatic = true;
if (!helper.tryConvertConstToFloatAttr("start", op.getStart())) {
allStatic = false;
helper.addOperand("start", adaptor.getStart());
}
if (!helper.tryConvertConstToFloatAttr("end", op.getEnd())) {
allStatic = false;
helper.addOperand("end", adaptor.getEnd());
}
if (!helper.tryConvertConstToFloatAttr("step", op.getStep())) {
allStatic = false;
helper.addOperand("step", adaptor.getStep());
}
// static start, end, and step case will be handled through TOSA dialect
if (allStatic)
return rewriter.notifyMatchFailure(op,
"only non-constant values supported");

return helper.replace();
}
};

} // namespace

void torch_to_tcp::populateTcpCustomOpPatternsAndLegality(
Expand All @@ -365,8 +409,10 @@ void torch_to_tcp::populateTcpCustomOpPatternsAndLegality(
INSERT_ATEN_TO_TCP_CUSTOM_OP_PATTERN(AtenCumsumOp);
INSERT_ATEN_TO_TCP_CUSTOM_OP_PATTERN(AtenMinDimOp);
INSERT_ATEN_TO_TCP_CUSTOM_OP_PATTERN(AtenSliceScatterOp);
// AtenViewOp can still live after torch-to-tcp conversion
// Following ops can still live after torch-to-tcp conversion
patterns.add<ConvertAtenViewOp>(typeConverter, patterns.getContext());
patterns.add<ConvertAtenArangeStartStepOp>(typeConverter,
patterns.getContext());
#undef INSERT_ATEN_TO_TCP_CUSTOM_OP_PATTERN

// Torch -> TOSA doesn't handle transposed convolutions; map them to
Expand Down
23 changes: 23 additions & 0 deletions lib/Conversion/TorchToTcp/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,29 @@ void TorchToTcpCustomOpConversionHelper::addFloatAttr(std::string attrName,
rewriter.getNamedAttr(attrName, rewriter.getF64FloatAttr(constVal)));
}

bool TorchToTcpCustomOpConversionHelper::tryConvertConstToFloatAttr(
std::string attrName, Value value) {
if (conversionResult.failed())
return false;

double constFPVal;
if (matchPattern(value, torch::Torch::m_TorchConstantFloat(&constFPVal))) {
attrs.push_back(
rewriter.getNamedAttr(attrName, rewriter.getF64FloatAttr(constFPVal)));
return true;
}

// convert constant int to fp if possible
int64_t constIntVal;
if (matchPattern(value, torch::Torch::m_TorchConstantInt(&constIntVal))) {
attrs.push_back(rewriter.getNamedAttr(
attrName, rewriter.getF64FloatAttr(static_cast<double>(constIntVal))));
return true;
}
Comment on lines +544 to +550
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to convert int to fp here? Why not convert them to IntAttr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aten.arange.start_step can take either int or fp for start and step arguments. However, trt-mlir only accepts F64Attr. Converting all constants to FPAttr saves us some time for type checking.

I can also add the conversion in tcp-to-trtmlir. LMKWYT

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Can you add a comment in the code to explain this?


return false;
}

void TorchToTcpCustomOpConversionHelper::addListOfIntsAttr(std::string attrName,
Value value) {
if (conversionResult.failed())
Expand Down
3 changes: 3 additions & 0 deletions lib/Conversion/TorchToTcp/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class TorchToTcpCustomOpConversionHelper {
// Add value as a named float attribute
void addFloatAttr(std::string attrName, Value value);

// Try to convert a const value to a float attribute.
bool tryConvertConstToFloatAttr(std::string attrName, Value value);

// Add value as a named list of integers attribute
void addListOfIntsAttr(std::string attrName, Value value);

Expand Down
19 changes: 19 additions & 0 deletions test/Conversion/TorchToTcp/tcp_custom_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,22 @@ func.func @torch.aten.slice_scatter(%arg0: !torch.vtensor<[1,3],f32>, %arg1: !to
%0 = torch.aten.slice_scatter %arg0, %arg1, %dim, %start, %end, %step : !torch.vtensor<[1,3],f32>, !torch.vtensor<[1,2],f32>, !torch.int, !torch.int, !torch.int, !torch.int -> !torch.vtensor<[1,3],f32>
return %0 : !torch.vtensor<[1,3],f32>
}

// -----

// CHECK-LABEL: func.func @torch.aten.arange.start_step(
// CHECK-SAME: %[[ARG0:.*]]: !torch.int) -> !torch.vtensor<[?],si32> {
// CHECK: %[[IN:.*]] = torch_c.to_i64 %[[ARG0]]
// CHECK: %[[OUT:.*]] = tcp.custom_op("torch.aten.arange.start_step") %[[IN]] {start = 0.000000e+00 : f64, step = 1.000000e+00 : f64, torch_operand_names = ["end"]} : i64 -> tensor<?xi32>
// CHECK: %[[RET:.*]] = torch_c.from_builtin_tensor %[[OUT]] : tensor<?xi32> -> !torch.vtensor<[?],si32>
// CHECK: return %[[RET]] : !torch.vtensor<[?],si32>
func.func @torch.aten.arange.start_step(%arg0: !torch.int) -> !torch.vtensor<[?],si32> {
%false = torch.constant.bool false
%none = torch.constant.none
%cpu = torch.constant.device "cpu"
%int0 = torch.constant.int 0
%int1 = torch.constant.int 1
%int3 = torch.constant.int 3
%1 = torch.aten.arange.start_step %int0, %arg0, %int1, %int3, %none, %cpu, %false : !torch.int, !torch.int, !torch.int, !torch.int, !torch.none, !torch.Device, !torch.bool -> !torch.vtensor<[?],si32>
return %1 : !torch.vtensor<[?],si32>
}