Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

深度学习框架 C++ 项目结构

deep-learning-framework/
│
├── README.md
├── CMakeLists.txt
├── LICENSE
│
├── include/
│   ├── tensor/
│   │   ├── tensor.h              # 张量定义
│   │   ├── tensor_impl.h         # 张量实现细节
│   │   └── storage.h             # 底层存储管理
│   │
│   ├── operator/
│   │   ├── operator.h            # 算子基类
│   │   ├── operator_registry.h   # 算子注册机制
│   │   ├── compute_ops.h         # 计算算子(MatMul, Conv等)
│   │   └── comm_ops.h            # 通信算子(AllReduce, Broadcast等)
│   │
│   ├── graph/
│   │   ├── node.h                # 计算图节点
│   │   ├── graph.h               # 计算图
│   │   ├── graph_optimizer.h     # 图优化器
│   │   └── graph_executor.h      # 图执行器
│   │
│   ├── module/
│   │   ├── module.h              # Module基类
│   │   ├── linear.h              # 线性层
│   │   ├── conv.h                # 卷积层
│   │   └── layer_factory.h       # 层工厂
│   │
│   ├── cuda/
│   │   ├── cuda_tensor.h         # CUDA张量
│   │   ├── cuda_kernels.h        # CUDA核函数声明
│   │   └── cuda_utils.h          # CUDA工具函数
│   │
│   └── parallel/
│       ├── communicator.h        # 通信器接口
│       ├── collective_ops.h      # 集合通信操作
│       ├── parallel_context.h    # 并行上下文
│       └── distributed_tensor.h  # 分布式张量
│
├── src/
│   ├── tensor/
│   │   ├── tensor.cpp
│   │   └── storage.cpp
│   │
│   ├── operator/
│   │   ├── operator.cpp
│   │   ├── operator_registry.cpp
│   │   ├── ops/
│   │   │   ├── matmul_op.cpp     # 矩阵乘法算子
│   │   │   ├── conv_op.cpp       # 卷积算子
│   │   │   ├── relu_op.cpp       # ReLU算子
│   │   │   └── softmax_op.cpp    # Softmax算子
│   │   └── comm/
│   │       ├── allreduce_op.cpp  # AllReduce算子
│   │       ├── broadcast_op.cpp  # Broadcast算子
│   │       └── gather_op.cpp     # Gather算子
│   │
│   ├── graph/
│   │   ├── node.cpp
│   │   ├── graph.cpp
│   │   ├── graph_optimizer.cpp
│   │   └── optimizations/
│   │       ├── operator_fusion.cpp    # 算子融合
│   │       ├── dead_code_elimination.cpp
│   │       └── constant_folding.cpp
│   │
│   ├── module/
│   │   ├── module.cpp
│   │   ├── linear.cpp
│   │   └── conv.cpp
│   │
│   ├── cuda/
│   │   ├── cuda_tensor.cu
│   │   ├── kernels/
│   │   │   ├── matmul_kernel.cu      # 矩阵乘法CUDA核
│   │   │   ├── elementwise_kernel.cu # 逐元素操作核
│   │   │   └── reduction_kernel.cu   # 规约操作核
│   │   └── cuda_allocator.cu
│   │
│   └── parallel/
│       ├── communicator.cpp
│       ├── nccl_backend.cpp          # NCCL后端实现
│       ├── mpi_backend.cpp           # MPI后端实现
│       └── distributed_tensor.cpp
│
├── examples/
│   ├── basic_operations.cpp          # 基础张量操作
│   ├── build_graph.cpp               # 构建计算图
│   ├── custom_operator.cpp           # 自定义算子
│   ├── distributed_training.cpp      # 分布式训练
│   └── model_inference.cpp           # 模型推理
│
├── tests/
│   ├── tensor_test.cpp
│   ├── operator_test.cpp
│   ├── graph_test.cpp
│   ├── cuda_test.cu
│   └── parallel_test.cpp
│
└── docs/
    ├── architecture.md               # 架构设计文档
    ├── operator_guide.md             # 算子开发指南
    ├── graph_optimization.md         # 图优化说明
    └── parallel_training.md          # 并行训练指南

