heter_comm.h 7.7 KB
Newer Older
T
Thunderbrook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2020 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
T
Thunderbrook 已提交
16
#include <thread>
T
Thunderbrook 已提交
17
#include <vector>
18
#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
19
#include "paddle/fluid/framework/fleet/heter_ps/optimizer.cuh.h"
T
Thunderbrook 已提交
20
#include "paddle/fluid/platform/cuda_device_guard.h"
21
#include "paddle/fluid/platform/dynload/nccl.h"
T
Thunderbrook 已提交
22
#include "thrust/pair.h"
23
#elif defined(PADDLE_WITH_XPU_KP)
24
// #include "paddle/fluid/framework/fleet/heter_ps/optimizer_conf.h"
25 26 27 28 29 30 31 32 33 34
#include <xpu/runtime.h>
#include "paddle/fluid/platform/device/xpu/enforce_xpu.h"
#endif

#include "paddle/fluid/framework/fleet/heter_ps/hashtable.h"
#include "paddle/fluid/framework/fleet/heter_ps/heter_comm_kernel.h"
#include "paddle/fluid/framework/fleet/heter_ps/heter_resource.h"
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/platform/place.h"
T
Thunderbrook 已提交
35

T
Thunderbrook 已提交
36
#ifdef PADDLE_WITH_HETERPS
T
Thunderbrook 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

namespace paddle {
namespace framework {

template <typename KeyType, typename ValType, typename GradType>
class HeterComm {
 public:
  HeterComm(size_t capacity, std::shared_ptr<HeterPsResource> resource);
  virtual ~HeterComm();
  HeterComm(const HeterComm&) = delete;
  HeterComm& operator=(const HeterComm&) = delete;

  void split_input_to_shard(KeyType* d_keys, int* d_idx_ptr, size_t len,
                            int* left, int* right, int gpu_num);
  void merge_grad(int gpu_num, KeyType* d_keys, GradType* d_grads, size_t len,
52
                  int& uniq_len);  // NOLINT
T
Thunderbrook 已提交
53 54 55 56 57 58 59
  void pull_sparse(int num, KeyType* d_keys, ValType* d_vals, size_t len);
  void build_ps(int num, KeyType* h_keys, ValType* h_vals, size_t len,
                size_t chunk_size, int stream_num);
  void dump();
  void show_one_table(int gpu_num);
  int get_index_by_devid(int devid);

60
#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
61 62
  template <typename Sgd>
  void push_sparse(int num, KeyType* d_keys, GradType* d_grads, size_t len,
63
                   Sgd& sgd);  // NOLINT
64 65 66 67
#elif defined(PADDLE_WITH_XPU_KP)
  void push_sparse(int num, KeyType* d_keys, GradType* d_grads, size_t len);
#endif

68 69 70
  void set_sparse_sgd(const OptimizerConfig& optimizer_config);
  void set_embedx_sgd(const OptimizerConfig& optimizer_config);

71
  int log2i(int x);
T
Thunderbrook 已提交
72

73 74 75 76 77
  template <typename DstPlace, typename SrcPlace, typename StreamType>
  void memory_copy(DstPlace dst_place, void* dst, SrcPlace src_place,
                   const void* src, size_t count, StreamType stream = 0);

#if defined(PADDLE_WITH_CUDA)
78 79
  template <typename Sgd>
  void push_sparse_multi_node(int num, KeyType* d_keys, GradType* d_grads,
80
                              size_t len, Sgd& sgd);  // NOLINT
81 82 83

  template <typename Sgd>
  void update_one_table(int num, KeyType* d_keys, GradType* d_grads, size_t len,
84
                        Sgd& sgd);  // NOLINT
85 86 87 88 89 90 91 92 93 94 95 96 97 98

  int gather_one_node_grad(int num, KeyType* d_keys, GradType* d_grads,
                           int len);

  int gather_multi_node_grad(int num, KeyType* d_keys, GradType* d_grads,
                             int len);

  void set_nccl_comm_and_size(const std::vector<ncclComm_t>& inner_comms,
                              const std::vector<ncclComm_t>& inter_comms,
                              int comm_size) {
    nccl_inner_comms_ = inner_comms;
    nccl_inter_comms_ = inter_comms;
    node_size_ = comm_size;
  }
99
#endif
100

101 102 103 104
  bool need_transfer(int send_id, int receive_id) {
    return ((send_id / 4 != receive_id / 4) && (send_id + 4) % 8 != receive_id);
  }

T
Thunderbrook 已提交
105 106
  // void dump_to_cpu(int index);

107 108
  int get_transfer_devid(int send_id) { return (send_id + 4) % 8; }

109 110
  void end_pass();

111
  struct Node {
112 113
    ppStream in_stream;
    ppStream out_stream;
114 115 116 117 118
    char* key_storage;
    char* val_storage;
    int sync;
    int key_bytes_len;
    int val_bytes_len;
119
    int dev_num;
120 121 122 123 124 125
  };

  struct Path {
    std::vector<Node> nodes_;
  };

126 127 128 129 130 131
  struct CopyTask {
    Path* path;
    int step;
    CopyTask(Path* path_, int step_) : path(path_), step(step_) {}
  };

132 133 134 135 136 137 138
  struct LocalStorage {
    LocalStorage() {}
    void init(int size, int dev_id) {
      place_ = platform::CUDAPlace(dev_id);
      alloc(size, true);
    }

139
    void alloc(size_t size, bool force = false) {
140 141 142
      if (force || size > all_keys_mem->size()) {
        all_keys_mem.reset();
        all_grads_mem.reset();
143 144
        all_keys_mem = memory::Alloc(place_, size * sizeof(KeyType));
        all_grads_mem = memory::Alloc(place_, size * sizeof(GradType));
145 146 147 148 149 150
        all_keys = reinterpret_cast<KeyType*>(all_keys_mem->ptr());
        all_grads = reinterpret_cast<GradType*>(all_grads_mem->ptr());
      }
      if (force || size > local_keys_mem->size()) {
        local_keys_mem.reset();
        local_grads_mem.reset();
151 152
        local_keys_mem = memory::Alloc(place_, size * sizeof(KeyType));
        local_grads_mem = memory::Alloc(place_, size * sizeof(GradType));
153 154 155 156 157
        local_keys = reinterpret_cast<KeyType*>(local_keys_mem->ptr());
        local_grads = reinterpret_cast<GradType*>(local_grads_mem->ptr());
      }
    }

158
#if defined(PADDLE_WITH_CUDA)
159
    platform::CUDAPlace place_;
F
Fan Zhang 已提交
160

161 162 163
#elif defined(PADDLE_WITH_XPU_KP)
    platform::XPUPlace place_;
#endif
164 165
    std::shared_ptr<memory::Allocation> all_keys_mem;
    std::shared_ptr<memory::Allocation> all_grads_mem;
F
Fan Zhang 已提交
166

167 168 169 170 171 172 173 174 175
    KeyType* all_keys;
    GradType* all_grads;

    std::shared_ptr<memory::Allocation> local_keys_mem;
    std::shared_ptr<memory::Allocation> local_grads_mem;
    KeyType* local_keys;
    GradType* local_grads;
  };

176
  void init_path();
T
Thunderbrook 已提交
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  template <typename StreamType>
  void sync_stream(const StreamType& stream) {
#if defined(PADDLE_WITH_CUDA)
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamSynchronize(stream));
#elif defined(PADDLE_WITH_XPU_KP)
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_wait(stream));
#endif
  }

  template <typename StreamType>
  void create_stream(StreamType* stream) {
#if defined(PADDLE_WITH_CUDA)
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamCreate(stream));
#elif defined(PADDLE_WITH_XPU_KP)
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_create(stream));
#endif
  }

  template <typename StreamType>
  void destroy_stream(StreamType stream) {
#if defined(PADDLE_WITH_CUDA)
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamDestroy(stream));
#elif defined(PADDLE_WITH_XPU_KP)
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_destroy(stream));
#endif
  }

T
Thunderbrook 已提交
205 206
  void create_storage(int start_index, int end_index, int keylen, int vallen);
  void destroy_storage(int start_index, int end_index);
207 208 209 210
  void walk_to_dest(int start_index, int gpu_num, int* h_left, int* h_right,
                    KeyType* src_key, GradType* src_val);
  void walk_to_src(int start_index, int gpu_num, int* h_left, int* h_right,
                   ValType* src_val);
T
Thunderbrook 已提交
211

S
seemingwang 已提交
212
 protected:
T
Thunderbrook 已提交
213 214 215
  using Table = HashTable<KeyType, ValType>;
  std::vector<Table*> tables_;
  std::shared_ptr<HeterPsResource> resource_;
216
  std::vector<std::vector<Path>> path_;
S
seemingwang 已提交
217 218
  float load_factor_{0.75};
  int block_size_{256};
S
seemingwang 已提交
219
  std::unique_ptr<HeterCommKernel> heter_comm_kernel_;
S
seemingwang 已提交
220 221

 private:
S
seemingwang 已提交
222
  int topo_aware_{0};
223 224
  std::vector<LocalStorage> storage_;
  int feanum_{1800 * 2048};
T
Thunderbrook 已提交
225
  int multi_node_{0};
226 227 228
  int node_size_;

#if defined(PADDLE_WITH_CUDA)
229 230
  std::vector<ncclComm_t> nccl_inner_comms_;
  std::vector<ncclComm_t> nccl_inter_comms_;
T
Thunderbrook 已提交
231
  std::vector<std::shared_ptr<cub::CachingDeviceAllocator>> allocators_;
232
#endif
T
Thunderbrook 已提交
233 234 235 236
};

}  // end namespace framework
}  // end namespace paddle
F
Fan Zhang 已提交
237

T
Thunderbrook 已提交
238
#include "paddle/fluid/framework/fleet/heter_ps/heter_comm_inl.h"
F
Fan Zhang 已提交
239

T
Thunderbrook 已提交
240
#endif