Skip to content
Merged
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
25 changes: 20 additions & 5 deletions src/utils/serialize.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// torch
#include <torch/script.h>
#include <torch/serialize.h>

// kintera
Expand All @@ -15,17 +16,31 @@ void save_tensors(const std::map<std::string, torch::Tensor>& tensor_map,
archive.save_to(filename);
}

void load_tensors(std::map<std::string, torch::Tensor>& tensor_map,
const std::string& filename) {
torch::serialize::InputArchive archive;
std::map<std::string, torch::Tensor> load_tensors(const std::string& filename) {
std::map<std::string, torch::Tensor> data;

// get keys
torch::jit::Module m = torch::jit::load(filename);

for (const auto& p : m.named_parameters(/*recurse=*/true)) {
data[p.name] = p.value;
}

for (const auto& p : m.named_buffers(/*recurse=*/true)) {
data[p.name] = p.value;
}
Comment on lines +19 to +31
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_tensors now calls torch::jit::load and returns named_parameters/named_buffers, but save_tensors writes a torch::serialize::OutputArchive. These serialization formats are not compatible, so load_tensors will fail (or silently return unrelated data) when given files produced by save_tensors. Consider either (a) switching load_tensors back to torch::serialize::InputArchive to read the same keys that were written, or (b) updating save_tensors to write a TorchScript Module (register tensors as buffers/parameters) if TorchScript loading is the new intended format.

Copilot uses AI. Check for mistakes.

/*torch::serialize::InputArchive archive;
archive.load_from(filename);
for (auto& pair : tensor_map) {
for (auto& pair : data) {
try {
archive.read(pair.first, pair.second);
} catch (const c10::Error& e) {
// skip missing tensors
}
}
}*/
Comment on lines +33 to +41
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The large commented-out InputArchive block should be removed or re-enabled behind a clear conditional/compile-time flag. Leaving dead code in production makes the intended behavior ambiguous and increases maintenance cost.

Copilot uses AI. Check for mistakes.

return data;
}

} // namespace kintera
3 changes: 1 addition & 2 deletions src/utils/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace kintera {
void save_tensors(const std::map<std::string, torch::Tensor>& tensor_map,
const std::string& filename);

void load_tensors(std::map<std::string, torch::Tensor>& tensor_map,
const std::string& filename);
std::map<std::string, torch::Tensor> load_tensors(const std::string& filename);
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The load_tensors API now returns a new map, but the implementation currently only extracts TorchScript module parameters/buffers rather than loading the tensors written by save_tensors. If this is meant to load diagnostic .pt files created by save_tensors, the declaration/semantics should match that format (or the function name/docs should be updated to reflect the TorchScript-only behavior).

Copilot uses AI. Check for mistakes.

} // namespace kintera
Loading