From 310490a5996e81a6cd2a29499673f53c27422858 Mon Sep 17 00:00:00 2001 From: Arseny Date: Fri, 29 Mar 2024 01:18:53 +0100 Subject: [PATCH] refactor: static analysis --- .github/workflows/lint.yml | 2 +- .pre-commit-config.yaml | 25 ++++------- .pylintrc | 44 ------------------- .../node_converters/base_element_wise.py | 16 ++++--- .../node_converters/global_average_pool.py | 20 ++++----- onnx2torch/node_converters/logical.py | 25 ++++++----- onnx2torch/node_converters/nms.py | 16 ++++--- onnx2torch/node_converters/range.py | 15 ++++--- onnx2torch/node_converters/reshape.py | 7 +-- onnx2torch/node_converters/tile.py | 21 +++++---- pyproject.toml | 28 ++++++++++++ 11 files changed, 105 insertions(+), 114 deletions(-) delete mode 100644 .pylintrc diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index eeb9ffb1..9bb1ca97 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -22,7 +22,7 @@ jobs: python -m pip install -e . - name: Analysing the code with pylint run: | - pylint --rcfile .pylintrc --output-format=colorized $(git ls-files '*.py') + pylint --output-format=colorized $(git ls-files '*.py') lint-python-format: name: Python format diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c3c97f07..67e8fb0e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,21 +1,14 @@ repos: - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 24.3.0 hooks: - id: black - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort - args: - [ - "--force-single-line-imports", - "--ensure-newline-before-comments", - "--line-length=120", - "--profile=black", - ] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: check-toml @@ -29,18 +22,18 @@ repos: - id: end-of-file-fixer - id: requirements-txt-fixer - id: no-commit-to-branch - args: - [ - "-b=main", - ] + args: ["-b=main"] - repo: https://github.com/PyCQA/pylint - rev: v2.16.0 + rev: v3.1.0 hooks: - id: pylint + name: pylint + entry: pylint + language: system + types: [ python ] args: [ "-rn", "-sn", - "--rcfile=.pylintrc", "--output-format=colorized", ] diff --git a/.pylintrc b/.pylintrc deleted file mode 100644 index 757551f4..00000000 --- a/.pylintrc +++ /dev/null @@ -1,44 +0,0 @@ -[MAIN] -load-plugins=pylint.extensions.docparams - -[BASIC] -# Good variable names which should always be accepted, separated by a comma. -good-names=x,y,z - -[DESIGN] -# Maximum number of arguments for function / method -max-args=12 -# Maximum number of locals for function / method body -max-locals=30 -# Maximum number of statements in function / method body -max-statements=60 -# Maximum number of attributes for a class (see R0902). -max-attributes=20 -# Minimum number of public methods for a class (see R0903). -min-public-methods=0 -# Maximum number of public methods for a class (see R0904). -max-public-methods=20 - -[FORMAT] -max-line-length=120 - -[MESSAGES] -disable=logging-fstring-interpolation, import-error, missing-module-docstring, missing-raises-doc, duplicate-code, fixme, unnecessary-lambda-assignment - -[SIMILARITIES] -# Minimum lines number of a similarity. -min-similarity-lines=4 -# Ignore comments when computing similarities. -ignore-comments=yes -# Ignore docstrings when computing similarities. -ignore-docstrings=yes -# Ignore imports when computing similarities. -ignore-imports=yes -# Ignore function signatures when computing similarities. -ignore-signatures=no - -[TYPECHECK] -# List of members which are set dynamically and missed by Pylint inference -# system, and so shouldn't trigger E1101 when accessed. -generated-members=numpy.*, torch.*, onnx.*, onnxruntime.* -ignored-modules=onnx.onnx_ml_pb2 diff --git a/onnx2torch/node_converters/base_element_wise.py b/onnx2torch/node_converters/base_element_wise.py index 60e94214..762e513a 100644 --- a/onnx2torch/node_converters/base_element_wise.py +++ b/onnx2torch/node_converters/base_element_wise.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring import torch from torch import nn @@ -5,7 +6,7 @@ from onnx2torch.utils.custom_export_to_onnx import OnnxToTorchModuleWithCustomExport -class OnnxBaseElementWise(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-docstring +class OnnxBaseElementWise(nn.Module, OnnxToTorchModuleWithCustomExport): def __init__(self, op_type: str): super().__init__() self._op_type = op_type @@ -16,17 +17,20 @@ def _broadcast_shape(*tensors: torch.Tensor): broadcast_shape = torch.broadcast_shapes(*shapes) return broadcast_shape - def apply_reduction(self, *tensors: torch.Tensor) -> torch.Tensor: # pylint: disable=missing-function-docstring + def apply_reduction(self, *tensors: torch.Tensor) -> torch.Tensor: + del tensors raise NotImplementedError - def forward(self, *input_tensors: torch.Tensor) -> torch.Tensor: # pylint: disable=missing-function-docstring + def forward(self, *input_tensors: torch.Tensor) -> torch.Tensor: if len(input_tensors) == 1: # If there is a single element, return it (no op). # Also, no need for manually building the ONNX node. return input_tensors[0] - forward_lambda = lambda: self.apply_reduction(*input_tensors) + def _forward() -> torch.Tensor: + return self.apply_reduction(*input_tensors) + if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, self._op_type, *input_tensors, {}) + return DefaultExportToOnnx.export(_forward, self._op_type, *input_tensors, {}) - return forward_lambda() + return _forward() diff --git a/onnx2torch/node_converters/global_average_pool.py b/onnx2torch/node_converters/global_average_pool.py index f57726dd..4e27287a 100644 --- a/onnx2torch/node_converters/global_average_pool.py +++ b/onnx2torch/node_converters/global_average_pool.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring __all__ = [ 'OnnxGlobalAveragePool', 'OnnxGlobalAveragePoolWithKnownInputShape', @@ -18,8 +19,8 @@ from onnx2torch.utils.custom_export_to_onnx import OnnxToTorchModuleWithCustomExport -class OnnxGlobalAveragePool(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-docstring - def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: # pylint: disable=missing-function-docstring +class OnnxGlobalAveragePool(nn.Module, OnnxToTorchModuleWithCustomExport): + def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: def _forward(): x_dims = list(range(2, len(input_tensor.shape))) return torch.mean(input_tensor, dim=x_dims, keepdim=True) @@ -30,24 +31,23 @@ def _forward(): return _forward() -class OnnxGlobalAveragePoolWithKnownInputShape( - nn.Module, OnnxToTorchModuleWithCustomExport -): # pylint: disable=missing-docstring +class OnnxGlobalAveragePoolWithKnownInputShape(nn.Module, OnnxToTorchModuleWithCustomExport): def __init__(self, input_shape: List[int]): super().__init__() self._x_dims = list(range(2, len(input_shape))) - def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: # pylint: disable=missing-function-docstring - forward_lambda = lambda: torch.mean(input_tensor, dim=self._x_dims, keepdim=True) + def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: + def _forward() -> torch.Tensor: + return torch.mean(input_tensor, dim=self._x_dims, keepdim=True) if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, 'GlobalAveragePool', input_tensor, {}) + return DefaultExportToOnnx.export(_forward, 'GlobalAveragePool', input_tensor, {}) - return forward_lambda() + return _forward() @add_converter(operation_type='GlobalAveragePool', version=1) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: input_value_info = graph.value_info[node.input_values[0]] input_shape = get_shape_from_value_info(input_value_info) diff --git a/onnx2torch/node_converters/logical.py b/onnx2torch/node_converters/logical.py index 79ea08c0..ac5b4681 100644 --- a/onnx2torch/node_converters/logical.py +++ b/onnx2torch/node_converters/logical.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring __all__ = [ 'OnnxNot', 'OnnxLogical', @@ -25,16 +26,18 @@ } -class OnnxNot(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-class-docstring - def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: # pylint: disable=missing-function-docstring - forward_lambda = lambda: torch.logical_not(input_tensor) +class OnnxNot(nn.Module, OnnxToTorchModuleWithCustomExport): + def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: + def _forward() -> torch.Tensor: + return torch.logical_not(input_tensor) + if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, 'Not', input_tensor, {}) + return DefaultExportToOnnx.export(_forward, 'Not', input_tensor, {}) - return forward_lambda() + return _forward() -class OnnxLogical(nn.Module, OnnxToTorchModule): # pylint: disable=missing-class-docstring +class OnnxLogical(nn.Module, OnnxToTorchModule): def __init__(self, operation_type: str, broadcast: Optional[int] = None, axis: Optional[int] = None): super().__init__() self.broadcast = broadcast @@ -42,9 +45,7 @@ def __init__(self, operation_type: str, broadcast: Optional[int] = None, axis: O self.logic_op_function = _TORCH_FUNCTION_FROM_ONNX_TYPE[operation_type] - def forward( # pylint: disable=missing-function-docstring - self, first_tensor: torch.Tensor, second_tensor: torch.Tensor - ): + def forward(self, first_tensor: torch.Tensor, second_tensor: torch.Tensor): if self.broadcast == 1 and self.axis is not None: second_tensor = old_style_broadcast(first_tensor, second_tensor, self.axis) @@ -57,7 +58,8 @@ def forward( # pylint: disable=missing-function-docstring @add_converter(operation_type='And', version=7) @add_converter(operation_type='Or', version=1) @add_converter(operation_type='Or', version=7) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: + del graph return OperationConverterResult( torch_module=OnnxLogical( operation_type=node.operation_type, @@ -69,7 +71,8 @@ def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: @add_converter(operation_type='Not', version=1) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: + del graph return OperationConverterResult( torch_module=OnnxNot(), onnx_mapping=onnx_mapping_from_node(node=node), diff --git a/onnx2torch/node_converters/nms.py b/onnx2torch/node_converters/nms.py index 37bbfa46..c6b87317 100644 --- a/onnx2torch/node_converters/nms.py +++ b/onnx2torch/node_converters/nms.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring __all__ = [ 'OnnxNonMaxSuppression', ] @@ -20,15 +21,16 @@ from onnx2torch.utils.custom_export_to_onnx import OnnxToTorchModuleWithCustomExport -class OnnxNonMaxSuppression(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-class-docstring +class OnnxNonMaxSuppression(nn.Module, OnnxToTorchModuleWithCustomExport): def __init__(self, center_point_box: int = 0): super().__init__() self._center_point_box = center_point_box def _onnx_attrs(self, opset_version: int) -> Dict[str, Any]: + del opset_version return {'center_point_box_i': self._center_point_box} - def forward( # pylint: disable=missing-function-docstring + def forward( self, boxes: torch.Tensor, scores: torch.Tensor, @@ -36,7 +38,8 @@ def forward( # pylint: disable=missing-function-docstring iou_threshold: Optional[torch.Tensor] = None, score_threshold: Optional[torch.Tensor] = None, ) -> torch.Tensor: - forward_lambda = lambda: self._nms(boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold) + def _forward() -> torch.Tensor: + return self._nms(boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold) if torch.onnx.is_in_onnx_export(): if max_output_boxes_per_class is None: @@ -48,7 +51,7 @@ def forward( # pylint: disable=missing-function-docstring onnx_attrs = self._onnx_attrs(opset_version=get_onnx_version()) return DefaultExportToOnnx.export( - forward_lambda, + _forward, 'NonMaxSuppression', boxes, scores, @@ -58,7 +61,7 @@ def forward( # pylint: disable=missing-function-docstring onnx_attrs, ) - return forward_lambda() + return _forward() def _nms( self, @@ -109,7 +112,8 @@ def _nms( @add_converter(operation_type='NonMaxSuppression', version=10) @add_converter(operation_type='NonMaxSuppression', version=11) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: + del graph center_point_box = node.attributes.get('center_point_box', 0) return OperationConverterResult( torch_module=OnnxNonMaxSuppression(center_point_box=center_point_box), diff --git a/onnx2torch/node_converters/range.py b/onnx2torch/node_converters/range.py index 8535e613..392e32e8 100644 --- a/onnx2torch/node_converters/range.py +++ b/onnx2torch/node_converters/range.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring __all__ = [ 'OnnxRange', ] @@ -16,7 +17,7 @@ from onnx2torch.utils.custom_export_to_onnx import OnnxToTorchModuleWithCustomExport -class OnnxRange(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-class-docstring +class OnnxRange(nn.Module, OnnxToTorchModuleWithCustomExport): def __init__(self): super().__init__() self.register_buffer('dummy_buffer', torch.Tensor(), persistent=False) @@ -41,22 +42,24 @@ def _arange( device=self.dummy_buffer.device, ) - def forward( # pylint: disable=missing-function-docstring + def forward( self, start: Union[torch.Tensor, float, int], limit: Union[torch.Tensor, float, int], delta: Union[torch.Tensor, float, int], ) -> torch.Tensor: - forward_lambda = lambda: self._arange(start, limit, delta) + def _forward() -> torch.Tensor: + return self._arange(start, limit, delta) if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, 'Range', start, limit, delta, {}) + return DefaultExportToOnnx.export(_forward, 'Range', start, limit, delta, {}) - return forward_lambda() + return _forward() @add_converter(operation_type='Range', version=11) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: + del graph return OperationConverterResult( torch_module=OnnxRange(), onnx_mapping=onnx_mapping_from_node(node), diff --git a/onnx2torch/node_converters/reshape.py b/onnx2torch/node_converters/reshape.py index edb8f05f..e8ec3aa7 100644 --- a/onnx2torch/node_converters/reshape.py +++ b/onnx2torch/node_converters/reshape.py @@ -27,12 +27,13 @@ def forward( # pylint: disable=missing-function-docstring input_tensor: torch.Tensor, shape: torch.Tensor, ) -> torch.Tensor: - forward_lambda = lambda: self._do_reshape(input_tensor, shape) + def _forward() -> torch.Tensor: + return self._do_reshape(input_tensor, shape) if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, 'Reshape', input_tensor, shape, {}) + return DefaultExportToOnnx.export(_forward, 'Reshape', input_tensor, shape, {}) - return forward_lambda() + return _forward() @add_converter(operation_type='Reshape', version=5) diff --git a/onnx2torch/node_converters/tile.py b/onnx2torch/node_converters/tile.py index 9cfd0db4..0508a877 100644 --- a/onnx2torch/node_converters/tile.py +++ b/onnx2torch/node_converters/tile.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-docstring __all__ = [ 'OnnxTile', ] @@ -14,23 +15,21 @@ from onnx2torch.utils.custom_export_to_onnx import OnnxToTorchModuleWithCustomExport -class OnnxTile(nn.Module, OnnxToTorchModuleWithCustomExport): # pylint: disable=missing-class-docstring - def forward( # pylint: disable=missing-function-docstring - self, - input_tensor: torch.Tensor, - repeats: torch.Tensor, - ) -> torch.Tensor: - # torch.tile(input_tensor, repeats) is not supported for exporting - forward_lambda = lambda: input_tensor.repeat(torch.Size(repeats)) +class OnnxTile(nn.Module, OnnxToTorchModuleWithCustomExport): + def forward(self, input_tensor: torch.Tensor, repeats: torch.Tensor) -> torch.Tensor: + def _forward() -> torch.Tensor: + return input_tensor.repeat(torch.Size(repeats)) + if torch.onnx.is_in_onnx_export(): - return DefaultExportToOnnx.export(forward_lambda, 'Tile', input_tensor, repeats, {}) + return DefaultExportToOnnx.export(_forward, 'Tile', input_tensor, repeats, {}) - return forward_lambda() + return _forward() @add_converter(operation_type='Tile', version=6) @add_converter(operation_type='Tile', version=13) -def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: # pylint: disable=unused-argument +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: + del graph return OperationConverterResult( torch_module=OnnxTile(), onnx_mapping=onnx_mapping_from_node(node=node), diff --git a/pyproject.toml b/pyproject.toml index c4d03d30..4b576c1b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,3 +51,31 @@ profile = 'black' line_length = 120 ensure_newline_before_comments = true force_single_line = true + +[tool.pylint.master] +load-plugins = ['pylint.extensions.docparams'] + +[tool.pylint.format] +max-line-length = 120 + +[tool.pylint.design] +max-args = 12 +max-locals = 30 +max-attributes = 20 +min-public-methods = 0 + +[tool.pylint.typecheck] +generated-members = ['torch.*'] + +[tool.pylint.messages_control] +disable = [ + 'logging-fstring-interpolation', + 'cyclic-import', + 'duplicate-code', + 'missing-module-docstring', + 'unnecessary-pass', + 'no-name-in-module', +] + +[tool.pylint.BASIC] +good-names = ['bs', 'bn']