Closed
[Bug-Report]: Tensor类缺少反向运算符实现(__rmul__, __rsub__, __rtruediv__) #617
wangzheng97Add on Mar 9Closed at Mar 13
杨旭
Mar 10 Comments:
Mar 10 Comments:
感谢反馈,我来看一下


Mar 10 Assign wangzheng97 as responsible person
15 event messages are collapsed here。 See more
Mar 13 Associated milestone: 0.2.0.rc3
LiuChang
Mar 13 Comments:
Mar 13 Comments:
开发者你好,感谢提出的宝贵建议。
当前已合入相关改动


Mar 13 changed custom state from 待办的 to 已完成
Mar 13 Close issue
Mar 19 Associated milestone: 0.2.0
Mar 20 Added label: resolved
Sep 28 Associated pull request
问题描述 / Describe the current behavior
PyPTO的Tensor类缺少反向运算符(
__rmul__,__rsub__,__rtruediv__)的实现,导致当标量(float/int)在左侧时,会抛出TypeError。具体表现:
2.0 * tensor→TypeError: unsupported operand type(s) for *: 'float' and 'Tensor'1.0 - tensor→TypeError: unsupported operand type(s) for -: 'float' and 'Tensor'2.0 / tensor→TypeError: unsupported operand type(s) for /: 'float' and 'Tensor'源码位置:
python/pypto/tensor.py__mul__(支持tensor * scalar)__radd__(支持scalar + tensor)__rmul__,__rsub__,__rtruediv__的实现对比:
tensor + scalar✅ 和scalar + tensor✅ 都支持tensor * scalar✅,不支持scalar * tensor❌环境信息 / Environment
重现步骤 / Steps to reproduce
import pypto # 创建测试tensor x = pypto.tensor((4,), pypto.DT_FP32) # 测试1:tensor * scalar(成功) print("测试1: tensor * scalar") try: result = x * 2.0 print("✅ x * 2.0 成功") except Exception as e: print(f"❌ 失败: {e}") # 测试2:scalar * tensor(失败) print("\n测试2: scalar * tensor") try: result = 2.0 * x print("✅ 2.0 * x 成功") except TypeError as e: print(f"❌ 2.0 * x 失败: {e}") # 测试3:tensor + scalar(成功) print("\n测试3: tensor + scalar") try: result = x + 1.0 print("✅ x + 1.0 成功") except Exception as e: print(f"❌ 失败: {e}") # 测试4:scalar + tensor(成功,因为实现了__radd__) print("\n测试4: scalar + tensor") try: result = 1.0 + x print("✅ 1.0 + x 成功") except Exception as e: print(f"❌ 失败: {e}")运行结果:
预期结果 / Expected behavior
预期行为:
scalar * tensor应该等价于tensor * scalarscalar - tensor应该支持(返回scalar + (-tensor))scalar / tensor应该支持建议实现(参考
__radd__的实现方式):# python/pypto/tensor.py def __rmul__(self, other): """支持 scalar * tensor""" return self.mul(other) def __rsub__(self, other): """支持 scalar - tensor""" # scalar - tensor = scalar + (-tensor) return pypto.add(other, pypto.neg(self)) def __rtruediv__(self, other): """支持 scalar / tensor""" return pypto.div(other, self)日志/截图 / Log/Screenshot
错误堆栈
Traceback (most recent call last): File "test_operator_order.py", line XX, in <module> result = 2.0 * x TypeError: unsupported operand type(s) for *: 'float' and 'Tensor'Python运算符查找机制说明
当执行
a * b时,Python的查找顺序:a.__mul__(b)NotImplemented,尝试b.__rmul__(a)TypeError当前情况:
2.0 * tensor:float.__mul__(2.0, tensor)→NotImplementedtensor.__rmul__(2.0)→ 未实现 →TypeErrortensor * 2.0:tensor.__mul__(2.0)→ 已实现 → 成功备注 / Special notes
影响范围
用户体验:
tensor * scalar而非自然的scalar * tensor)实际案例:
tensor * scalar形式一致性:
__add__和__radd__相关文档
test_operator_order.py(已构造完整测试用例)examples/02_intermediate/operators/activation/activation.pyL184 有相关注释# NOTE: '1.702 * x' leads to TypeError: unsupported operand type(s) for *: 'float' and 'Tensor'优先级建议
中等优先级
补充说明
如果这是设计决策而非bug,建议: