1 #include "caffe2/core/logging.h" 2 #include "caffe2/core/module.h" 10 static std::mutex& gModuleChangeMutex() {
15 static CaffeMap<string, const ModuleSchema*>& MutableCurrentModules() {
16 static CaffeMap<string, const ModuleSchema*> module_schema_map_;
17 return module_schema_map_;
22 static CaffeMap<string, void*> CurrentModuleHandles() {
23 static CaffeMap<string, void*> module_handle_map_;
24 return module_handle_map_;
28 return MutableCurrentModules();
31 ModuleSchema::ModuleSchema(
const char* name,
const char* description)
32 : name_(name), description_(description) {
33 std::lock_guard<std::mutex> guard(gModuleChangeMutex());
34 MutableCurrentModules().emplace(name,
this);
39 return (modules.find(name) != modules.end());
44 void LoadModule(
const string& name,
const string& filename) {
46 "On Windows, LoadModule is currently not supported yet and you should " 47 "use static linking for any module that you intend to use.");
52 void LoadModule(
const string& name,
const string& filename) {
54 name.size() > 0 || filename.size() > 0,
55 "You must provide at least one of name and filename.");
57 VLOG(1) <<
"Module " << name <<
" already present. Skip loading.";
60 void* handle =
nullptr;
61 if (filename.size()) {
63 filename.c_str(), RTLD_NOW | RTLD_GLOBAL);
64 CAFFE_ENFORCE(handle !=
nullptr,
65 "Cannot load module ",
67 " (with given filename ",
69 "), are you sure it is correct?");
71 string inferred_name = string(
"lib") + name +
".so";
73 inferred_name.c_str(), RTLD_NOW | RTLD_GLOBAL);
77 string inferred_name = string(
"lib") + name +
".dylib";
79 inferred_name.c_str(), RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
82 CAFFE_ENFORCE(handle !=
nullptr,
83 "Cannot load module ",
85 " (with inferred filename ",
87 "), are you sure it is in the dynamic linker search path?");
93 string module_name_check =
"gCaffe2ModuleSanityCheck" + name;
94 CAFFE_ENFORCE(dlsym(handle, module_name_check.c_str()),
97 " did not pass the module name sanity check. Is it built with the " 98 "right configs? Make sure the file name and the CAFFE2_MODULE name " 102 std::lock_guard<std::mutex> guard(gModuleChangeMutex());
103 CurrentModuleHandles()[name] = handle;
108 <<
"Module file " << filename
109 <<
" was loaded without a proper module name. It is recommended " 110 "that one load a model with an explicit module name in addition " 114 std::lock_guard<std::mutex> guard(gModuleChangeMutex());
115 CurrentModuleHandles()[filename] = handle;
bool HasModule(const string &name)
Checks whether a module is already present in the current binary.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void LoadModule(const string &name, const string &filename)
Load a module.
const CaffeMap< string, const ModuleSchema * > & CurrentModules()
Current Modules present in the Caffe2 runtime.