teddykoker / torchsort

Fast, differentiable sorting and ranking in PyTorch
https://pypi.org/project/torchsort/
Apache License 2.0
765 stars 33 forks source link

can not import torchsort successfully #77

Closed msn199959 closed 8 months ago

msn199959 commented 8 months ago

When I installed torchsort successfully, I tried to import it, but an error occurred:

Traceback (most recent call last): File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/example.py", line 2, in import torchsort File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/torchsort/init.py", line 1, in from torchsort.ops import soft_rank, soft_sort File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/torchsort/ops.py", line 18, in from .isotonic_cpu import isotonic_kl as isotonic_kl_cpu ModuleNotFoundError: No module named 'torchsort.isotonic_cpu'

teddykoker commented 8 months ago

How are you installing torchsort? Based on the paths in your error it seems you have likely cloned the repo within your project. If this is the case you will need to run pip install . within the torchsort directory, or just install from pypi with pip install torchsort. If you are already doing one of these could you try reinstalling with pip install --force-reinstall torchsort --no-deps --no-cache-dir and paste the output here?

msn199959 commented 8 months ago

How are you installing torchsort? Based on the paths in your error it seems you have likely cloned the repo within your project. If this is the case you will need to run pip install . within the torchsort directory, or just install from pypi with pip install torchsort. If you are already doing one of these could you try reinstalling with pip install --force-reinstall torchsort --no-deps --no-cache-dir and paste the output here?

Thank you for your quick response. I tried to install torchsort in a conda environment. My conda environment is as follows:

attrs                    23.1.0
cmd2                     2.4.3
et-xmlfile               1.1.0
Mypackage                0.1
ninja                    1.11.1.1
numpy                    1.26.2
nvidia-cublas-cu11       11.10.3.66
nvidia-cuda-nvrtc-cu11   11.7.99
nvidia-cuda-runtime-cu11 11.7.99
nvidia-cudnn-cu11        8.5.0.96
opencv-python            4.8.1.78
openpyxl                 3.1.2
pip                      23.3.1
pyperclip                1.8.2
setuptools               68.2.2
torch                    1.13.0
torchsort                0.1.9+pt113cpu
typing_extensions        4.9.0
wcwidth                  0.2.12
wheel                    0.42.0

the gcc version is 9.4.0, and nvcc version is 11.3 When I use this command

pip install --force-reinstall torchsort --no-deps --no-cache-dir

an error occurs:

Collecting torchsort
  Downloading torchsort-0.1.9.tar.gz (12 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: torchsort
  Building wheel for torchsort (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [64 lines of output]
      No CUDA runtime is found, using CUDA_HOME='/usr/local/cuda'
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-310
      creating build/lib.linux-x86_64-cpython-310/torchsort
      copying torchsort/__init__.py -> build/lib.linux-x86_64-cpython-310/torchsort
      copying torchsort/ops.py -> build/lib.linux-x86_64-cpython-310/torchsort
      running egg_info
      writing torchsort.egg-info/PKG-INFO
      writing dependency_links to torchsort.egg-info/dependency_links.txt
      writing requirements to torchsort.egg-info/requires.txt
      writing top-level names to torchsort.egg-info/top_level.txt
      reading manifest file 'torchsort.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      writing manifest file 'torchsort.egg-info/SOURCES.txt'
      copying torchsort/isotonic_cpu.cpp -> build/lib.linux-x86_64-cpython-310/torchsort
      copying torchsort/isotonic_cuda.cu -> build/lib.linux-x86_64-cpython-310/torchsort
      running build_ext
      /mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/torch/utils/cpp_extension.py:387: UserWarning: The detected CUDA version (11.3) has a minor version mismatch with the version that was used to compile PyTorch (11.7). Most likely this shouldn't be a problem.
        warnings.warn(CUDA_MISMATCH_WARN.format(cuda_str_version, torch.version.cuda))
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-lgu1yen_/torchsort_a13d652d70a442a3841ee234f2c2c251/setup.py", line 52, in <module>
          setup(
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/__init__.py", line 103, in setup
          return distutils.core.setup(**attrs)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 185, in setup
          return run_commands(dist)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
          dist.run_commands()
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands
          self.run_command(cmd)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/dist.py", line 989, in run_command
          super().run_command(command)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
          cmd_obj.run()
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/wheel/bdist_wheel.py", line 368, in run
          self.run_command("build")
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 318, in run_command
          self.distribution.run_command(command)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/dist.py", line 989, in run_command
          super().run_command(command)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
          cmd_obj.run()
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/command/build.py", line 131, in run
          self.run_command(cmd_name)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 318, in run_command
          self.distribution.run_command(command)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/dist.py", line 989, in run_command
          super().run_command(command)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
          cmd_obj.run()
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 88, in run
          _build_ext.run(self)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 345, in run
          self.build_extensions()
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 499, in build_extensions
          _check_cuda_version(compiler_name, compiler_version)
        File "/mnt/afs/user/mashengnan/miniconda3/envs/cltr/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 416, in _check_cuda_version
          raise RuntimeError(
      RuntimeError: The current installed version of /mnt/afs/user/mashengnan/miniconda3/envs/cltr/bin/x86_64-conda-linux-gnu-c++ (13.2.0) is greater than the maximum required version by CUDA 11.3 (10.2.1). Please make sure to use an adequate version of /mnt/afs/user/mashengnan/miniconda3/envs/cltr/bin/x86_64-conda-linux-gnu-c++ (>=5.0.0, <=10.2.1).
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for torchsort
  Running setup.py clean for torchsort
Failed to build torchsort
ERROR: Could not build wheels for torchsort, which is required to install pyproject.toml-based projects
msn199959 commented 8 months ago

Sometimes I can also install successfully with this command:

Install g++ with conda install -c conda-forge gxx_linux-64=9.40
Run export CXX=/path/to/miniconda3/envs/env_name/bin/x86_64-conda_cos6-linux-gnu-g++
Run export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/miniconda3/lib
pip install --force-reinstall --no-cache-dir --no-deps torchsort

or

python setup.py install

But this error still occurs:

Traceback (most recent call last):
File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/example.py", line 2, in
import torchsort
File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/torchsort/init.py", line 1, in
from torchsort.ops import soft_rank, soft_sort
File "/mnt/afs/user/mashengnan/crowd_counting/torchsort/torchsort/ops.py", line 18, in
from .isotonic_cpu import isotonic_kl as isotonic_kl_cpu
ModuleNotFoundError: No module named 'torchsort.isotonic_cpu'

I've tried everything, but it doesn't work, so sad...

teddykoker commented 8 months ago

Could you try running import torchsort in a python file that's in a different directory, using the conda environment that installed successfully? I'm wondering if having the torchsort code in your current directory is messing with python since import torchsort will first look in that directory, instead of the directory where it is installed.

msn199959 commented 8 months ago

Thanks for your advice! I can successfully use torchsort in other directories. Thanks again for your help and your work. Have a good day :D

teddykoker commented 8 months ago

Great, glad that worked :)