Failed to build with setuptools

I am trying to bind a custom function to python using setup tools and following the tutorial but I get the following error message although i was able to build it using cmake and worked successfully.
I would appreciate any help, thank you.

Blockquote
running install
running bdist_egg
running egg_info
creating custom_conv2d_cpp.egg-info
writing custom_conv2d_cpp.egg-info/PKG-INFO
writing dependency_links to custom_conv2d_cpp.egg-info/dependency_links.txt
writing top-level names to custom_conv2d_cpp.egg-info/top_level.txt
writing manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’
reading manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’
writing manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
building ‘custom_conv2d_cpp’ extension
creating /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6
Emitting ninja build file /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/build.ninja…
Compiling objects…
Allowing ninja to set a default number of workers… (overridable by setting the environment variable MAX_JOBS=N)
[1/1] c++ -MMD -MF /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o.d -pthread -B /home/hatem/anaconda3/envs/bind/compiler_compat -Wl,–sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/TH -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/THC -I/home/hatem/anaconda3/envs/bind/include/python3.6m -c -c /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp -o /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H ‘-DPYBIND11_COMPILER_TYPE=“_gcc”’ ‘-DPYBIND11_STDLIB=“_libstdcpp”’ ‘-DPYBIND11_BUILD_ABI=“_cxxabi1011”’ -DTORCH_EXTENSION_NAME=custom_conv2d_cpp -D_GLIBCXX_USE_CXX11_ABI=0
FAILED: /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o
c++ -MMD -MF /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o.d -pthread -B /home/hatem/anaconda3/envs/bind/compiler_compat -Wl,–sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/TH -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/THC -I/home/hatem/anaconda3/envs/bind/include/python3.6m -c -c /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp -o /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H ‘-DPYBIND11_COMPILER_TYPE=“_gcc”’ ‘-DPYBIND11_STDLIB=“_libstdcpp”’ ‘-DPYBIND11_BUILD_ABI=“_cxxabi1011”’ -DTORCH_EXTENSION_NAME=custom_conv2d_cpp -D_GLIBCXX_USE_CXX11_ABI=0
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/ATen/Parallel.h:140,
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/utils.h:3,
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/nn/cloneable.h:5,
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/nn.h:3,
from /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:5:
/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/ATen/ParallelOpenMP.h:83: warning: ignoring #pragma omp parallel [-Wunknown-pragmas]
83 | #pragma omp parallel for if ((end - begin) >= grain_size)
|
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: In function ‘at::Tensor custom_conv2d_forward(at::Tensor, at::Tensor, int64_t, int64_t, int64_t)’:
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:32:38: warning: unused variable ‘ch_in’ [-Wunused-variable]
32 | auto ch_in = input.size(1);
| ^~~~~
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:34:38: warning: unused variable ‘iw’ [-Wunused-variable]
34 | auto iw = input.size(3);
| ^~
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:37:38: warning: unused variable ‘kch_i’ [-Wunused-variable]
37 | auto kch_i = weight.size(1);
| ^~~~~
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: At global scope:
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:74:16: error: expected constructor, destructor, or type conversion before ‘(’ token
74 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
| ^
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1673, in _run_ninja_build
env=env)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/subprocess.py”, line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command ‘[‘ninja’, ‘-v’]’ returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Blockquote
Traceback (most recent call last):
File “setup.py”, line 10, in
‘build_ext’: BuildExtension
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/init.py”, line 153, in setup
return distutils.core.setup(**attrs)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/core.py”, line 148, in setup
dist.run_commands()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 955, in run_commands
self.run_command(cmd)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command
cmd_obj.run()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install.py”, line 67, in run
self.do_egg_install()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install.py”, line 109, in do_egg_install
self.run_command(‘bdist_egg’)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command
self.distribution.run_command(command)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command
cmd_obj.run()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/bdist_egg.py”, line 164, in run
cmd = self.call_command(‘install_lib’, warn_dir=0)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/bdist_egg.py”, line 150, in call_command
self.run_command(cmdname)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command
self.distribution.run_command(command)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command
cmd_obj.run()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install_lib.py”, line 11, in run
self.build()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/install_lib.py”, line 107, in build
self.run_command(‘build_ext’)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command
self.distribution.run_command(command)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command
cmd_obj.run()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/build_ext.py”, line 79, in run
_build_ext.run(self)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 339, in run
self.build_extensions()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 708, in build_extensions
build_ext.build_extensions(self)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 448, in build_extensions
self._build_extensions_serial()
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 473, in _build_extensions_serial
self.build_extension(ext)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/build_ext.py”, line 202, in build_extension
_build_ext.build_extension(self, ext)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 533, in build_extension
depends=ext.depends)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 538, in unix_wrap_ninja_compile
with_cuda=with_cuda)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1359, in _write_ninja_file_and_compile_objects
error_prefix=‘Error compiling objects for extension’)
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1683, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension

Your custom extension seems to have trouble here:

/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: At global scope:
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:74:16: error: expected constructor, destructor, or type conversion before ‘(’ token

Did you change anything in the code or are you just rebuilding the tutorial?

1 Like

Thank you for your reply @ptrblck . I managed to make it work after your advice. Please delete the post since it is not important for anyone.