Mirage-LLM编译成大Kernel

Ref

  1. Mirage github
  2. Mirage 博客
  3. 关于uGraph
  4. deepwiki

源码

mirage/src/kernel/customized.cc

实现自定义内核算子(KNCustomizedOp)的核心文件,主要负责将内核级图和线程块级图连接起来

Graph::customized() - 自己定义算子

cpp 复制代码
std::vector<DTensor> Graph::customized(std::vector<DTensor> const &inputs,
                                       threadblock::Graph const &bgraph) {
  KNOperator *op = create_customized_op(inputs, bgraph);
  assert(op != nullptr);
  operators.push_back(op);
  return op->output_tensors;
}

int Graph::customized(std::vector<DTensor const *> _inputs,
                      DTensor **outputs,
                      mirage::threadblock::Graph const *bgraph) {
  std::vector<DTensor> inputs;
  for (auto const &t : _inputs) {
    inputs.push_back(t == nullptr ? DTensor::EMPTY_TENSOR : *t);
  }
  KNOperator *op = create_customized_op(inputs, *bgraph);
  assert(op != nullptr);
  operators.push_back(op);
  for (size_t i = 0; i < op->output_tensors.size(); i++) {
    outputs[i] = &op->output_tensors[i];
  }
  return op->output_tensors.size();
}

mirage/src/transpiler/transpile.cc

mirage/src/kernel/chunk.cc

chunk算子用于对于给定张量在指定维度上进行切分,注意这里貌似只涉及了描述chunk的行为,具体的可能是通过Transpiler代码生成器进行翻译成CUDA代码

算子创建

cpp 复制代码
KNOperator *
    Graph::create_chunk_op(DTensor const &input, int chunk_size, int dim) {
  if (dim < 0 || dim >= input.num_dims || chunk_size <= 0) {
    return nullptr;
  }
  if (input.dim[dim] % chunk_size != 0) {
    return nullptr;
  }
  if (!this->can_allocate(input)) {
    return nullptr;
  }

  KNChunkOp *op = new KNChunkOp(this, input, chunk_size, dim);
  return op;
}

运行chunk算子

cpp 复制代码
std::vector<DTensor>
    Graph::chunk(DTensor const &input, int chunk_size, int dim) {
  KNOperator *op = create_chunk_op(input, chunk_size, dim);
  assert(op != nullptr);
  operators.push_back(op);
  assert(op->output_tensors.size() > 0);
  return op->output_tensors;
}

创建chunk算子并加入算子库operators之后,并调用该算子运行得到结果

相关推荐
何事误红尘1 天前
KF32A学习笔记(二):IDE基本使用、srec和hex格式、FreeRTOS移植
学习
IT古董1 天前
【MES学习笔记系列】MES 推荐书籍与资源
笔记·学习
迪丽热爱1 天前
每日英语-11
学习
艾莉丝努力练剑1 天前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
青山是哪个青山1 天前
LangChain 学习笔记(四):Message 与提示词模板
笔记·学习·langchain
hsjiasb1 天前
FreeRTOS学习(二十六)——动态内存管理heap_1到heap_5
stm32·单片机·学习·学习笔记·freertos
崇子嵘1 天前
基于zynqMP15eg的linux驱动学习
学习
知识分享小能手2 天前
线性代数学习教程,从入门到精通,向量组的线性相关性 — 完整知识点梳理(7)
学习·线性代数·机器学习
Shell运维手记2 天前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
动词ing2 天前
【学习笔记】C语言(数组指针与指针数组+字符数组+函数+参数传递+字符串作为形参+递归函数+指针函数+回调函数+结构体嵌套+内存动态分配函数)
c语言·笔记·学习