Skip to content

fix(dataloader): track distributed progress by global batch - #204

Open
chen2021673 wants to merge 3 commits into
masterfrom
fix/dataloader-global-batches
Open

fix(dataloader): track distributed progress by global batch#204
chen2021673 wants to merge 3 commits into
masterfrom
fix/dataloader-global-batches

Conversation

@chen2021673

Copy link
Copy Markdown
Contributor

概述

修复分布式训练中 DataLoader 使用局部 batch 进行数据划分和索引,可能导致 batch 访问越界的问题。

主要修改

  • 统一使用 global batch 作为分布式数据划分和读取单位。
  • 按 data parallel rank 计算各进程应读取的数据范围。
  • 增加 DataLoader 单元测试,覆盖多 rank 下的 batch 划分和边界行为。

Comment threadinfini_train/include/dataloader.h Outdated
size_t batch_size_ = 0;
size_t batch_idx_ = 0;
size_t max_batch_idx_ = 0;
size_t global_batch_idx_ = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这里变量不是直接表示batch维度的,为了防止理解混淆,建议改个名字,比如 dataloader_step_ ,另外其他变量也统一改下命名

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

改动:
global_batch_idx_ -> dataloader_step_
num_global_batches_ -> num_dataloader_steps_

Comment threadinfini_train/src/dataloader.cc Outdated

DataLoader::DataLoader(const std::shared_ptr<Dataset> &dataset, size_t batch_size)
: dataset_(dataset), batch_size_(batch_size), max_batch_idx_((dataset_->Size() + batch_size_ - 1) / batch_size_) {}
: dataset_(dataset), batch_size_(batch_size), num_global_batches_(CheckedCeilDiv(dataset_->Size(), batch_size_)) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

建议先校验 dataset_ != nullptr 和 batch_size_ > 0,再计算 num_global_batches_。当前在初始化列表中直接访问 dataset_->Size(),空指针会导致非法解引用,batch_size_ == 0 也会引发除零。

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

新增
CHECK(dataset_ != nullptr) 和 CHECK_GT(batch_size_, 0)

@chen2021673
chen2021673force-pushed the fix/dataloader-global-batches branch from cf6fe96 to ee324e4CompareAugust 27, 2026 02:36
- rename global batch tracking to dataloader steps
- validate dataset and batch size before computing steps
friend bool operator==(const DataLoaderIterator &lhs, const DataLoaderIterator &rhs);

size_t DataLoaderStep() const;
DataLoaderIterator &SeekDataLoaderStep(size_t dataloader_step);

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.

“从第 xx 个 step 继续读取” 的状态控制应当是 Sampler 的职责:Sampler 根据恢复进度决定起始 sample/index,DataLoader 本身不需要额外维护一份可 seek 的状态。

Megatron 也是在 Sampler 层通过 consumed_samples 控制数据恢复位置:
https://github.com/NVIDIA/Megatron-LM/blob/f7f584d7a04c1891bc583a1213ebb1dbcac4e158/megatron/training/datasets/data_samplers.py#L172

建议暂时不要在 DataLoader / DataLoaderIterator 中引入额外的 dataloader_step 和 SeekDataLoaderStep 状态。现阶段继续在 main 侧通过迭代并跳过前 N 个 batch 的方式完成断点恢复,作为临时方案;同时保留 Sampler 的 TODO,后续引入 Sampler 抽象后,再将恢复位置的控制下沉到 Sampler。

Sign up for freeto 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

@chen2021673@kilinchange@JYMiracle305