Skip to content

changed get() and slicing #714

Open
manuschneider wants to merge 4 commits intomasterfrom
slicing
Open

changed get() and slicing #714
manuschneider wants to merge 4 commits intomasterfrom
slicing

Conversation

@manuschneider
Copy link
Collaborator

UniTensors keep their metadata (possibly with bonds removed for constant access of one element in a given dimension).
Diagonal UniTensors can be accessed by two-dimensional slicing as well now.

This fixes #397

@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

❌ Patch coverage is 63.49206% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 35.32%. Comparing base (e87c3a2) to head (25121c9).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
include/UniTensor.hpp 58.69% 14 Missing and 5 partials ⚠️
src/backend/Tensor_impl.cpp 60.00% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #714      +/-   ##
==========================================
+ Coverage   32.35%   35.32%   +2.97%     
==========================================
  Files         215      215              
  Lines       36363    33133    -3230     
  Branches    14597    13215    -1382     
==========================================
- Hits        11764    11705      -59     
+ Misses      22659    19509    -3150     
+ Partials     1940     1919      -21     

☔ 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.

@hunghaoti
Copy link
Collaborator

Is it possible to add some simple unit test?

@manuschneider manuschneider added the Pending check/approval Issue fixed, and need feedback label Feb 24, 2026
@ianmccul
Copy link
Contributor

@codex review

Copy link

@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: 25121c9397

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

*/
Tensor get(const std::vector<cytnx::Accessor> &accessors) const {
Tensor get(const std::vector<cytnx::Accessor> &accessors,
std::vector<cytnx_int64> &removed = *new std::vector<cytnx_int64>()) const {

Choose a reason for hiding this comment

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

P1 Badge Remove heap-allocated default output arg in Tensor::get

Tensor::get now defaults removed to *new std::vector<cytnx_int64>(), so every call site that omits this argument leaks one heap allocation; this includes common slicing paths where get is called repeatedly. Because the pointer is never owned or freed, long-running workloads will steadily grow memory usage. Please replace this with an overload (or another non-owning optional pattern) instead of allocating in the default argument.

Useful? React with 👍 / 👎.

Comment on lines +407 to +408
cytnx_error_msg(Args.size() > 2,
"[ERROR][slicing] A diagonal UniTensor can only be accessed with one- or two dimensional slicing.%s", "\n");

Choose a reason for hiding this comment

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

P2 Badge Disallow 2D diagonal setitem until backend handles two axes

This branch now accepts 2D tuple indexing for diagonal __setitem__, but DenseUniTensor::set still forwards directly to the rank-1 diagonal block tensor, so two accessors trigger the Tensor rank check (accessors.size() > _shape.size()) and fail at runtime. In practice, assignments like diagonal ut[1, :] = ... are now parsed as valid here but cannot succeed in the backend (same issue exists in the UniTensor rhs overload below).

Useful? React with 👍 / 👎.

boost::intrusive_ptr<UniTensor_base> out(new DenseUniTensor());
out->Init_by_Tensor(this->_block.get(accessors), false, 0); // wrapping around.
return out;
if (accessors.empty()) return this;

Choose a reason for hiding this comment

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

P2 Badge Return a clone when no accessors are provided

The new early return gives back this for an empty accessor list, which makes UniTensor::get({}) alias the original object instead of producing an independent result. That contradicts the documented get contract (no shared memory) and can cause unexpected mutation coupling when callers modify the returned tensor. This also becomes reachable from Python diagonal indexing with an empty tuple.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Pending check/approval Issue fixed, and need feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rowrank is modified after slicing

3 participants