table.h 5.3 KB
Newer Older
T
tangwei12 已提交
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.

#pragma once

#include <assert.h>
#include <atomic>
#include <future>  // NOLINT
#include <memory>
#include <string>
#include <utility>
#include "paddle/fluid/distributed/table/accessor.h"
S
seemingwang 已提交
24
#include "paddle/fluid/distributed/table/graph_node.h"
25 26 27 28
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/place.h"
T
tangwei12 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
#include "paddle/fluid/string/string_helper.h"

namespace paddle {
namespace distributed {
class Table {
 public:
  Table() {}
  virtual ~Table() {}
  virtual int32_t initialize(const TableParameter &config,
                             const FsClientParameter &fs_config) final;

  virtual int32_t pull_dense(float *values, size_t num) = 0;
  virtual int32_t push_dense(const float *values, size_t num) = 0;
42 43 44 45
  // for push global_step
  virtual int32_t push_dense(const int64_t *values, const int32_t trainer_id) {
    return 0;
  }
T
tangwei12 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  virtual int32_t push_dense_param(const float *values, size_t num) {
    return 0;
  }

  virtual int32_t pull_sparse(float *values, const uint64_t *keys,
                              size_t num) = 0;
  virtual int32_t push_sparse(const uint64_t *keys, const float *values,
                              size_t num) = 0;
  virtual int32_t push_sparse_param(const uint64_t *keys, const float *values,
                                    size_t num) {
    return 0;
  }

  // only for sparse geo table
  virtual int32_t pull_geo_param(const uint32_t trainer_id,
                                 std::vector<float> *values,
                                 std::vector<uint64_t> *keys) {
    return 0;
  }

  // only for barrier
  virtual int32_t barrier(const uint32_t trainer_id,
                          const std::string barrier_type) {
    return 0;
  }

  // only for barrier table
  virtual int32_t set_table_map(
      std::unordered_map<uint32_t, std::shared_ptr<Table>> *table_map) {
    return 0;
  }

78 79 80 81 82 83 84 85 86 87 88 89
  // only for tensor table
  virtual int32_t set_program_env(
      framework::Scope *scope, platform::Place place,
      const std::vector<framework::ProgramDesc> *sub_program) {
    return 0;
  }

  virtual int32_t set_global_lr(float *lr) {
    _global_lr = lr;
    return 0;
  }

S
seemingwang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  // only for graph table
  virtual int32_t pull_graph_list(int start, int total_size,
                                  std::unique_ptr<char[]> &buffer,
                                  int &actual_size, bool need_feature,
                                  int step = 1) {
    return 0;
  }
  // only for graph table
  virtual int32_t random_sample_neighboors(
      uint64_t *node_ids, int sample_size,
      std::vector<std::unique_ptr<char[]>> &buffers,
      std::vector<int> &actual_sizes) {
    return 0;
  }

  virtual int32_t random_sample_nodes(int sample_size,
                                      std::unique_ptr<char[]> &buffers,
                                      int &actual_sizes) {
    return 0;
  }
  virtual int32_t get_node_feat(const std::vector<uint64_t> &node_ids,
                                const std::vector<std::string> &feature_names,
                                std::vector<std::vector<std::string>> &res) {
    return 0;
  }
T
tangwei12 已提交
115 116 117 118
  virtual int32_t pour() { return 0; }

  virtual void clear() = 0;
  virtual int32_t flush() = 0;
119
  virtual int32_t shrink(const std::string &param) = 0;
T
tangwei12 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

  //指定加载路径
  virtual int32_t load(const std::string &path,
                       const std::string &converter) = 0;
  //指定保存路径
  virtual int32_t save(const std::string &path,
                       const std::string &converter) = 0;

  virtual int32_t set_shard(size_t shard_idx, size_t shard_num) final {
    _shard_idx = shard_idx;
    _shard_num = shard_num;
    return initialize_shard();
  }

  inline std::shared_ptr<ValueAccessor> value_accesor() {
    return _value_accesor;
  }

  virtual void *get_shard(size_t shard_idx) = 0;
  virtual std::pair<int64_t, int64_t> print_table_stat() { return {0, 0}; }

 protected:
  virtual int32_t initialize() = 0;
  virtual int32_t initialize_accessor() final;
  virtual int32_t initialize_shard() = 0;
  virtual std::string table_dir(const std::string &model_dir) {
    return paddle::string::format_string("%s/%03d/", model_dir.c_str(),
                                         _config.table_id());
  }

  size_t _shard_idx;  // table 分片编号
  size_t _shard_num;  // table 分片总数
  TableParameter _config;
153
  float *_global_lr = nullptr;
T
tangwei12 已提交
154 155
  std::shared_ptr<ValueAccessor> _value_accesor;
};
T
tangwei12 已提交
156
REGISTER_PSCORE_REGISTERER(Table);
T
tangwei12 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169

class TableManager {
 public:
  static TableManager &instance() {
    static TableManager manager;
    return manager;
  }
  int32_t initialize();

 private:
  TableManager() {}
  ~TableManager() {}
};
S
seemingwang 已提交
170

T
tangwei12 已提交
171 172
}  // namespace distributed
}  // namespace paddle