heter_ps.cu 5.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 30 31 32 33 34 35 36 37 38 39 40
    size_t capacity,
    std::shared_ptr<HeterPsResource> resource,
    std::unordered_map<std::string, float> fleet_config,
    std::string accessor_type,
    int optimizer_type) {
  if (accessor_type == "CtrDymfAccessor" &&
      (optimizer_type == 1 || optimizer_type == 3 || optimizer_type == 4)) {
    return new HeterPs<CommonFeatureValueAccessor>(
        capacity, resource, fleet_config, accessor_type, optimizer_type);
  } else {
    VLOG(0) << " HeterPsBase get_instance Warning: now only support "
               "CtrDymfAccessor, but get "
            << accessor_type;
    return new HeterPs<CommonFeatureValueAccessor>(
        capacity, resource, fleet_config, accessor_type, optimizer_type);
  }
T
Thunderbrook 已提交
41 42
}

D
danleifeng 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
template <typename FVAccessor>
HeterPs<FVAccessor>::HeterPs(
    size_t capacity,
    std::shared_ptr<HeterPsResource> resource,
    std::unordered_map<std::string, float> fleet_config,
    std::string accessor_type,
    int optimizer_type) {
  comm_ = std::make_shared<HeterComm<FeatureKey, float*, float*, FVAccessor>>(
      capacity, resource);
  feature_value_accessor_.Configure(fleet_config);
  set_accessor(feature_value_accessor_);
  accessor_type_ = accessor_type;
  optimizer_type_ = optimizer_type;
T
Thunderbrook 已提交
56 57
}

D
danleifeng 已提交
58 59
template <typename FVAccessor>
HeterPs<FVAccessor>::~HeterPs() {}
T
Thunderbrook 已提交
60

D
danleifeng 已提交
61 62 63 64 65
template <typename FVAccessor>
void HeterPs<FVAccessor>::pull_sparse(int num,
                                      FeatureKey* d_keys,
                                      float* d_vals,
                                      size_t len) {
T
Thunderbrook 已提交
66 67 68
  comm_->pull_sparse(num, d_keys, d_vals, len);
}

D
danleifeng 已提交
69 70 71 72 73 74 75 76
template <typename FVAccessor>
void HeterPs<FVAccessor>::build_ps(int num,
                                   FeatureKey* h_keys,
                                   char* pool,
                                   size_t len,
                                   size_t feature_value_size,
                                   size_t chunk_size,
                                   int stream_num) {
77 78
  comm_->build_ps(
      num, h_keys, pool, len, feature_value_size, chunk_size, stream_num);
Y
yaoxuefeng 已提交
79 80
}

D
danleifeng 已提交
81 82
template <typename FVAccessor>
int HeterPs<FVAccessor>::get_index_by_devid(int devid) {
T
Thunderbrook 已提交
83 84 85
  return comm_->get_index_by_devid(devid);
}

D
danleifeng 已提交
86 87 88
template <typename FVAccessor>
void HeterPs<FVAccessor>::set_sparse_sgd(
    const OptimizerConfig& optimizer_config) {
Z
zmxdream 已提交
89 90 91
  comm_->set_sparse_sgd(optimizer_config);
}

D
danleifeng 已提交
92 93 94
template <typename FVAccessor>
void HeterPs<FVAccessor>::set_embedx_sgd(
    const OptimizerConfig& optimizer_config) {
Z
zmxdream 已提交
95 96 97
  comm_->set_embedx_sgd(optimizer_config);
}

D
danleifeng 已提交
98 99 100 101
template <typename FVAccessor>
void HeterPs<FVAccessor>::end_pass() {
  comm_->end_pass();
}
T
Thunderbrook 已提交
102

D
danleifeng 已提交
103 104 105 106
template <typename FVAccessor>
void HeterPs<FVAccessor>::show_one_table(int gpu_num) {
  comm_->show_one_table(gpu_num);
}
T
Thunderbrook 已提交
107

D
danleifeng 已提交
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
template <typename FVAccessor>
void HeterPs<FVAccessor>::push_sparse(int num,
                                      FeatureKey* d_keys,
                                      float* d_grads,
                                      size_t len) {
  if (accessor_type_ == "CtrDymfAccessor") {
    if (optimizer_type_ == 3) {  // adam
      auto optimizer = SparseAdamOptimizer(feature_value_accessor_);
      VLOG(5) << "INTO push_sparse SparseAdamOptimizer, EmbedDim():"
              << optimizer.EmbedDim();
      comm_->push_sparse(num, d_keys, d_grads, len, optimizer);
    } else if (optimizer_type_ == 4) {  // shared_adam
      auto optimizer = SparseAdamSharedOptimizer(feature_value_accessor_);
      VLOG(5) << "INTO push_sparse SparseAdamSharedOptimizer, EmbedDim():"
              << optimizer.EmbedDim();
      comm_->push_sparse(num, d_keys, d_grads, len, optimizer);
    } else if (optimizer_type_ == 1) {  // adagrad  {
      auto optimizer = SparseAdagradOptimizer(feature_value_accessor_);
      VLOG(5) << "INTO push_sparse SparseAdagradOptimizer, EmbedDim():"
              << optimizer.EmbedDim();
      comm_->push_sparse(num, d_keys, d_grads, len, optimizer);
    } else {
      VLOG(0) << " push sparse Error: CtrDymfAccessor only support adagrad(1),"
                 "adam(3) or shared_adam(4), bug get optimizer type:"
              << optimizer_type_;
    }
  } else {
    VLOG(0) << " push sparse Error: now only support CtrDymfAccessor, but get "
            << accessor_type_;
  }
138 139
}

D
danleifeng 已提交
140 141 142 143 144
template <typename FVAccessor>
void HeterPs<FVAccessor>::set_nccl_comm_and_size(
    const std::vector<ncclComm_t>& inner_comms,
    const std::vector<ncclComm_t>& inter_comms,
    int comm_size) {
145
  comm_->set_nccl_comm_and_size(inner_comms, inter_comms, comm_size);
T
Thunderbrook 已提交
146 147
}

D
danleifeng 已提交
148 149
template <typename FVAccessor>
void HeterPs<FVAccessor>::set_multi_mf_dim(int multi_mf_dim, int max_mf_dim) {
Y
yaoxuefeng 已提交
150 151 152
  comm_->set_multi_mf_dim(multi_mf_dim, max_mf_dim);
}

D
danleifeng 已提交
153 154 155 156 157
template <typename FVAccessor>
void HeterPs<FVAccessor>::set_accessor(FVAccessor& accessor) {
  comm_->set_accessor(accessor);
}

T
Thunderbrook 已提交
158 159 160
}  // end namespace framework
}  // end namespace paddle
#endif