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. 维护性:相关功能的代码集中存放
相关推荐
Codebee3 小时前
能力中心 (Agent SkillCenter):开启AI技能管理新时代
人工智能
聆风吟º4 小时前
CANN runtime 全链路拆解:AI 异构计算运行时的任务管理与功能适配技术路径
人工智能·深度学习·神经网络·cann
在路上看风景4 小时前
19. 成员初始化列表和初始化对象
c++
uesowys4 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
AI_56784 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
User_芊芊君子4 小时前
CANN大模型推理加速引擎ascend-transformer-boost深度解析:毫秒级响应的Transformer优化方案
人工智能·深度学习·transformer
zmzb01034 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹4 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
智驱力人工智能5 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
qq_160144875 小时前
亲测!2026年零基础学AI的入门干货,新手照做就能上手
人工智能