common_sparse_table.h 6.6 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 24 25 26
// 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 <ThreadPool.h>
#include <assert.h>
#include <pthread.h>
#include <memory>
#include <mutex>  // NOLINT
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "Eigen/Dense"
27 28 29 30 31
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/common_table.h"
#include "paddle/fluid/distributed/ps/table/depends/initializers.h"
#include "paddle/fluid/distributed/ps/table/depends/large_scale_kv.h"
#include "paddle/fluid/distributed/ps/table/depends/sparse.h"
T
tangwei12 已提交
32
#include "paddle/fluid/string/string_helper.h"
33
#include "paddle/phi/core/utils/rw_lock.h"
T
tangwei12 已提交
34

T
Thunderbrook 已提交
35 36
#define PSERVER_SAVE_SUFFIX ".shard"

T
tangwei12 已提交
37 38 39
namespace paddle {
namespace distributed {

40 41
class SparseOptimizer;

T
Thunderbrook 已提交
42 43 44 45 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
enum SaveMode { all, base, delta };

struct Meta {
  std::string param;
  int shard_id;
  std::vector<std::string> names;
  std::vector<int> dims;
  uint64_t count;
  std::unordered_map<std::string, int> dims_map;

  explicit Meta(const std::string& metapath) {
    std::ifstream file(metapath);
    std::string line;
    int num_lines = 0;
    while (std::getline(file, line)) {
      if (StartWith(line, "#")) {
        continue;
      }
      auto pairs = paddle::string::split_string<std::string>(line, "=");
      PADDLE_ENFORCE_EQ(
          pairs.size(), 2,
          paddle::platform::errors::InvalidArgument(
              "info in %s except k=v, but got %s", metapath, line));

      if (pairs[0] == "param") {
        param = pairs[1];
      }
      if (pairs[0] == "shard_id") {
        shard_id = std::stoi(pairs[1]);
      }
      if (pairs[0] == "row_names") {
        names = paddle::string::split_string<std::string>(pairs[1], ",");
      }
      if (pairs[0] == "row_dims") {
        auto dims_strs =
            paddle::string::split_string<std::string>(pairs[1], ",");
        for (auto& str : dims_strs) {
          dims.push_back(std::stoi(str));
        }
      }
      if (pairs[0] == "count") {
        count = std::stoull(pairs[1]);
      }
    }
    for (int x = 0; x < names.size(); ++x) {
      dims_map[names[x]] = dims[x];
    }
  }

  Meta(std::string param, int shard_id, std::vector<std::string> row_names,
       std::vector<int> dims, uint64_t count) {
    this->param = param;
    this->shard_id = shard_id;
    this->names = row_names;
    this->dims = dims;
    this->count = count;
  }

  std::string ToString() {
    std::stringstream ss;
    ss << "param=" << param << "\n";
    ss << "shard_id=" << shard_id << "\n";
    ss << "row_names=" << paddle::string::join_strings(names, ',') << "\n";
    ss << "row_dims=" << paddle::string::join_strings(dims, ',') << "\n";
    ss << "count=" << count << "\n";
    return ss.str();
  }
};

111
class CommonSparseTable : public Table {
T
tangwei12 已提交
112
 public:
113
  CommonSparseTable() { rwlock_.reset(new phi::RWLock); }
T
tangwei12 已提交
114 115 116
  virtual ~CommonSparseTable() {}

  // unused method begin
117 118 119 120
  //  virtual int32_t PullDense(float* pull_values, size_t num) { return 0; }
  //  virtual int32_t PushDenseParam(const float* values, size_t num) { return
  //  0; }
  //  virtual int32_t PushDense(const float* values, size_t num) { return 0; }
T
tangwei12 已提交
121 122
  // unused method end

Y
yaoxuefeng 已提交
123 124 125
  virtual int32_t Pull(TableContext& context);
  virtual int32_t Push(TableContext& context);

Z
zhaocaibei123 已提交
126 127 128 129 130
  virtual int32_t Initialize();
  virtual int32_t InitializeShard() { return 0; }
  virtual int32_t InitializeValue();
  virtual int32_t InitializeOptimizer();
  virtual int32_t InitializeRecorder();
T
tangwei12 已提交
131

Z
zhaocaibei123 已提交
132
  virtual int32_t Load(const std::string& path, const std::string& param);
T
Thunderbrook 已提交
133

Z
zhaocaibei123 已提交
134
  virtual int32_t Save(const std::string& path, const std::string& param);
T
Thunderbrook 已提交
135 136 137

