Skip to content

deepx-cpu-compute 重建为 kvlang main-runtime:嵌入 runtime-c + CPU tensor myrwircaps(tensor.*) #1

Description

@miaobyte

目标

把 deepx-cpu-compute 从「旧 deepxIR + Redis 队列派发的算子执行器」重建为 kvlang main-runtime:嵌入 kvlang/runtime(C 核心),只在自己的 myrwircaps 里登记 CPU tensor 运算 tensor.*,其余 rwir(控制流、native builtin、用户函数)全部交给它所调用的 runtime-c 核心。

依据 kvlang stdlib/kvlang/spec/05-runtime语义/:runtime 核心只有一套(runtime-c),其余 runtime 一律是扩展运行时——嵌入 C 核心、套用其取指-解码-执行/native 派发/正典 codec,只追加己方 opcode。runtime-rs 是参考实现;deepx-gpu-compute 规划为同架构。deepx-cpu-compute 即 CPU 侧的同架构 main-runtime。

架构(对齐 runtime-rs)

嵌入的 C ABI(均已安装):

  • kvlang_runtime.hlibkvlang_runtime.so):kvlangRuntimeConnect / KvspaceHandle / Bootstrap / ExecuteVthread(模式2:跑到遇 ext rwir 冒泡 PC)。
  • kvlang_rwirext.hRegister / Params / ResolveRead / ResolveReadPath / ResolveWrite / NextPc / Handoff / KindexprValid / KindexprMatch
  • kvspace/kvspace.hlibkvspace.so):Get(借用零拷贝读)/ WriteNewPlace / WriteInPlace / TlvEncode / DecodeHead

驱动循环(镜像 runtime-rs run_vid/drive_vid):

  1. kvlangRuntimeConnect(dsn) → rt;kvlangRuntimeKvspaceHandle(rt) → kv(复用同一句柄,durable 惰性 flush 只在同句柄内相干,禁止另开连接)。
  2. register():对 myrwircaps 每个 tensor.* opcode 调 kvlang_rwirextRegister/lib/<op> 签名。
  3. Bootstrap(entry) → vid;循环 ExecuteVthread(rt, vid, &pc):rc=0 结束、rc=1 遇 ext rwir @ pc、rc<0 错误。
  4. rc=1:Params(pc) 取 opcode → op ∈ 本 myrwircaps 就地 dispatch CPU kernel(零拷贝),NextPc 推进;否则 Handoff 交外部扩展进程(print/json/… 等非 tensor rwir)。

myrwircaps = CPU tensor 运算表,opcode 契约与 numpy/op-gpu 一致(tensor.add/tensor.matmul/…,见 deepx-op-gpukernelcompiler fusion.py)。同一 rwir 无论落哪个 runtime 行为一致(五语言对齐)。

tensor 零拷贝ResolveReadPath 拿容器路径 → kvspaceGet 取 TLV 指针 → DecodeHead 定位 body → 在该地址原位建 deepx-core Tensor<T>data=body 指针deleter=nullptr 借用不释放)跑 kernel;写 TlvEncode+WriteNewPlace/WriteInPlace。tensor data 直接落 kvspace-c 的 shm(见 kvspace-c#14 方案1)。

myrwircaps 首批(现有 deepx-core kernel 已覆盖)

opcode 现有实现
elementwise tensor.add/sub/mul/div(+ scalar 变体)、sqrt/pow/log/exp/sin/cos/tanmax/minneg/abs、比较 eq/lt/gt/... src/deepx/tensorfunc/elementwise_*
matmul tensor.matmul matmul_cblas.hpp / matmul_miaobyte.hpp
reduce tensor.sum/prod/mean/max/min reduce_miaobyte.hpp
changeshape tensor.reshape/transpose/concat/broadcastTo changeshape_miaobyte.hpp
init tensor.zeros/ones/full/arange/uniform/normal init_miaobyte.hpp

落地步骤(自底向上,逐层测通)

  1. 新增 src/main-runtime/:C++ 驱动循环 + rwirext 宿主 glue + tensor myrwircaps 注册表,CMake 链 kvlang_runtime/kvspace。删除旧 src/client/(Redis 队列模型)。
  2. 零拷贝 tensor 桥:kvspace body ↔ Tensor<T>(借用)。
  3. 先通 tensor.add(单算子垂直切片,三后端一致)→ 再接线全 elementwise → matmul → reduce → changeshape → init。
  4. lib/ 下补 kvlang tensor tutorial(一致性测试)。
  5. 更新 README。

存废前提

本重建默认 tensor.data 由 kvspace shm 直管(kvspace-c#14 方案1),故 deepx-cpu-kvspace-extstore 无独立价值、可删除(待 kvspace-c#15 扩容落地)。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions