-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Support Delay Loading of c10.dll in when using libtorch as a thirdparty library. #105058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
in “include\c10\macros\Export.h”,you can see as follow: so you need to define macro "NO_EXPORT", e.g in unreal engine 5,you need to add: |
The following steps enable delay loading of
@kohillyang, if the problem persists, please share:
|
@taras-janea Thank you for the prompt response and suggestions. To clarify, the core issue arises because c10.dll exports a non-function symbol (specifically, a global variable). Since delay-loading only works for function symbols (as global variables require immediate address resolution at load time), this makes c10.dll fundamentally incompatible with delay-loading mechanisms, regardless of NO_EXPORT or linker flags. In our current setup, we’ve successfully applied delay-loading to other larger LibTorch DLLs (e.g., torch_cpu.dll), but c10.dll remains a mandatory immediate dependency due to this limitation. While c10.dll itself is small, its forced inclusion defeats the flexibility of fully optional dependency management. Thanks again for your support! |
@kohillyang, the If the problem persists, please provide a sample project that reproduces the issue using the latest PyTorch version, along with the steps outlined in the comment above. |
30>LINK : fatal error LNK1194: 无法延迟加载“c10_cuda.dll”,原因在于数据符号“"__declspec(dllimport) struct std::atomic<class c10::cuda::CUDACachingAllocator::CUDAAllocator *> c10::cuda::CUDACachingAllocator::allocator" (_imp?allocator@CUDACachingAllocator@cuda@c10@@3u?$atomic@PEAVCUDAAllocator@CUDACachingAllocator@cuda@c10@@@std@@A)”的导入;链接时不使用 /DELAYLOAD:c10_cuda.dll @taras-janea Thank you for the clarification. Let me verify our understanding: We use pre-built binaries specifically to avoid maintenance overhead from custom builds. So I want to ask if the NO_EXPORT macro needs to be defined during libtorch's compilation to prevent exporting global variables in c10.dll. Simply defining it in our application project (using pre-built libtorch binaries) cannot retroactively modify the existing DLL's export table. Is this correct? For users of official pre-built libtorch binaries, is there any way to achieve delay-loading of c10.dll without recompiling libtorch from source? Our tests show that even with NO_EXPORT defined in our project and delay-load flags set, we still get the same error during linking. |
@kohillyang thank you for the question.
Yes, that's correct. The Defining Suggest to ensure that the macro is correctly defined throughout your project. |
Thank you for the detailed explanation, @taras-janea. I've created a minimal reproducible example at https://github.com/kohillyang/libtorch-delay-load-example to demonstrate the issue. After examining 7974 the linker errors, it appears that NO_EXPORT doesn't seem to be controlling all the relevant symbols in c10_cuda.dll. Even with NO_EXPORT defined globally in my project, I still get the LNK1194 error about data symbols being incompatible with delay loading. |
🚀 The feature, motivation and pitch
Hi PyTorch team,
I'm currently working on a project where I have added libtorch as a third-party dependency. Due to the size of libtorch, I wanted to make it an optional dependency using delay load hooks. However, I've encountered an issue with c10.dll, which is a dependency of libtorch. It seems that c10.dll exports a global variable, causing it to be unable to be delay loaded.
Here's the error message I encountered

I'm wondering if it's possible to add support for delay loading c10.dll in libtorch. Having this feature would be really helpful for my project, as it will allow me to better manage the dependency loading and reduce the initial load time.
Alternatives
No response
Additional context
No response
cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @vladimir-aubrecht @iremyux @Blackhex @cristianPanaite
The text was updated successfully, but these errors were encountered: