-
Notifications
You must be signed in to change notification settings - Fork 57
[wip] Support premul_sum #1948
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
Open
Chao1Han
wants to merge
2
commits into
main
Choose a base branch
from
xccl/premulsum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[wip] Support premul_sum #1948
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,12 @@ namespace { | |||||
#define XCCL_HAS_AVG 1 | ||||||
#endif // oneCCL version >= 2021.15 | ||||||
|
||||||
#if defined(CCL_MAJOR_VERSION) && \ | ||||||
((CCL_MAJOR_VERSION > 2021) || \ | ||||||
(CCL_MAJOR_VERSION == 2021) && (CCL_MINOR_VERSION >= 17)) | ||||||
#define ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
#endif // oneCCL version >= 2021.17 | ||||||
|
||||||
const std::map<c10d::ReduceOp, ccl::reduction> xcclOps = { | ||||||
{ReduceOp::MIN, ccl::reduction::min}, | ||||||
{ReduceOp::MAX, ccl::reduction::max}, | ||||||
|
@@ -44,6 +50,33 @@ const std::map<at::ScalarType, ccl::datatype> xcclDatatypes = { | |||||
{at::kFloat8_e5m2fnuz, ccl::datatype::uint8}, | ||||||
}; | ||||||
|
||||||
struct xcclRedOpRAII { | ||||||
xcclRedOpRAII() = default; | ||||||
xcclRedOpRAII(ccl::reduction op) : op_(op) {} | ||||||
xcclRedOpRAII(ccl::reduction op, const xcclComm_t* comm) | ||||||
: op_(op), comm_(comm), premul_sum_(true) {} | ||||||
xcclRedOpRAII(const xcclRedOpRAII&) = delete; | ||||||
xcclRedOpRAII& operator=(const xcclRedOpRAII&) = delete; | ||||||
xcclRedOpRAII(xcclRedOpRAII&& tmp) noexcept : xcclRedOpRAII() { | ||||||
std::swap(tmp.op_, this->op_); | ||||||
std::swap(tmp.comm_, this->comm_); | ||||||
std::swap(tmp.premul_sum_, this->premul_sum_); | ||||||
} | ||||||
#if defined(ENABLE_XCCL_PREMUL_SUM_SUPPORT) | ||||||
~xcclRedOpRAII() { | ||||||
if (premul_sum_) { | ||||||
ccl::reduction_destroy(op_, *comm_); | ||||||
} | ||||||
} | ||||||
#endif // ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
operator ccl::reduction() const { | ||||||
return op_; | ||||||
} | ||||||
ccl::reduction op_{}; | ||||||
const xcclComm_t* comm_ = nullptr; | ||||||
bool premul_sum_ = false; | ||||||
}; | ||||||
|
||||||
bool computeLengthsAndCheckAndGetFlat( | ||||||
const std::vector<at::Tensor>& tensors, | ||||||
std::vector<size_t>& lengths, | ||||||
|
@@ -152,7 +185,37 @@ ccl::datatype getXcclDataType( | |||||
return it->second; | ||||||
} | ||||||
|
||||||
ccl::reduction getXcclReduceOp(const ReduceOp& reduceOp, at::Tensor& input) { | ||||||
#ifdef ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
template <typename T, ccl::datatype dataType> | ||||||
xcclRedOpRAII unpackPreMulSum( | ||||||
const ReduceOp& reduceOp, | ||||||
const xcclComm_t& comm) { | ||||||
const auto* preMulSupplement = | ||||||
reinterpret_cast<XCCLPreMulSumSupplement*>(reduceOp.supplement_.get()); | ||||||
ccl::reduction preMulSum{}; | ||||||
bool has_tensor = preMulSupplement->tensor_factor.defined(); | ||||||
auto residence = has_tensor | ||||||
? ccl::scalar_residence_type::scalar_device | ||||||
: ccl::scalar_residence_type::scalar_host_immediate; | ||||||
const T* ptr_factor = has_tensor | ||||||
? preMulSupplement->tensor_factor.const_data_ptr<T>() | ||||||
: nullptr; | ||||||
T scalar_factor = T(preMulSupplement->double_factor); | ||||||
ccl::reduction_create_pre_mul_sum( | ||||||
&preMulSum, | ||||||
/*scalar=*/has_tensor ? const_cast<T*>(ptr_factor) : &scalar_factor, | ||||||
dataType, | ||||||
residence, | ||||||
comm); | ||||||
return xcclRedOpRAII(preMulSum, &comm); | ||||||
} | ||||||
#endif // ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
|
||||||
xcclRedOpRAII getXcclReduceOp( | ||||||
const ReduceOp& reduceOp, | ||||||
at::Tensor& input, | ||||||
const ccl::datatype& dataType, | ||||||
xcclComm_t& comm) { | ||||||
try { | ||||||
if (input.scalar_type() == at::kBool) { | ||||||
if (reduceOp == ReduceOp::SUM) { | ||||||
|
@@ -171,6 +234,30 @@ ccl::reduction getXcclReduceOp(const ReduceOp& reduceOp, at::Tensor& input) { | |||||
return ccl::reduction::sum; | ||||||
} | ||||||
#endif | ||||||
if (reduceOp == ReduceOp::PREMUL_SUM) { | ||||||
#ifdef ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
switch (dataType) { | ||||||
case ccl::datatype::float16: | ||||||
return unpackPreMulSum<at::Half, ccl::datatype::float16>( | ||||||
reduceOp, comm); | ||||||
case ccl::datatype::float32: | ||||||
return unpackPreMulSum<float, ccl::datatype::float32>(reduceOp, comm); | ||||||
case ccl::datatype::bfloat16: | ||||||
return unpackPreMulSum<float, ccl::datatype::bfloat16>( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bfloat16 data type, the template should use
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
reduceOp, comm); | ||||||
case ccl::datatype::float64: | ||||||
return unpackPreMulSum<double, ccl::datatype::float64>( | ||||||
reduceOp, comm); | ||||||
default: | ||||||
C10_THROW_ERROR( | ||||||
TypeError, | ||||||
"PreMulSum Data type must be half, float, bfloat16 or double"); | ||||||
return ccl::reduction{}; | ||||||
} | ||||||
#else | ||||||
C10_THROW_ERROR(ValueError, "PreMulSum requires oneCCL>=2021.17"); | ||||||
#endif // ENABLE_XCCL_PREMUL_SUM_SUPPORT | ||||||
} | ||||||
return xcclOps.at(reduceOp); | ||||||
} catch (const std::out_of_range&) { | ||||||
C10_THROW_ERROR( | ||||||
|
@@ -1266,7 +1353,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::allreduce_impl( | |||||
at::xpu::XPUStream& stream, | ||||||
ccl::stream& xcclStream) { | ||||||
auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::allreduce( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1363,7 +1451,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::allreduce_coalesced( | |||||
at::xpu::XPUStream& stream, | ||||||
ccl::stream& xcclStream) { | ||||||
auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::allreduce( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1525,7 +1614,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::reduce( | |||||
ccl::stream& xcclStream) { | ||||||
const int root = opts.rootRank + opts.rootTensor; | ||||||
const auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
const auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
const auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::reduce( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1569,7 +1659,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::_reduce_oop( | |||||
ccl::stream& xcclStream) { | ||||||
const int root = opts.rootRank + opts.rootTensor; | ||||||
const auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
const auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
const auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::reduce( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1829,7 +1920,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::reduce_scatter( | |||||
at::xpu::XPUStream& stream, | ||||||
ccl::stream& xcclStream) { | ||||||
auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::reduce_scatter( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1924,7 +2016,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::_reduce_scatter_base( | |||||
at::xpu::XPUStream& stream, | ||||||
ccl::stream& xcclStream) { | ||||||
auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::reduce_scatter( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
@@ -1983,7 +2076,8 @@ c10::intrusive_ptr<Work> ProcessGroupXCCL::reduce_scatter_tensor_coalesced( | |||||
at::xpu::XPUStream& stream, | ||||||
ccl::stream& xcclStream) { | ||||||
auto xcclDataType = getXcclDataType(input.scalar_type(), true); | ||||||
auto xcclReduceOp = getXcclReduceOp(opts.reduceOp, input); | ||||||
auto xcclReduceOp = | ||||||
getXcclReduceOp(opts.reduceOp, input, xcclDataType, comm); | ||||||
ccl::reduce_scatter( | ||||||
input.data_ptr(), | ||||||
output.data_ptr(), | ||||||
|
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructor is only defined when
ENABLE_XCCL_PREMUL_SUM_SUPPORT
is defined, but the class is used regardless of this macro. This will cause linking errors when the macro is not defined. The destructor should be defined unconditionally with appropriate conditional logic inside.Copilot uses AI. Check for mistakes.