Skip to content

perf: reduce redundant CUDA initialization and synchronization - #190

Open
chen2021673 wants to merge 1 commit into
masterfrom
remove_fill
Open

perf: reduce redundant CUDA initialization and synchronization#190
chen2021673 wants to merge 1 commit into
masterfrom
remove_fill

Conversation

@chen2021673

@chen2021673 chen2021673 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

  • defer loss host synchronization until after backward in GPT-2, Llama3, MNIST, and pipeline training
  • skip zero initialization where CUDA kernels or cuBLAS fully overwrite output tensors
  • add numerical CPU and CUDA coverage for the affected operators

Test

image image

class AutogradLinearForwardTest : public infini_train::test::InfiniTrainTest {};

namespace {
std::shared_ptr<Tensor> CopyToCPU(const std::shared_ptr<Tensor> &tensor) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接在调用处用 Tensor->to(device) 函数就行吧,不需要额外封装工具函数,其他测试文件同理,

class AutogradNormalizationBackwardTest : public infini_train::test::InfiniTrainTest {};

namespace {
void ExpectValues(const std::shared_ptr<Tensor> &tensor, float expected) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他测试函数似乎都用到了类似的对比逻辑,建议封装成 tests 里的统一工具函数供所有测例使用。

class AutogradReductionBackwardTest : public infini_train::test::InfiniTrainTest {};

namespace {
void ExpectValues(const std::shared_ptr<Tensor> &tensor, const std::vector<float> &expected) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,其他测试文件里直接写在测例里的精度对比逻辑,也都改成调用统一的精度对比工具函数吧。

Comment thread example/gpt2/main.cc

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

麻烦在所有涉及 loss 统计位置调整的地方补充注释,说明将 loss 拷贝到 CPU 并读取标量放在 backward 之后,是为了避免在反向开始前触发 D2H 同步、阻塞反向任务提交,从而让反向 kernel 尽早启动。

- defer loss host synchronization until after backward across training paths
- skip zero fills when CUDA kernels or cuBLAS fully overwrite outputs
- retain and document initialization required by partial-write paths
- add CPU and CUDA numerical tests for affected operators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants