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

pnnx add missing Tensor.to pattern #4908

Merged
merged 1 commit into from
Aug 3, 2023
Merged
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
37 changes: 31 additions & 6 deletions tools/pnnx/src/pass_level2/Tensor_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ pnnx.Output output 1 0 out

op->params["copy"] = captured_params.at("copy");

if (captured_params.at("memory_format").i == 0)
op->params["memory_format"] = "torch.contiguous_format";
if (captured_params.at("memory_format").i == 1)
op->params["memory_format"] = "torch.preserve_format";
if (captured_params.at("memory_format").i == 2)
op->params["memory_format"] = "torch.channels_last";
if (captured_params.at("memory_format").type == 2)
{
if (captured_params.at("memory_format").i == 0)
op->params["memory_format"] = "torch.contiguous_format";
if (captured_params.at("memory_format").i == 1)
op->params["memory_format"] = "torch.preserve_format";
if (captured_params.at("memory_format").i == 2)
op->params["memory_format"] = "torch.channels_last";
}
}
};

Expand All @@ -83,7 +86,29 @@ pnnx.Output output 1 0 out
}
};

class Tensor_to_2 : public Tensor_to
{
public:
const char* match_pattern_graph() const
{
return R"PNNXIR(7767517
10 9
pnnx.Input input_0 0 1 input
prim::Constant op_0 0 1 dtype value=%dtype
prim::Constant op_1 0 1 layout value=*
prim::Constant op_2 0 1 device value=*
prim::Constant op_3 0 1 pin_memory value=*
prim::Constant op_4 0 1 non_blocking value=*
prim::Constant op_5 0 1 copy value=%copy
prim::Constant op_6 0 1 memory_format value=%memory_format
aten::to op_7 8 1 input dtype layout device pin_memory non_blocking copy memory_format out
pnnx.Output output 1 0 out
)PNNXIR";
}
};

REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_to, 20)
REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_to_1, 20)
REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(Tensor_to_2, 20)

} // namespace pnnx
3 changes: 2 additions & 1 deletion tools/pnnx/tests/test_Tensor_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def forward(self, x, y):
x = x.to(device='cpu', dtype=torch.int, copy=True)
x = x + 1
y = y - 2
return x, y
z = x.to(y.device)
return x, y, z

def test():
net = Model()
Expand Down
Loading