  void SaveMetaToText(std::ostream* os, const CommonAccessorParameter& common,
                      const size_t shard_idx, const int64_t total);
T
tangwei12 已提交
138

T
Thunderbrook 已提交
139 140 141 142 143 144 145 146 147 148 149 150
  int64_t SaveValueToText(std::ostream* os, std::shared_ptr<ValueBlock> block,
                          std::shared_ptr<::ThreadPool> pool, const int mode,
                          int shard_id);

  virtual void ProcessALine(const std::vector<std::string>& columns,
                            const Meta& meta, const int64_t id,
                            std::vector<std::vector<float>>* values);

  virtual int64_t LoadFromText(
      const std::string& valuepath, const std::string& metapath,
      const int pserver_id, const int pserver_num, const int local_shard_num,
      std::vector<std::shared_ptr<ValueBlock>>* blocks);
T
tangwei12 已提交
151

Z
zhaocaibei123 已提交
152 153
  virtual std::pair<int64_t, int64_t> PrintTableStat();
  virtual int32_t PullSparse(float* values, const PullSparseValue& pull_value);
T
tangwei12 已提交
154

Z
zhaocaibei123 已提交
155 156
  virtual int32_t PullSparsePtr(char** pull_values, const uint64_t* keys,
                                size_t num);
T
Thunderbrook 已提交
157

Z
zhaocaibei123 已提交
158 159
  virtual int32_t PushSparse(const uint64_t* keys, const float* values,
                             size_t num);
T
tangwei12 已提交
160

Z
zhaocaibei123 已提交
161 162
  virtual int32_t PushSparse(const uint64_t* keys, const float** values,
                             size_t num);
T
Thunderbrook 已提交
163

T
tangwei12 已提交
164
  // only for sparse geo table
Z
zhaocaibei123 已提交
165 166
  virtual int32_t PushSparseParam(const uint64_t* keys, const float* values,
                                  size_t num);
167
  virtual int32_t SetGlobalLR(float* lr);
168

Z
zhaocaibei123 已提交
169 170 171 172
  virtual int32_t Pour();
  virtual int32_t Flush();
  virtual int32_t Shrink(const std::string& param);
  virtual void Clear();
T
tangwei12 已提交
173

174 175
  virtual void* GetShard(size_t shard_idx) { return 0; }

T
tangwei12 已提交
176
 protected:
Z
zhaocaibei123 已提交
177 178 179 180
  virtual int32_t _PushSparse(const uint64_t* keys, const float* values,
                              size_t num);
  virtual int32_t _PushSparse(const uint64_t* keys, const float** values,
                              size_t num);
T
tangwei12 已提交
181

T
Thunderbrook 已提交
182
 protected:
T
tangwei12 已提交
183 184 185 186 187
  const int task_pool_size_ = 11;
  std::vector<std::shared_ptr<::ThreadPool>> _shards_task_pool;

  bool sync = false;
  int param_dim_ = 0;
T
tangwei12 已提交
188 189 190 191 192 193 194 195
  int param_offset_ = 0;

  std::unordered_map<std::string, int> value_idx_;
  std::vector<std::string> value_names_;
  std::vector<int> value_dims_;
  std::vector<int> value_offsets_;
  std::vector<std::string> initializer_attrs_;

T
tangwei12 已提交
196 197 198
  std::shared_ptr<SparseOptimizer> optimizer_;
  std::vector<std::shared_ptr<ValueBlock>> shard_values_;
  std::unordered_map<uint64_t, ReservoirValue<float>> pull_reservoir_;
199
  std::unique_ptr<phi::RWLock> rwlock_{nullptr};
T
tangwei12 已提交
200 201 202 203
};

}  // namespace distributed
}  // namespace paddle