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. 维护性:相关功能的代码集中存放
相关推荐
Shockang2 小时前
AI 智能体安全沙盒实战
人工智能
yuhulkjv3353 小时前
Claude表格复制到word不再崩溃,AI导出鸭批量导出+格式无损一键搞定
人工智能·ai·c#·word·ai导出鸭
大明者省4 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
抱抱宝4 小时前
Agent-study项目教程(03):手写 Mini-ReAct Agent(不依赖框架)
javascript·人工智能·gpt·react.js·prompt·agent
抱抱宝4 小时前
大模型应用开发教程08 | 构建完整 RAG 应用(Chroma/FAISS 实战)
人工智能·gpt·prompt·agent
美团技术团队5 小时前
KDD‘26 美团学术论文精选及KDD Cup‘26 DataAgents赛道冠军思路解读
人工智能
AKAMAI5 小时前
当AI模型超出存储增长时
人工智能·云计算
科技绘图5 小时前
快鲸GEO vs 传统AI搜索优化:全链路自动化与高效内容生产在转化闭环上的对比
数据库·人工智能·自动化
DS随心转小程序5 小时前
ChatGPT 文字怎么转为 word?解析各类转换方案,AI 导出鸭成为高效文档转换新选择
人工智能·chatgpt·word·豆包·deepseek·ai导出鸭
乌恩大侠6 小时前
【AI-RAN】硬件产品:DELL 前传交换机
人工智能·spark·aerial·o-ru·ai-ran