Skip to content

Conversation

vahvero
Copy link
Contributor

@vahvero vahvero commented May 4, 2023

Description

closes #5182 by adding os.PathLike to type unions and using os.fspath at some points.

Motivation and Context

Context

onnx file handling accepts only IO[bytes] | str. If passing Python pathlib.Path, static type checkers such as pyright and pylance emit errors which is incorrect.

import onnx
import pathlib

model = onnx.load(pathlib.Path("model.onnx"))

Causes PyRight error.

Motivation

  1. Using pathlib to handle paths is considered best practice by many.
  2. Would remove unnecessary object transformations to satisfy type checkers
  3. Reduces # type: ignore usage in user code base.
    These, in general, should benefit general code quality.

Initial issue discussion was with

justinchuby

This is my first time contributing to anything public. Changes are very minor and any feedback is welcome.

@vahvero vahvero requested a review from a team as a code owner May 4, 2023 17:11
Copy link
Member

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vahvero added 3 commits May 6, 2023 13:25
Signed-off-by: vahvero <otso.brummer@gmail.com>
Signed-off-by: vahvero <otso.brummer@gmail.com>
Signed-off-by: vahvero <otso.brummer@gmail.com>
@vahvero
Copy link
Contributor Author

vahvero commented May 6, 2023

I have some issues trying to build ONNX from source to run local tests.

Obtaining file:///home/vahvero/Documents/onnx
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Checking if build backend supports build_editable: started
  Checking if build backend supports build_editable: finished with status 'done'
  Getting requirements to build editable: started
  Getting requirements to build editable: finished with status 'done'
  Installing backend dependencies: started
  Installing backend dependencies: finished with status 'done'
  Preparing editable metadata (pyproject.toml): started
  Preparing editable metadata (pyproject.toml): finished with status 'done'
Collecting numpy (from onnx==1.15.0)
  Using cached numpy-1.24.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
Collecting protobuf>=3.20.2 (from onnx==1.15.0)
  Using cached protobuf-4.22.4-cp37-abi3-manylinux2014_x86_64.whl (302 kB)
Collecting typing-extensions>=3.6.2.1 (from onnx==1.15.0)
  Using cached typing_extensions-4.5.0-py3-none-any.whl (27 kB)
