Elffuss BraidHub

kikocis/ncnn public

pnnx logaddexp (#5598)

Changesetcba7413d558466ff06b3efc438452773e8ef82af666c4eb2199e3d1b7fbd3fff
Authornihui <nihuini@tencent.com> · 2024-07-20 00:16 +08:00
CommitterGitHub <noreply@github.com> · 2024-07-20 00:16 +08:00
Parents7ef63a9e3978

Browse the files at this changeset

6 files changed

modified tools/pnnx/src/ir.cpp +3 −1
@@ -1091,7 +1091,8 @@
10911091 || t == "maximum"
10921092 || t == "min"
10931093 || t == "minimum"
1094- || t == "pow")
1094+ || t == "pow"
1095+ || t == "logaddexp")
10951096 {
10961097 std::string binaryop;
10971098 if (t == "atan2") binaryop = "torch.atan2";
@@ -1101,6 +1102,7 @@
11011102 if (t == "min") binaryop = "torch.min";
11021103 if (t == "minimum") binaryop = "torch.minimum";
11031104 if (t == "pow") binaryop = "torch.pow";
1105+ if (t == "logaddexp") binaryop = "torch.logaddexp";
11041106
11051107 std::string a = exprstack.top();
11061108 exprstack.pop();
modified tools/pnnx/src/pass_level3/fuse_expression.cpp +3 −0
@@ -154,6 +154,7 @@
154154 || op->type == "aten::div"
155155 || op->type == "aten::floor_divide"
156156 || op->type == "aten::fmod"
157+ || op->type == "aten::logaddexp"
157158 || op->type == "aten::max"
158159 || op->type == "aten::maximum"
159160 || op->type == "aten::min"
@@ -653,6 +654,7 @@
653654 else if (op->type == "aten::atan2"
654655 || op->type == "aten::floor_divide"
655656 || op->type == "aten::fmod"
657+ || op->type == "aten::logaddexp"
656658 || op->type == "aten::max"
657659 || op->type == "aten::maximum"
658660 || op->type == "aten::min"
@@ -867,6 +869,7 @@
867869 || op->type == "aten::fmod"
868870 || op->type == "aten::log"
869871 || op->type == "aten::log10"
872+ || op->type == "aten::logaddexp"
870873 || op->type == "aten::max"
871874 || op->type == "aten::maximum"
872875 || op->type == "aten::min"
modified tools/pnnx/src/pass_level5/eval_expression.cpp +7 −1
@@ -390,7 +390,8 @@
390390 || t == "floor_divide"
391391 || t == "fmod"
392392 || t == "pow"
393- || t == "remainder")
393+ || t == "remainder"
394+ || t == "logaddexp")
394395 {
395396 std::string a = exprstack.top();
396397 exprstack.pop();
@@ -459,6 +460,11 @@
459460 r += bf;
460461 exprstack.push(std::to_string(r));
461462 }
463+ if (t == "logaddexp")
464+ {
465+ float r = log(exp(af) + exp(bf));
466+ exprstack.push(std::to_string(r));
467+ }
462468 }
463469 else
464470 {
modified tools/pnnx/src/pass_ncnn/expand_expression.cpp +2 −0
@@ -185,6 +185,7 @@
185185 || t == "div"
186186 || t == "floor_divide"
187187 || t == "fmod"
188+ || t == "logaddexp"
188189 || t == "max"
189190 || t == "maximum"
190191 || t == "min"
@@ -211,6 +212,7 @@
211212 if (t == "sub") op_binary->params["0"] = 1;
212213 if (t == "mul") op_binary->params["0"] = 2;
213214 if (t == "div") op_binary->params["0"] = 3;
215+ if (t == "logaddexp") fprintf(stderr, "BinaryOp logaddexp not supported yet\n"); // TODO
214216 if (t == "max" || t == "maximum") op_binary->params["0"] = 4;
215217 if (t == "min" || t == "minimum") op_binary->params["0"] = 5;
216218 if (t == "floor_divide") fprintf(stderr, "BinaryOp floor_divide not supported yet\n"); // TODO
modified tools/pnnx/tests/CMakeLists.txt +1 −0
@@ -295,6 +295,7 @@
295295 pnnx_add_test(torch_imag)
296296 pnnx_add_test(torch_log)
297297 pnnx_add_test(torch_log10)
298+pnnx_add_test(torch_logaddexp)
298299 pnnx_add_test(torch_maximum)
299300 pnnx_add_test(torch_minimum)
300301 pnnx_add_test(torch_neg)
added tools/pnnx/tests/test_torch_logaddexp.py +61 −0
@@ -0,0 +1,61 @@
1+# Tencent is pleased to support the open source community by making ncnn available.
2+#
3+# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4+#
5+# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6+# in compliance with the License. You may obtain a copy of the License at
7+#
8+# https://opensource.org/licenses/BSD-3-Clause
9+#
10+# Unless required by applicable law or agreed to in writing, software distributed
11+# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+# specific language governing permissions and limitations under the License.
14+
15+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ def forward(self, x, y, z):
24+ out0 = torch.logaddexp(x, y)
25+ out1 = torch.logaddexp(y, y)
26+ out2 = torch.logaddexp(z, torch.ones_like(z) + 0.5)
27+ return out0, out1, out2
28+
29+def test():
30+ net = Model()
31+ net.eval()
32+
33+ torch.manual_seed(0)
34+ x = torch.rand(3, 16)
35+ y = torch.rand(3, 16)
36+ z = torch.rand(5, 9, 3)
37+
38+ a = net(x, y, z)
39+
40+ # export torchscript
41+ mod = torch.jit.trace(net, (x, y, z))
42+ mod.save("test_torch_logaddexp.pt")
43+
44+ # torchscript to pnnx
45+ import os
46+ os.system("../src/pnnx test_torch_logaddexp.pt inputshape=[3,16],[3,16],[5,9,3]")
47+
48+ # pnnx inference
49+ import test_torch_logaddexp_pnnx
50+ b = test_torch_logaddexp_pnnx.test_inference()
51+
52+ for a0, b0 in zip(a, b):
53+ if not torch.equal(a0, b0):
54+ return False
55+ return True
56+
57+if __name__ == "__main__":
58+ if test():
59+ exit(0)
60+ else:
61+ exit(1)