Repository navigation
Enhance performance with memory reuse, SIMD, and safety fixes - #4
Merged
Merged
Conversation
- matrix.hpp: 添加 resize/data_ptr/multiply_to/transpose_to 方法 支持预分配缓冲区,避免热路径反复分配内存 使用原始指针帮助编译器自动向量化 - layer.hpp Linear: 预分配 product_buf_/result_buf_/grad_input_buf_ 融合矩阵乘法+bias加法,减少一次临时矩阵分配 backward 使用 transpose_to+multiply_to 预分配缓冲区 - layer.hpp ReLU: 预分配 result_buf_/grad_input_buf_ 使用原始指针循环替代 std::transform,帮助自动向量化 - train.cpp: 预分配 batch 矩阵,使用行优先 memcpy 提取数据 比逐列复制更缓存友好
- matrix.hpp: 移除 resize/multiply_to/transpose_to 的错误 noexcept vector::resize 可能抛 bad_alloc,不应标记 noexcept - layer.hpp: 移除预分配缓冲区返回值,改用 NRVO 返回新矩阵 修复 Model 链式调用中移动赋值导致缓冲区失效的问题 Linear 仅保留 product_buf_/grad_WT_buf_ 作为内部计算缓冲 ReLU 移除不需要的成员缓冲区 - nn_config.hpp: 修正 BLOCK_SIZE 注释 32×32→64×64 统一 transform_reduce 使用自定义线程池(一元/二元一致) - thread_pool.hpp: 添加缺失的 #include <iterator> - loss.hpp: MSELoss::forward 使用 resize 复用 grad_input_ 内存 - train.cpp: 空文件检测,避免整数除零 UB 添加 --epochs/--batch-size 负数校验
- matrix.hpp: multiply_to/transpose_to 添加 assert 防护自引用 防止 A.multiply_to(A, B) 导致数据被清零 - nn_config.hpp: 添加 BLOCK_SIZE 栈安全 static_assert 确保 b_block 栈分配不超过 64KB - loss.hpp: CrossEntropyLoss 支持 >128 类 超过 128 类自动降级为 vector,避免栈溢出 - model_io.hpp: load_model 添加读取完整性校验 防止截断文件导致静默数据损坏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Smart Policy 性能优化与线程池实现
📋 概述
本 PR 实现了全局线程池和 SmartPolicy 执行策略的优化,显著改进神经网络训练的性能。主要包括:
std::execution::par_unseq,避免频繁创建/销毁线程的开销data_ptr()接口,便于编译器向量化🎯 关键改进
1. 线程池实现 (
thread_pool.hpp)parallel_for_each、parallel_transform、parallel_transform_reduceglobal_thread_pool()懒初始化,训练期间常驻,程序结束自动析构2. Linear 层优化 (
layer.hpp)