Relay算子注册(在pytorch.py端调用)

1. Relay算子注册 (C++层)

(a) 算子属性注册

路径 : src/relay/op/nn/nn.cc

cpp 复制代码
RELAY_REGISTER_OP("hardswish")
  .set_num_inputs(1)
  .add_argument("data", "Tensor", "Input tensor.")
  .set_support_level(3)
  .add_type_rel("Identity", Identity);
(b) 调用节点构造

路径 : src/relay/op/nn/activation.cc

cpp 复制代码
TVM_REGISTER_GLOBAL("relay.op._make.hardswish")
  .set_body_typed([](Expr data) {
    static const Op& op = Op::Get("hardswish");
    return Call(op, {data}, Attrs(), {});
  });

2. TOPI计算实现 (C++层)

© TOPI注册入口

路径 : src/topi/elemwise.cc

cpp 复制代码
TVM_REGISTER_GLOBAL("topi.hardswish")
  .set_body([](TVMArgs args, TVMRetValue* rv) {
    *rv = hardswish(args[0]);
  });
(d) 数学内核实现

路径 : include/tvm/topi/nn.h

cpp 复制代码
inline Tensor hardswish(const Tensor& x, std::string name = "T_hardswish") {
  auto three = make_const(x->dtype, 3);
  auto six = make_const(x->dtype, 6);
  return compute(
    x->shape,
    [&](const Array<Var>& i) {
      return x(i) * max(min(x(i) + three, six), 0) / six;
    },
    name, kElementWise
  );
}

3. Python接口层

(e) Relay Python API

路径 : python/tvm/relay/op/nn/_nn.py

python 复制代码
def hardswish(data):
    return _make.hardswish(data)
(f) TOPI通用接口

路径 : python/tvm/topi/nn.py

python 复制代码
@tvm.target.generic_func
def hardswish(x):
    return cpp.hardswish(x)

4. 计算调度注册

(g) Compute注册

路径 : python/tvm/relay/op/strategy/generic.py

python 复制代码
@register_compute("hardswish")
def hardswish_compute(attrs, inputs, out_type):
    return [topi.hardswish(inputs[0])]
(h) 调度策略

路径: `python/tvm/relay/op/op.py**

python 复制代码
register_broadcast_schedule("hardswish")
register_shape_func("hardswish", False, elemwise_shape_func)

5. 硬件专用实现

(i) NPU支持声明

路径: `src/relay/backend/contrib/npu/src/op_map.cc**

cpp 复制代码
const std::vector<std::string> _NPU_OP = {
  ...,
  "hardswish"  // 添加算子名
};
(j) NPU内核实现

路径: `python/tvm/relay/backend/contrib/npu/ops.py**

python 复制代码
def custom_hardswish(x):
    x1 = custom_add(x, te.extern_scalar_value(3.0))
    x2 = custom_relu(x1)
    return npu_hardwish(x2, ...)
(k) NPU策略注册

路径: `python/tvm/relay/op/strategy/npu.py**

python 复制代码
@hardswish.register("npu")
def hardswish_npu(x):
    return npu_api.custom_hardswish(x)

6. 前端框架对接

(l) PyTorch转换器

路径: `python/tvm/relay/frontend/pytorch.py**

python 复制代码
def _hardswish():
    def _impl(inputs, input_types):
        return _op.hardswish(inputs[0])
    return _impl

关键文件路径总结

功能模块 关键路径
Relay核心注册 src/relay/op/nn/{nn.cc, activation.cc}
TOPI计算 {include,src}/topi/{nn.h, elemwise.cc}
Python接口 python/tvm/{relay/op/nn/_nn.py, topi/nn.py}
策略注册 python/tvm/relay/op/strategy/{generic.py, npu.py}
硬件后端 src/relay/backend/contrib/npu/
前端对接 python/tvm/relay/frontend/pytorch.py

开发流程示意图

Relay注册 TOPI实现 Python接口 硬件后端 前端框架

通过这种清晰的路径划分,TVM实现了:

  1. 模块化开发:各层级代码物理隔离
  2. 可扩展性:新增硬件只需在对应目录添加实现
  3. 维护性:相关功能的代码集中存放
相关推荐
ZhengEnCi1 分钟前
09bad-斯坦福CS336作业一-构建优化器
人工智能
ZhengEnCi35 分钟前
09bac-斯坦福CS336作业一-实现训练损失计算
人工智能
冬奇Lab1 小时前
Skill 系列(01):Skill 评测体系——如何量化一个 AI Skill 的质量
人工智能
IT_陈寒4 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
用户3521802454756 小时前
🎆从 Prompt 到 Skill:让 Spring AI Agent 学会"装新技能"
人工智能·spring boot·ai编程
米小虾6 小时前
手把手教你搭建第一个生产级AI Agent:从选型到实战的完整指南
人工智能·agent
任沫6 小时前
Agent之Function Call
javascript·人工智能·go
米小虾6 小时前
2026年AI Agent全面爆发:从开源生态到企业级应用的进化之路
人工智能·agent
用户6919026813397 小时前
Vibe Coding 开发项目的基本范式
人工智能·设计模式·代码规范
To_OC7 小时前
别再跟 AI 死磕 prompt 了,我写了个 Loop 让它自己改到满意为止
人工智能·aigc·agent