|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | +#ifdef _WIN32 |
| 18 | +#include <gloo/common/win.h> |
| 19 | +#include <winsock2.h> |
| 20 | +#include <ws2tcpip.h> |
| 21 | +#else |
| 22 | +#include <netdb.h> |
| 23 | +#include <sys/socket.h> |
| 24 | +#include <unistd.h> |
| 25 | +#endif |
| 26 | + |
| 27 | +#include <gloo/broadcast.h> |
| 28 | +#include "paddle/fluid/distributed/collective/ProcessGroupGloo.h" |
| 29 | +#include "paddle/fluid/framework/fleet/gloo_wrapper.h" |
| 30 | +#include "paddle/fluid/platform/enforce.h" |
| 31 | + |
| 32 | +namespace paddle { |
| 33 | +namespace distributed { |
| 34 | + |
| 35 | +#ifdef _WIN32 |
| 36 | +#define GENERATE_FUNC(type, func, ...) \ |
| 37 | + switch (type) { \ |
| 38 | + case experimental::DataType::FLOAT32: \ |
| 39 | + func<float>(__VA_ARGS__); \ |
| 40 | + break; \ |
| 41 | + case experimental::DataType::FLOAT64: \ |
| 42 | + func<double>(__VA_ARGS__); \ |
| 43 | + break; \ |
| 44 | + case experimental::DataType::FLOAT16: \ |
| 45 | + func<gloo::float16>(__VA_ARGS__); \ |
| 46 | + break; \ |
| 47 | + case experimental::DataType::INT32: \ |
| 48 | + func<int32_t>(__VA_ARGS__); \ |
| 49 | + break; \ |
| 50 | + case experimental::DataType::INT64: \ |
| 51 | + func<int64_t>(__VA_ARGS__); \ |
| 52 | + break; \ |
| 53 | + default: \ |
| 54 | + VLOG(0) << "Error: Unknown DataType."; \ |
| 55 | + exit(-1); \ |
| 56 | + } |
| 57 | + |
| 58 | +#define HOST_NAME_MAX 256 |
| 59 | + |
| 60 | +#else |
| 61 | +#define GENERATE_FUNC(type, func, args...) \ |
| 62 | + switch (type) { \ |
| 63 | + case experimental::DataType::FLOAT32: \ |
| 64 | + func<float>(args); \ |
| 65 | + break; \ |
| 66 | + case experimental::DataType::FLOAT64: \ |
| 67 | + func<double>(args); \ |
| 68 | + break; \ |
| 69 | + case experimental::DataType::FLOAT16: \ |
| 70 | + func<gloo::float16>(args); \ |
| 71 | + break; \ |
| 72 | + case experimental::DataType::INT32: \ |
| 73 | + func<int32_t>(args); \ |
| 74 | + break; \ |
| 75 | + case experimental::DataType::INT64: \ |
| 76 | + func<int64_t>(args); \ |
| 77 | + break; \ |
| 78 | + default: \ |
| 79 | + VLOG(0) << "Error: Unknown DataType."; \ |
| 80 | + exit(-1); \ |
| 81 | + } |
| 82 | +#endif |
| 83 | + |
| 84 | +typedef void (*reduce_func)(void*, const void*, const void*, size_t); |
| 85 | + |
| 86 | +template <typename T> |
| 87 | +reduce_func get_function(const ReduceOp& r) { |
| 88 | + switch (r) { |
| 89 | + case ReduceOp::SUM: |
| 90 | + return reduce_func(&::gloo::sum<T>); |
| 91 | + case ReduceOp::PRODUCT: |
| 92 | + return reduce_func(&::gloo::product<T>); |
| 93 | + case ReduceOp::MIN: |
| 94 | + return reduce_func(&::gloo::min<T>); |
| 95 | + case ReduceOp::MAX: |
| 96 | + return reduce_func(&::gloo::max<T>); |
| 97 | + case ReduceOp::AVG: |
| 98 | + VLOG(0) << "Error: Unsupported ReduceOp::AVG."; |
| 99 | + exit(-1); |
| 100 | + } |
| 101 | + |
| 102 | + VLOG(0) << "Error: Unknown ReduceOp."; |
| 103 | + exit(-1); |
| 104 | +} |
| 105 | + |
| 106 | +bool CheckTensorsInCPUPlace(const std::vector<Tensor>& tensors) { |
| 107 | + return std::all_of(tensors.cbegin(), tensors.cend(), [&](const Tensor& t) { |
| 108 | + return t.place() == PlaceType::kCPU; |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +template <typename T> |
| 113 | +T* get_data(const Tensor& tensor) { |
| 114 | + auto raw_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl()); |
| 115 | + return static_cast<T*>(raw_tensor->data()); |
| 116 | +} |
| 117 | + |
| 118 | +template <typename T> |
| 119 | +std::vector<T*> get_multi_data(const std::vector<Tensor>& tensors) { |
| 120 | + std::vector<T*> ret(tensors.size()); |
| 121 | + for (size_t i = 0; i < tensors.size(); i++) { |
| 122 | + ret[i] = get_data<T>(tensors[i]); |
| 123 | + } |
| 124 | + return ret; |
| 125 | +} |
| 126 | + |
| 127 | +template <typename T, typename P> |
| 128 | +void set_output(P& opts, const Tensor& tensor) { // NOLINT |
| 129 | + opts.setOutput(get_data<T>(tensor), tensor.numel()); |
| 130 | +} |
| 131 | + |
| 132 | +template <typename T, typename P> |
| 133 | +void set_input(P& opts, const Tensor& tensor) { // NOLINT |
| 134 | + opts.setInput(get_data<T>(tensor), tensor.numel()); |
| 135 | +} |
| 136 | + |
| 137 | +template <typename T, typename P> |
| 138 | +void set_outputs(P& opts, const std::vector<Tensor>& tensors) { // NOLINT |
| 139 | + opts.setOutputs(get_multi_data<T>(tensors), tensors[0].numel()); |
| 140 | +} |
| 141 | + |
| 142 | +template <typename T, typename P> |
| 143 | +void set_inputs(P& opts, const std::vector<Tensor>& tensors) { // NOLINT |
| 144 | + opts.setInputs(get_multi_data<T>(tensors), tensors[0].numel()); |
| 145 | +} |
| 146 | + |
| 147 | +ProcessGroupGloo::GlooTask::GlooTask(int rank, |
| 148 | + const std::vector<Tensor>& inputs, |
| 149 | + CommType comm_type) |
| 150 | + : ProcessGroup::Task(rank, inputs, comm_type) { |
| 151 | + PADDLE_ENFORCE_EQ(CheckTensorsInCPUPlace(inputs), true, |
| 152 | + platform::errors::Fatal( |
| 153 | + "Only CPU place is supported for ProcessGroupGloo.")); |
| 154 | +} |
| 155 | + |
| 156 | +ProcessGroupGloo::ProcessGroupGloo(const std::shared_ptr<GlooStore>& store, |
| 157 | + int rank, int world_size, |
| 158 | + const std::shared_ptr<GlooOptions> options) |
| 159 | + : ProcessGroup(rank, world_size), _tag(0), _store(store) { |
| 160 | + _context = std::make_shared<gloo::rendezvous::Context>(rank, world_size); |
| 161 | + auto prefix_store = |
| 162 | + ::gloo::rendezvous::PrefixStore(std::to_string(0), *_store); |
| 163 | + _context->connectFullMesh(prefix_store, options->device); |
| 164 | +} |
| 165 | + |
| 166 | +class BroadcastGlooTask : public ProcessGroupGloo::GlooTask { |
| 167 | + public: |
| 168 | + BroadcastGlooTask(const std::shared_ptr<gloo::Context>& context, |
| 169 | + const std::vector<Tensor>& inputs, int rank, int root, |
| 170 | + uint32_t tag) |
| 171 | + : ProcessGroupGloo::GlooTask(rank, inputs, CommType::BROADCAST), |
| 172 | + _context(context), |
| 173 | + _root(root), |
| 174 | + _inputs(inputs), |
| 175 | + _tag(tag) {} |
| 176 | + |
| 177 | + void Run() override { _do_broadcast(_inputs[0]); } |
| 178 | + |
| 179 | + private: |
| 180 | + std::shared_ptr<gloo::Context> _context; |
| 181 | + const int _root; |
| 182 | + std::vector<Tensor> _inputs{}; |
| 183 | + const uint32_t _tag; |
| 184 | + |
| 185 | + void _do_broadcast(const Tensor& tensor) { |
| 186 | + gloo::BroadcastOptions opts(_context); |
| 187 | + const auto& dtype = tensor.type(); |
| 188 | + GENERATE_FUNC(dtype, set_output, opts, tensor); |
| 189 | + opts.setRoot(_root); |
| 190 | + opts.setTag(_tag); |
| 191 | + gloo::broadcast(opts); |
| 192 | + } |
| 193 | +}; |
| 194 | + |
| 195 | +std::shared_ptr<ProcessGroup::Task> ProcessGroupGloo::Broadcast( |
| 196 | + std::vector<Tensor>& inputs, const BroadcastOptions& opts) { |
| 197 | + auto root = opts.source_rank; |
| 198 | + std::unique_ptr<BroadcastGlooTask> task; |
| 199 | + auto tag = next_tag(); |
| 200 | + auto context = get_context(); |
| 201 | + task = std::make_unique<BroadcastGlooTask>(context, inputs, rank_, root, tag); |
| 202 | + task->Run(); |
| 203 | + return task; |
| 204 | +} |
| 205 | + |
| 206 | +class AllreduceGlooTask : public ProcessGroupGloo::GlooTask { |
| 207 | + public: |
| 208 | + AllreduceGlooTask(int rank, const std::shared_ptr<gloo::Context>& context, |
| 209 | + std::vector<Tensor>& inputs, ReduceOp reduce_op, // NOLINT |
| 210 | + uint32_t tag) |
| 211 | + : ProcessGroupGloo::GlooTask(rank, inputs, CommType::ALLREDUCE), |
| 212 | + _context(context), |
| 213 | + _inputs(inputs), |
| 214 | + _reduce_op(reduce_op), |
| 215 | + _tag(tag) {} |
| 216 | + |
| 217 | + void Run() override { _do_allreduce(_inputs); } |
| 218 | + |
| 219 | + private: |
| 220 | + std::shared_ptr<gloo::Context> _context; |
| 221 | + std::vector<Tensor> _inputs; |
| 222 | + const ReduceOp _reduce_op; |
| 223 | + uint32_t _tag; |
| 224 | + |
| 225 | + gloo::AllreduceOptions::Func _get_function(const experimental::DataType type, |
| 226 | + const ReduceOp op) { |
| 227 | + gloo::AllreduceOptions::Func fn; |
| 228 | + GENERATE_FUNC(type, _get_function_impl, fn, op); |
| 229 | + return fn; |
| 230 | + } |
| 231 | + |
| 232 | + template <typename T> |
| 233 | + void _get_function_impl(gloo::AllreduceOptions::Func& fn, // NOLINT |
| 234 | + const ReduceOp op) { |
| 235 | + fn = get_function<T>(op); |
| 236 | + } |
| 237 | + |
| 238 | + void _do_allreduce(std::vector<Tensor>& tensors) { // NOLINT |
| 239 | + const auto& dtype = tensors[0].type(); |
| 240 | + gloo::AllreduceOptions opts(_context); |
| 241 | + GENERATE_FUNC(dtype, set_inputs, opts, tensors); |
| 242 | + GENERATE_FUNC(dtype, set_outputs, opts, tensors); |
| 243 | + opts.setReduceFunction(_get_function(dtype, _reduce_op)); |
| 244 | + opts.setTag(_tag); |
| 245 | + gloo::allreduce(opts); |
| 246 | + } |
| 247 | +}; |
| 248 | + |
| 249 | +std::shared_ptr<ProcessGroup::Task> ProcessGroupGloo::AllReduce( |
| 250 | + std::vector<Tensor>& inputs, const AllreduceOptions& opts) { |
| 251 | + auto tag = next_tag(); |
| 252 | + std::shared_ptr<GlooTask> task; |
| 253 | + auto context = get_context(); |
| 254 | + task = std::make_shared<AllreduceGlooTask>(rank_, context, inputs, |
| 255 | + opts.reduce_op, tag); |
| 256 | + task->Run(); |
| 257 | + return task; |
| 258 | +} |
| 259 | + |
| 260 | +std::shared_ptr<::gloo::transport::Device> |
| 261 | +ProcessGroupGloo::createDeviceForInterface(const std::string& ifname) { |
| 262 | + ::gloo::transport::tcp::attr attr; |
| 263 | + attr.iface = ifname; |
| 264 | + return ::gloo::transport::tcp::CreateDevice(attr); |
| 265 | +} |
| 266 | + |
| 267 | +std::shared_ptr<::gloo::transport::Device> |
| 268 | +ProcessGroupGloo::createDeviceForHostname(const std::string& hostname) { |
| 269 | + ::gloo::transport::tcp::attr attr; |
| 270 | + attr.hostname = hostname; |
| 271 | + return ::gloo::transport::tcp::CreateDevice(attr); |
| 272 | +} |
| 273 | + |
| 274 | +std::shared_ptr<::gloo::transport::Device> |
| 275 | +ProcessGroupGloo::createDefaultDevice() { |
| 276 | + std::array<char, HOST_NAME_MAX> hostname{}; |
| 277 | + auto ret = ::gethostname(hostname.data(), HOST_NAME_MAX); |
| 278 | + PADDLE_ENFORCE_EQ(ret, 0, platform::errors::Fatal( |
| 279 | + "Get hostname error for createDefaultDevice.")); |
| 280 | + ::addrinfo* result; |
| 281 | + result = tcputils::get_addr_info(hostname.data(), "", 0, AF_UNSPEC); |
| 282 | + ::addrinfo* cur; |
| 283 | + for (cur = result; cur != nullptr; cur = cur->ai_next) { |
| 284 | + SocketType socket = |
| 285 | + ::socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); |
| 286 | + if (socket == -1) { |
| 287 | + continue; |
| 288 | + } |
| 289 | + ret = ::bind(socket, cur->ai_addr, cur->ai_addrlen); |
| 290 | +#ifdef _WIN32 |
| 291 | + closesocket(socket); |
| 292 | +#else |
| 293 | + close(socket); |
| 294 | +#endif |
| 295 | + if (ret == -1) { |
| 296 | + continue; |
| 297 | + } |
| 298 | + break; |
| 299 | + } |
| 300 | + freeaddrinfo(result); |
| 301 | + if (cur != nullptr) { |
| 302 | + return createDeviceForHostname(hostname.data()); |
| 303 | + } |
| 304 | + return createDeviceForHostname("127.0.0.1"); |
| 305 | +} |
| 306 | + |
| 307 | +} // namespace distributed |
| 308 | +} // namespace paddle |
0 commit comments