核心组件关系说明

1. 算子、张量、节点、计算图、图优化、算子注册的关系

关系链:

张量(Tensor) 
    ↓ (作为输入/输出)
算子(Operator) ← 算子注册(OperatorRegistry)
    ↓ (封装为)
节点(Node)
    ↓ (组成)
计算图(Graph)
    ↓ (优化)
图优化器(GraphOptimizer) → 优化后的图

文件关联:

  • tensor/tensor.h - 定义数据容器
  • operator/operator.h - 定义计算逻辑,接收Tensor作为输入
  • operator/operator_registry.h - 注册算子到全局表
  • graph/node.h - 将Operator包装为图节点,记录输入输出Tensor
  • graph/graph.h - 管理Node之间的依赖关系
  • graph/graph_optimizer.h - 分析Graph,进行算子融合等优化

2. CUDA矩阵计算与算子、张量的关系

关系链:

张量(Tensor)
    ↓ (分配GPU内存)
CUDA张量(CUDATensor) ← cuda/cuda_tensor.h
    ↓ (提供数据)
矩阵计算算子(MatMulOp) ← operator/ops/matmul_op.cpp
    ↓ (调用)
CUDA核函数(matmul_kernel) ← cuda/kernels/matmul_kernel.cu
    ↓ (执行GPU计算)
结果张量(Output Tensor)

文件关联:

  • cuda/cuda_tensor.h - 封装GPU内存管理
  • cuda/kernels/matmul_kernel.cu - 实现矩阵乘法的并行计算
  • operator/ops/matmul_op.cpp - 算子逻辑,调用CUDA kernel
  • tensor/tensor.h - 统一CPU/GPU张量接口

3. 通信算子、算子和张量之间的关系

关系链:

分布式张量(DistributedTensor) ← parallel/distributed_tensor.h
    ↓ (分片存储)
本地张量(Local Tensor)
    ↓ (需要同步)
通信算子(CommOp: AllReduce/Broadcast) ← operator/comm_ops.h
    ↓ (调用)
通信后端(NCCL/MPI Backend) ← parallel/nccl_backend.cpp
    ↓ (执行)
集合通信操作(Collective Operations)
    ↓ (更新)
张量数据(Tensor Data)

文件关联:

  • operator/comm/allreduce_op.cpp - 实现AllReduce通信算子
  • parallel/communicator.h - 定义通信接口
  • parallel/nccl_backend.cpp - GPU间高效通信
  • parallel/distributed_tensor.h - 管理跨设备的张量分片

4. 模型的Layer/Module和计算图之间的关系

关系链:

Module(如Linear/Conv) ← module/linear.h
    ↓ (包含)
参数张量(Weight/Bias Tensors)
    ↓ (forward时创建)
算子序列(Ops: MatMul + Add + Activation)
    ↓ (动态/静态构建)
计算图(Computation Graph) ← graph/graph.h
    ↓ (优化执行)
图执行器(GraphExecutor)

文件关联:

  • module/module.h - 定义Module基类(forward/backward接口)
  • module/linear.cpp - 实现具体层,forward中调用算子
  • graph/graph.h - Module的forward调用会向Graph添加Node
  • graph/graph_executor.h - 执行优化后的计算图

两种模式:

  • 动态图: Module.forward() → 立即执行算子 → 记录到动态图
  • 静态图: Module.forward() → 构建图结构 → 编译优化 → 执行

5. 并行计算和通信之间的关系

关系链:

并行上下文(ParallelContext) ← parallel/parallel_context.h
    ↓ (配置)
数据并行 / 模型并行 / 流水线并行
    ↓ (需要)
通信模式(Communication Pattern)
    ↓ (实现为)
通信算子(AllReduce/AllGather/ReduceScatter)
    ↓ (与)
计算算子(Forward/Backward Ops)
    ↓ (交织执行)
计算-通信重叠(Computation-Communication Overlap)

文件关联:

  • parallel/parallel_context.h - 管理rank、world_size等信息
  • parallel/collective_ops.h - 定义集合通信原语
  • operator/comm/ - 实现各种通信算子
  • graph/graph_optimizer.h - 优化计算与通信的调度顺序

