chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Waiting to run
Docker Image CI / build-ubuntu2004 (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
add_library(trt_shared STATIC)
|
||||
|
||||
function(add_shared_source)
|
||||
target_sources(trt_shared PRIVATE ${ARGN})
|
||||
endfunction()
|
||||
|
||||
target_link_libraries(trt_shared PRIVATE
|
||||
$<COMPILE_ONLY:tensorrt>
|
||||
tensorrt_headers
|
||||
trt_global_definitions
|
||||
)
|
||||
|
||||
add_subdirectory(utils)
|
||||
|
||||
target_include_directories(trt_shared PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
add_shared_source(
|
||||
fileLock.cpp
|
||||
fileLock.h
|
||||
cacheUtils.cpp
|
||||
cacheUtils.h
|
||||
)
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "cacheUtils.h"
|
||||
#include "NvInfer.h"
|
||||
#include "fileLock.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace nvinfer1::utils
|
||||
{
|
||||
std::vector<char> loadCacheFile(ILogger& logger, std::string const& inFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileLock fileLock{logger, inFileName};
|
||||
std::ifstream iFile(inFileName, std::ios::in | std::ios::binary);
|
||||
if (!iFile)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Could not read cache from: " << inFileName << ". A new cache will be generated and written.";
|
||||
logger.log(ILogger::Severity::kWARNING, ss.str().c_str());
|
||||
return std::vector<char>();
|
||||
}
|
||||
iFile.seekg(0, std::ifstream::end);
|
||||
size_t fsize = iFile.tellg();
|
||||
iFile.seekg(0, std::ifstream::beg);
|
||||
std::vector<char> content(fsize);
|
||||
iFile.read(content.data(), fsize);
|
||||
iFile.close();
|
||||
std::stringstream ss;
|
||||
ss << "Loaded " << fsize << " bytes of cache from file: " << inFileName;
|
||||
logger.log(ILogger::Severity::kINFO, ss.str().c_str());
|
||||
return content;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
std::cerr << "Exception while loading cache file " << inFileName << ": " << e.what() << std::endl;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<ITimingCache> buildTimingCacheFromFile(
|
||||
ILogger& logger, IBuilderConfig& config, std::string const& timingCacheFile)
|
||||
{
|
||||
std::unique_ptr<nvinfer1::ITimingCache> timingCache{};
|
||||
std::vector<char> timingCacheContents = loadCacheFile(logger, timingCacheFile);
|
||||
|
||||
timingCache.reset(config.createTimingCache(timingCacheContents.data(), timingCacheContents.size()));
|
||||
if (timingCache == nullptr)
|
||||
{
|
||||
logger.log(ILogger::Severity::kERROR, ("Failed to create ITimingCache from file " + timingCacheFile).c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
config.clearFlag(BuilderFlag::kDISABLE_TIMING_CACHE);
|
||||
if (!config.setTimingCache(*timingCache, true))
|
||||
{
|
||||
logger.log(ILogger::Severity::kERROR,
|
||||
("IBuilderConfig#setTimingCache failed with timing cache from file " + timingCacheFile).c_str());
|
||||
return nullptr;
|
||||
}
|
||||
return timingCache;
|
||||
}
|
||||
|
||||
void saveCacheFile(ILogger& logger, std::string const& outFileName, IHostMemory const* blob)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileLock fileLock{logger, outFileName};
|
||||
std::ofstream oFile(outFileName, std::ios::out | std::ios::binary);
|
||||
if (!oFile)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Could not write cache to file: " << outFileName;
|
||||
logger.log(ILogger::Severity::kWARNING, ss.str().c_str());
|
||||
return;
|
||||
}
|
||||
oFile.write(reinterpret_cast<char const*>(blob->data()), blob->size());
|
||||
oFile.close();
|
||||
std::stringstream ss;
|
||||
ss << "Saved " << blob->size() << " bytes of cache to file: " << outFileName;
|
||||
logger.log(ILogger::Severity::kINFO, ss.str().c_str());
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
std::cerr << "Exception while saving cache file " << outFileName << ": " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void updateTimingCacheFile(nvinfer1::ILogger& logger, std::string const& fileName,
|
||||
nvinfer1::ITimingCache const* timingCache, nvinfer1::IBuilder& builder)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::unique_ptr<IBuilderConfig> config{builder.createBuilderConfig()};
|
||||
std::vector<char> timingCacheContents = loadCacheFile(logger, fileName);
|
||||
std::unique_ptr<ITimingCache> fileTimingCache{
|
||||
config->createTimingCache(timingCacheContents.data(), timingCacheContents.size())};
|
||||
|
||||
fileTimingCache->combine(*timingCache, false);
|
||||
std::unique_ptr<IHostMemory> blob{fileTimingCache->serialize()};
|
||||
if (!blob)
|
||||
{
|
||||
throw std::runtime_error("Failed to serialize combined ITimingCache!");
|
||||
}
|
||||
|
||||
FileLock fileLock{logger, fileName};
|
||||
std::ofstream oFile(fileName, std::ios::out | std::ios::binary);
|
||||
if (!oFile)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Could not write timing cache to: " << fileName;
|
||||
logger.log(ILogger::Severity::kWARNING, ss.str().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
oFile.write(reinterpret_cast<char const*>(blob->data()), blob->size());
|
||||
oFile.close();
|
||||
std::stringstream ss;
|
||||
ss << "Saved " << blob->size() << " bytes of timing cache to " << fileName;
|
||||
logger.log(ILogger::Severity::kINFO, ss.str().c_str());
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
std::cerr << "Exception while updating timing cache file " << fileName << ": " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace nvinfer1::utils
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef TRT_SHARED_TIMINGCACHE_H_
|
||||
#define TRT_SHARED_TIMINGCACHE_H_
|
||||
|
||||
#include "NvInfer.h"
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nvinfer1::utils
|
||||
{
|
||||
|
||||
//! \brief Loads the binary contents of a cache file into a char vector. Used for both timing cache and runtime cache.
|
||||
//!
|
||||
//! \note This is a blocking operation, as this method will acquire an exclusive file lock on the cache file for
|
||||
//! the duration of the read. \returns The binary data from the file, or an empty vector if an error occurred.
|
||||
std::vector<char> loadCacheFile(nvinfer1::ILogger& logger, std::string const& inFileName);
|
||||
|
||||
//! \brief Helper method to load a timing cache from a file, build an ITimingCache with the data, and then set the new
|
||||
//! timing cache to the builder config. If the file is blank, or cannot be read, a new timing cache will be created from
|
||||
//! scratch.
|
||||
//!
|
||||
//! \returns The newly created timing cache, or nullptr if an error occurred during creation.
|
||||
std::unique_ptr<ITimingCache> buildTimingCacheFromFile(
|
||||
ILogger& logger, IBuilderConfig& config, std::string const& timingCacheFile);
|
||||
|
||||
//! \brief Saves the contents of a cache object to a binary file.
|
||||
//!
|
||||
//! \note This is a blocking operation, as this method will acquire an exclusive file lock on the cache file for
|
||||
//! the duration of the write.
|
||||
void saveCacheFile(nvinfer1::ILogger& logger, std::string const& outFileName, nvinfer1::IHostMemory const* blob);
|
||||
|
||||
//! \brief Updates the contents of a timing cache binary file.
|
||||
//! This operation loads the timing cache file, combines it with the passed timingCache, and serializes the combined
|
||||
//! timing cache.
|
||||
//!
|
||||
//! \note This is a blocking operation, as this method will acquire an exclusive file lock on the timing cache file for
|
||||
//! the duration of the write.
|
||||
void updateTimingCacheFile(nvinfer1::ILogger& logger, std::string const& fileName,
|
||||
nvinfer1::ITimingCache const* timingCache, nvinfer1::IBuilder& builder);
|
||||
|
||||
} // namespace nvinfer1::utils
|
||||
|
||||
#endif // TRT_SHARED_TIMINGCACHE_H_
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "fileLock.h"
|
||||
#include "NvInfer.h"
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace nvinfer1::utils
|
||||
{
|
||||
|
||||
FileLock::FileLock(ILogger& logger, std::string fileName)
|
||||
: mLogger(logger)
|
||||
, mFileName(std::move(fileName))
|
||||
{
|
||||
std::string lockFileName = mFileName + ".lock";
|
||||
#ifdef _MSC_VER
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Trying to set exclusive file lock " << lockFileName << std::endl;
|
||||
mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str());
|
||||
}
|
||||
// MS docs said this is a blocking IO if "FILE_FLAG_OVERLAPPED" is not provided
|
||||
mHandle = CreateFileA(lockFileName.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
|
||||
if (mHandle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
throw std::runtime_error("Failed to lock " + lockFileName + "!");
|
||||
}
|
||||
#elif defined(__QNX__)
|
||||
// Calling lockf(F_TLOCK) on QNX returns -1; the reported error is 89 (function not implemented).
|
||||
mLogger.log(ILogger::Severity::kVERBOSE, "FileLock is not supported on QNX or HOS.");
|
||||
#else
|
||||
mHandle = fopen(lockFileName.c_str(), "wb+");
|
||||
if (mHandle == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Cannot open " + lockFileName + "!");
|
||||
}
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Trying to set exclusive file lock " << lockFileName << std::endl;
|
||||
mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str());
|
||||
}
|
||||
mDescriptor = fileno(mHandle);
|
||||
auto ret = lockf(mDescriptor, F_LOCK, 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
mDescriptor = -1;
|
||||
fclose(mHandle);
|
||||
throw std::runtime_error("Failed to lock " + lockFileName + "!");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
FileLock::~FileLock()
|
||||
{
|
||||
std::string lockFileName = mFileName + ".lock";
|
||||
#ifdef _MSC_VER
|
||||
if (mHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(mHandle);
|
||||
}
|
||||
#elif defined(__QNX__)
|
||||
// Calling lockf(F_TLOCK) on QNX returns -1; the reported error is 89 (function not implemented).
|
||||
mLogger.log(ILogger::Severity::kVERBOSE, "FileLock is not supported on QNX or HOS.");
|
||||
#else
|
||||
if (mDescriptor != -1)
|
||||
{
|
||||
auto ret = lockf(mDescriptor, F_ULOCK, 0);
|
||||
if (mHandle != nullptr)
|
||||
{
|
||||
fclose(mHandle);
|
||||
}
|
||||
if (ret != 0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Failed to unlock " << lockFileName << ", please remove " << lockFileName << ".lock manually!"
|
||||
<< std::endl;
|
||||
mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace nvinfer1::utils
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TRT_SHARED_FILELOCK_H_
|
||||
#define TRT_SHARED_FILELOCK_H_
|
||||
#include "NvInfer.h"
|
||||
#ifdef _MSC_VER
|
||||
// Needed so that the max/min definitions in windows.h do not conflict with std::max/min.
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#undef NOMINMAX
|
||||
#else
|
||||
#include <stdio.h> // fileno
|
||||
#include <unistd.h> // lockf
|
||||
#endif
|
||||
#include <string>
|
||||
|
||||
namespace nvinfer1::utils
|
||||
{
|
||||
|
||||
//! \brief RAII object that locks the specified file.
|
||||
//!
|
||||
//! The FileLock class uses a lock file to specify that the
|
||||
//! current file is being used by a TensorRT tool or sample
|
||||
//! so that things like the TimingCache can be updated across
|
||||
//! processes without having conflicts.
|
||||
class FileLock
|
||||
{
|
||||
public:
|
||||
explicit FileLock(nvinfer1::ILogger& logger, std::string fileName);
|
||||
~FileLock();
|
||||
FileLock() = delete; // no default ctor
|
||||
FileLock(FileLock const&) = delete; // no copy ctor
|
||||
FileLock& operator=(FileLock const&) = delete; // no copy assignment
|
||||
FileLock(FileLock&&) = delete; // no move ctor
|
||||
FileLock& operator=(FileLock&&) = delete; // no move assignment
|
||||
|
||||
private:
|
||||
//!
|
||||
//! The logger that emits any error messages that might show up.
|
||||
//!
|
||||
nvinfer1::ILogger& mLogger;
|
||||
|
||||
//!
|
||||
//! The filename that the FileLock is protecting from multiple
|
||||
//! TensorRT processes from writing to.
|
||||
//!
|
||||
std::string const mFileName;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//!
|
||||
//! The file handle on windows for the file lock.
|
||||
//!
|
||||
HANDLE mHandle{};
|
||||
#elif !defined(__QNX__)
|
||||
//!
|
||||
//! The file handle on linux for the file lock.
|
||||
//!
|
||||
FILE* mHandle{};
|
||||
//!
|
||||
//! The file descriptor on linux of the file lock.
|
||||
//!
|
||||
int32_t mDescriptor{-1};
|
||||
#endif
|
||||
}; // class FileLock
|
||||
|
||||
} // namespace nvinfer1::utils
|
||||
|
||||
#endif // TRT_SHARED_FILELOCK_H_
|
||||
Reference in New Issue
Block a user