heter_ps.cc 4.3 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
/* 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 "paddle/fluid/framework/fleet/heter_ps/heter_ps.h"
#include <vector>

#ifdef PADDLE_WITH_HETERPS

namespace paddle {
namespace framework {

HeterPsBase* HeterPsBase::get_instance(
D
danleifeng 已提交
24 25 26 27 28
    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 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  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 已提交
45 46 47
  } else {
    VLOG(0) << " HeterPsBase get_instance Warning: now only support "
               "CtrDymfAccessor, but get "
D
danleifeng 已提交
48 49 50
            << accessor_type;
    return new HeterPs<CommonFeatureValueAccessor, SparseAdagradOptimizer>(
        capacity, resource, fleet_config, accessor_type, optimizer_type);
D
danleifeng 已提交
51
  }
52 53
}

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

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

D
danleifeng 已提交
67 68 69 70 71
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) {
72 73 74
  comm_->pull_sparse(num, d_keys, d_vals, len);
}

D
danleifeng 已提交
75 76
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
int HeterPs<GPUAccessor, GPUOptimizer>::get_index_by_devid(int devid) {
77 78 79
  return comm_->get_index_by_devid(devid);
}

D
danleifeng 已提交
80 81 82
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::set_sparse_sgd(
    const OptimizerConfig& optimizer_config) {
83 84 85
  comm_->set_sparse_sgd(optimizer_config);
}

D
danleifeng 已提交
86 87
void HeterPs<GPUAccessor, GPUOptimizer>::set_embedx_sgd(
    const OptimizerConfig& optimizer_config) {
88 89 90
  comm_->set_embedx_sgd(optimizer_config);
}

D
danleifeng 已提交
91 92 93 94
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::end_pass() {
  comm_->end_pass();
}
95

D
danleifeng 已提交
96 97 98 99
template <typename GPUAccessor, template <typename T> class GPUOptimizer>
void HeterPs<GPUAccessor, GPUOptimizer>::show_one_table(int gpu_num) {
  comm_->show_one_table(gpu_num);
}
100

D
danleifeng 已提交
101 102 103 104 105
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) {
106 107 108 109 110 111 112
  comm_->push_sparse(num, d_keys, d_grads, len);
  // comm_->push_sparse_multi_node(num, d_keys, d_grads, len, opt_);
}

}  // end namespace framework
}  // end namespace paddle
#endif