mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/63379 this codepath has been prone to bugs as seen in the below diff, this will help ensure against changes/refactors that touch this, as a basic sanity check. Enabled it in debug-only builds to not affect the perf. ghstack-source-id: 136056093 Test Plan: CI Reviewed By: SciPioneer Differential Revision: D30358440 fbshipit-source-id: e1b3893a223722c2593ceed8696a09c7d07d47c1
54 lines
1.8 KiB
C++
54 lines
1.8 KiB
C++
#include <c10d/default_comm_hooks.hpp>
|
|
#include <c10/core/ScalarType.h>
|
|
#include <c10/util/Exception.h>
|
|
|
|
#include <c10d/ProcessGroup.hpp>
|
|
#include <c10d/comm.hpp>
|
|
#include <torch/torch.h>
|
|
|
|
namespace c10d {
|
|
|
|
c10::intrusive_ptr<c10::ivalue::Future> AllReduceCommHook::runHook(
|
|
GradBucket& bucket) {
|
|
std::vector<at::Tensor> tensors = {bucket.getBufferRef()};
|
|
// Apply the division first to avoid overflow, especially for FP16.
|
|
tensors[0] /= state_->getSize();
|
|
return state_->allreduce(tensors)->getFuture();
|
|
}
|
|
|
|
c10::intrusive_ptr<c10::ivalue::Future> FP16CompressCommHook::runHook(
|
|
GradBucket& bucket) {
|
|
|
|
auto compressed_tensor = bucket.getBufferRef().to(torch::kFloat16);
|
|
// Apply the division first to avoid overflow.
|
|
compressed_tensor /= state_->getSize();
|
|
std::vector<at::Tensor> tensors = {compressed_tensor};
|
|
|
|
auto allreduce_fut = state_->allreduce(tensors)->getFuture();
|
|
auto decompressed_tensor = bucket.getBufferRef();
|
|
auto decompress = [decompressed_tensor](c10::ivalue::Future& allreduce_fut) {
|
|
auto result = allreduce_fut.value();
|
|
TORCH_INTERNAL_ASSERT(
|
|
result.isTensorList(),
|
|
"ProcessGroup::allreduce should return TensorList");
|
|
|
|
auto reduce_tensor = result.toTensorVector()[0];
|
|
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
|
|
reduce_tensor.scalar_type() == at::ScalarType::Half,
|
|
"Expected reduced tensor to be fp16 in FP16CompressHook, but got type ",
|
|
reduce_tensor.scalar_type()
|
|
);
|
|
decompressed_tensor.copy_(reduce_tensor);
|
|
return c10::IValue(decompressed_tensor);
|
|
};
|
|
|
|
return allreduce_fut->then(decompress, allreduce_fut->elementType());
|
|
}
|
|
|
|
c10::intrusive_ptr<c10::ivalue::Future> _AllReduceBySumCommHook::
|
|
runHook(GradBucket& bucket) {
|
|
std::vector<at::Tensor> tensors = {bucket.getBufferRef()};
|
|
return state_->allreduce(tensors)->getFuture();
|
|
}
|
|
|
|
} // namespace c10d
|