hashtable.h 5.0 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
16
#ifdef PADDLE_WITH_HETERPS
T
Thunderbrook 已提交
17
#include <glog/logging.h>
T
Thunderbrook 已提交
18 19 20
#include <limits>
#include <memory>
#include <vector>
21

T
Thunderbrook 已提交
22
#ifdef PADDLE_WITH_PSLIB
T
Thunderbrook 已提交
23
#include "common_value.h"  // NOLINT
T
Thunderbrook 已提交
24
#endif
25 26

#if defined(PADDLE_WITH_PSCORE)
27
#include "paddle/fluid/distributed/ps/table/depends/feature_value.h"
T
Thunderbrook 已提交
28
#endif
29
#include "paddle/fluid/framework/fleet/heter_ps/feature_value.h"
30
#include "paddle/phi/core/utils/rw_lock.h"
31 32

#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
33
#include "paddle/fluid/framework/fleet/heter_ps/cudf/concurrent_unordered_map.cuh.h"
34
#include "paddle/fluid/framework/fleet/heter_ps/mem_pool.h"
35
#include "paddle/fluid/platform/device/gpu/gpu_types.h"
36 37 38 39 40 41 42
#include "thrust/pair.h"
#elif defined(__xpu__)
#include <xpu/runtime.h>
#include "xpu/kernel/cluster_header.h"
#include "xpu/kernel/math.h"
#include "xpu/kernel/simd.h"
#endif
T
Thunderbrook 已提交
43 44 45 46

namespace paddle {
namespace framework {

47
#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
48 49 50 51 52 53 54 55 56 57
template <typename KeyType, typename ValType>
class TableContainer
    : public concurrent_unordered_map<KeyType, ValType,
                                      std::numeric_limits<KeyType>::max()> {
 public:
  TableContainer(size_t capacity)
      : concurrent_unordered_map<KeyType, ValType,
                                 std::numeric_limits<KeyType>::max()>(
            capacity, ValType()) {}
};
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
#elif defined(PADDLE_WITH_XPU_KP)

template <typename KeyType, typename ValType>
class XPUCacheArray {
 public:
  explicit XPUCacheArray(size_t capacity) : capacity_(capacity), size_(0) {
    xpu_malloc(reinterpret_cast<void**>(&keys), capacity_ * sizeof(KeyType));
    xpu_malloc(reinterpret_cast<void**>(&vals), capacity_ * sizeof(ValType));
  }

  virtual ~XPUCacheArray() {
    xpu_free(keys);
    xpu_free(vals);
  }

  void print() {}
  // ValType* find(const KeyType& key) { return NULL; }
  // bool insert(const KeyType& key, const ValType& val) { return true; }

  int prefetch(const int dev_id, XPUStream stream = NULL) {}
  size_t size() { return size_; }

 private:
  long long capacity_;
  long long size_;
  KeyType* keys;
  ValType* vals;
};
#endif
T
Thunderbrook 已提交
87 88 89 90

template <typename KeyType, typename ValType>
class HashTable {
 public:
91
  explicit HashTable(size_t capacity);
T
Thunderbrook 已提交
92 93 94
  virtual ~HashTable();
  HashTable(const HashTable&) = delete;
  HashTable& operator=(const HashTable&) = delete;
95 96

  template <typename StreamType>
T
Thunderbrook 已提交
97
  void insert(const KeyType* d_keys, const ValType* d_vals, size_t len,
98 99 100
              StreamType stream);

  template <typename StreamType>
101
  void insert(const KeyType* d_keys, size_t len, char* pool, size_t start_index,
102 103 104
              StreamType stream);

  template <typename StreamType>
T
Thunderbrook 已提交
105
  void get(const KeyType* d_keys, ValType* d_vals, size_t len,
106 107 108 109 110
           StreamType stream);

  template <typename StreamType>
  void get(const KeyType* d_keys, char* d_vals, size_t len, StreamType stream);

T
Thunderbrook 已提交
111 112
  void show();

113 114 115 116 117 118
  template <typename StreamType>
  void dump_to_cpu(int devid, StreamType stream);

#if defined(PADDLE_WITH_CUDA)

  template <typename GradType, typename Sgd, typename StreamType>
T
Thunderbrook 已提交
119
  void update(const KeyType* d_keys, const GradType* d_grads, size_t len,
120
              Sgd sgd, StreamType stream);
T
Thunderbrook 已提交
121

122
  template <typename Sgd, typename StreamType>
123
  void update(const KeyType* d_keys, const char* d_grads, size_t len, Sgd sgd,
124 125 126 127 128 129 130 131 132 133 134 135
              StreamType stream);

#elif defined(PADDLE_WITH_XPU_KP)
  template <typename GradType, typename StreamType>
  void update(const KeyType* d_keys, const GradType* d_grads, size_t len,
              StreamType stream);

  template <typename StreamType>
  void update(const KeyType* d_keys, const char* d_grads, size_t len,
              StreamType stream);

#endif
136

137 138
  int size() { return container_->size(); }

139 140 141 142 143 144 145 146
  void set_feature_value_size(size_t pull_feature_value_size,
                              size_t push_grad_value_size) {
    pull_feature_value_size_ = pull_feature_value_size;
    push_grad_value_size_ = push_grad_value_size;
    VLOG(3) << "hashtable set pull value size: " << pull_feature_value_size_
            << " push value size: " << push_grad_value_size_;
  }

147
  std::unique_ptr<phi::RWLock> rwlock_{nullptr};
148

T
Thunderbrook 已提交
149
 private:
150
#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
151
  TableContainer<KeyType, ValType>* container_;
152 153 154
#elif defined(PADDLE_WITH_XPU_KP)
  XPUCacheArray<KeyType, ValType>* container_;
#endif
T
Thunderbrook 已提交
155 156 157
  int BLOCK_SIZE_{256};
  float LOAD_FACTOR{0.75f};
  size_t capacity_;
158 159 160
  size_t max_mf_dim_ = 8;
  size_t pull_feature_value_size_;
  size_t push_grad_value_size_;
T
Thunderbrook 已提交
161 162 163 164
};
}  // end namespace framework
}  // end namespace paddle
#endif