Skip to content

Commit 2ecd187

Browse files
committed
fix: fix bugs in aten::to
Signed-off-by: Bo Wang <[email protected]>
1 parent 474e7bf commit 2ecd187

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

core/conversion/converters/impl/cast.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ auto cast_registrations TORCHTRT_UNUSED =
2626
return true;
2727
}})
2828
.pattern(
29+
{"aten::to.device(Tensor(a) self, Device device, int dtype, bool non_blocking=False, bool copy=False, int? memory_format=None) -> (Tensor(a))",
30+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
31+
// what this function does is basically the same with the previous one, however, we cannot lower this
32+
// signature to previous one because this will incur the device issues when we run Torchscript module in
33+
// later shape analysis phase of fallback
34+
auto self = args[0].ITensorOrFreeze(ctx);
35+
auto output_dtype = args[2].unwrapToScalar().to<int64_t>();
36+
37+
auto trt_dtype = util::ScalarTypeToTRTDataType(static_cast<at::ScalarType>(output_dtype));
38+
39+
auto casted_itensor = castITensor(ctx, self, trt_dtype);
40+
auto output = ctx->AssociateValueAndTensor(n->outputs()[0], casted_itensor);
41+
LOG_DEBUG("[aten::to.device] Output tensor shape: " << output->getDimensions());
42+
43+
return true;
44+
}})
45+
.pattern(
2946
{"aten::to.other(Tensor self, Tensor other, bool non_blocking=False, bool copy=False, int? memory_format=None) -> (Tensor)",
3047
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
3148
auto self = args[0].ITensorOrFreeze(ctx);

core/lowering/passes/reduce_to.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ namespace lowering {
88
namespace passes {
99

1010
void ReduceToOperation(std::shared_ptr<torch::jit::Graph>& graph) {
11-
std::string to_device_pattern = R"IR(
12-
graph(%x, %device, %dtype, %nb, %copy, %format):
13-
%out : Tensor = aten::to(%x, %device, %dtype, %nb, %copy, %format)
14-
return (%out))IR";
15-
std::string to_dtype_pattern = R"IR(
16-
graph(%x, %device, %dtype, %nb, %copy, %format):
17-
%out : Tensor = aten::to(%x, %dtype, %nb, %copy, %format)
18-
return (%out))IR";
1911
std::string to_dtype_layout_pattern = R"IR(
20-
graph(%x, %device, %dtype, %layout, %pm, %nb, %copy, %format):
21-
%out : Tensor = aten::to(%x, %device, %dtype, %layout, %pm, %nb, %copy, %format)
12+
graph(%x, %dtype, %layout, %device, %pm, %nb, %copy, %format):
13+
%out : Tensor = aten::to(%x, %dtype, %layout, %device, %pm, %nb, %copy, %format)
2214
return (%out))IR";
2315

2416
std::string to_dtype_multi_input_pattern = R"IR(
25-
graph(%x, %device, %dtype, %layout, %pm, %nb, %copy, %format):
26-
%out : Tensor = aten::to(%x, %dtype, %nb, %copy, %format)
17+
graph(%x, %dtype, %layout, %device, %pm, %nb, %copy, %format):
18+
%out : Tensor = aten::to(%x, %device, %dtype, %nb, %copy, %format)
2719
return (%out))IR";
2820

2921
std::string to_type_as_pattern = R"IR(
@@ -38,11 +30,6 @@ void ReduceToOperation(std::shared_ptr<torch::jit::Graph>& graph) {
3830
%out : Tensor = aten::to(%input, %other, %5, %5, %6)
3931
return (%out))IR";
4032

41-
// replace aten::to.device with aten::to.dtype
42-
torch::jit::SubgraphRewriter map_aten_device_to_dtype;
43-
map_aten_device_to_dtype.RegisterRewritePattern(to_device_pattern, to_dtype_pattern);
44-
map_aten_device_to_dtype.runOnGraph(graph);
45-
4633
// replace aten::to.dtype_layout with aten::to.dtype
4734
torch::jit::SubgraphRewriter map_aten_dtype_layout;
4835
map_aten_dtype_layout.RegisterRewritePattern(to_dtype_layout_pattern, to_dtype_multi_input_pattern);

0 commit comments

Comments
 (0)