Skip to content

Commit

Permalink
Use std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
fpetrini15 committed Jan 23, 2024
1 parent 333ba38 commit b627e34
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 58 deletions.
40 changes: 20 additions & 20 deletions src/pb_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@

namespace triton { namespace backend { namespace python {

bool
FileExists(std::string& path)
{
struct stat buffer;
return stat(path.c_str(), &buffer) == 0;
}

void
LastModifiedTime(const std::string& path, time_t* last_modified_time)
{
struct stat result;
if (stat(path.c_str(), &result) == 0) {
*last_modified_time = result.st_mtime;
} else {
throw PythonBackendException(std::string(
"LastModifiedTime() failed as file \'" + path +
std::string("\' does not exists.")));
}
}

#ifndef _WIN32
void
CopySingleArchiveEntry(archive* input_archive, archive* output_archive)
Expand Down Expand Up @@ -309,24 +329,4 @@ EnvironmentManager::~EnvironmentManager()
}
#endif

bool
FileExists(std::string& path)
{
struct stat buffer;
return stat(path.c_str(), &buffer) == 0;
}

void
LastModifiedTime(const std::string& path, time_t* last_modified_time)
{
struct stat result;
if (stat(path.c_str(), &result) == 0) {
*last_modified_time = result.st_mtime;
} else {
throw PythonBackendException(std::string(
"LastModifiedTime() failed as file \'" + path +
std::string("\' does not exists.")));
}
}

}}} // namespace triton::backend::python
9 changes: 4 additions & 5 deletions src/pb_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,7 @@ Logger::Log(
// and pass messages to cerr
if (!BackendLoggingActive()) {
std::string path(filename);
const std::string os_slash = FileSeparator();
size_t pos = path.rfind(os_slash);
size_t pos = path.rfind(std::filesystem::path::preferred_separator);
if (pos != std::string::npos) {
path = path.substr(pos + 1, std::string::npos);
}
Expand Down Expand Up @@ -1841,7 +1840,7 @@ ModelContext::Init(
const std::string& model_path, const std::string& runtime_modeldir,
const std::string& triton_install_path, const std::string& model_version)
{
const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
type_ = ModelType::kDefault;
if (runtime_modeldir != "DEFAULT") {
// For python based backends, existence of `model.py` in the corresponding
Expand All @@ -1867,7 +1866,7 @@ ModelContext::Init(
void
ModelContext::StubSetup(py::module& sys)
{
const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
std::string model_name =
python_model_path_.substr(python_model_path_.find_last_of(os_slash) + 1);

Expand Down Expand Up @@ -1943,7 +1942,7 @@ main(int argc, char** argv)

// Find the package name from model path.
size_t prev = 0, pos = 0;
const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
do {
pos = model_path.find(os_slash, prev);
if (pos == std::string::npos)
Expand Down
4 changes: 3 additions & 1 deletion src/pb_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include <filesystem>

#include "infer_request.h"
#include "infer_response.h"
#include "ipc_message.h"
Expand Down Expand Up @@ -125,7 +127,7 @@ class LogMessage {
LogMessage(const char* file, int line, LogLevel level) : level_(level)
{
std::string path(file);
const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
size_t pos = path.rfind(os_slash);
if (pos != std::string::npos) {
path = path.substr(pos + 1, std::string::npos);
Expand Down
26 changes: 4 additions & 22 deletions src/pb_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,34 +286,16 @@ IsUsingCUDAPool(

#endif // TRITON_ENABLE_GPU

const std::string&
FileSeparator()
{
#ifdef _WIN32
static std::string file_separator = "\\";
#else
static std::string file_separator = "/";
#endif
return file_separator;
}

// FIXME: We should not need this function. However, some
// paths are being retrieved from core that are not platform-
// agnostic. DLIS-6078 has been created to track the effort
// to clean up external modules and remove this function.
void
SanitizePath(std::string& path)
{
std::replace(path.begin(), path.end(), '/', '\\');
}

const std::string&
StubExecutableName()
{
#ifdef _WIN32
static std::string executable_name = "triton_python_backend_stub.exe";
#else
static std::string executable_name = "triton_python_backend_stub";
#endif
return executable_name;
}

#ifndef TRITON_PB_STUB
std::shared_ptr<TRITONSERVER_Error*>
WrapTritonErrorInSharedPtr(TRITONSERVER_Error* error)
Expand Down
2 changes: 0 additions & 2 deletions src/pb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ class ScopedSetDevice {

// Utility functions to get the correct file separator depending on the OS.
// Can likely be replaced with std::filesystem with the upgrade to C++17
const std::string& FileSeparator();
void SanitizePath(std::string& path);
const std::string& StubExecutableName();

// Check if the data is allocated from the pool by the base address.
bool IsUsingCUDAPool(
Expand Down
8 changes: 4 additions & 4 deletions src/python_be.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "python_be.h"

#include <filesystem>

#include "gpu_buffers.h"
#include "infer_payload.h"
#include "model_loader.h"
Expand Down Expand Up @@ -2238,19 +2240,17 @@ TRITONBACKEND_Initialize(TRITONBACKEND_Backend* backend)
RETURN_IF_ERROR(
TRITONBACKEND_BackendArtifacts(backend, &artifact_type, &clocation));

const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
std::string location(clocation);
#ifdef _WIN32
const std::string stub_executable_name = "triton_python_backend_stub.exe";
std::string location(clocation);
SanitizePath(location);
SanitizePath(default_backend_dir_string);
#else
const std::string stub_executable_name = "triton_python_backend_stub";
#endif
// Check if `triton_python_backend_stub` and `triton_python_backend_utils.py`
// are located under `location`.
// DLIS-5596: Add forward slash to be platform agnostic
// (i.e. For Windows, we need to use backward slash).
std::string default_python_backend_dir =
default_backend_dir_string + os_slash + "python";
std::string backend_stub_path = location + os_slash + stub_executable_name;
Expand Down
9 changes: 5 additions & 4 deletions src/stub_launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "stub_launcher.h"

#include <filesystem>

#include "python_be.h"

#ifdef _WIN32
Expand Down Expand Up @@ -86,7 +88,7 @@ StubLauncher::Initialize(ModelState* model_state)
model_version_ = model_state->Version();

std::stringstream ss;
const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
ss << model_repository_path_ << os_slash << model_version_ << os_slash;
std::string artifact_name;
RETURN_IF_ERROR(model_state->ModelConfig().MemberAsString(
Expand Down Expand Up @@ -229,7 +231,7 @@ StubLauncher::Launch()
stub_name = model_instance_name_;
}

const std::string os_slash = FileSeparator();
const char os_slash = std::filesystem::path::preferred_separator;
#ifdef _WIN32
const std::string stub_executable_name = "triton_python_backend_stub.exe";
SanitizePath(model_path_);
Expand Down Expand Up @@ -366,8 +368,7 @@ StubLauncher::Launch()
stub_args[3] = nullptr; // Last argument must be nullptr

// Default Python backend stub
std::string python_backend_stub =
python_lib_ + os_slash_ + "triton_python_backend_stub";
std::string python_backend_stub = python_lib_ + "/triton_python_backend_stub";

// Path to alternative Python backend stub
std::string model_python_backend_stub =
Expand Down

0 comments on commit b627e34

Please sign in to comment.