Skip to content

Conversation

@gausshj
Copy link
Collaborator

@gausshj gausshj commented Oct 24, 2025

  • Fix corner cases for reshape none edge into (1, 1, ...) shape
  • Add input new shape validity check
  • Add corner cases in reshape

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

merging_sign: list[tuple[int, torch.Tensor]] = []

original_self_is_scalar = self.tensor.dim() == 0
if original_self_is_scalar:
Copy link
Member

Choose a reason for hiding this comment

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

把这个判断放最外面挺好的,不过为啥只有original是scalar的判断,没有target是scalar的判断?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

放在外面确实好很多,现在已经在607dfd8把target判断放在了外面。代码复杂度和可读性大大提高了。

return even, odd

@staticmethod
def calculate_even_odd(edges: tuple[tuple[int, int], ...]) -> tuple[int, int]:
Copy link
Member

Choose a reason for hiding this comment

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

这两个函数有一个就行,不用封装得这么狠,再说,你这个函数也没用到上面那个函数。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

好的,已经在607dfd8中提交了修改。

Copy link
Member

Choose a reason for hiding this comment

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

这个函数原则上可以被上面那个reorder_indices覆盖功能, 但是你在结果是(1, 0)的情况下, 希望做简单/快速的判断. 所以我建议: 直接写一个 is_xxx_valid 之类的函数, 或者直接判断 edges_only.count((0, 1)) % 2 == 0 .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这里这个函数后面开发会不会也有用?我在svd的测试中也用到了类似的函数,这里实现了后续可以直接调用。

def calculate_even_odd(edges: tuple[tuple[int, int], ...]) -> tuple[int, int]:
even, odd = 1, 0
for e, o in edges:
even, odd = even * e + odd * o, even * o + odd * e
Copy link
Member

Choose a reason for hiding this comment

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

试试用functool reduce写吧,其他地方是用reduce写的,保持下一致性

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

好的,已经在607dfd8中提交了修改。

Copy link
Member

@hzhangxyz hzhangxyz left a comment

Choose a reason for hiding this comment

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

内容都没啥问题, 只是看着代码习惯不大好, 比如eo你过个一个月就认不出来是even odd的意思了...

f"Ambiguous integer dim {item} from scalar. "
"Use explicit (even, odd) pairs, or only use 1 for trivial edges."
)
normalized.append((1, 0))
Copy link
Member

Choose a reason for hiding this comment

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

normalized 这个变量的名字是怎么来到? 这个的含义是 归一化的, 你这里是new_shape_list ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

部分参考了GPT,这里是因为输入new_shape是tuple[int | tuple[int, int], ...],但是在“标量情况”中(self.tensor.dim() == 0),不允许这种混合,normarized是类型规范化的意思。

return even, odd

@staticmethod
def calculate_even_odd(edges: tuple[tuple[int, int], ...]) -> tuple[int, int]:
Copy link
Member

Choose a reason for hiding this comment

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

这个函数原则上可以被上面那个reorder_indices覆盖功能, 但是你在结果是(1, 0)的情况下, 希望做简单/快速的判断. 所以我建议: 直接写一个 is_xxx_valid 之类的函数, 或者直接判断 edges_only.count((0, 1)) % 2 == 0 .

@gausshj
Copy link
Collaborator Author

gausshj commented Oct 24, 2025

内容都没啥问题, 只是看着代码习惯不大好, 比如eo你过个一个月就认不出来是even odd的意思了...

那我要不加上注释或者把变量名改成易读懂的类型?

- Fix corner cases when reshaping a None edge into (1,1,…) while preserving arrow/parity.
- Validate and normalize new_shape; reject incompatible dims early.
- Move `target` check outside the while-loop to reduce nesting and improve readability.
- Unify `_calculate_even_odd` and `calculate_even_odd` via `functools.reduce`.
- Add/extend corner-case tests for reshape to prevent regressions.
@gausshj gausshj force-pushed the fix/reshape-with-none-edge branch from 27e75dc to f4979fa Compare October 24, 2025 09:01
@gausshj gausshj merged commit 290bcd3 into main Oct 24, 2025
33 checks passed
@gausshj gausshj changed the title fix(reshape): fix corner cases in reshape and improve its robustness fix: fix corner cases in reshape and improve its robustness Nov 3, 2025
@gausshj gausshj changed the title fix: fix corner cases in reshape and improve its robustness Fix corner cases in reshape and improve its robustness Nov 3, 2025
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.

3 participants