Custom c++ extension error: pasting "pybind11_init_" and "'sigmoid'" does not give a valid preprocessing token

optimus: <p>I’m trying to add custom c++ extension with pytorch.<br>
I’m following the tutorial at <a href="https://pytorch.org/tutorials/advanced/cpp_extension.html" class="inline-onebox" rel="noopener nofollow ugc">Custom C++ and CUDA Extensions — PyTorch Tutorials 2.1.1+cu121 documentation</a><br>
I have created two files.<br>
project/<br>
main.py<br>
sigmoid.cpp</p>
<h2><a name="mainpy-1" class="anchor" href="#mainpy-1"></a>main.py</h2>
<blockquote>
<p>from torch.utils.cpp_extension import load<br>
lltm_cpp = load(name=‘sigmoid’, sources=[‘sigmoid.cpp’], verbose=True)</p>
</blockquote>
<hr>
<h2><a name="sigmoidcpp-2" class="anchor" href="#sigmoidcpp-2"></a>sigmoid.cpp</h2>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/extension.h&gt;</p>
<p><span class="hashtag-raw">#include</span> </p>
<p>torch::Tensor d_sigmoid(torch::Tensor z) {<br>
auto s = torch::sigmoid(z);<br>
return (1 - s) * s;<br>
}</p>
<p>PYBIND11_MODULE(‘sigmoid’, m) {<br>
m.def(‘d_sigmoid’, &amp;d_sigmoid, ‘sigmoid’);<br>
}</p>
</blockquote>
<hr>
<p>when I run main.py I get the following error</p>
<blockquote>
<p>error: pasting “pybind11_init_” and “‘sigmoid’” does not give a valid preprocessing token</p>
</blockquote>
<p><em>How can I fix the error?</em></p>
<p>Full error report</p>
<blockquote>
<p>Using /tmp/torch_extensions as PyTorch extensions root…<br>
Emitting ninja build file /tmp/torch_extensions/sigmoid/build.ninja…<br>
Building extension module sigmoid…<br>
[1/2] c++ -MMD -MF sigmoid.o.d -DTORCH_EXTENSION_NAME=sigmoid -DTORCH_API_INCLUDE_EXTENSION_H -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/TH -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/THC -isystem /home/wickrama/anaconda3/envs/pytorch/include/python3.6m -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -std=c++11 -c /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp -o sigmoid.o<br>
FAILED: sigmoid.o<br>
c++ -MMD -MF sigmoid.o.d -DTORCH_EXTENSION_NAME=sigmoid -DTORCH_API_INCLUDE_EXTENSION_H -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/TH -isystem /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/THC -isystem /home/wickrama/anaconda3/envs/pytorch/include/python3.6m -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -std=c++11 -c /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp -o sigmoid.o<br>
In file included from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pytypes.h:12:0,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/cast.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/attr.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pybind11.h:44,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/utils/pybind.h:6,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/python.h:12,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/extension.h:6,<br>
from /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:1:<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: error: pasting “pybind11_init_” and “‘sigmoid’” does not give a valid preprocessing token<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: warning: character constant too long for its type<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^~~~~~~~~~~~~~<br>
In file included from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pytypes.h:12:0,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/cast.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/attr.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pybind11.h:44,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/utils/pybind.h:6,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/python.h:12,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/extension.h:6,<br>
from /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:1:<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: error: pasting “PyInit_” and “‘sigmoid’” does not give a valid preprocessing token<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: warning: character constant too long for its type<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^~~~~~~<br>
In file included from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pytypes.h:12:0,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/cast.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/attr.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pybind11.h:44,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/utils/pybind.h:6,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/python.h:12,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/extension.h:6,<br>
from /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:1:<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: error: pasting “pybind11_init_” and “‘sigmoid’” does not give a valid preprocessing token<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: warning: character constant too long for its type<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^~~~~~~~~~~~~~<br>
In file included from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pytypes.h:12:0,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/cast.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/attr.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pybind11.h:44,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/utils/pybind.h:6,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/python.h:12,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/extension.h:6,<br>
from /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:1:<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: error: pasting “pybind11_init_” and “‘sigmoid’” does not give a valid preprocessing token<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:1: warning: character constant too long for its type<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^~~~~~~~~~~~~~<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:11:9: warning: character constant too long for its type<br>
m.def(‘d_sigmoid’, &amp;d_sigmoid, ‘sigmoid’);<br>
^~~~~~~~~~~<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:11:34: warning: character constant too long for its type<br>
m.def(‘d_sigmoid’, &amp;d_sigmoid, ‘sigmoid’);<br>
^~~~~~~~~<br>
In file included from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pytypes.h:12:0,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/cast.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/attr.h:13,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/pybind11/pybind11.h:44,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/utils/pybind.h:6,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/python.h:12,<br>
from /home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/include/torch/extension.h:6,<br>
from /home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:1:<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:17: error: expected initializer before ‘\x6d6f6964’<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:17: error: expected initializer before ‘\x6d6f6964’<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
/home/wickrama/projects/torch_cpp_ext/sigmoid.cpp:10:17: error: expected initializer before ‘\x6d6f6964’<br>
PYBIND11_MODULE(‘sigmoid’, m) {<br>
^<br>
ninja: build stopped: subcommand failed.<br>
Traceback (most recent call last):<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 949, in _build_extension_module<br>
check=True)<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/subprocess.py”, line 438, in run<br>
output=stdout, stderr=stderr)<br>
subprocess.CalledProcessError: Command ‘[‘ninja’, ‘-v’]’ returned non-zero exit status 1.</p>
<p>During handling of the above exception, another exception occurred:</p>
<p>Traceback (most recent call last):<br>
File “/home/wickrama/projects/torch_cpp_ext/main.py”, line 3, in <br>
lltm_cpp = load(name=‘sigmoid’, sources=[‘sigmoid.cpp’], verbose=True)<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 644, in load<br>
is_python_module)<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 813, in _jit_compile<br>
with_cuda=with_cuda)<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 866, in _write_ninja_file_and_build<br>
_build_extension_module(name, build_directory, verbose)<br>
File “/home/wickrama/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 962, in _build_extension_module<br>
raise RuntimeError(message)<br>
RuntimeError: Error building extension ‘sigmoid’</p>
</blockquote>

TomB: <p>Unlike Python, strings in C++ have to use double quotes, single quotes are only used for single characters (a different data type). So it should be:</p>
<pre><code class="lang-auto">PYBIND11_MODULE("sigmoid", m) {
m.def("d_sigmoid", &amp;d_sigmoid, "sigmoid");
}
</code></pre>