Building wheels for collected packages: onnx
  Building editable for onnx (pyproject.toml): started
  Building editable for onnx (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error
  
  × Building editable for onnx (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [2824 lines of output]
      running editable_wheel
      creating /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info
      writing /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/dependency_links.txt
      writing entry points to /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/entry_points.txt
      writing requirements to /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/requires.txt
      writing top-level names to /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/top_level.txt
      writing manifest file '/tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/SOURCES.txt'
      reading manifest file '/tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      warning: no files found matching '*.c' under directory 'onnx'
      adding license file 'LICENSE'
      writing manifest file '/tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx.egg-info/SOURCES.txt'
      creating '/tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx-1.15.0.dist-info'
      creating /tmp/pip-wheel-zosivw_u/.tmp-s7s3loqj/onnx-1.15.0.dist-info/WHEEL
      running build_py
      running create_version
      running cmake_build
      Extra cmake args: ['-DONNX_USE_LITE_PROTO=ON']
      Using cmake args: ['/usr/bin/cmake', '-DPYTHON_INCLUDE_DIR=/home/vahvero/.pyenv/versions/3.9.16/include/python3.9', '-DPYTHON_EXECUTABLE=/home/vahvero/Documents/onnx/venv/bin/python3.9', '-DBUILD_ONNX_PYTHON=ON', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '-DONNX_NAMESPACE=onnx', '-DPY_EXT_SUFFIX=.cpython-39-x86_64-linux-gnu.so', '-DCMAKE_BUILD_TYPE=Release', '-DONNX_ML=1', '-DONNX_USE_LITE_PROTO=ON', '/home/vahvero/Documents/onnx']
      CMake Warning at /usr/share/cmake-3.25/Modules/FindProtobuf.cmake:524 (message):
        Protobuf compiler version 22.3 doesn't match library version 3.20.2
      Call Stack (most recent call first):
        CMakeLists.txt:179 (find_package)
      
      
      Generated: /home/vahvero/Documents/onnx/.setuptools-cmake-build/onnx/onnx-ml.proto
      Generated: /home/vahvero/Documents/onnx/.setuptools-cmake-build/onnx/onnx-operators-ml.proto
      Generated: /home/vahvero/Documents/onnx/.setuptools-cmake-build/onnx/onnx-data.proto
      -- Could NOT find pybind11 (missing: pybind11_DIR)
      -- pybind11 v2.10.3
      --
      -- ******** Summary ********
      --   CMake version             : 3.25.1
      --   CMake command             : /usr/bin/cmake
      --   System                    : Linux
      --   C++ compiler              : /usr/bin/c++
      --   C++ compiler version      : 12.2.0
      --   CXX flags                 :  -Wnon-virtual-dtor
      --   Build type                : Release
      --   Compile definitions       : __STDC_FORMAT_MACROS
      --   CMAKE_PREFIX_PATH         :
      --   CMAKE_INSTALL_PREFIX      : /usr/local
      --   CMAKE_MODULE_PATH         :
      --
      --   ONNX version              : 1.15.0
      --   ONNX NAMESPACE            : onnx
      --   ONNX_USE_LITE_PROTO       : ON
<continues for a while>

                File "/home/vahvero/.pyenv/versions/3.9.16/lib/python3.9/subprocess.py", line 373, in check_call
                  raise CalledProcessError(retcode, cmd)
              subprocess.CalledProcessError: Command '['/usr/bin/cmake', '--build', '.', '--', '-j', '12']' returned non-zero exit status 2.
      
      
                              If you are seeing this warning it is very likely that a setuptools
                              plugin or customization overrides the `build_py` command, without
                              taking into consideration how editable installs run build steps
                              starting from setuptools v64.0.0.
      
                              Plugin authors and developers relying on custom build steps are
                              encouraged to update their `build_py` implementation considering the
                              information about editable installs in
                              https://setuptools.pypa.io/en/latest/userguide/extension.html.
      
                              For the time being `setuptools` will silence this error and ignore
                              the faulty command, but this behaviour will change in future versions.
      
              ********************************************************************************
      
      !!
        self._safely_run(name)
      running build_ext
      running cmake_build
      Traceback (most recent call last):
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/command/editable_wheel.py", line 155, in run
          self._create_wheel_file(bdist_wheel)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/command/editable_wheel.py", line 344, in _create_wheel_file
          files, mapping = self._run_build_commands(dist_name, unpacked, lib, tmp)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/command/editable_wheel.py", line 267, in _run_build_commands
          self._run_build_subcommands()
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/command/editable_wheel.py", line 294, in _run_build_subcommands
          self.run_command(name)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/cmd.py", line 318, in run_command
          self.distribution.run_command(command)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 1244, in run_command
          super().run_command(command)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
          cmd_obj.run()
        File "<string>", line 260, in run
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/command/build_ext.py", line 84, in run
          _build_ext.run(self)
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py", line 345, in run
          self.build_extensions()
        File "<string>", line 277, in build_extensions
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/cmd.py", line 350, in copy_file
          return file_util.copy_file(
        File "/tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/file_util.py", line 115, in copy_file
          raise DistutilsFileError(
      distutils.errors.DistutilsFileError: can't copy '/home/vahvero/Documents/onnx/.setuptools-cmake-build/onnx_cpp2py_export.cpython-39-x86_64-linux-gnu.so': doesn't exist or not a regular file
      /tmp/pip-build-env-hnr9o_ab/overlay/lib/python3.9/site-packages/setuptools/_distutils/dist.py:988: _DebuggingTips: Problem in editable installation.
      !!
      
              ********************************************************************************
              An error happened while installing `onnx` in editable mode.
      
              The following steps are recommended to help debug this problem:
      
              - Try to install the project normally, without using the editable mode.
                Does the error still persist?
                (If it does, try fixing the problem before attempting the editable mode).
              - If you are using binary extensions, make sure you have all OS-level
                dependencies installed (e.g. compilers, toolchains, binary libraries, ...).
              - Try the latest version of setuptools (maybe the error was already fixed).
              - If you (or your project dependencies) are using any setuptools extension
                or customization, make sure they support the editable mode.
      
              After following the steps above, if the problem still persists and
              you think this is related to how setuptools handles editable installations,
              please submit a reproducible example
              (see https://stackoverflow.com/help/minimal-reproducible-example) to:
      
                  https://github.com/pypa/setuptools/issues
      
              See https://setuptools.pypa.io/en/latest/userguide/development_mode.html for details.
              ********************************************************************************
      
      !!
        cmd_obj.run()
      error: can't copy '/home/vahvero/Documents/onnx/.setuptools-cmake-build/onnx_cpp2py_export.cpython-39-x86_64-linux-gnu.so': doesn't exist or not a regular file
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building editable for onnx
Failed to build onnx
ERROR: Could not build wheels for onnx, which is required to install pyproject.toml-based projects

Whole output

I am not experienced with large scale C++. I will try later again, installing pybind11 did not fix error.

>>> pip show pybind11
Name: pybind11
Version: 2.10.3
Summary: Seamless operability between C++11 and Python
Home-page: https://github.com/pybind/pybind11
Author: Wenzel Jakob
Author-email: wenzel.jakob@epfl.ch
License: BSD
Location: /home/vahvero/Documents/onnx/venv/lib/python3.9/site-packages
Requires: 
Required-by: 

Copy link
Member

@jcwchen jcwchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some issues trying to build ONNX from source to run local tests.

According to the error message, it seems that you have multiple Protobuf compiler. Please git clean -xdf to clean all build files, pip install --upgrade pip setuptools to get the latest setuptools, and then try pip install -e . again.

@vahvero
Copy link
Contributor Author

vahvero commented May 13, 2023

I have some issues trying to build ONNX from source to run local tests.

According to the error message, it seems that you have multiple Protobuf compiler. Please git clean -xdf to clean all build files, pip install --upgrade pip setuptools to get the latest setuptools, and then try pip install -e . again.

I had installed protobuf from source, and copied it to /usr/local/bin/protoc. Removing this file removed the errors. Thank you.

@justinchuby
Copy link
Member

Please also make sure the change is free of lint errors: https://github.com/onnx/onnx/blob/2e9a6757ad33ef4bc0c4a4ecc61837f79885a82a/docs/CONTRIBUTING.md#code-style thanks a lot!

Signed-off-by: vahvero <otso.brummer@gmail.com>
@vahvero
Copy link
Contributor Author

vahvero commented May 14, 2023

Please also make sure the change is free of lint errors: https://github.com/onnx/onnx/blob/2e9a6757ad33ef4bc0c4a4ecc61837f79885a82a/docs/CONTRIBUTING.md#code-style thanks a lot!

I did not realize all pushes automatically request review. Should now pass all linters and tests, at least did in local.

@vahvero vahvero requested a review from justinchuby May 14, 2023 17:57
vahvero and others added 4 commits May 16, 2023 18:00
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
vahvero and others added 3 commits May 19, 2023 13:31
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
@jcwchen jcwchen added the module: utility Helper modules label May 19, 2023
Copy link
Member

@jcwchen jcwchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for extending! Could you please also update this function:

def infer_shapes_path(
model_path: str,
output_path: str = "",

@jcwchen jcwchen added the run release CIs Use this label to trigger release tests in CI label May 19, 2023
Signed-off-by: vahvero <otso.brummer@gmail.com>
@vahvero
Copy link
Contributor Author

vahvero commented May 28, 2023

Thank you for extending! Could you please also update this function:

def infer_shapes_path(
model_path: str,
output_path: str = "",

Included to 495be78

@jcwchen jcwchen enabled auto-merge (squash) May 28, 2023 16:06
vahvero added 2 commits May 28, 2023 21:08
Signed-off-by: vahvero <otso.brummer@gmail.com>
Signed-off-by: vahvero <otso.brummer@gmail.com>
auto-merge was automatically disabled May 28, 2023 18:08

Head branch was pushed to by a user without write access

@justinchuby
Copy link
Member

You may want to run lintrunner -a to fix the rest of the lint errors

@vahvero
Copy link
Contributor Author

vahvero commented May 30, 2023

You may want to run lintrunner -a to fix the rest of the lint errors

I receive no errors when running lintrunner. Are there any common configuration mistakes with it?

@justinchuby
Copy link
Member

Maybe try lintrunner init to make sure all linters are up to date then run lintrunner --all -a to run it on all files?

vahvero and others added 2 commits May 30, 2023 21:37
Signed-off-by: vahvero <otso.brummer@gmail.com>
@vahvero
Copy link
Contributor Author

vahvero commented May 30, 2023

Maybe try lintrunner init to make sure all linters are up to date then run lintrunner --all -a to run it on all files?

Fixed in 14b4b7c

@jcwchen jcwchen merged commit ba9d006 into onnx:main May 30, 2023
adityagoel4512 pushed a commit to adityagoel4512/onnx that referenced this pull request Jul 28, 2023
Signed-off-by: vahvero <otso.brummer@gmail.com>
Signed-off-by: vahvero <30704280+vahvero@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Chun-Wei Chen <jacky82226@gmail.com>
Signed-off-by: Aditya Goel <agoel4512@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: utility Helper modules run release CIs Use this label to trigger release tests in CI
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[Feature request] Add possibility to pass pathlib.Path where filepath is expected
3 participants