Skip to content

feat: add ATen operator implementations - #1022

Open
zhoujf1 wants to merge 4 commits into
InfiniTensor:masterfrom
zhoujf1:feat/add-ops
Open

zhoujf1 wants to merge 4 commits into
InfiniTensor:masterfrom
zhoujf1:feat/add-ops

Conversation

@zhoujf1

@zhoujf1 zhoujf1 commented Nov 4, 2026

Copy link
Copy Markdown

Summary

  • 新增 21 个算子的手写 ATen 实现。

  • 为每个算子添加 base 接口、Torch 后端声明与实现,以及对应测试。

Motivation

补充训练路径所需的算子覆盖,使调用方能够通过 InfiniOps 接口使用 ATen 实现。

Test Result

image

@zhoujf1
zhoujf1 requested a review from a team November 4, 2026 07:10
Comment on lines +41 to +42
at_grad_input.copy_(at_grad_output * scalar_ *
at::pow(at_input, scalar_ - 1.0));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

at_grad_output * scalar_ * pow(input, scalar_ - 1)按左结合顺序执行,每一步都产生原 dtype 的 Tensor。输入 FP16input=0.5, scalar=2, grad_output=40000时,第一步40000*2已溢出,最终得到inf;PyTorch autograd 返回正确的 40000`。这在梯度放大训练中尤其可能出现,不是容差能解决的误差。

建议在 FP32/opmath 中计算低精度导数、最后转换为输出 dtype;至少应先构造局部导数再乘上游梯度,并对 scalar-base 分支作相同数值审查。test文件中应增加大但有限的上游梯度用例。现有测试只用小幅随机数和正输入,无法暴露此问题。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

已改为 FP32 中间计算,调整导数计算顺序,两种分支均补充大梯度测试。

Comment on lines +39 to +44
at_grad_a.copy_((at_grad * at_b)
.sum_to_size(std::vector<int64_t>(grad_a_shape_.begin(),
grad_a_shape_.end())));
at_grad_b.copy_((at_grad * at_a)
.sum_to_size(std::vector<int64_t>(grad_b_shape_.begin(),
grad_b_shape_.end())));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

MulBackward 先写 grad_a,再使用原始 a 计算 grad_b。若调用方复用 a 的存储作 grad_a,例如 a=[2,3], b=[5,7], grad_output=[1,1],grad_b 会错误地变成 [5,7],应为 [2,3]。当前 base 的 shape/dtype/device 校验全部允许这个调用。

建议在接口中明确别名规则,允许的情况先保存所需结果/克隆源;不支持的情况按实际字节范围拒绝输入输出或输出输出的重叠。可参考既有 AtenCast 对外部存储范围的处理Ref cast。应覆盖完全重叠、部分重叠及非连续布局,不能只比较 Tensor 对象或 Storage 身份。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

已改为先计算两路梯度结果,再写回输出,支持输入和输出共用同一块内存。两个输出之间的重叠按实际字节跨度检查并拒绝,同时补充完全重叠、部分重叠及非连续布局测试。

Comment thread src/torch/ops/transpose/transpose.cc Outdated
auto at_out = ToAtenTensor<kDev>(out.data(), out_shape_, out_strides_,
out_type_, device_index_);

at_out.copy_(at_input.transpose(dim0_, dim1_));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Transpose 的同地址输入输出也有问题:ToAtenTensor 分别包装裸指针,两个 ATen Tensor 的 Storage 身份不同,不能依赖 ATen 检出底层内存的重叠。独立包装同一块 3×3 外部内存,再执行 copy_(transpose(...)),已复现静默破坏矩阵。

建议在接口中明确别名规则,允许的情况先保存所需结果/克隆源;不支持的情况按实际字节范围拒绝输入输出或输出输出的重叠。可参考既有 AtenCast 对外部存储范围的处理Ref cast。应覆盖完全重叠、部分重叠及非连续布局,不能只比较 Tensor 对象或 Storage 身份。

Comment thread src/base/add_backward.h Outdated

class AddBackward : public Operator<AddBackward> {
public:
AddBackward(const Tensor grad_output, Tensor grad_a, Tensor grad_b)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

现有 Add、Sub 的 canonical forward 都有 alpha。对应 out=input+alpha*other,grad_other=alpha*grad_output;减法为 -alpha*grad_output。新增 backward 没有任何参数能表达 alpha,只能处理 alpha=1。例如 alpha=2、上游梯度全 1 时,Add 的第二个梯度应为 2,这份实现只能给 1。

建议把 alpha 放在输入与输出之间,并按输出形状作广播归约;如果本来只想支持无缩放的自定义加减法,应明确命名和契约。现有加减乘 backward 测试均只用等形状 Tensor,也没有验证已经实现的广播归约路径。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

已补充 alpha 参数及广播归约,增加 alpha=0/2/-1 和不同广播形状的测试

Comment thread src/base/sub_backward.h Outdated

class SubBackward : public Operator<SubBackward> {
public:
SubBackward(const Tensor grad_output, Tensor grad_a, Tensor grad_b)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

参考add_backward.h的comment

Tensor grad_input) const = 0;

protected:
static void AssertTrainingDevices(std::initializer_list<Tensor> tensors) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AssertTrainingDevices 在 21 个新 base 类重复;NormalizeDim、IsTrainingFloat、TrainingSliceLength 也重复。抽取小型、无训练专用命名的通用校验工具,避免以后修一处漏多处。设备校验还与若干显式 same-device assert 重复。

Comment thread src/base/embedding_backward.h Outdated

namespace infini::ops {

class EmbeddingBackward : public Operator<EmbeddingBackward> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

PR 算子 使用的 ATen schema 需要调整什么
EmbeddingBackward embedding_dense_backward.out 改成 dense backward,删除 sparse 参数,使用 EmbeddingDenseBackward
SliceBackward slice_backward.out 将多维 starts/ends/steps 改为 input_sizes, dim, start, end, step
Slice slice_copy.Tensor_out 改为单轴 dim, optional start, optional end, step,使用 SliceCopy
Transpose transpose_copy.int_out 输入参数基本一致,使用 TransposeCopy 表明写入输出、不是返回 view

这四个算子看看有没有可能改成aten接口自动生成,如果可以自动生成优先自动生成。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

已将 EmbeddingDenseBackward、SliceBackward、SliceCopy、TransposeCopy 迁移到 ATen 自动生成路径

Tensor tensor) const override;

private:
int device_index_{0};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

很多派生 provider 再声明一个与 base 同名的 device_index_。例如 AccumulateGrad base 和 provider 各保存一次同一信息,构成遮蔽和冗余;可保留一个来源。

const double beta2, const double eps, const int64_t step,
Tensor param, Tensor m, Tensor v);

[[deprecated("Use grad, attributes, param, m, v argument order.")]]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

为啥一个新添加的算子里面会有这个废弃的接口呢?

Comment thread tests/test_accumulate_grad.py Outdated
@@ -0,0 +1,87 @@
import infini.ops

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

再加一下这些测试吧

算子 应补的测试 原因
PowBackward FP16:input=0.5, exponent=2, grad_output=40000,期望梯度 40000;再覆盖指数 0/1、零输入、负输入配整数指数 前面的等价运算复现已发现:中间乘法溢出,有限梯度变成 inf。当前测试把输入限制为正数、上游梯度幅度很小
AddBackward / SubBackward / MulBackward 广播输入,如 (2,3,4) 与 (4,)、(2,1,4) 与 (1,3,1),检查梯度归约回原形状;Add/Sub 接口补齐后测 alpha=0/2/-1 当前全部使用等形状输入,广播归约路径没有被验证;alpha 还涉及接口缺失
MulBackward、Transpose / TransposeBackward 输出与输入完全重叠、部分重叠,以及多个输出相互重叠 MulBackward、Transpose 已发现静默写坏结果的情形。允许重叠就验证结果;不允许就验证明确拒绝
MaskedFillBackward 小 mask 广播到大 input;input (1,3)、mask (2,3),验证梯度归约到 (1,3);不兼容 shape 的拒绝行为 当前只测等形状 mask,并直接用 masked_fill 公式作参考,无法验证广播后的反传
AdamAccumulateGrad FP16/BF16 参数 + FP32 的 m/v;从零状态开始连续更新多步,逐步比较 param/m/v;补零梯度、很小梯度 base 明确允许混合 dtype,但当前测试中四个 tensor 都是同 dtype;单次更新不能验证多步状态演进
EmbeddingBackward sparse=true 的返回契约或明确拒绝;空 indices、全 padding;padding 与 scale_grad_by_freq=true 组合 当前重复索引、padding、频率缩放已有测试,但 sparse 始终传 false
CrossEntropy / CrossEntropyBackward 全部 target 为 -100、仅一个有效 target、极端 logits;长序列下有效 token 数跨过 65504 的低精度行为 当前只覆盖普通随机 logits 和部分 ignore。长序列还需明确 FP32 累积策略;单纯与同 dtype 的 Torch 比较可能共同出现数值问题
AccumulateGrad 整数 tensor 配 rate=0.5,验证按契约拒绝或正确处理;rate=0;连续多次累积 已发现整数 rate 被截断的问题,当前仅测浮点 dtype

This branch has not been deployed

No deployments
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