Skip to content

Save and Load objects#778

Open
manuschneider wants to merge 9 commits into
masterfrom
save_load
Open

Save and Load objects#778
manuschneider wants to merge 9 commits into
masterfrom
save_load

Conversation

@manuschneider
Copy link
Copy Markdown
Collaborator

@manuschneider manuschneider commented Apr 11, 2026

  • Pickle support
  • HDF5 support

addresses #157 and #660

The Pickle support can already be checked and merged at this stage, and HDF5 can be implemented subsequently (check #660 for this).

…ethods: to_binary(std::ostream) and from_binary(std::istream)

Clearer separation: Save and Load handle file IO, while to_binary and from_binary only write to ostream or istream
implementation classes provide to_binary_dispatch(std::ostream) and from_binary_dispatch(std::istream)
…ectly because the char* is not terminated by \0. Potentially illegal memory access
@manuschneider manuschneider changed the title Save and Load complex data Save and Load objects Apr 11, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 11, 2026

Codecov Report

❌ Patch coverage is 48.59155% with 73 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.25%. Comparing base (c4ad1cf) to head (13ad2a0).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/backend/Storage.cpp 41.66% 18 Missing and 3 partials ⚠️
src/tn_algo/MPS.cpp 0.00% 15 Missing ⚠️
src/Symmetry.cpp 20.00% 8 Missing ⚠️
src/Bond.cpp 41.66% 7 Missing ⚠️
src/Tensor.cpp 64.70% 6 Missing ⚠️
src/tn_algo/RegularMPS.cpp 0.00% 4 Missing ⚠️
src/tn_algo/iMPS.cpp 0.00% 4 Missing ⚠️
src/BlockUniTensor.cpp 50.00% 2 Missing ⚠️
src/UniTensor.cpp 88.23% 2 Missing ⚠️
src/UniTensor_base.cpp 0.00% 2 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #778      +/-   ##
==========================================
+ Coverage   36.23%   36.25%   +0.02%     
==========================================
  Files         214      214              
  Lines       33132    33140       +8     
  Branches    13171    13159      -12     
==========================================
+ Hits        12005    12016      +11     
- Misses      19184    19191       +7     
+ Partials     1943     1933      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@manuschneider manuschneider added the Pending check/approval Issue fixed, and need feedback label Apr 22, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c00d2384da

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/backend/Storage.cpp Outdated
Comment thread src/Tensor.cpp Outdated
Comment thread src/tn_algo/MPS.cpp Outdated
@IvanaGyro
Copy link
Copy Markdown
Collaborator

The C++ coding style we use uses PascalCase for function names. We should be consistent with it for the new functions especially the functions in the changing files have already follow the coding style.

@manuschneider
Copy link
Copy Markdown
Collaborator Author

The C++ coding style we use uses PascalCase for function names. We should be consistent with it for the new functions especially the functions in the changing files have already follow the coding style.

I checked the Cytnx API. We use one-word names, both upper- and lowercase (Save, permute). For longer names, we use snake_case (set_name), and I even found one outlier with camelCase (combineBonds).
I agree that we should be more consistent.

But I suggest deviating from Google's style guide here, because I could not find any instance where we currently use PascalCase for functions or methods, and we should not introduce yet another convention for new functions. Another reason to deviate from Google style guide is that we want to have a consistent API on Python and C++. In Python, snake_case is recommended for function names, and therefore I would also suggest this for our API (and because we already use it a lot).

I renamed some functions in this branch because they started with an underscore (which should be avoided) and did not have very meaningful names (e.g.,_Save -> to_binary).

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 64b25e4bc0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Bond.cpp
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 19ac5b06f8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pybind/tensor_py.cpp Outdated
},
py::arg("fname"), py::arg("dtype"), py::arg("count") = cytnx::cytnx_int64(-1))
[](const std::string &fname, const unsigned int &dtype, const cytnx::cytnx_int64 &count,
const bool restore_device) { return cytnx::Tensor::Load(fname, restore_device); },
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Bind Tensor.Fromfile to the actual Fromfile backend

The Python Tensor.Fromfile binding now takes dtype/count plus a fourth flag, but the lambda still calls Tensor::Load instead of Tensor::Fromfile. That means raw binary inputs are parsed as serialized .cytn tensors, and the dtype/count arguments are silently ignored. In this patch this also became more misleading because the new fourth parameter is bool restore_device, so callers passing an integer device ID will be coerced to true/false and still not control placement.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 13ad2a01c2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Bond.cpp
Comment on lines +613 to +614
} else {
this->_impl->_syms.clear();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear Bond qnum/deg metadata when loading ver-0 data

Fresh evidence after the earlier “fixed” thread: Bond::from_binary now clears only _syms in the Nsym_in == 0 path, but it still leaves _qnums/_degs untouched for version-0 payloads. With the new in-place Load_ path, loading a non-symmetric or old-format bond into an already-initialized symmetric/new-format bond can retain stale degeneracy metadata and make later logic (e.g. format-dependent getDegeneracy behavior) inconsistent with the file contents.

Useful? React with 👍 / 👎.

Comment thread src/UniTensor.cpp
Comment on lines 127 to +130
if (len_name != 0) {
char *cname = (char *)malloc(sizeof(char) * len_name);
f.read(cname, sizeof(char) * len_name);
this->_impl->_name = std::string(cname);
this->_impl->_name = std::string(cname, len_name);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reset UniTensor name when serialized name length is zero

UniTensor::from_binary only assigns _name when len_name != 0; otherwise it leaves the previous name in place. This becomes a regression with the new in-place Load_ API: loading a file whose tensor name is empty into an existing named tensor preserves the old name, so the loaded object's metadata no longer matches what was serialized.

Useful? React with 👍 / 👎.

@manuschneider manuschneider removed the Pending check/approval Issue fixed, and need feedback label May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants