device_mesh.h 7.9 KB
Newer Older
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
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <vector>

#include "paddle/fluid/distributed/auto_parallel/auto_parallel.pb.h"
#include "paddle/fluid/distributed/auto_parallel/utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace distributed {
namespace auto_parallel {
struct DeviceCapability {
  double single_precision_flops = 0.0;
  double double_precision_flops = 0.0;
  double memory_size_in_bytes = 0.0;
  double clock_rate_in_ghz = 0.0;

  // DeviceCapability from_string(const std::string& str);
  std::string to_string() const;

  static DeviceCapability from_proto(const DeviceCapabilityProto& proto);
  DeviceCapabilityProto to_proto() const;
};

inline std::ostream& operator<<(std::ostream& os, const DeviceCapability& obj) {
  os << obj.to_string();
  return os;
}

class Device {
 public:
  Device() = default;
  Device(int64_t global_id,
         int64_t local_id,
         int64_t machine_id,
         const std::string& type)
      : global_id_(global_id),
        local_id_(local_id),
        machine_id_(machine_id),
        type_(type) {}

  int64_t global_id() const { return global_id_; }
  int64_t local_id() const { return local_id_; }
  int64_t machine_id() const { return machine_id_; }
  const std::string& type() const { return type_; }

  const DeviceCapability& capability() const { return capability_; }
  void set_capability(const DeviceCapability& capability) {
    capability_ = capability;
  }

  // Device from_string(const std::string& mesh_str);
  std::string to_string() const;

  static Device from_proto(const DeviceProto& proto);
  DeviceProto to_proto() const;

 private:
  int64_t global_id_;
  int64_t local_id_;
  int64_t machine_id_;
  std::string type_;
  DeviceCapability capability_;
};

inline std::ostream& operator<<(std::ostream& os, const Device& obj) {
  os << obj.to_string();
  return os;
}

bool operator==(const Device& lhs, const Device& rhs);

inline bool operator!=(const Device& lhs, const Device& rhs) {
  return !operator==(lhs, rhs);
}

struct LinkCapability {
  double bandwidth = 0.0;  // Bytes/s
  double latency = 0.0;

  // LinkCapability from_string(const std::string& str);
  std::string to_string() const;

  static LinkCapability from_proto(const LinkCapabilityProto& proto);
  LinkCapabilityProto to_proto() const;
};

inline std::ostream& operator<<(std::ostream& os, const LinkCapability& obj) {
  os << obj.to_string();
  return os;
}

class Link {
 public:
  Link() = default;

  Link(int64_t source_id, int64_t target_id, const std::string& type)
      : source_id_(source_id), target_id_(target_id), type_(type) {}

  int64_t source_id() const { return source_id_; }
  int64_t target_id() const { return target_id_; }
  const std::string& type() const { return type_; }

  const LinkCapability& capability() const { return capability_; }
  void set_capability(const LinkCapability& capability) {
    capability_ = capability;
  }

  // Link from_string(const std::string& str);
  std::string to_string() const;

  static Link from_proto(const LinkProto& proto);
  LinkProto to_proto() const;

 private:
  int64_t source_id_;
  int64_t target_id_;
  std::string type_;
  LinkCapability capability_;
};

inline std::ostream& operator<<(std::ostream& os, const Link& obj) {
  os << obj.to_string();
  return os;
}

bool operator==(const Link& lhs, const Link& rhs);

inline bool operator!=(const Link& lhs, const Link& rhs) {
  return !operator==(lhs, rhs);
}

class Machine {
 public:
  Machine() = default;

  explicit Machine(int64_t id) : id_(id) {}

  int64_t id() const { return id_; }

  void set_id(int64_t id) { id_ = id; }

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  const std::unordered_map<int64_t, const Device*>& devices() const {
    return devices_;
  }

  const std::unordered_map<int64_t, std::unordered_map<int64_t, const Link*>>&
  links() const {
    return links_;
  }

  const Device& device(int64_t global_id) const {
    return *devices_.at(global_id);
  }

  const Link& link(int64_t source_id, int64_t target_id) const {
    return *links_.at(source_id).at(target_id);
  }

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  bool contains(int64_t device_id) const;

  void add_device(const Device& device);

  void add_link(const Link& link);

  // Machine from_string(const std::string& str);
  std::string to_string() const;

 private:
  int64_t id_ = -1;
  std::unordered_map<int64_t, const Device*> devices_;
  std::unordered_map<int64_t, std::unordered_map<int64_t, const Link*>> links_;
};

class DeviceMesh {
 public:
  DeviceMesh() = default;

  DeviceMesh(const std::string& name,
             const std::vector<int64_t>& shape,
             const std::vector<int64_t>& device_ids,
             const std::vector<std::string>& dim_names);

  const std::string& name() const { return name_; }

  void set_name(const std::string& name) { name_ = name; }

  const std::vector<int64_t>& shape() const { return shape_; }

  const std::vector<int64_t>& device_ids() const { return device_ids_; }

  const std::vector<std::string>& dim_names() const { return dim_names_; }

  std::string device_type() const {
216 217 218 219 220
    if (empty()) return "UNKNOWN";
    if (devices_.empty())
      return "UNKNOWN";
    else
      return std::begin(devices_)->second.type();
221 222 223 224 225 226 227 228 229 230 231
  }

  const std::unordered_map<int64_t, Device>& devices() const {
    return devices_;
  }

  const std::unordered_map<int64_t, std::unordered_map<int64_t, Link>>& links()
      const {
    return links_;
  }

232 233 234 235
  const std::unordered_map<int64_t, Machine>& machines() const {
    return machines_;
  }

236 237 238 239 240 241 242 243
  const Device& device(int64_t global_id) const {
    return devices_.at(global_id);
  }

  const Link& link(int64_t source_id, int64_t target_id) const {
    return links_.at(source_id).at(target_id);
  }

244 245 246 247
  const Machine& machine(int64_t machine_id) const {
    return machines_.at(machine_id);
  }

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  int64_t size() const;
  int64_t ndim() const { return shape_.size(); }

  int64_t dim_size(int64_t dim) const {
    int64_t cdim = canonical_dim(dim, shape_.size());
    return shape_[cdim];
  }

  int64_t dim_size(const std::string& dim_name) const {
    for (std::size_t i = 0; i < dim_names_.size(); ++i) {
      if (dim_names_[i] == dim_name) {
        return shape_[i];
      }
    }
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Cannot find the dimension of %s in this device mesh.", dim_name));
  }

  bool empty() const { return (shape_.empty() || device_ids_.empty()); }
  bool contains(int64_t device_id) const;

  void add_device(const Device& device);
  void add_link(const Link& link);

  // DeviceMesh from_string(const std::string& mesh_str);
  std::string to_string() const;

  static DeviceMesh from_proto(const DeviceMeshProto& proto);
  DeviceMeshProto to_proto() const;

 private:
  std::string name_;
  std::vector<int64_t> shape_;
  std::vector<int64_t> device_ids_;
  std::vector<std::string> dim_names_;
  std::unordered_map<int64_t, Device> devices_;
  std::unordered_map<int64_t, std::unordered_map<int64_t, Link>> links_;
  std::unordered_map<int64_t, Machine> machines_;
};

inline std::ostream& operator<<(std::ostream& os, const DeviceMesh& obj) {
  os << obj.to_string();
  return os;
}

bool operator==(const DeviceMesh& lhs, const DeviceMesh& rhs);

inline bool operator!=(const DeviceMesh& lhs, const DeviceMesh& rhs) {
  return !operator==(lhs, rhs);
}

}  // namespace auto_parallel
}  // namespace distributed
}  // namespace paddle