Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions example/gpt2/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ DEFINE_uint32(recompute_num_layers, 0,
// precision
DEFINE_string(dtype, "float32",
"precision used in training (float32/bfloat16)");
DEFINE_string(pipeline_dtype, "float32",

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.

现在写死了 --pipeline_dtype=fp32,有没有验证过--pipeline_dtype=bf16 的正确性?test_config 里可以配一个测例。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

在dtype = bf16 场景下,设置 pipeline_dtype=bf16,会在stage之间进行 fp32 到 bf16、bf16 到 fp32 的两次转换,这会导致精度下降,在LLaMA3、Qwen3 都会因为误差累积出现异常,所以现在 pipeline_dtype 默认用 fp32

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.

确实。看了一下 Megatron的 params_dtype 和 pipeline_dtype 逻辑,是训练精度指定的 dtype 可以直接控制权重和激活的精度(params_dtype)、op 计算精度 、pp边界精度(pipeline_dtype 默认跟随该 params_dtype);但也同样支持在自定义 transformer_config 的时候传入。但 InfiniTrain 目前只有 op 级 autocast,--dtype 不改激活精度,没有 params_dtype 这一参数,PP 边界始终是 fp32,所以设置 --pipeline_dtype bf16 会有问题。
可以加个 TODO 来描述这个问题。以后如果需要进一步优化,需要把现在的 --dtype=bf16 从”只控制 op 级 autocast”扩展到“完整的“bf16 栈”,统一控制 params/计算精度/wire。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已新增TODO

"dtype used for pipeline parallel communication "
"(float32/bfloat16)");
DEFINE_uint32(save_interval, 0,
"save checkpoint every N steps; 0 disables saving");
DEFINE_string(load, "", "checkpoint directory to resume from");
Expand All @@ -188,8 +191,6 @@ namespace {
// validation
const std::unordered_set<std::string> kSupportedModels = {
"gpt2", "gpt2-medium", "gpt2-large", "gpt2-xl", "d12", "d24", "d36", "d48"};
constexpr char kDtypeFP32[] = "float32";
constexpr char kDtypeBF16[] = "bfloat16";
const std::unordered_set<std::string> kSupportedLRDecayStyles = {
"none", "constant", "linear", "cosine", "inverse-square-root"};

Expand Down Expand Up @@ -426,15 +427,7 @@ void Train(const nn::parallel::Rank &rank) {
// select the data type
// TODO(lzm): change to solely rely on the weight file info for determining
// the dtype when autocast is supported
DataType dtype;
if (FLAGS_dtype == kDtypeFP32) {
dtype = DataType::kFLOAT32;
} else if (FLAGS_dtype == kDtypeBF16) {
dtype = DataType::kBFLOAT16;
} else {
LOG(FATAL) << "Rank " << rank.GlobalRank() << ": Datatype " << FLAGS_dtype
<< " not supported.";
}
const DataType dtype = ParseDtype(FLAGS_dtype);

auto num_micro_batches =
FLAGS_total_batch_size /
Expand Down Expand Up @@ -830,6 +823,7 @@ int main(int argc, char *argv[]) {
auto precision_config =
utils::PrecisionCheckConfig::Parse(FLAGS_precision_check);
nn::parallel::ModelParallelConfig model_parallel_config;
model_parallel_config.pipeline_dtype = ParseDtype(FLAGS_pipeline_dtype);
model_parallel_config.tensor_model_parallel_size = FLAGS_tensor_parallel;
model_parallel_config.pipeline_model_parallel_size = FLAGS_pipeline_parallel;
model_parallel_config.virtual_pipeline_model_parallel_size =
Expand Down
16 changes: 5 additions & 11 deletions example/llama3/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ DEFINE_uint32(recompute_num_layers, 0,
// precision
DEFINE_string(dtype, "float32",
"precision used in training (float32/bfloat16)");
DEFINE_string(pipeline_dtype, "float32",
"dtype used for pipeline parallel communication "
"(float32/bfloat16)");
DEFINE_uint32(save_interval, 0,
"save checkpoint every N steps; 0 disables saving");
DEFINE_string(load, "", "checkpoint directory to resume from");
Expand Down Expand Up @@ -187,8 +190,6 @@ using namespace infini_train;
namespace {
// validation
const std::unordered_set<std::string> kSupportedModels = {"llama3"};
constexpr char kDtypeFP32[] = "float32";
constexpr char kDtypeBF16[] = "bfloat16";
const std::unordered_set<std::string> kSupportedLRDecayStyles = {
"none", "constant", "linear", "cosine", "inverse-square-root"};

Expand Down Expand Up @@ -402,15 +403,7 @@ void Train(const nn::parallel::Rank &rank) {

LOG(INFO) << "Rank " << rank.GlobalRank() << ": Model loaded to device.";

DataType dtype;
if (FLAGS_dtype == kDtypeFP32) {
dtype = DataType::kFLOAT32;
} else if (FLAGS_dtype == kDtypeBF16) {
dtype = DataType::kBFLOAT16;
} else {
LOG(FATAL) << "Rank " << rank.GlobalRank() << ": Datatype " << FLAGS_dtype
<< " not supported.";
}
const DataType dtype = ParseDtype(FLAGS_dtype);

auto num_micro_batches =
FLAGS_total_batch_size /
Expand Down Expand Up @@ -805,6 +798,7 @@ int main(int argc, char *argv[]) {
auto precision_config =
utils::PrecisionCheckConfig::Parse(FLAGS_precision_check);
nn::parallel::ModelParallelConfig model_parallel_config;
model_parallel_config.pipeline_dtype = ParseDtype(FLAGS_pipeline_dtype);
model_parallel_config.tensor_model_parallel_size = FLAGS_tensor_parallel;
model_parallel_config.pipeline_model_parallel_size = FLAGS_pipeline_parallel;
model_parallel_config.virtual_pipeline_model_parallel_size =
Expand Down
18 changes: 6 additions & 12 deletions example/mixtral/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ DEFINE_uint32(expert_tensor_parallel, 1,
// precision
DEFINE_string(dtype, "float32",
"precision used in training (float32/bfloat16)");
DEFINE_string(pipeline_dtype, "float32",
"dtype used for pipeline parallel communication "
"(float32/bfloat16)");
// logging
DEFINE_uint32(log_interval, 1,
"Print train loss every N steps. 0 disables step loss logging.");
Expand All @@ -101,9 +104,6 @@ using namespace infini_train;

namespace {

constexpr char kDtypeFP32[] = "float32";
constexpr char kDtypeBF16[] = "bfloat16";

void ValidateRuntimeFlags(const nn::TransformerConfig &config) {
CHECK(!FLAGS_input_bin.empty()) << "Mixtral training requires --input_bin";
CHECK_GT(FLAGS_micro_batch_size, 0);
Expand Down Expand Up @@ -204,15 +204,7 @@ void Train(const nn::parallel::Rank &rank) {

auto loss_fn = std::make_shared<nn::CrossEntropyLoss>();
loss_fn->To(device);
DataType dtype;
if (FLAGS_dtype == kDtypeFP32) {
dtype = DataType::kFLOAT32;
} else if (FLAGS_dtype == kDtypeBF16) {
dtype = DataType::kBFLOAT16;
} else {
LOG(FATAL) << "Rank " << rank.GlobalRank() << ": Datatype " << FLAGS_dtype
<< " not supported.";
}
const DataType dtype = ParseDtype(FLAGS_dtype);

if (pp_world_size > 1) {
const std::vector<std::vector<int64_t>> recv_shapes{
Expand Down Expand Up @@ -395,13 +387,15 @@ int main(int argc, char *argv[]) {
CHECK_GT(FLAGS_expert_parallel, 0);

nn::parallel::ModelParallelConfig model_parallel_config;
model_parallel_config.pipeline_dtype = ParseDtype(FLAGS_pipeline_dtype);
model_parallel_config.tensor_model_parallel_size = FLAGS_tensor_parallel;
model_parallel_config.pipeline_model_parallel_size = FLAGS_pipeline_parallel;
model_parallel_config.virtual_pipeline_model_parallel_size =
FLAGS_virtual_pipeline_parallel;
model_parallel_config.expert_model_parallel_size = FLAGS_expert_parallel;
model_parallel_config.expert_tensor_parallel_size =
FLAGS_expert_tensor_parallel;

nn::parallel::global::InitAllEnv(FLAGS_nthread_per_process,
model_parallel_config);

Expand Down
17 changes: 6 additions & 11 deletions example/qwen3/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ DEFINE_uint32(recompute_num_layers, 0,
// precision
DEFINE_string(dtype, "float32",
"precision used in training (float32/bfloat16)");
DEFINE_string(pipeline_dtype, "float32",
"dtype used for pipeline parallel communication "
"(float32/bfloat16)");
DEFINE_uint32(save_interval, 0,
"save checkpoint every N steps; 0 disables saving");
DEFINE_string(load, "", "checkpoint directory to resume from");
Expand Down Expand Up @@ -188,10 +191,9 @@ using namespace infini_train;
namespace {
// validation
const std::unordered_set<std::string> kSupportedModels = {"qwen3"};
constexpr char kDtypeFP32[] = "float32";
constexpr char kDtypeBF16[] = "bfloat16";
const std::unordered_set<std::string> kSupportedLRDecayStyles = {
"none", "constant", "linear", "cosine", "inverse-square-root"};

} // namespace

DEFINE_validator(model, [](const char *, const std::string &value) {
Expand Down Expand Up @@ -400,15 +402,7 @@ void Train(const nn::parallel::Rank &rank) {

LOG(INFO) << "Rank " << rank.GlobalRank() << ": Model loaded to device.";

DataType dtype;
if (FLAGS_dtype == kDtypeFP32) {
dtype = DataType::kFLOAT32;
} else if (FLAGS_dtype == kDtypeBF16) {
dtype = DataType::kBFLOAT16;
} else {
LOG(FATAL) << "Rank " << rank.GlobalRank() << ": Datatype " << FLAGS_dtype
<< " not supported.";
}
const DataType dtype = ParseDtype(FLAGS_dtype);

auto num_micro_batches =
FLAGS_total_batch_size /
Expand Down Expand Up @@ -803,6 +797,7 @@ int main(int argc, char *argv[]) {
auto precision_config =
utils::PrecisionCheckConfig::Parse(FLAGS_precision_check);
nn::parallel::ModelParallelConfig model_parallel_config;
model_parallel_config.pipeline_dtype = ParseDtype(FLAGS_pipeline_dtype);
model_parallel_config.tensor_model_parallel_size = FLAGS_tensor_parallel;
model_parallel_config.pipeline_model_parallel_size = FLAGS_pipeline_parallel;
model_parallel_config.virtual_pipeline_model_parallel_size =
Expand Down
7 changes: 7 additions & 0 deletions infini_train/include/datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <unordered_map>

namespace infini_train {
Expand Down Expand Up @@ -99,6 +100,12 @@ enum class DataType : int8_t {
kFLOAT64,
};

inline constexpr std::string_view kDtypeFP32 = "float32";
inline constexpr std::string_view kDtypeBF16 = "bfloat16";

/// Parses the dtype names accepted by example command-line flags.
DataType ParseDtype(std::string_view dtype);

inline const std::unordered_map<DataType, size_t> kDataTypeToSize = {
{DataType::kBOOL, 1}, {DataType::kUINT8, 1}, {DataType::kINT8, 1},
{DataType::kUINT16, 2}, {DataType::kINT16, 2}, {DataType::kUINT32, 4},
Expand Down
8 changes: 8 additions & 0 deletions infini_train/include/nn/parallel/model_parallel_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <optional>
#include <string>

#include "infini_train/include/datatype.h"

namespace infini_train::nn::parallel {

enum class ContextParallelCommType {
Expand All @@ -25,6 +27,12 @@ struct ModelParallelConfig {
int tensor_model_parallel_size = 1;
int pipeline_model_parallel_size = 1;
int virtual_pipeline_model_parallel_size = 1;
// Dtype used by pipeline payloads on the communication wire.
// TODO: Extend --dtype from op-level autocast to a complete low-precision
// stack that uniformly controls parameter dtype, compute dtype, and PP wire
// dtype. Until then, PP-boundary tensors may remain FP32, so explicit BF16
// pipeline_dtype can introduce repeated FP32<->BF16 casts and precision loss.
DataType pipeline_dtype = DataType::kFLOAT32;
bool sequence_parallel = false;
int expert_model_parallel_size = 1;
std::optional<int> expert_tensor_parallel_size = std::nullopt;
Expand Down
5 changes: 3 additions & 2 deletions infini_train/include/nn/parallel/pp/send_recv.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

namespace infini_train {
enum class DataType : int8_t;
class Tensor;
class Device;
} // namespace infini_train
Expand All @@ -15,9 +16,9 @@ namespace infini_train::nn::parallel {
std::vector<std::shared_ptr<Tensor>> ISend(
const std::vector<std::shared_ptr<Tensor>> &input_tensors,
Device target_device, int peer_rank,
const std::vector<std::vector<int64_t>> &shape);
const std::vector<std::vector<int64_t>> &shape, DataType pipeline_dtype);

std::vector<std::shared_ptr<Tensor>> IRecv(
const std::vector<std::shared_ptr<Tensor>> &outputs, Device src_device,
int peer_rank);
int peer_rank, DataType pipeline_dtype);
} // namespace infini_train::nn::parallel
14 changes: 14 additions & 0 deletions infini_train/src/datatype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

#include <bit>
#include <cstdint>
#include <ostream>
#include <unordered_map>

#include "glog/logging.h"

namespace infini_train {

DataType ParseDtype(std::string_view dtype) {
if (dtype == kDtypeFP32) {
return DataType::kFLOAT32;
}
if (dtype == kDtypeBF16) {
return DataType::kBFLOAT16;
}
LOG(FATAL) << "Datatype " << dtype << " not supported.";
return DataType::kFLOAT32;
}

namespace detail {

// ---------------------------
Expand Down
15 changes: 8 additions & 7 deletions infini_train/src/nn/parallel/pp/pipeline_schedule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "glog/logging.h"

#include "infini_train/include/autocast.h"
#include "infini_train/include/datatype.h"
#include "infini_train/include/device.h"
#include "infini_train/include/nn/modules/module.h"
#include "infini_train/include/nn/parallel/global.h"
#include "infini_train/include/nn/parallel/model_parallel_config.h"
#include "infini_train/include/nn/parallel/pp/pipeline_stage.h"
#include "infini_train/include/nn/parallel/pp/send_recv.h"
#include "infini_train/include/nn/parallel/utils.h"
Expand Down Expand Up @@ -56,11 +56,10 @@ std::vector<std::shared_ptr<Tensor>> PipelineSchedule::ReceiveFromPrev(
int peer_rank) {
std::vector<std::shared_ptr<Tensor>> recv_tensors;
auto &shapes = stage_->recv_shape();
const auto pipeline_dtype = global::GetModelParallelConfig().pipeline_dtype;
for (size_t i = 0; i < shapes.size(); ++i) {
// FIXME(jym): The data type between stages is not float32, which will cause
// a crash
auto tensor = std::make_shared<Tensor>(shapes[i], DataType::kFLOAT32,
stage_->device());
auto tensor =
std::make_shared<Tensor>(shapes[i], pipeline_dtype, stage_->device());
tensor->set_requires_grad(true);

// Mark as non-leaf to prevent the autograd engine from creating an
Expand All @@ -75,12 +74,14 @@ std::vector<std::shared_ptr<Tensor>> PipelineSchedule::ReceiveFromPrev(
recv_tensors.push_back(tensor);
}

return IRecv(recv_tensors, stage_->device(), peer_rank);
return IRecv(recv_tensors, stage_->device(), peer_rank, pipeline_dtype);
}

std::vector<std::shared_ptr<Tensor>> PipelineSchedule::SendToNext(
const std::vector<std::shared_ptr<Tensor>> &tensors, int peer_rank) {
return ISend(tensors, stage_->device(), peer_rank, stage_->recv_shape());
const auto pipeline_dtype = global::GetModelParallelConfig().pipeline_dtype;
return ISend(tensors, stage_->device(), peer_rank, stage_->recv_shape(),
pipeline_dtype);
}

PipelineParallelScheduler::Task PipelineParallelScheduler::CreateTask(
Expand Down
Loading
Loading