|
26 | 26 | } \ |
27 | 27 | } while (0) |
28 | 28 |
|
| 29 | +namespace |
| 30 | +{ |
| 31 | + |
| 32 | +//! Translates a "logical" library name into an OS-dependent DSO or DLL name |
| 33 | +std::string getOSLibraryName(char const* logicalName) |
| 34 | +{ |
| 35 | + std::stringstream libName; |
| 36 | +#if defined(_WIN32) |
| 37 | + libName << logicalName << ".dll"; |
| 38 | +#else |
| 39 | + libName << "lib" << logicalName << ".so." << NV_TENSORRT_MAJOR; |
| 40 | +#endif |
| 41 | + return libName.str(); |
| 42 | +} |
| 43 | + |
| 44 | +//! Platform-agnostic wrapper around dynamic libraries. |
| 45 | +class DynamicLibrary |
| 46 | +{ |
| 47 | +public: |
| 48 | + explicit DynamicLibrary(std::string const& name) |
| 49 | + : mLibName{name} |
| 50 | + { |
| 51 | +#if defined(_WIN32) |
| 52 | + mHandle = LoadLibraryA(name.c_str()); |
| 53 | +#else // defined(_WIN32) |
| 54 | + int32_t flags{RTLD_LAZY}; |
| 55 | + mHandle = dlopen(name.c_str(), flags); |
| 56 | +#endif // defined(_WIN32) |
| 57 | + |
| 58 | + if (mHandle == nullptr) |
| 59 | + { |
| 60 | + std::string errorStr{}; |
| 61 | +#if !defined(_WIN32) |
| 62 | + errorStr = std::string{" due to "} + std::string{dlerror()}; |
| 63 | +#endif |
| 64 | + throw std::runtime_error("Unable to open library: " + name + errorStr); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + DynamicLibrary(DynamicLibrary const&) = delete; |
| 69 | + DynamicLibrary(DynamicLibrary const&&) = delete; |
| 70 | + |
| 71 | + ~DynamicLibrary() |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | +#if defined(_WIN32) |
| 76 | + RT_ASSERT(static_cast<bool>(FreeLibrary(static_cast<HMODULE>(mHandle)))); |
| 77 | +#else |
| 78 | + RT_ASSERT(dlclose(mHandle) == 0); |
| 79 | +#endif |
| 80 | + } |
| 81 | + catch (...) |
| 82 | + { |
| 83 | + std::cerr << "Unable to close library: " << mLibName << std::endl; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + //! |
| 88 | + //! Retrieve a function symbol from the loaded library. |
| 89 | + //! |
| 90 | + //! \return the loaded symbol on success |
| 91 | + //! \throw std::invalid_argument if loading the symbol failed. |
| 92 | + //! |
| 93 | + template <typename Signature> |
| 94 | + std::function<Signature> symbolAddress(char const* name) |
| 95 | + { |
| 96 | + if (mHandle == nullptr) |
| 97 | + { |
| 98 | + throw std::runtime_error("Handle to library is nullptr."); |
| 99 | + } |
| 100 | + void* ret; |
| 101 | +#if defined(_MSC_VER) |
| 102 | + ret = static_cast<void*>(GetProcAddress(static_cast<HMODULE>(mHandle), name)); |
| 103 | +#else |
| 104 | + ret = dlsym(mHandle, name); |
| 105 | +#endif |
| 106 | + if (ret == nullptr) |
| 107 | + { |
| 108 | + std::string const kERROR_MSG(mLibName + ": error loading symbol: " + std::string(name)); |
| 109 | + throw std::invalid_argument(kERROR_MSG); |
| 110 | + } |
| 111 | + return reinterpret_cast<Signature*>(ret); |
| 112 | + } |
| 113 | + |
| 114 | + std::string getFullPath() const |
| 115 | + { |
| 116 | + RT_ASSERT(mHandle != nullptr); |
| 117 | +#if defined(__linux__) |
| 118 | + link_map* linkMap = nullptr; |
| 119 | + auto const err = dlinfo(mHandle, RTLD_DI_LINKMAP, &linkMap); |
| 120 | + RT_ASSERT(err == 0 && linkMap != nullptr && linkMap->l_name != nullptr); |
| 121 | + return std::string{linkMap->l_name}; |
| 122 | +#elif defined(_WIN32) |
| 123 | + constexpr int32_t kMAX_PATH_LEN{4096}; |
| 124 | + std::string path(kMAX_PATH_LEN, '\0'); // since C++11, std::string storage is guaranteed to be contiguous |
| 125 | + auto const pathLen = GetModuleFileNameA(static_cast<HMODULE>(mHandle), &path[0], kMAX_PATH_LEN); |
| 126 | + RT_ASSERT(GetLastError() == ERROR_SUCCESS); |
| 127 | + path.resize(pathLen); |
| 128 | + path.shrink_to_fit(); |
| 129 | + return path; |
| 130 | +#else |
| 131 | + RT_ASSERT(!"Unsupported operation: getFullPath()"); |
| 132 | +#endif |
| 133 | + } |
| 134 | + |
| 135 | +private: |
| 136 | + std::string mLibName{}; //!< Name of the DynamicLibrary |
| 137 | + void* mHandle{}; //!< Handle to the DynamicLibrary |
| 138 | +}; |
| 139 | + |
| 140 | +//! Translates an OS-dependent DSO/DLL name into a path on the filesystem |
| 141 | +std::string getOSLibraryPath(std::string const& osLibName) |
| 142 | +{ |
| 143 | + DynamicLibrary lib{osLibName}; |
| 144 | + return lib.getFullPath(); |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace |
| 148 | + |
29 | 149 | namespace onnx2trt |
30 | 150 | { |
31 | 151 |
|
@@ -105,7 +225,8 @@ void ImporterContext::registerTensor(TensorOrWeights tensor, std::string const& |
105 | 225 | p.first->second = std::move(tensor); |
106 | 226 | } |
107 | 227 |
|
108 | | -void ImporterContext::registerLayer(nvinfer1::ILayer* layer, std::string const& basename, ::ONNX_NAMESPACE::NodeProto const* node) |
| 228 | +void ImporterContext::registerLayer( |
| 229 | + nvinfer1::ILayer* layer, std::string const& basename, ::ONNX_NAMESPACE::NodeProto const* node) |
109 | 230 | { |
110 | 231 | // No layer will be added for Constant nodes in ONNX. |
111 | 232 | if (layer) |
@@ -149,99 +270,6 @@ void ImporterContext::registerLayer(nvinfer1::ILayer* layer, ::ONNX_NAMESPACE::N |
149 | 270 | registerLayer(layer, basename, &node); |
150 | 271 | } |
151 | 272 |
|
152 | | -namespace |
153 | | -{ |
154 | | - |
155 | | -//! Translates a "logical" library name into an OS-dependent DSO or DLL name |
156 | | -std::string getOSLibraryName(char const* logicalName) |
157 | | -{ |
158 | | - std::stringstream libName; |
159 | | -#if defined(_WIN32) |
160 | | - libName << logicalName << ".dll"; |
161 | | -#else |
162 | | - libName << "lib" << logicalName << ".so." << NV_TENSORRT_MAJOR; |
163 | | -#endif |
164 | | - return libName.str(); |
165 | | -} |
166 | | - |
167 | | -//! Platform-agnostic wrapper around dynamic libraries. |
168 | | -class DynamicLibrary |
169 | | -{ |
170 | | -public: |
171 | | - explicit DynamicLibrary(std::string const& name) |
172 | | - : mLibName{name} |
173 | | - { |
174 | | -#if defined(_WIN32) |
175 | | - mHandle = LoadLibraryA(name.c_str()); |
176 | | -#else // defined(_WIN32) |
177 | | - int32_t flags{RTLD_LAZY}; |
178 | | - mHandle = dlopen(name.c_str(), flags); |
179 | | -#endif // defined(_WIN32) |
180 | | - |
181 | | - if (mHandle == nullptr) |
182 | | - { |
183 | | - std::string errorStr{}; |
184 | | -#if !defined(_WIN32) |
185 | | - errorStr = std::string{" due to "} + std::string{dlerror()}; |
186 | | -#endif |
187 | | - throw std::runtime_error("Unable to open library: " + name + errorStr); |
188 | | - } |
189 | | - } |
190 | | - |
191 | | - DynamicLibrary(DynamicLibrary const&) = delete; |
192 | | - DynamicLibrary(DynamicLibrary const&&) = delete; |
193 | | - |
194 | | - ~DynamicLibrary() |
195 | | - { |
196 | | - try |
197 | | - { |
198 | | -#if defined(_WIN32) |
199 | | - RT_ASSERT(static_cast<bool>(FreeLibrary(static_cast<HMODULE>(mHandle)))); |
200 | | -#else |
201 | | - RT_ASSERT(dlclose(mHandle) == 0); |
202 | | -#endif |
203 | | - } |
204 | | - catch (...) |
205 | | - { |
206 | | - std::cerr << "Unable to close library: " << mLibName << std::endl; |
207 | | - } |
208 | | - } |
209 | | - |
210 | | - std::string getFullPath() const |
211 | | - { |
212 | | - RT_ASSERT(mHandle != nullptr); |
213 | | -#if defined(__linux__) |
214 | | - link_map* linkMap = nullptr; |
215 | | - auto const err = dlinfo(mHandle, RTLD_DI_LINKMAP, &linkMap); |
216 | | - RT_ASSERT(err == 0 && linkMap != nullptr && linkMap->l_name != nullptr); |
217 | | - return std::string{linkMap->l_name}; |
218 | | -#elif defined(_WIN32) |
219 | | - constexpr int32_t kMAX_PATH_LEN{4096}; |
220 | | - std::string path(kMAX_PATH_LEN, '\0'); // since C++11, std::string storage is guaranteed to be contiguous |
221 | | - auto const pathLen = GetModuleFileNameA(static_cast<HMODULE>(mHandle), &path[0], kMAX_PATH_LEN); |
222 | | - RT_ASSERT(GetLastError() == ERROR_SUCCESS); |
223 | | - path.resize(pathLen); |
224 | | - path.shrink_to_fit(); |
225 | | - return path; |
226 | | -#else |
227 | | - RT_ASSERT(!"Unsupported operation: getFullPath()"); |
228 | | -#endif |
229 | | - } |
230 | | - |
231 | | -private: |
232 | | - std::string mLibName{}; //!< Name of the DynamicLibrary |
233 | | - void* mHandle{}; //!< Handle to the DynamicLibrary |
234 | | -}; |
235 | | - |
236 | | -//! Translates an OS-dependent DSO/DLL name into a path on the filesystem |
237 | | -std::string getOSLibraryPath(std::string const& osLibName) |
238 | | -{ |
239 | | - DynamicLibrary lib{osLibName}; |
240 | | - return lib.getFullPath(); |
241 | | -} |
242 | | - |
243 | | -} // namespace |
244 | | - |
245 | 273 | void ImporterContext::addUsedVCPluginLibrary( |
246 | 274 | ::ONNX_NAMESPACE::NodeProto const& node, char const* pluginName, char const* pluginLib) |
247 | 275 | { |
@@ -272,3 +300,4 @@ std::vector<std::string> ImporterContext::getUsedVCPluginLibraries() |
272 | 300 | } |
273 | 301 |
|
274 | 302 | } // namespace onnx2trt |
| 303 | + |
0 commit comments