heter_ps.cu 6.6 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. */

#include <vector>
16

T
Thunderbrook 已提交
17 18
#include "paddle/fluid/framework/fleet/heter_ps/heter_ps.h"

T
Thunderbrook 已提交
19
#ifdef PADDLE_WITH_HETERPS
T
Thunderbrook 已提交
20 21 22 23 24

namespace paddle {
namespace framework {

HeterPsBase* HeterPsBase::get_instance(
D
danleifeng 已提交
25 26 27 28 29
    size_t capacity,
    std::shared_ptr<HeterPsResource> resource,
    std::unordered_map<std::string, float> fleet_config,
    std::string accessor_type,
    int optimizer_type) {
D
danleifeng 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  if (accessor_type == "CtrDymfAccessor") {
    auto* accessor_wrapper_ptr =
        GlobalAccessorFactory::GetInstance().GetAccessorWrapper();
    CommonFeatureValueAccessor* gpu_accessor =
        ((AccessorWrapper<CommonFeatureValueAccessor>*)accessor_wrapper_ptr)
            ->AccessorPtr();
    if (optimizer_type == 1) {
      return new HeterPs<CommonFeatureValueAccessor, SparseAdagradOptimizer>(
          capacity, resource, *gpu_accessor);
    } else if (optimizer_type == 3) {
      return new HeterPs<CommonFeatureValueAccessor, SparseAdamOptimizer>(
          capacity, resource, *gpu_accessor);
    } else if (optimizer_type == 4) {
      return new HeterPs<CommonFeatureValueAccessor, SparseAdamSharedOptimizer>(
          capacity, resource, *gpu_accessor);
    }
D
danleifeng 已提交
46 47 48 49 50
  } else {
    VLOG(0) << " HeterPsBase get_instance Warning: now only support "
               "CtrDymfAccessor, but get "
            << accessor_type;
  }
T
Thunderbrook 已提交
51 52
}

D
danleifeng 已提交
53 54
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
HeterPs<GPUAccessor, GPUOptimizer>::HeterPs(
D
danleifeng 已提交
55 56
    size_t capacity,
    std::shared_ptr<HeterPsResource> resource,
L
lxsbupt 已提交
57
    const GPUAccessor& gpu_accessor) {
D
danleifeng 已提交
58 59 60
  comm_ = std::make_shared<HeterComm<FeatureKey, float*, float*, GPUAccessor>>(
      capacity, resource, gpu_accessor);
  opt_ = GPUOptimizer<GPUAccessor>(gpu_accessor);
T
Thunderbrook 已提交
61 62
}

D
danleifeng 已提交
63 64
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
HeterPs<GPUAccessor, GPUOptimizer>::~HeterPs() {}
T
Thunderbrook 已提交
65

D
danleifeng 已提交
66 67 68 69 70
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::pull_sparse(int num,
                                                     FeatureKey* d_keys,
                                                     float* d_vals,
                                                     size_t len) {
T
Thunderbrook 已提交
71 72 73
  comm_->pull_sparse(num, d_keys, d_vals, len);
}

D
danleifeng 已提交
74 75 76 77 78 79 80 81
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::build_ps(int num,
                                                  FeatureKey* h_keys,
                                                  char* pool,
                                                  size_t len,
                                                  size_t feature_value_size,
                                                  size_t chunk_size,
                                                  int stream_num) {
82 83
  comm_->build_ps(
      num, h_keys, pool, len, feature_value_size, chunk_size, stream_num);
Y
yaoxuefeng 已提交
84 85
}

D
danleifeng 已提交
86 87
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
int HeterPs<GPUAccessor, GPUOptimizer>::get_index_by_devid(int devid) {
T
Thunderbrook 已提交
88 89 90
  return comm_->get_index_by_devid(devid);
}

D
danleifeng 已提交
91 92
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::set_sparse_sgd(
D
danleifeng 已提交
93
    const OptimizerConfig& optimizer_config) {
Z
zmxdream 已提交
94 95 96
  comm_->set_sparse_sgd(optimizer_config);
}

D
danleifeng 已提交
97 98
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::set_embedx_sgd(
D
danleifeng 已提交
99
    const OptimizerConfig& optimizer_config) {
Z
zmxdream 已提交
100 101 102
  comm_->set_embedx_sgd(optimizer_config);
}

D
danleifeng 已提交
103 104
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::end_pass() {
D
danleifeng 已提交
105 106
  comm_->end_pass();
}
T
Thunderbrook 已提交
107

D
danleifeng 已提交
108 109
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::show_one_table(int gpu_num) {
D
danleifeng 已提交
110 111
  comm_->show_one_table(gpu_num);
}
T
Thunderbrook 已提交
112

D
danleifeng 已提交
113 114 115 116 117 118
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::push_sparse(int num,
                                                     FeatureKey* d_keys,
                                                     float* d_grads,
                                                     size_t len) {
  comm_->push_sparse(num, d_keys, d_grads, len, opt_);
119 120
}

D
danleifeng 已提交
121 122
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::set_nccl_comm_and_size(
D
danleifeng 已提交
123 124
    const std::vector<ncclComm_t>& inner_comms,
    const std::vector<ncclComm_t>& inter_comms,
L
lxsbupt 已提交
125 126 127
    int comm_size,
    int rank_id) {
  comm_->set_nccl_comm_and_size(inner_comms, inter_comms, comm_size, rank_id);
T
Thunderbrook 已提交
128 129
}

D
danleifeng 已提交
130 131 132
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::set_multi_mf_dim(int multi_mf_dim,
                                                          int max_mf_dim) {
Y
yaoxuefeng 已提交
133 134 135
  comm_->set_multi_mf_dim(multi_mf_dim, max_mf_dim);
}

D
danleifeng 已提交
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
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::show_table_collisions() {
  comm_->show_table_collisions();
}

template <typename GPUAccessor, template <typename T> class GPUOptimizer>
int HeterPs<GPUAccessor, GPUOptimizer>::dedup_keys_and_fillidx(
    const int gpu_id,
    const int total_fea_num,
    const FeatureKey* d_keys,   // input
    FeatureKey* d_merged_keys,  // output
    FeatureKey* d_sorted_keys,
    uint32_t* d_restore_idx,
    uint32_t* d_sorted_idx,
    uint32_t* d_offset,
    uint32_t* d_merged_cnts,
    bool filter_zero) {
  return comm_->dedup_keys_and_fillidx(gpu_id,
                                       total_fea_num,
                                       d_keys,         // input
                                       d_merged_keys,  // output
                                       d_sorted_keys,
                                       d_restore_idx,
                                       d_sorted_idx,
                                       d_offset,
                                       d_merged_cnts,
                                       filter_zero);
D
danleifeng 已提交
163 164
}

T
Thunderbrook 已提交
165 166 167
}  // end namespace framework
}  // end namespace paddle
#endif