典型场景:

  1. 数据并行: 前向计算(本地) → AllReduce梯度(通信) → 参数更新(本地)
  2. 模型并行: Tensor分片 → 跨设备计算 → AllGather/ReduceScatter同步
  3. 流水线并行: 微批次计算 → 点对点通信(Send/Recv) → 梯度累积

关键代码示例

算子注册与使用

// operator/operator_registry.cpp
REGISTER_OP("MatMul")
    .Input("A: float")
    .Input("B: float")
    .Output("C: float")
    .SetKernelFn(MatMulKernel);

// 使用
auto op = OperatorRegistry::CreateOp("MatMul");
Tensor output = op->Forward({tensor_a, tensor_b});

计算图构建

// graph/graph.h
Graph graph;
auto node1 = graph.AddNode("MatMul", {input, weight});
auto node2 = graph.AddNode("ReLU", {node1->output(0)});
graph.Compile();
graph.Execute();

分布式训练

// parallel/distributed_tensor.h
DistributedTensor dt(tensor, ParallelMode::DATA_PARALLEL);
dt.AllReduce(ReduceOp::SUM);  // 梯度同步

完整的 include 目录结构总结

1. tensor/ - 张量核心

  • tensor.h: 张量类,提供多维数组抽象,支持CPU/GPU
  • tensor_impl.h: 张量内部实现,管理shape、stride、存储
  • storage.h: 底层内存管理,支持多设备分配器

2. operator/ - 算子系统

  • operator.h: 算子基类,定义forward/backward接口
  • operator_registry.h: 算子注册机制,支持动态创建算子
  • compute_ops.h: 计算算子(MatMul, Conv2d, ReLU等)
  • comm_ops.h: 通信算子(AllReduce, Broadcast等)

3. graph/ - 计算图

  • node.h: 计算图节点,封装算子和数据依赖
  • graph.h: 计算图管理,支持动态图/静态图
  • graph_optimizer.h: 图优化(算子融合、常量折叠等)
  • graph_executor.h: 图执行器(顺序/并行/分布式执行)

4. module/ - 神经网络层

  • module.h: Module基类,类似PyTorch的nn.Module
  • linear.h: 全连接层、Embedding、Attention等
  • conv.h: 卷积层、池化层、Normalization等
  • layer_factory.h: 层工厂,支持从配置创建网络

5. cuda/ - CUDA加速

  • cuda_tensor.h: CUDA张量管理
  • cuda_kernels.h: CUDA kernel声明(矩阵乘法、卷积等)
  • cuda_utils.h: CUDA工具(错误检查、cuBLAS/cuDNN句柄)

6. parallel/ - 分布式并行

  • communicator.h: 通信器接口,支持NCCL/MPI/Gloo
  • collective_ops.h: 集合通信API
  • parallel_context.h: 并行上下文,管理3D并行配置
  • distributed_tensor.h: 分布式张量,支持多种分片策略

核心关系说明

算子-张量-节点-计算图链路

Tensor (数据) 
  ↓
Operator (计算逻辑)
  ↓
Node (图节点,包装算子)
  ↓
Graph (管理节点依赖)
  ↓
GraphOptimizer (优化图结构)
  ↓
GraphExecutor (执行优化后的图)

CUDA与算子的关系

CUDATensor (GPU内存)
  ↓
MatMulOp.forward() 检测CUDA设备
  ↓
调用 cuda::matmul_kernel()
  ↓
GPU并行计算

通信算子与分布式张量

DistributedTensor (分布式数据)
  ↓
需要同步时调用 AllReduceOp
  ↓
AllReduceOp 使用 Communicator
  ↓
NCCL/MPI 执行实际通信

Module与计算图

Linear.forward(input)
  ↓
创建 MatMulOp 和 AddOp
  ↓
添加节点到 Graph
  ↓
动态图:立即执行
静态图:延迟执行,先优化

这套头文件设计体现了现代深度学习框架的核心架构,与PyTorch、TensorFlow等主流框架的设计理念一致!

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages