[pytorch] add header docs for TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT (#150854)

Summary: Add header docs for the experimental TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT feature, and guard behind C10_MOBILE.

Reviewed By: albanD

Differential Revision: D72572345

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150854
Approved by: https://github.com/larryliu0820, https://github.com/zou3519
This commit is contained in:
Richard Howell
2025-04-09 12:59:24 +00:00
committed by PyTorch MergeBot
parent 142f0f86ce
commit 64ac41f68d
3 changed files with 10 additions and 4 deletions

View File

@ -58,7 +58,7 @@ void Library::reset() {
#define ERROR_CONTEXT "(Error occurred while processing ", toString(kind_), " block at ", file_, ":", line_, ")" #define ERROR_CONTEXT "(Error occurred while processing ", toString(kind_), " block at ", file_, ":", line_, ")"
#ifdef TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT #if defined(TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT) && defined(C10_MOBILE)
namespace detail { namespace detail {
std::vector<TorchLibraryInit*> torch_library_initializers; std::vector<TorchLibraryInit*> torch_library_initializers;
} // namespace detail } // namespace detail

View File

@ -647,7 +647,7 @@ mobile::Module _load_for_mobile(
std::optional<at::Device> device, std::optional<at::Device> device,
ExtraFilesMap& extra_files, ExtraFilesMap& extra_files,
uint64_t module_load_options) { uint64_t module_load_options) {
#ifdef TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT #if defined(TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT) && defined(C10_MOBILE)
torch::initialize_torch_libraries(); torch::initialize_torch_libraries();
#endif #endif
auto observer = torch::observerConfig().getModuleObserver(); auto observer = torch::observerConfig().getModuleObserver();

View File

@ -884,13 +884,19 @@ class TORCH_API Library final {
at::OperatorName _parseNameForLib(const char* name_str) const; at::OperatorName _parseNameForLib(const char* name_str) const;
}; };
#ifdef TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT #if defined(TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT) && defined(C10_MOBILE)
void initialize_torch_libraries(); void initialize_torch_libraries();
#endif #endif
namespace detail { namespace detail {
#ifdef TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT #if defined(TORCH_LIBRARY_THREAD_UNSAFE_LAZY_INIT) && defined(C10_MOBILE)
// This is an experimental feature to defer TorchLibraryInit cost to run either
// at model load time, or when a client application explicitly calls
// torch::initialize_torch_libraries().
//
// This is not thread safe, the client is required to ensure that libraries
// containing TORCH_LIBRARY initializers are loaded in a thread safe manner.
extern std::vector<TorchLibraryInit*> torch_library_initializers; extern std::vector<TorchLibraryInit*> torch_library_initializers;
class TorchLibraryInit final { class TorchLibraryInit final {
private: private: