Repository: pytorch/pytorch
Pull request: [ONNX] Add log10 symbolic
Fixes #61332


Problem statement:
log10 is not one of supported operators in torch.onnx
## 🐛 Bug
When we use LogmelFilterBank from torchlibrosa.stft, we use log10 operator. But log10 operator is not one of supported operators in torch.onnx. Thus, when we convert torch model which contains LogmelFilterBank layer to onnx, we will meet the error. Hope to add log10 operator in supported operators in torch.onnx.


cc @BowenBao @neginraoof

Hints from issue discussion:

Commit message: Added log10 symbolic and tests

Patch to review:
diff --git a/test/onnx/test_pytorch_onnx_onnxruntime.py b/test/onnx/test_pytorch_onnx_onnxruntime.py
index 14cf62d362790..810c887286db9 100644
--- a/test/onnx/test_pytorch_onnx_onnxruntime.py
+++ b/test/onnx/test_pytorch_onnx_onnxruntime.py
@@ -6182,6 +6182,14 @@ def forward(self, input):
         model = Log1p()
         self.run_test(model, x)
 
+    def test_log10(self):
+        class Log10(torch.nn.Module):
+            def forward(self, input):
+                return torch.log10(input)
+        x = torch.rand(2, 3, 4)
+        model = Log10()
+        self.run_test(model, x)
+
     @skipIfUnsupportedMinOpsetVersion(11)
     def test_round(self):
         class Round(torch.nn.Module):
diff --git a/torch/onnx/symbolic_opset9.py b/torch/onnx/symbolic_opset9.py
index c8bb6ceddd820..4aae690d3c795 100644
--- a/torch/onnx/symbolic_opset9.py
+++ b/torch/onnx/symbolic_opset9.py
@@ -1539,6 +1539,11 @@ def log1p(g, self):
     return log(g, add(g, sym_help._if_scalar_type_as(g, torch.ones(1), self), self))
 
 
+def log10(g, self):
+    log_div = g.op("Log", g.op("Constant", value_t=torch.tensor(10.0)))
+    return g.op("Div", g.op("Log", self), log_div)
+
+
 def pow(g, self, exponent):
     f_dtype = self_dtype = self.type().scalarType()
     if not sym_help._is_fp(self):


Code review comments:
diff_hunk: @@ -1539,6 +1539,11 @@ def log1p(g, self):
     return log(g, add(g, sym_help._if_scalar_type_as(g, torch.ones(1), self), self))
 
 
+def log10(g, self):
+    log_div = g.op("Log", g.op("Constant", value_t=torch.tensor(10.0)))
path: torch/onnx/symbolic_opset9.py
text: @user1:
`log_e{10.0}` could be added as a constant, similar to how existing symbolic log2 works. what do you think?

@author:
Modified to match the format