Changesets · Branches · Tags · Compare
| Changeset | cba7413d558466ff06b3efc438452773e8ef82af666c4eb2199e3d1b7fbd3fff |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-07-20 00:16 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-20 00:16 +08:00 |
| Parents | 7ef63a9e3978 |
6 files changed
modified tools/pnnx/src/ir.cpp +3 −1
| @@ -1091,7 +1091,8 @@ | ||
| 1091 | 1091 | || t == "maximum" |
| 1092 | 1092 | || t == "min" |
| 1093 | 1093 | || t == "minimum" |
| 1094 | - || t == "pow") | |
| 1094 | + || t == "pow" | |
| 1095 | + || t == "logaddexp") | |
| 1095 | 1096 | { |
| 1096 | 1097 | std::string binaryop; |
| 1097 | 1098 | if (t == "atan2") binaryop = "torch.atan2"; |
| @@ -1101,6 +1102,7 @@ | ||
| 1101 | 1102 | if (t == "min") binaryop = "torch.min"; |
| 1102 | 1103 | if (t == "minimum") binaryop = "torch.minimum"; |
| 1103 | 1104 | if (t == "pow") binaryop = "torch.pow"; |
| 1105 | + if (t == "logaddexp") binaryop = "torch.logaddexp"; | |
| 1104 | 1106 | |
| 1105 | 1107 | std::string a = exprstack.top(); |
| 1106 | 1108 | exprstack.pop(); |
modified tools/pnnx/src/pass_level3/fuse_expression.cpp +3 −0
| @@ -154,6 +154,7 @@ | ||
| 154 | 154 | || op->type == "aten::div" |
| 155 | 155 | || op->type == "aten::floor_divide" |
| 156 | 156 | || op->type == "aten::fmod" |
| 157 | + || op->type == "aten::logaddexp" | |
| 157 | 158 | || op->type == "aten::max" |
| 158 | 159 | || op->type == "aten::maximum" |
| 159 | 160 | || op->type == "aten::min" |
| @@ -653,6 +654,7 @@ | ||
| 653 | 654 | else if (op->type == "aten::atan2" |
| 654 | 655 | || op->type == "aten::floor_divide" |
| 655 | 656 | || op->type == "aten::fmod" |
| 657 | + || op->type == "aten::logaddexp" | |
| 656 | 658 | || op->type == "aten::max" |
| 657 | 659 | || op->type == "aten::maximum" |
| 658 | 660 | || op->type == "aten::min" |
| @@ -867,6 +869,7 @@ | ||
| 867 | 869 | || op->type == "aten::fmod" |
| 868 | 870 | || op->type == "aten::log" |
| 869 | 871 | || op->type == "aten::log10" |
| 872 | + || op->type == "aten::logaddexp" | |
| 870 | 873 | || op->type == "aten::max" |
| 871 | 874 | || op->type == "aten::maximum" |
| 872 | 875 | || op->type == "aten::min" |
modified tools/pnnx/src/pass_level5/eval_expression.cpp +7 −1
| @@ -390,7 +390,8 @@ | ||
| 390 | 390 | || t == "floor_divide" |
| 391 | 391 | || t == "fmod" |
| 392 | 392 | || t == "pow" |
| 393 | - || t == "remainder") | |
| 393 | + || t == "remainder" | |
| 394 | + || t == "logaddexp") | |
| 394 | 395 | { |
| 395 | 396 | std::string a = exprstack.top(); |
| 396 | 397 | exprstack.pop(); |
| @@ -459,6 +460,11 @@ | ||
| 459 | 460 | r += bf; |
| 460 | 461 | exprstack.push(std::to_string(r)); |
| 461 | 462 | } |
| 463 | + if (t == "logaddexp") | |
| 464 | + { | |
| 465 | + float r = log(exp(af) + exp(bf)); | |
| 466 | + exprstack.push(std::to_string(r)); | |
| 467 | + } | |
| 462 | 468 | } |
| 463 | 469 | else |
| 464 | 470 | { |
modified tools/pnnx/src/pass_ncnn/expand_expression.cpp +2 −0
| @@ -185,6 +185,7 @@ | ||
| 185 | 185 | || t == "div" |
| 186 | 186 | || t == "floor_divide" |
| 187 | 187 | || t == "fmod" |
| 188 | + || t == "logaddexp" | |
| 188 | 189 | || t == "max" |
| 189 | 190 | || t == "maximum" |
| 190 | 191 | || t == "min" |
| @@ -211,6 +212,7 @@ | ||
| 211 | 212 | if (t == "sub") op_binary->params["0"] = 1; |
| 212 | 213 | if (t == "mul") op_binary->params["0"] = 2; |
| 213 | 214 | if (t == "div") op_binary->params["0"] = 3; |
| 215 | + if (t == "logaddexp") fprintf(stderr, "BinaryOp logaddexp not supported yet\n"); // TODO | |
| 214 | 216 | if (t == "max" || t == "maximum") op_binary->params["0"] = 4; |
| 215 | 217 | if (t == "min" || t == "minimum") op_binary->params["0"] = 5; |
| 216 | 218 | 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 @@ | ||
| 295 | 295 | pnnx_add_test(torch_imag) |
| 296 | 296 | pnnx_add_test(torch_log) |
| 297 | 297 | pnnx_add_test(torch_log10) |
| 298 | +pnnx_add_test(torch_logaddexp) | |
| 298 | 299 | pnnx_add_test(torch_maximum) |
| 299 | 300 | pnnx_add_test(torch_minimum) |
| 300 | 301 | 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) |