Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for fp8 allgather FSDP #109654

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 8 additions & 1 deletion aten/src/ATen/native/TensorFactories.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ inline void check_supported_max_int_with_precision(int64_t n, const Tensor& tens
// fill the tensor with NaN if it is floating point or complex type, or fill
// with max value if it is integer type
inline Tensor& fill_empty_deterministic_(Tensor& tensor) {
if (tensor.is_floating_point() || tensor.is_complex()) {
constexpr auto FP8_NAN = 0b01111111;
if (tensor.scalar_type() == ScalarType::Float8_e5m2) {
at::Float8_e5m2 nan(FP8_NAN, at::Float8_e5m2::from_bits_t{});
tensor.fill_(nan);
} else if (tensor.scalar_type() == ScalarType::Float8_e4m3fn) {
at::Float8_e4m3fn nan(FP8_NAN, at::Float8_e4m3fn::from_bits_t{});
tensor.fill_(nan);
} else if (tensor.is_floating_point() || tensor.is_complex()) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(
kBFloat16, kHalf, tensor.scalar_type(), "fill_empty_deterministic_", [&]() {
tensor.fill_(std::numeric_limits<scalar_t>::quiet_NaN());
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC for other types that aren't standard C++ types (i.e. float16, bfloat16) we just fill out the numerics_limit so this code works throughout the codebase and we don't need to special case each place.

Expand Down
8 changes: 4 additions & 4 deletions aten/src/ATen/native/cuda/Shape.cu
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ TORCH_IMPL_FUNC(cat_out_cuda)
all_contiguous &&
all32BitIndexable &&
all_same_dtype) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND4(
kComplexHalf, kHalf, kBool, kBFloat16,
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND6(
kComplexHalf, kHalf, kBool, kBFloat16, kFloat8_e5m2, kFloat8_e4m3fn,
result.scalar_type(), "cat_cuda", [&]() {
parallel_cat<scalar_t, CAT_ARRAY_BATCH_SIZE, 1>(result, materialized, dim, nDims, memory_format);
});
Expand All @@ -427,8 +427,8 @@ TORCH_IMPL_FUNC(cat_out_cuda)
all32BitIndexable &&
all_same_dtype &&
memory_format == c10::MemoryFormat::Contiguous) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND4(
kComplexHalf, kHalf, kBool, kBFloat16,
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND6(
kComplexHalf, kHalf, kBool, kBFloat16, kFloat8_e5m2, kFloat8_e4m3fn,
result.scalar_type(), "cat_cuda", [&]() {
parallel_cat<scalar_t, CAT_ARRAY_BATCH_SIZE/2, CAT_ARRAY_BATCH_SIZE/2>(result, materialized, dim, nDims, memory_format);
});
Expand Down
11 changes: 10 additions & 1 deletion torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3342,11 +3342,20 @@ c10::intrusive_ptr<Work> ProcessGroupNCCL::_allgather_base(
c10::cuda::CUDACachingAllocator::recordStream(
output.storage().data_ptr(), stream);
}

ncclDataType_t nccl_dtype;
if (input.scalar_type() == at::kFloat8_e5m2 ||
input.scalar_type() == at::kFloat8_e4m3fn) {
nccl_dtype = ncclInt8;
} else {
nccl_dtype = getNcclDataType(input.scalar_type());
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious, why not add the type mapping into ncclDataType above so that getNcclDataType returns it?

Are we wanting to potentially have different behavior for the type depending on which collective we're in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is based on feedback from someone in the PyTorch team (I think it was in workplace chat).

Copy link
Contributor

Choose a reason for hiding this comment

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

ping @kwen2501 @H-Huang to comment further- do we want it this way? or inside ncclDataType

}

return ncclAllGather(
input.data_ptr(),
output.data_ptr(),
input.numel(),
getNcclDataType(input.scalar_type()),
nccl_dtype,
comm,
stream.stream());
},
Expand Down
Loading