Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,27 @@ bool ModelLoader::init_from_safetensors_file(const std::string& file_path, const
return true;
}

static std::string canonical_loader_path(const std::string& path) {
std::error_code error;
const std::filesystem::path canonical = std::filesystem::weakly_canonical(std::filesystem::u8path(path), error);
if (!error) {
return canonical.generic_string();
}
return std::filesystem::u8path(path).lexically_normal().generic_string();
}

bool ModelLoader::init_from_safetensors_index_file(const std::string& file_path, const std::string& prefix) {
const std::string index_key = canonical_loader_path(file_path);
if (!loading_safetensors_indexes_.insert(index_key).second) {
LOG_ERROR("cyclic safetensors index reference '%s'", file_path.c_str());
return false;
}
struct SafetensorsIndexGuard {
std::unordered_set<std::string>& active;
std::string key;
~SafetensorsIndexGuard() { active.erase(key); }
} guard{loading_safetensors_indexes_, index_key};

LOG_VERBOSE("init from safetensors index '%s', prefix = '%s'", file_path.c_str(), prefix.c_str());

std::vector<std::string> shard_paths;
Expand All @@ -322,6 +342,10 @@ bool ModelLoader::init_from_safetensors_index_file(const std::string& file_path,
}

for (const std::string& shard_path : shard_paths) {
if (loading_safetensors_indexes_.count(canonical_loader_path(shard_path)) != 0) {
LOG_ERROR("cyclic safetensors index reference '%s'", shard_path.c_str());
return false;
}
if (!parse_file(shard_path, prefix)) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/model_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#include "model.h"
Expand Down Expand Up @@ -62,6 +63,7 @@ class ModelLoader {
std::string tensor_type_rules_;
std::vector<FileStamp> parsed_dependencies_;
std::map<std::string, std::set<std::string>> parsed_tensor_names_;
std::unordered_set<std::string> loading_safetensors_indexes_;

static bool read_file_stamp(const std::string& path, FileStamp& stamp);
static bool file_unchanged(const FileStamp& stamp);
Expand Down