Add -rdc=True flag when compiling C++ CUDA code with setup.py

username: Ramansh_Sharma
cooked: <p>Hello,</p>
<p>I am trying to make a custom C++ CUDA kernel to use in my PyTorch code. This is my setup.py file. The problem is, by default, the compilation command the setup.py invokes does not have <code>-rdc=True</code> in the command, which I need to add for my C++ code. Is there a way I can add this somewhere in the setup.py file?</p>
<p>Thank you.</p>
<pre data-code-wrap="python"><code class="lang-python">from setuptools import setup, Extension
from torch.utils import cpp_extension
from torch.utils.cpp_extension import BuildExtension, CUDAExtension, load

setup(
    name='cpp_kernel',
    ext_modules=[
        CUDAExtension('cpp_kernel', [
            'main.cpp',
            'cuda_kernel.cu',
        ])
    ],
    cmdclass={
        'build_ext': BuildExtension
})
</code></pre>
<p>The default command is this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55cd2f9139f549603dd0e74927bd2f67b4e89354.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/55cd2f9139f549603dd0e74927bd2f67b4e89354" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55cd2f9139f549603dd0e74927bd2f67b4e89354_2_690x194.jpeg" alt="image" data-base62-sha1="cf2c02iWtvp1OlTesMPYCIbcsDy" width="690" height="194" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55cd2f9139f549603dd0e74927bd2f67b4e89354_2_690x194.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55cd2f9139f549603dd0e74927bd2f67b4e89354_2_1035x291.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55cd2f9139f549603dd0e74927bd2f67b4e89354_2_1380x388.jpeg 2x" data-dominant-color="201F20"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×540 195 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Ramansh_Sharma
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, I thought of tagging you here in case you may know about this (or know someone who I could ask). Thank you!</p>

username: ptrblck
cooked: <p>Relocatable device code linking was added in <a href="https://github.com/pytorch/pytorch/pull/78225">this PR</a> and is explained in <a href="https://pytorch.org/docs/stable/cpp_extension.html#torch.utils.cpp_extension.CUDAExtension">this doc</a>.</p>

username: Ramansh_Sharma
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, I modified my <code>setup.py</code> file to this:</p>
<pre data-code-wrap="python"><code class="lang-python">from setuptools import setup, Extension
from torch.utils import cpp_extension
from torch.utils.cpp_extension import BuildExtension, CUDAExtension

setup(
    name='cpp_kernel',
    ext_modules=[
        CUDAExtension(
            name='cpp_kernel',
            sources=['main.cpp', 'cuda_kernel.cu'],
            extra_compile_args={'nvcc': ['-lcudadevrt', '-rdc=true']})
    ],                   
    cmdclass={
        'build_ext': BuildExtension.with_options(use_ninja=False)
})
</code></pre>
<p>Upon compiling, I now get this error that I cannot figure out how to solve. If you know how to fix this, can you please tell me? Thank you so much for your help.</p>
<p>(I highlighted the error at the bottom of the output)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fb23fa659d75ba786f4c46825604bf6fbcb421d1.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fb23fa659d75ba786f4c46825604bf6fbcb421d1" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb23fa659d75ba786f4c46825604bf6fbcb421d1_2_690x210.jpeg" alt="image" data-base62-sha1="zPGVwAUhWKGGpKRBdawXKADDbRn" width="690" height="210" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb23fa659d75ba786f4c46825604bf6fbcb421d1_2_690x210.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb23fa659d75ba786f4c46825604bf6fbcb421d1_2_1035x315.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb23fa659d75ba786f4c46825604bf6fbcb421d1_2_1380x420.jpeg 2x" data-dominant-color="313131"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×585 201 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>