#pragma once
#include <sys/types.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <string>
#include <system_error>
#include <tuple>
#include <vector>
#include <ATen/ATen.h>
#include <c10d/Types.hpp>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef SSIZE_T ssize_t;
#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/socket.h>
#include <sys/poll.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#endif
namespace c10d {
// Turns at::IntArrayRef into "(1, 2, 3, 4)".
inline std::string toString(at::IntArrayRef l) {
std::stringstream ss;
ss << "(";
for (size_t i = 0; i < l.size(); i++) {
if (i > 0) {
ss << ", ";
}
ss << l[i];
}
ss << ")";
return ss.str();
}
inline std::string toString(const c10::Layout& layout) {
std::stringstream ss;
ss << layout;
return ss.str();
}
inline void assertSameType(
const at::DeprecatedTypeProperties& type,
const std::vector<at::Tensor>& tensors) {
for (size_t i = 0; i < tensors.size(); i++) {
if (!tensors[i].options().type_equal(type.options())) {
const std::string expected = type.toString();
const std::string actual = tensors[i].toString();
throw std::invalid_argument(
"mixed types (" + expected + " and " + actual + ")");
}
}
}
inline bool parseEnvVarFlag(const char* envVarName) {
char* stringValue = std::getenv(envVarName);
if (stringValue != nullptr) {
int val;
try {
val = std::stoi(stringValue);
} catch (std::exception& e) {
throw std::runtime_error(
"Invalid value for environment variable: " +
std::string(envVarName));
}
if (val == 1) {
return true;
} else if (val == 0) {
return false;
} else {
throw std::runtime_error(
"Invalid value for environment variable: " +
std::string(envVarName));
}
}
return false;
}
inline void assertSameSizes(
const at::IntArrayRef& sizes,
const std::vector<at::Tensor>& tensors) {
for (size_t i = 0; i < tensors.size(); i++) {
if (!tensors[i].sizes().equals(sizes)) {
const auto expected = toString(sizes);
const auto actual = toString(tensors[i].sizes());
throw std::invalid_argument(
"mixed sizes (" + expected + " and " + actual + ")");
}
}
}
inline void assertSameSizeAndType(const std::vector<at::Tensor>& tensors) {
// Ensure we have at least one tensor
if (tensors.size() == 0) {
throw std::invalid_argument("argument is empty");
}
// Ensure all tensors have identical type and shape
auto options = tensors[0].options();
auto sizes = tensors[0].sizes();
for (size_t i = 1; i < tensors.size(); i++) {
if (!tensors[i].options().type_equal(options)) {
const auto expected = toString(options);
const auto actual = toString(tensors[i].options());
throw std::invalid_argument(
"argument contains mixed types (" + expected + " and " + actual +
")");
}
if (!tensors[i].sizes().equals(sizes)) {
const auto expected = toString(sizes);
const auto actual = toString(tensors[i].sizes());
throw std::invalid_argument(
"argument contains mixed sizes (" + expected + " and " + actual +
")");
}
}
}
inline void assertTypeMatch(
std::function<void(const std::string&)> fn,
const at::DeprecatedTypeProperties& type,
const at::ArrayRef<at::Tensor> tensors,
size_t index) {
if (!tensors[index].options().type_equal(type.options())) {
fn("invalid tensor type at index " + std::to_string(index) + " (expected " +
type.toString() + ", got " + tensors[index].toString() + ")");
}
}
inline void assertTypeMatch(
std::function<void(const std::string&)> fn,
const at::TensorOptions& options,
const at::ArrayRef<at::Tensor> tensors,
size_t index) {
if (!tensors[index].options().type_equal(options)) {
fn("invalid tensor type at index " + std::to_string(index) + " (expected " +
toString(options) + ", got " + toString(tensors[index].options()) + ")");
}
}
inline void assertSizesMatch(
std::function<void(const std::string&)> fn,
const at::IntArrayRef& sizes,
const at::ArrayRef<at::Tensor> tensors,
size_t index) {
if (tensors[index].sizes() != sizes) {
fn("invalid tensor size at index " + std::to_string(index) + " (expected " +
toString(sizes) + ", got " + toString(tensors[index].sizes()) + ")");
}
}
inline void assertLayoutMatch(
std::function<void(const std::string&)> fn,
const c10::Layout& expected,
const at::ArrayRef<at::Tensor> tensors,
size_t index) {
const auto& actual = tensors[index].layout();
if (actual != expected) {
fn("invalid tensor layout at index " + std::to_string(index) +
" (expected " + toString(expected) + ", got " + toString(actual) + ")");
}
}
inline void assertLayoutMatch(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
const auto& layout = tensors[0].layout();
for (size_t i = 1; i < tensors.size(); i++) {
assertLayoutMatch(fn, layout, tensors, i);
}
}
inline void assertNonEmpty(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
if (tensors.size() == 0) {
fn("requires non-empty tensor list");
}
}
inline void assertSingleElement(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
if (tensors.size() != 1) {
fn("requires a single-element tensor list");
}
}
inline void assertSingleElementInput(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
if (tensors.size() != 1) {
fn("requires a single-element input tensor list");
}
}
inline void assertSingleElementOutput(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
if (tensors.size() != 1) {
fn("requires a single-element output tensor list");
}
}
inline void assertRootRank(
std::function<void(const std::string&)> fn,
int rank,
int size) {
if (rank < 0 || rank >= size) {
fn("invalid root rank: " + std::to_string(rank));
}
}
inline void assertRootTensor(
std::function<void(const std::string&)> fn,
int rank,
int size) {
if (rank < 0 || rank >= size) {
fn("invalid root tensor: " + std::to_string(rank));
}
}
inline void assertDense(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
const auto& layout = tensors[0].layout();
if (layout != at::kStrided) {
fn("only supports dense tensors");
}
}
inline void assertCPU(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
const auto& device = tensors[0].device();
if (device.type() != at::kCPU) {
fn("only supports CPU tensors");
}
}
inline void assertSameDevice(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
if (tensors.size() < 2) {
return;
}
const auto& device = tensors[0].device();
for (int i = 1; i < tensors.size(); ++i) {
if (tensors[i].device() != device) {
fn("tensors should be on the same device");
}
}
}
inline void assertTypeAndSizesMatch(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors,
const at::DeprecatedTypeProperties& type,
const at::IntArrayRef& sizes) {
for (size_t i = 0; i < tensors.size(); i++) {
assertTypeMatch(fn, type, tensors, i);
assertSizesMatch(fn, sizes, tensors, i);
}
}
inline void assertTypeAndSizesMatch(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors,
const at::TensorOptions& options,
const at::IntArrayRef& sizes) {
for (size_t i = 0; i < tensors.size(); i++) {
assertTypeMatch(fn, options, tensors, i);
assertSizesMatch(fn, sizes, tensors, i);
}
}
inline void assertTypeAndSizesMatch(
std::function<void(const std::string&)> fn,
const at::ArrayRef<at::Tensor> tensors) {
const auto& options = tensors[0].options();
const auto sizes = tensors[0].sizes();
assertTypeAndSizesMatch(fn, tensors.slice(1), options, sizes);
}
// Copied from ATen/core/functional.h.
template <typename F, typename T>
inline auto fmap(T& inputs, const F& fn)
-> std::vector<decltype(fn(*inputs.begin()))> {
std::vector<decltype(fn(*inputs.begin()))> r;
r.reserve(inputs.size());
for (auto& input : inputs) {
r.push_back(fn(input));
}
return r;
}
// Copied from torch/csrc/utils/tensor_flatten.h.
inline at::Tensor flattenDenseTensors(at::TensorList tensors) {
static const auto flatten = [](const at::Tensor& t) {
return t.contiguous().view({-1});
};
if (tensors.size() == 1) {
return flatten(tensors[0]);
}
return at::cat(::c10d::fmap(tensors, flatten));
}
inline at::Tensor newLikeFlat(
std::vector<std::vector<at::Tensor>>& tensors,
size_t deviceIdx) {
if (tensors.size() == 0 || tensors[0].size() == 0) {
throw std::runtime_error("Received an empty list");
}
if (deviceIdx >= tensors.size()) {
throw std::runtime_error("Invalid device index");
}
auto& t = tensors[deviceIdx][0];
auto device = t.device();
for (size_t i = 1; i < tensors[deviceIdx].size(); ++i) {
if (tensors[deviceIdx][i].device() != device) {
throw std::runtime_error("Expecting all tensors on the same device");
}
}
at::DeviceGuard gpuGuard(device);
std::vector<int64_t> sizes{static_cast<int64_t>(tensors[deviceIdx].size())};
std::vector<int64_t> strides{static_cast<int64_t>(t.numel())};
sizes.insert(sizes.end(), t.sizes().begin(), t.sizes().end());
strides.insert(strides.end(), t.strides().begin(), t.strides().end());
return at::empty_strided(sizes, strides, t.options().memory_format(c10::nullopt));
}
inline at::Tensor newLikeFlat(std::vector<at::Tensor>& tensors) {
Loading ...