1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <ATen/ThreadLocalState.h>
#include <torch/csrc/distributed/c10d/Work.hpp>
namespace c10d {
Work::Work(
int rank,
OpType opType,
const char* profilingTitle,
const c10::optional<std::vector<at::Tensor>>& inputTensors)
: rank_(rank), opType_(opType) {
if (profilingTitle != nullptr) {
auto recordingFunction =
std::make_shared<at::RecordFunction>(at::RecordScope::USER_SCOPE);
if (recordingFunction->isActive()) {
// Work events follow a future like pattern and can potentially be marked
// as complete by different threads, so explicitly set as async event.
recordingFunction->_setAsync();
// Passing input tensor to recordFunction allows for shape information in
// profiling output.
std::vector<c10::IValue> inputs;
if (inputTensors) {
inputs.reserve(inputTensors->size());
for (const auto& tensor : *inputTensors) {
inputs.emplace_back(tensor);
}
}
recordingFunction->before(
profilingTitle,
c10::ArrayRef<const c10::IValue>(inputs.data(), inputs.size()));
std::function<void()> end_handler = [recordingFunction]() {
recordingFunction->end();
};
recordFunctionEndCallback_ = at::wrapPropagateTLSState(end_handler);
}
}
}
OpType Work::retrieveOpType() {
return opType_;
}
Work::~Work() = default;
bool Work::isCompleted() {
std::lock_guard<std::mutex> lock(mutex_);
return completed_;
}
bool Work::isSuccess() const {
std::lock_guard<std::mutex> lock(mutex_);
return !exception_;
}
std::exception_ptr Work::exception() const {
std::lock_guard<std::mutex> lock(mutex_);
return exception_;
}
int Work::sourceRank() const {
TORCH_CHECK(
false,
"sourceRank() may only be called on work objects "
"that correspond to a recv or recv-from-any call.");
}
std::vector<at::Tensor> Work::result() {
TORCH_CHECK(false, "result() not implemented.");
}
void Work::synchronize() {}
bool Work::wait(std::chrono::milliseconds timeout) {
std::unique_lock<std::mutex> lock(mutex_);
if (timeout == kNoTimeout) {
// This waits without a timeout.
cv_.wait(lock, [&] { return completed_; });
} else {
// Waits for the user-provided timeout.
cv_.wait_for(lock, timeout, [&] { return completed_; });
if (!completed_) {
// Throw exception if the wait operation timed out and the work was not
// completed.
TORCH_CHECK(false, "Operation timed out!");
}
}
if (exception_) {
std::rethrow_exception(exception_);
}
synchronize();
// Always return true, because abort API is not implemented.
return true;
}
void Work::abort() {
TORCH_CHECK(false, "Work::abort not implemented.");
}
c10::intrusive_ptr<c10::ivalue::Future> Work::getFuture() {
TORCH_CHECK(false, "Work::getFuture not implemented.")
}
void Work::finish(std::exception_ptr exception) {
std::unique_lock<std::mutex> lock(mutex_);
completed_ = true;
exception_ = exception;
if (recordFunctionEndCallback_) {
recordFunctionEndCallback_();
recordFunctionEndCallback_ = nullptr;
}
lock.unlock();
cv_.notify_all();
}
void Work::finishAndThrow(std::exception_ptr exception) {
std::unique_lock<std::mutex> lock(mutex_);
completed_ = true;
exception_ = exception;
if (recordFunctionEndCallback_) {
recordFunctionEndCallback_();
recordFunctionEndCallback_ = nullptr;
}
if (exception_) {
std::rethrow_exception(exception_);
}
}
class FutureWrappingWork : public Work {
public:
FutureWrappingWork(c10::intrusive_ptr<c10::ivalue::Future> fut)
: Work(), _fut(fut) {}
~FutureWrappingWork() {}
bool isCompleted() override {
return _fut->completed();
}
bool isSuccess() const override {
return _fut->hasValue();
}
std::exception_ptr exception() const override {
return _fut->exception_ptr();
}
int sourceRank() const override {
TORCH_CHECK(false, "FutureWrappingWork::sourceRank() not implemented");
}
std::vector<at::Tensor> result() override {
return _fut->value().toPyObjectHolder()->extractTensors();
}
bool wait(std::chrono::milliseconds timeout) override {
// FIXME
TORCH_CHECK(
timeout == kNoTimeout,
"FutureWrappingWork::wait() with finite timeout not implemented");
_fut->wait();
return true;
}
void abort() override {
TORCH_CHECK(false, "FutureWrappingWork::abort() not implemented");
}
c10::intrusive_ptr<c10::ivalue::Future> getFuture() override {
return _fut;
}
private:
c10::intrusive_ptr<c10::ivalue::Future> _fut;
};
c10::intrusive_ptr<Work> Work::create_from_future(
c10::intrusive_ptr<c10::ivalue::Future> future) {
return c10::make_intrusive<FutureWrappingWork>(future);
}
} // namespace c10d
|