Repository: pfnet/pytorch-pfn-extras

Issue:
ONNX export fails because of deprecated keywords such as `_retain_param_name`
# Problem
In ONNX export, ppe calls `torch_export` with `_retain_param_name` keyword here.

https://github.com/pfnet/pytorch-pfn-extras/blob/633670f2afac99cca0097c5306232b0c47ad2ce7/pytorch_pfn_extras/onnx/export_testcase.py#L121

, but this keyword is deprecated and totally removed from latest pytorch.

https://github.com/pytorch/pytorch/commit/43d51254bf22a064a5de3e181c101c4a767678db#diff-849a5778e2dcf7f36587967273cee0bf20642e35bf4c79405111ea3417c3fb3c

# Version
- ppe: 0.5.4
- torch: 1.10.1
- python 3.8.9

# Reproduction
```python
from pytorch_pfn_extras.onnx import export_testcase
import torch
from torch import nn

export_testcase(
  nn.Linear(4, 4),
  (torch.randn(32, 4),),
  "hoge")
```

This outputs
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/XXXXXX/site-packages/pytorch_pfn_extras/onnx/export_testcase.py", line 218, in export_testcase
    onnx_graph, outs = _export(
  File "/XXXXXX/site-packages/pytorch_pfn_extras/onnx/export_testcase.py", line 124, in _export
    outs = _export_util(
  File "/XXXXXX/site-packages/pytorch_pfn_extras/onnx/export_testcase.py", line 107, in _export_util
    return torch_export(model, args, f, _retain_param_name=True, **kwargs)
TypeError: _export() got an unexpected keyword argument '_retain_param_name'
```

# Remark
- https://github.com/pytorch/pytorch/commit/43d51254bf22a064a5de3e181c101c4a767678db#diff-849a5778e2dcf7f36587967273cee0bf20642e35bf4c79405111ea3417c3fb3c removes not only `_retain_param_name`, but also some other keywords such as `strip_doc_string`, which seems to be used several times in current ppe.


Discussion:
I think https://github.com/pfnet/pytorch-pfn-extras/pull/484 will solve this

Resolving patch:
diff --git a/pytorch_pfn_extras/onnx/export_testcase.py b/pytorch_pfn_extras/onnx/export_testcase.py
--- a/pytorch_pfn_extras/onnx/export_testcase.py
+++ b/pytorch_pfn_extras/onnx/export_testcase.py
@@ -5,6 +5,7 @@
 import json
 import os
 import subprocess
+from packaging import version
 from typing import Any, Dict, IO, Mapping, Optional, Sequence, Tuple, Union
 import warnings
 
@@ -118,8 +119,18 @@ def _export_util(
     # This is a temporal workaround until a fix is introduced in PyTorch.
     try:
         torch.onnx.utils._model_to_graph = _model_to_graph_with_value_names
-        return torch_export(  # type: ignore[no-untyped-call]
-            model, args, f, _retain_param_name=True, **kwargs)
+        if version.Version(torch.__version__) >= version.Version('1.10.0'):
+            try:
+                enable_onnx_checker = kwargs.pop('enable_onnx_checker', None)
+                return torch_export(  # type: ignore[no-untyped-call]
+                    model, args, f, **kwargs)
+            # this class will change in torch 1.11 to torch.onnx.utils.CheckerError
+            except torch.onnx.utils.ONNXCheckerError:  # type: ignore[attr-defined]
+                if enable_onnx_checker:
+                    raise
+        else:
+            return torch_export(  # type: ignore[no-untyped-call]
+                model, args, f, **kwargs)
     finally:
         torch.onnx.utils._model_to_graph = old_model_to_graph
 
@@ -136,20 +147,25 @@ def _export(
     opset_ver = kwargs.get('opset_version', None)
     if opset_ver is None:
         opset_ver = _default_onnx_opset_version
-    strip_doc_string = kwargs.pop('strip_doc_string', True)
+    if version.Version(torch.__version__) < version.Version('1.10.0'):
+        strip_doc_string = kwargs.get('strip_doc_string', True)
+        kwargs['strip_doc_string'] = False
+    else:
+        strip_doc_string = kwargs.pop('strip_doc_string', True)
+        kwargs['verbose'] = True
     with init_annotate(model, opset_ver) as ann, \
             as_output.trace(model) as (model, outputs), \
             grad.init_grad_state():
         outs = _export_util(
-            model, args, bytesio, strip_doc_string=False, **kwargs)
+            model, args, bytesio, **kwargs)
         onnx_graph = onnx.load(io.BytesIO(bytesio.getvalue()))
         onnx_graph = ann.set_annotate(onnx_graph)
         onnx_graph = ann.reorg_anchor(onnx_graph)
         outputs.add_outputs_to_model(onnx_graph)
+        if strip_doc_string:
+            for node in onnx_graph.graph.node:
+                node.doc_string = b''
 
-    if strip_doc_string:
-        for node in onnx_graph.graph.node:
-            node.doc_string = b''
     if strip_large_tensor_data:
         _strip_large_initializer_raw_data(onnx_graph, large_tensor_threshold)
 
@@ -258,7 +274,8 @@ def export_testcase(
         input_names=input_names, **kwargs)
     if isinstance(outs, torch.Tensor):
         outs = outs,
-
+    elif outs is None:
+        outs = ()
     # Remove unused inputs
     # - When keep_initializers_as_inputs=True, inputs contains initializers.
     #   So we have to filt initializers.


Test patch:
diff --git a/tests/pytorch_pfn_extras_tests/onnx_tests/test_annotate.py b/tests/pytorch_pfn_extras_tests/onnx_tests/test_annotate.py
--- a/tests/pytorch_pfn_extras_tests/onnx_tests/test_annotate.py
+++ b/tests/pytorch_pfn_extras_tests/onnx_tests/test_annotate.py
@@ -197,6 +197,7 @@ def forward(self, x):
 @pytest.mark.filterwarnings(
     "ignore::torch.jit.TracerWarning",
     "ignore:floor_divide is deprecated:UserWarning",
+    "ignore:__floordiv__ is deprecated:UserWarning",
     "ignore:Using or importing the ABCs:DeprecationWarning",
 )
 def test_scoped_anchor():