|
| 1 | +# TinyInfiniTrain 作业报告 |
| 2 | + |
| 3 | +## 一、test 通过截图 |
| 4 | + |
| 5 | +## 二、作业步骤 |
| 6 | + |
| 7 | +> 将代码填入下面代码块中指定位置,并详细描述完成该作业的解决思路和遇到的问题。 |
| 8 | +
|
| 9 | +### 作业一:autograd机制调用Neg kernel的实现 |
| 10 | + |
| 11 | +难度:⭐ |
| 12 | + |
| 13 | +对应测例:`TEST(ElementwiseTest, NegForward)`,`TEST(ElementwiseTest, NegBackward)` |
| 14 | + |
| 15 | +需要实现的代码块位置:`infini_train/src/autograd/elementwise.cc` |
| 16 | + |
| 17 | +```c++ |
| 18 | +std::vector<std::shared_ptr<Tensor>> Neg::Forward(const std::vector<std::shared_ptr<Tensor>> &input_tensors) { |
| 19 | + // =================================== 作业 =================================== |
| 20 | + // TODO:通过Dispatcher获取设备专属kernel,对输入张量进行取反操作 |
| 21 | + // HINT: 依赖test_dispatcher,kernel实现已给出 |
| 22 | + // =================================== 作业 =================================== |
| 23 | +} |
| 24 | + |
| 25 | +std::vector<std::shared_ptr<Tensor>> Neg::Backward(const std::vector<std::shared_ptr<Tensor>> &grad_outputs) { |
| 26 | + // =================================== 作业 =================================== |
| 27 | + // TODO:通过Dispatcher获取设备专属的反向传播kernel,计算梯度 |
| 28 | + // HINT: 依赖test_dispatcher,kernel实现已给出 |
| 29 | + // =================================== 作业 =================================== |
| 30 | +} |
| 31 | +``` |
| 32 | +
|
| 33 | +#### 解决思路 |
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +#### 遇到问题 |
| 38 | +
|
| 39 | +
|
| 40 | +
|
| 41 | +### 作业二:实现矩阵乘法 |
| 42 | +
|
| 43 | +难度:⭐⭐ |
| 44 | +
|
| 45 | +#### CPU实现 |
| 46 | +
|
| 47 | +对应测例:`TEST(MatmulTest, BasicMatrixMultiply)`,`TEST(MatmulTest, BatchedMatrixMultiply)`, `TEST(MatmulTest, BackwardPass)` |
| 48 | +
|
| 49 | +需要实现的代码块位置:`infini_train/src/kernels/cpu/linear.cc` |
| 50 | +
|
| 51 | +```c++ |
| 52 | + std::shared_ptr<Tensor> MatmulForward(const std::shared_ptr<Tensor> &input, const std::shared_ptr<Tensor> &other) { |
| 53 | + // =================================== 作业 =================================== |
| 54 | + // TODO:实现CPU上的矩阵乘法前向计算 |
| 55 | + // REF: |
| 56 | + // =================================== 作业 =================================== |
| 57 | + } |
| 58 | +
|
| 59 | + std::tuple<std::shared_ptr<Tensor>, std::shared_ptr<Tensor>> |
| 60 | + MatmulBackward(const std::shared_ptr<Tensor> &input, const std::shared_ptr<Tensor> &other, |
| 61 | + const std::shared_ptr<Tensor> &grad_output) { |
| 62 | + // =================================== 作业 =================================== |
| 63 | + // TODO:实现CPU上的矩阵乘法反向传播 |
| 64 | + // REF: |
| 65 | + // =================================== 作业 =================================== |
| 66 | + } |
| 67 | +``` |
| 68 | + |
| 69 | +#### CUDA实现 |
| 70 | + |
| 71 | +对应测例:`TEST(MatmulTest, BasicMatrixMultiplyCuda)`,`TEST(MatmulTest, BatchedMatrixMultiplyCuda)`,`TEST(MatmulTest, BackwardPassCuda)` |
| 72 | + |
| 73 | +需要实现的代码块位置:`infini_train/src/kernels/cuda/linear.cu` |
| 74 | + |
| 75 | +```c++ |
| 76 | + std::shared_ptr<Tensor> MatmulForward(const std::shared_ptr<Tensor> &input, const std::shared_ptr<Tensor> &other) { |
| 77 | + // =================================== 作业 =================================== |
| 78 | + // TODO:实现CUDA上的矩阵乘法前向计算 |
| 79 | + // REF: |
| 80 | + // =================================== 作业 =================================== |
| 81 | + } |
| 82 | + |
| 83 | + std::tuple<std::shared_ptr<Tensor>, std::shared_ptr<Tensor>> |
| 84 | + MatmulBackward(const std::shared_ptr<Tensor> &input, const std::shared_ptr<Tensor> &other, |
| 85 | + const std::shared_ptr<Tensor> &grad_output) { |
| 86 | + // =================================== 作业 =================================== |
| 87 | + // TODO:实现CUDA上的矩阵乘法反向传播 |
| 88 | + // REF: |
| 89 | + // =================================== 作业 =================================== |
| 90 | + } |
| 91 | +``` |
| 92 | +
|
| 93 | +#### 解决思路 |
| 94 | +
|
| 95 | +
|
| 96 | +
|
| 97 | +#### 遇到问题 |
| 98 | +
|
| 99 | +
|
| 100 | +
|
| 101 | +### 作业三:实现Adam优化器 |
| 102 | +
|
| 103 | +难度:⭐ |
| 104 | +
|
| 105 | +#### CPU实现 |
| 106 | +
|
| 107 | +对应测例:`TEST(AdamOptimizerTest, BasicParameterUpdate)`,`TEST(AdamOptimizerTest, MomentumAccumulation)` |
| 108 | +
|
| 109 | +代码位置:infini_train/src/kernels/cpu/accumulate_grad.cc |
| 110 | +
|
| 111 | +```c++ |
| 112 | +void AdamAccumulateGrad(const std::shared_ptr<Tensor> &grad, const std::shared_ptr<Tensor> ¶m, |
| 113 | + const std::shared_ptr<Tensor> &m, const std::shared_ptr<Tensor> &v, float learning_rate, |
| 114 | + float beta1, float beta2, float eps, int64_t t) { |
| 115 | + // =================================== 作业 =================================== |
| 116 | + // TODO:实现Adam优化器的梯度累积和参数更新 |
| 117 | + // REF: |
| 118 | + // =================================== 作业 =================================== |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +#### CUDA实现 |
| 123 | + |
| 124 | +对应测例:`TEST(AdamOptimizerTest, BasicParameterUpdateCuda)`,`TEST(AdamOptimizerTest, MomentumAccumulationCuda)` |
| 125 | + |
| 126 | +代码位置:infini_train/src/kernels/cuda/accumulate_grad.cu |
| 127 | + |
| 128 | +```c++ |
| 129 | +void AdamAccumulateGrad(const std::shared_ptr<Tensor> &grad, const std::shared_ptr<Tensor> ¶m, |
| 130 | + const std::shared_ptr<Tensor> &m, const std::shared_ptr<Tensor> &v, float learning_rate, |
| 131 | + float beta1, float beta2, float eps, int64_t t) { |
| 132 | + // =================================== 作业 =================================== |
| 133 | + // TODO:实现Adam优化器的梯度累积和参数更新 |
| 134 | + // REF: |
| 135 | + // =================================== 作业 =================================== |
| 136 | +} |
| 137 | +``` |
| 138 | +
|
| 139 | +#### 解决思路 |
| 140 | +
|
| 141 | +
|
| 142 | +
|
| 143 | +#### 遇到问题 |
| 144 | +
|
| 145 | +
|
| 146 | +
|
| 147 | +### 作业四:实现Tensor基础操作 |
| 148 | +
|
| 149 | +#### 实现Tensor的Flatten操作 |
| 150 | +
|
| 151 | +难度:⭐ |
| 152 | +
|
| 153 | +对应测例:`TEST(TensorTransformTest, Flatten2DTo1D)`,`TEST(TensorTransformTest, FlattenWithRange) `,`TEST(TensorTransformTest, FlattenNonContiguous)` |
| 154 | +
|
| 155 | +代码位置:infini_train/src/tensor.cc |
| 156 | +
|
| 157 | +```c++ |
| 158 | +std::shared_ptr<Tensor> Tensor::Flatten(int64_t start, int64_t end) { |
| 159 | + // =================================== 作业 =================================== |
| 160 | + // TODO:实现张量扁平化操作,将指定维度范围[start, end]内的所有维度合并为一个维度 |
| 161 | + // HINT: |
| 162 | + // =================================== 作业 =================================== |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +#### 实现Tensor的反向传播机制 |
| 167 | + |
| 168 | +难度:⭐ |
| 169 | + |
| 170 | +对应测例:`TEST(TensorAutogradTest, BackwardComputesGradient)`,`TEST(TensorAutogradTest, BackwardWithMultipleOutputs)` |
| 171 | + |
| 172 | +代码位置:infini_train/src/tensor.cc |
| 173 | + |
| 174 | +```c++ |
| 175 | +void Tensor::Backward(std::shared_ptr<Tensor> gradient, bool retain_graph, bool create_graph) const { |
| 176 | + // =================================== 作业 =================================== |
| 177 | + // TODO:实现自动微分反向传播 |
| 178 | + // 功能描述:1. 计算当前张量对叶子节点的梯度 2. 支持多输出场景的梯度累加 |
| 179 | + // HINT: |
| 180 | + // =================================== 作业 =================================== |
| 181 | +} |
| 182 | +``` |
| 183 | +
|
| 184 | +#### 解决思路 |
| 185 | +
|
| 186 | +
|
| 187 | +
|
| 188 | +#### 遇到问题 |
| 189 | +
|
| 190 | +
|
| 191 | +
|
| 192 | +### 作业五 注册算子kernel的实现 |
| 193 | +
|
| 194 | +难度:⭐⭐⭐ |
| 195 | +
|
| 196 | +对应测例:`TEST(DispatcherTest, RegisterAndGetKernel)`,`TEST(DispatcherTest, DuplicateRegistration)`,`TEST(DispatcherTest, GetNonexistentKernel)` |
| 197 | +
|
| 198 | +代码位置:infini_train/include/dispatcher.h |
| 199 | +
|
| 200 | +```c++ |
| 201 | +template <typename RetT, class... ArgsT> RetT Call(ArgsT... args) const { |
| 202 | + // =================================== 作业 =================================== |
| 203 | + // TODO:实现通用kernel调用接口 |
| 204 | + // 功能描述:将存储的函数指针转换为指定类型并调用 |
| 205 | + // HINT: |
| 206 | + // =================================== 作业 =================================== |
| 207 | +} |
| 208 | +
|
| 209 | +template <typename FuncT> void Register(const KeyT &key, FuncT &&kernel) { |
| 210 | + // =================================== 作业 =================================== |
| 211 | + // TODO:实现kernel注册机制 |
| 212 | + // 功能描述:将kernel函数与设备类型、名称绑定 |
| 213 | + // =================================== 作业 =================================== |
| 214 | +} |
| 215 | +
|
| 216 | +#define REGISTER_KERNEL(device, kernel_name, kernel_func) \ |
| 217 | + // =================================== 作业 =================================== |
| 218 | + // TODO:实现自动注册宏 |
| 219 | + // 功能描述:在全局静态区注册kernel,避免显式初始化代码 |
| 220 | + // =================================== 作业 =================================== |
| 221 | +``` |
| 222 | + |
| 223 | +#### 解决思路 |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | +#### 遇到问题 |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +### 作业六:实现GPT-2整体训练 |
| 232 | + |
| 233 | +难度:⭐⭐⭐⭐ |
| 234 | + |
| 235 | +对应测例:`TEST_F(GPT2TrainingTest, LogitsConsistency)` |
| 236 | + |
| 237 | +#### 训练过程logits对比 |
| 238 | + |
| 239 | +完成以上所有作业,补齐训练框架的所有实现,理论上`TEST_F(GPT2TrainingTest, LogitsConsistency)`可以通过,在用例中判断比较预置的值和单步正向传播计算结果是否在误差允许范围内相等。 |
| 240 | + |
| 241 | +#### 数据读取实现 |
| 242 | + |
| 243 | +代码位置:example/common/tiny_shakespeare_dataset.cc |
| 244 | + |
| 245 | +```c++ |
| 246 | +TinyShakespeareFile ReadTinyShakespeareFile(const std::string &path, size_t sequence_length) { |
| 247 | + /* =================================== 作业 =================================== |
| 248 | + TODO:实现二进制数据集文件解析 |
| 249 | + 文件格式说明: |
| 250 | + ---------------------------------------------------------------------------------- |
| 251 | + | HEADER (1024 bytes) | DATA (tokens) | |
| 252 | + | magic(4B) | version(4B) | num_toks(4B) | reserved(1012B) | token数据 | |
| 253 | + ---------------------------------------------------------------------------------- |
| 254 | + =================================== 作业 =================================== */ |
| 255 | +} |
| 256 | + |
| 257 | +TinyShakespeareDataset::TinyShakespeareDataset(const std::string &filepath, size_t sequence_length) { |
| 258 | + // =================================== 作业 =================================== |
| 259 | + // TODO:初始化数据集实例 |
| 260 | + // HINT: 调用ReadTinyShakespeareFile加载数据文件 |
| 261 | + // =================================== 作业 =================================== |
| 262 | +} |
| 263 | +``` |
| 264 | +
|
| 265 | +#### Tokenizer功能实现 |
| 266 | +
|
| 267 | +代码位置:example/common/tokenizer.cc |
| 268 | +
|
| 269 | +```c++ |
| 270 | +Tokenizer::Tokenizer(const std::string &filepath) { |
| 271 | + /* ===================================== 作业 ===================================== |
| 272 | + TODO:实现Tokenizer二进制文件加载 |
| 273 | +
|
| 274 | + 文件格式说明: |
| 275 | + ---------------------------------------------------------------------------------- |
| 276 | + | HEADER (1024 bytes) | VOCAB TABLE | |
| 277 | + | magic(4B) | version(4B) | vocab_size(4B) | reserved(1012B) | token词表数据 | |
| 278 | + ---------------------------------------------------------------------------------- |
| 279 | + ===================================== 作业 ===================================== */ |
| 280 | +} |
| 281 | +``` |
| 282 | + |
| 283 | +```c++ |
| 284 | +std::string Tokenizer::Decode(uint32_t token_id) const { |
| 285 | + /* ===================================== 作业 ===================================== |
| 286 | + TODO:实现token_id到文本的转换 |
| 287 | + 功能描述:根据token_id返回对应的文本片段 |
| 288 | + ===================================== 作业 ===================================== */ |
| 289 | +} |
| 290 | +``` |
| 291 | +
|
| 292 | +```c++ |
| 293 | +void Tokenizer::GenerateText(infini_train::nn::Module &model, uint32_t batch_size, uint32_t sequence_length, |
| 294 | + uint32_t text_length, Device device) const { |
| 295 | + /* ...原代码... */ |
| 296 | + LOG(INFO) << "start generate text:"; |
| 297 | + for (int t = prompt_len; t < text_length; t++) { |
| 298 | + /* ===================================== 作业 ===================================== |
| 299 | + TODO:实现单步文本生成逻辑 |
| 300 | + HINT:调用model.Forward推理获取logits,根据推理结果进行随机采样,调用Decode获取文本结果 |
| 301 | + ===================================== 作业 ===================================== */ |
| 302 | + } |
| 303 | + std::cout << std::endl; |
| 304 | +} |
| 305 | +``` |
| 306 | + |
| 307 | +#### 解决思路 |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | +#### 遇到问题 |
| 312 | + |
0 commit comments