Skip to content

Commit 8474fa0

Browse files
committed
Generalized implementation of : added recursive flag and mount_dir
1 parent e5a40b7 commit 8474fa0

File tree

9 files changed

+92
-52
lines changed

9 files changed

+92
-52
lines changed

src/backend_model.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ TritonModel::Create(
7575
}
7676

7777
// Localize the content of the model repository corresponding to
78-
// 'model_path' and model's version. This model holds a handle to
78+
// 'model_path'. This model holds a handle to
7979
// the localized content so that it persists as long as the model is loaded.
8080
std::shared_ptr<LocalizedPath> localized_model_dir;
8181
RETURN_IF_ERROR(
82-
LocalizePath(model_path, std::to_string(version), &localized_model_dir));
82+
LocalizePath(model_path, false /*recursive*/, &localized_model_dir));
83+
84+
const auto version_path = JoinPath({model_path, std::to_string(version)});
85+
std::shared_ptr<LocalizedPath> localized_version_dir;
86+
RETURN_IF_ERROR(LocalizePath(
87+
version_path, true /*recursive*/, localized_model_dir->Path(),
88+
&localized_version_dir));
89+
90+
localized_model_dir->other_localized_path.push_back(localized_version_dir);
8391

8492
// Localize paths in backend model config
8593
// [FIXME] Remove once a more permanent solution is implemented (DLIS-4211)
@@ -111,12 +119,11 @@ TritonModel::Create(
111119
// Get the path to the backend shared library. Search path is
112120
// version directory, model directory, global backend directory.
113121
const auto localized_model_path = localized_model_dir->Path();
114-
const auto version_path =
115-
JoinPath({localized_model_path, std::to_string(version)});
122+
const auto localized_version_path = localized_version_dir->Path();
116123
const std::string global_path =
117124
JoinPath({backend_dir, specialized_backend_name});
118125
const std::vector<std::string> search_paths = {
119-
version_path, localized_model_path, global_path};
126+
localized_version_path, localized_model_path, global_path};
120127

121128
std::string backend_libdir;
122129
std::string backend_libpath;

src/filesystem/api.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,22 @@ ReadTextProto(const std::string& path, google::protobuf::Message* msg)
559559

560560
Status
561561
LocalizePath(
562-
const std::string& path, const std::string& fetch_subdir,
562+
const std::string& path, const bool recursive,
563563
std::shared_ptr<LocalizedPath>* localized)
564564
{
565565
std::shared_ptr<FileSystem> fs;
566566
RETURN_IF_ERROR(fsm_.GetFileSystem(path, fs));
567-
return fs->LocalizePath(path, fetch_subdir, localized);
567+
return fs->LocalizePath(path, recursive, "", localized);
568+
}
569+
570+
Status
571+
LocalizePath(
572+
const std::string& path, const bool recursive, const std::string& mount_dir,
573+
std::shared_ptr<LocalizedPath>* localized)
574+
{
575+
std::shared_ptr<FileSystem> fs;
576+
RETURN_IF_ERROR(fsm_.GetFileSystem(path, fs));
577+
return fs->LocalizePath(path, recursive, mount_dir, localized);
568578
}
569579

570580
Status

src/filesystem/api.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,26 @@ Status ReadTextFile(const std::string& path, std::string* contents);
145145

146146
/// Create an object representing a local copy of a path.
147147
/// \param path The path of the directory or file.
148-
/// \param fetch_subdir If specified, will only download provided
149-
/// sub directory, otherwise all subdirectories will be downloaded.
150-
/// Does not affect files individual files, located under `path`.
148+
/// \param recursive If true, will fetch all sub-directories in
149+
/// the provided path.
151150
/// \param localized Returns the LocalizedPath object
152151
/// representing the local copy of the path.
153152
/// \return Error status
154153
Status LocalizePath(
155-
const std::string& path, const std::string& fetch_subdir,
154+
const std::string& path, const bool recursive,
155+
std::shared_ptr<LocalizedPath>* localized);
156+
157+
/// Create an object representing a local copy of a path.
158+
/// \param path The path of the directory or file.
159+
/// \param recursive If true, will fetch all sub-directories in
160+
/// the provided path.
161+
/// \param mount_dir If specified, will use provided local directory
162+
/// for localization.
163+
/// \param localized Returns the LocalizedPath object
164+
/// representing the local copy of the path.
165+
/// \return Error status
166+
Status LocalizePath(
167+
const std::string& path, const bool recursive, const std::string& mount_dir,
156168
std::shared_ptr<LocalizedPath>* localized);
157169

158170
/// Write a string to a file.

src/filesystem/implementations/as.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class ASFileSystem : public FileSystem {
8686
const std::string& path, std::set<std::string>* files) override;
8787
Status ReadTextFile(const std::string& path, std::string* contents) override;
8888
Status LocalizePath(
89-
const std::string& path, const std::string& fetch_subdir,
89+
const std::string& path, const bool recursive,
90+
const std::string& mount_dir,
9091
std::shared_ptr<LocalizedPath>* localized) override;
9192
Status WriteTextFile(
9293
const std::string& path, const std::string& contents) override;
@@ -424,7 +425,7 @@ ASFileSystem::DownloadFolder(
424425

425426
Status
426427
ASFileSystem::LocalizePath(
427-
const std::string& path, const std::string& fetch_subdir,
428+
const std::string& path, const bool recursive, const std::string& mount_dir,
428429
std::shared_ptr<LocalizedPath>* localized)
429430
{
430431
bool exists;

src/filesystem/implementations/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class FileSystem {
8383
virtual Status ReadTextFile(
8484
const std::string& path, std::string* contents) = 0;
8585
virtual Status LocalizePath(
86-
const std::string& path, const std::string& fetch_subdir,
86+
const std::string& path, const bool recursive,
87+
const std::string& mount_dir,
8788
std::shared_ptr<LocalizedPath>* localized) = 0;
8889
virtual Status WriteTextFile(
8990
const std::string& path, const std::string& contents) = 0;

src/filesystem/implementations/gcs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class GCSFileSystem : public FileSystem {
7575
const std::string& path, std::set<std::string>* files) override;
7676
Status ReadTextFile(const std::string& path, std::string* contents) override;
7777
Status LocalizePath(
78-
const std::string& path, const std::string& fetch_subdir,
78+
const std::string& path, const bool recursive,
79+
const std::string& mount_dir,
7980
std::shared_ptr<LocalizedPath>* localized) override;
8081
Status WriteTextFile(
8182
const std::string& path, const std::string& contents) override;
@@ -363,7 +364,7 @@ GCSFileSystem::ReadTextFile(const std::string& path, std::string* contents)
363364

364365
Status
365366
GCSFileSystem::LocalizePath(
366-
const std::string& path, const std::string& fetch_subdir,
367+
const std::string& path, const bool recursive, const std::string& mount_dir,
367368
std::shared_ptr<LocalizedPath>* localized)
368369
{
369370
bool exists;

src/filesystem/implementations/local.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class LocalFileSystem : public FileSystem {
4949
const std::string& path, std::set<std::string>* files) override;
5050
Status ReadTextFile(const std::string& path, std::string* contents) override;
5151
Status LocalizePath(
52-
const std::string& path, const std::string& fetch_subdir,
52+
const std::string& path, const bool recursive,
53+
const std::string& mount_dir,
5354
std::shared_ptr<LocalizedPath>* localized) override;
5455
Status WriteTextFile(
5556
const std::string& path, const std::string& contents) override;
@@ -204,7 +205,7 @@ LocalFileSystem::ReadTextFile(const std::string& path, std::string* contents)
204205

205206
Status
206207
LocalFileSystem::LocalizePath(
207-
const std::string& path, const std::string& fetch_subdir,
208+
const std::string& path, const bool recursive, const std::string& mount_dir,
208209
std::shared_ptr<LocalizedPath>* localized)
209210
{
210211
// For local file system we don't actually need to download the
@@ -255,8 +256,12 @@ LocalFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
255256
if (mkdir(dir.c_str(), S_IRWXU) == -1)
256257
#endif
257258
{
258-
// Only allow the error due to parent directory does not exist
259-
// if 'recursive' is requested
259+
// Return success if directory already exists
260+
if (errno == EEXIST) {
261+
return Status::Success;
262+
}
263+
// In all other cases only allow the error due to parent directory
264+
// does not exist, if 'recursive' is requested
260265
if ((errno == ENOENT) && (!dir.empty()) && recursive) {
261266
RETURN_IF_ERROR(MakeDirectory(DirName(dir), recursive));
262267
// Retry the creation

src/filesystem/implementations/s3.h

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class S3FileSystem : public FileSystem {
151151
const std::string& path, std::set<std::string>* files) override;
152152
Status ReadTextFile(const std::string& path, std::string* contents) override;
153153
Status LocalizePath(
154-
const std::string& path, const std::string& fetch_subdir,
154+
const std::string& path, const bool recursive,
155+
const std::string& mount_dir,
155156
std::shared_ptr<LocalizedPath>* localized) override;
156157
Status WriteTextFile(
157158
const std::string& path, const std::string& contents) override;
@@ -628,7 +629,7 @@ S3FileSystem::ReadTextFile(const std::string& path, std::string* contents)
628629

629630
Status
630631
S3FileSystem::LocalizePath(
631-
const std::string& path, const std::string& fetch_subdir,
632+
const std::string& path, const bool recursive, const std::string& mount_dir,
632633
std::shared_ptr<LocalizedPath>* localized)
633634
{
634635
// Check if the directory or file exists
@@ -653,10 +654,18 @@ S3FileSystem::LocalizePath(
653654
effective_path = path;
654655
}
655656

656-
// Create temporary directory
657+
// Create a local directory for s3 model store
658+
const char* env_mount_dir = std::getenv("TRITON_AWS_MOUNT_DIRECTORY");
657659
std::string tmp_folder;
658-
RETURN_IF_ERROR(
659-
triton::core::MakeTemporaryDirectory(FileSystemType::LOCAL, &tmp_folder));
660+
if (mount_dir.empty() && env_mount_dir == nullptr) {
661+
RETURN_IF_ERROR(triton::core::MakeTemporaryDirectory(
662+
FileSystemType::LOCAL, &tmp_folder));
663+
} else {
664+
tmp_folder = mount_dir.empty() ? std::string(env_mount_dir) : mount_dir;
665+
tmp_folder =
666+
JoinPath({tmp_folder, path.substr(path.find_last_of('/') + 1)});
667+
RETURN_IF_ERROR(triton::core::MakeDirectory(tmp_folder, true));
668+
}
660669

661670
// Specify contents to be downloaded
662671
std::set<std::string> contents;
@@ -694,36 +703,30 @@ S3FileSystem::LocalizePath(
694703
: JoinPath({(*localized)->Path(), s3_removed_path});
695704
bool is_subdir;
696705
RETURN_IF_ERROR(IsDirectory(s3_fpath, &is_subdir));
697-
bool copy_subdir =
698-
!fetch_subdir.empty()
699-
? s3_fpath == JoinPath({effective_path, fetch_subdir})
700-
: true;
701-
if (is_subdir) {
702-
if (copy_subdir) {
703-
// Create local mirror of sub-directories
706+
if (recursive && is_subdir) {
707+
// Create local mirror of sub-directories
704708
#ifdef _WIN32
705-
int status = mkdir(const_cast<char*>(local_fpath.c_str()));
709+
int status = mkdir(const_cast<char*>(local_fpath.c_str()));
706710
#else
707-
int status = mkdir(
708-
const_cast<char*>(local_fpath.c_str()),
709-
S_IRUSR | S_IWUSR | S_IXUSR);
711+
int status = mkdir(
712+
const_cast<char*>(local_fpath.c_str()),
713+
S_IRUSR | S_IWUSR | S_IXUSR);
710714
#endif
711-
if (status == -1) {
712-
return Status(
713-
Status::Code::INTERNAL,
714-
"Failed to create local folder: " + local_fpath +
715-
", errno:" + strerror(errno));
716-
}
717-
718-
// Add sub-directories and deeper files to contents
719-
std::set<std::string> subdir_contents;
720-
RETURN_IF_ERROR(GetDirectoryContents(s3_fpath, &subdir_contents));
721-
for (auto itr = subdir_contents.begin(); itr != subdir_contents.end();
722-
++itr) {
723-
contents.insert(JoinPath({s3_fpath, *itr}));
724-
}
715+
if (status == -1) {
716+
return Status(
717+
Status::Code::INTERNAL,
718+
"Failed to create local folder: " + local_fpath +
719+
", errno:" + strerror(errno));
720+
}
721+
722+
// Add sub-directories and deeper files to contents
723+
std::set<std::string> subdir_contents;
724+
RETURN_IF_ERROR(GetDirectoryContents(s3_fpath, &subdir_contents));
725+
for (auto itr = subdir_contents.begin(); itr != subdir_contents.end();
726+
++itr) {
727+
contents.insert(JoinPath({s3_fpath, *itr}));
725728
}
726-
} else {
729+
} else if (!is_subdir) {
727730
// Create local copy of file
728731
std::string file_bucket, file_object;
729732
RETURN_IF_ERROR(ParsePath(s3_fpath, &file_bucket, &file_object));

src/model_config_utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ LocalizePythonBackendExecutionEnvironmentPath(
919919
// Localize the file
920920
std::shared_ptr<LocalizedPath> localized_exec_env_path;
921921
RETURN_IF_ERROR(LocalizePath(
922-
abs_exec_env_path, "" /*fetch_subdir*/, &localized_exec_env_path));
922+
abs_exec_env_path, true /*recursive*/, &localized_exec_env_path));
923923
// Persist the localized temporary path
924924
(*localized_model_dir)
925925
->other_localized_path.push_back(localized_exec_env_path);

0 commit comments

Comments
 (0)