dense_table_test.cc 7.7 KB
Newer Older
T
tangwei12 已提交
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 <ThreadPool.h>
16
#include <vector>
T
tangwei12 已提交
17 18
#include "gtest/gtest.h"
#include "paddle/fluid/distributed/ps.pb.h"
19
#include "paddle/fluid/distributed/ps/table/common_dense_table.h"
T
tangwei12 已提交
20 21 22 23 24

namespace paddle {
namespace distributed {

// CommonDenseTable + Adam
25 26
class Table;

T
tangwei12 已提交
27 28 29 30 31 32 33 34 35 36 37 38
TEST(CommonDenseTable, Adam) {
  int fea_dim = 10;
  int trainers = 2;

  TableParameter table_config;
  table_config.set_table_class("CommonDenseTable");
  FsClientParameter fs_config;
  Table *table = new CommonDenseTable();
  TableAccessorParameter *accessor_config = table_config.mutable_accessor();
  accessor_config->set_accessor_class("CommMergeAccessor");
  CommonAccessorParameter *common_config = table_config.mutable_common();
  // set adam optimize config
39
  common_config->set_name("adam_d2sum");
T
tangwei12 已提交
40 41 42 43 44
  common_config->set_table_name("adam_test_table");
  common_config->set_trainer_num(trainers);
  common_config->add_params("Param");
  common_config->add_dims(fea_dim);
  common_config->add_initializers("gaussian_random&0&0.0&1.0");
45 46 47 48
  common_config->add_params("D2Sum");
  common_config->add_dims(fea_dim);
  common_config->add_initializers("fill_constant&0.0");
  common_config->add_params("G2Sum");
T
tangwei12 已提交
49 50
  common_config->add_dims(fea_dim);
  common_config->add_initializers("fill_constant&0.0");
51
  common_config->add_params("Moment");
T
tangwei12 已提交
52 53
  common_config->add_dims(fea_dim);
  common_config->add_initializers("fill_constant&0.0");
54
  common_config->add_params("MomentDecayRate");
T
tangwei12 已提交
55
  common_config->add_dims(1);
56 57
  common_config->add_initializers("fill_constant&0.99");
  common_config->add_params("AdaDecayRate");
T
tangwei12 已提交
58
  common_config->add_dims(1);
59 60 61 62 63 64 65
  common_config->add_initializers("fill_constant&0.9999");
  common_config->add_params("AdaEpsilon");
  common_config->add_dims(1);
  common_config->add_initializers("fill_constant&1.0e-8");
  common_config->add_params("LearningRate");
  common_config->add_dims(1);
  common_config->add_initializers("fill_constant&5e-6");
Z
zhaocaibei123 已提交
66
  auto ret = table->Initialize(table_config, fs_config);
T
tangwei12 已提交
67 68 69 70 71
  ASSERT_EQ(ret, 0);

  // pull parameters for create and check
  std::vector<float> init_values;
  init_values.resize(fea_dim);
72 73 74 75 76 77 78

  TableContext table_context1;
  table_context1.value_type = Dense;
  table_context1.pull_context.values = init_values.data();
  table_context1.num = fea_dim;
  table->Pull(table_context1);
  // table->PullDense(init_values.data(), fea_dim);
T
tangwei12 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  // push gradient
  std::vector<std::vector<float>> trainer_gradient_values;
  trainer_gradient_values.resize(trainers);
  float start = 10.0;
  for (int i = 0; i < trainers; i++) {
    for (int k = 0; k < fea_dim; k++) {
      trainer_gradient_values[i].push_back(start);
      start += 0.1;
    }
  }

  // for adam
  for (int i = 0; i < trainers; i++) {
    auto &push_values = trainer_gradient_values[i];
94 95 96 97 98 99 100

    TableContext table_context;
    table_context.value_type = Dense;
    table_context.push_context.values = push_values.data();
    table_context.num = push_values.size();
    table->Push(table_context);
    // table->PushDense(push_values.data(), push_values.size());
T
tangwei12 已提交
101 102 103 104
  }

  std::vector<float> pull_values;
  pull_values.resize(fea_dim);
105 106 107 108 109 110 111

  TableContext table_context;
  table_context.value_type = Dense;
  table_context.pull_context.values = pull_values.data();
  table_context.num = fea_dim;
  table->Pull(table_context);
  // table->PullDense(pull_values.data(), fea_dim);
T
tangwei12 已提交
112

113 114 115 116 117
  float mom_rate = 0.99;
  float decay_rate = 0.9999;
  float epsilon = 1.0e-8;
  float lr = 5e-6;
  std::vector<float> d2sum, g2sum, mom, param;
T
tangwei12 已提交
118
  for (int i = 0; i < fea_dim; i++) {
119 120 121
    mom.push_back(0.0);
    d2sum.push_back(0.0);
    g2sum.push_back(0.0);
T
tangwei12 已提交
122 123 124 125 126
    param.push_back(init_values[i]);
  }

  for (int i = 0; i < trainers; i++) {
    for (int j = 0; j < fea_dim; j++) {
127 128 129 130 131 132 133 134 135
      d2sum[j] = d2sum[j] * decay_rate + 1;
      g2sum[j] = g2sum[j] * decay_rate +
                 trainer_gradient_values[i][j] * trainer_gradient_values[i][j];
      float scale = d2sum[j] * epsilon;
      scale = (scale + d2sum[j]) / (scale + g2sum[j]);
      scale = sqrt(scale);
      mom[j] = (mom[j] - trainer_gradient_values[i][j]) * mom_rate +
               trainer_gradient_values[i][j];
      param[j] = param[j] - lr * scale * mom[j];
T
tangwei12 已提交
136 137 138
    }
  }
  for (int j = 0; j < fea_dim; j++) {
Z
zhaocaibei123 已提交
139
    VLOG(0) << param[j] << " " << pull_values[j];
T
tangwei12 已提交
140
    ASSERT_TRUE(abs(param[j] - pull_values[j]) < 1e-5);
T
tangwei12 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  }
}

// CommonDenseTable + Adam
TEST(CommonDenseTable, SGD) {
  int fea_dim = 10;
  int trainers = 2;

  TableParameter table_config;
  table_config.set_table_class("CommonDenseTable");
  FsClientParameter fs_config;
  Table *table = new CommonDenseTable();
  TableAccessorParameter *accessor_config = table_config.mutable_accessor();
  accessor_config->set_accessor_class("CommMergeAccessor");
  CommonAccessorParameter *common_config = table_config.mutable_common();
  common_config->set_name("sgd");
  common_config->set_table_name("sgd_test_table");
  common_config->set_trainer_num(trainers);
  common_config->add_params("Param");
  common_config->add_dims(fea_dim);
  common_config->add_initializers("gaussian_random&0&0.0&1.0");
  common_config->add_params("LearningRate");
  common_config->add_dims(1);
  common_config->add_initializers("fill_constant&1.0");
Z
zhaocaibei123 已提交
165
  auto ret = table->Initialize(table_config, fs_config);
T
tangwei12 已提交
166 167 168 169 170
  ASSERT_EQ(ret, 0);

  // pull parameters for create and check
  std::vector<float> init_values;
  init_values.resize(fea_dim);
171 172 173 174 175 176 177

  TableContext table_context1;
  table_context1.value_type = Dense;
  table_context1.pull_context.values = init_values.data();
  table_context1.num = fea_dim;
  table->Pull(table_context1);
  // table->PullDense(init_values.data(), fea_dim);
T
tangwei12 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

  std::vector<float> total_gradients;
  total_gradients.resize(fea_dim);
  memset(total_gradients.data(), 0, sizeof(float) * total_gradients.size());
  // push gradient
  std::vector<std::vector<float>> trainer_gradient_values;
  trainer_gradient_values.resize(trainers);
  float start = 10.0;
  for (int i = 0; i < trainers; i++) {
    for (int k = 0; k < fea_dim; k++) {
      trainer_gradient_values[i].push_back(start);
      total_gradients[k] += start;
      start += 0.1;
    }
  }

  std::shared_ptr<::ThreadPool> pool_ =
      std::make_shared<::ThreadPool>(trainers);
  std::vector<std::future<void>> task_status;
  for (int i = 0; i < trainers; i++) {
    auto &push_values = trainer_gradient_values[i];
    auto task = [table, &push_values] {
200 201 202 203 204 205
      TableContext table_context;
      table_context.value_type = Dense;
      table_context.push_context.values = push_values.data();
      table_context.num = push_values.size();
      table->Push(table_context);
      //      table->PushDense(push_values.data(), push_values.size());
T
tangwei12 已提交
206 207 208 209 210 211 212 213 214
    };
    task_status.push_back(pool_->enqueue(std::move(task)));
  }
  for (auto &status : task_status) {
    status.wait();
  }

  std::vector<float> pull_values;
  pull_values.resize(fea_dim);
215 216 217 218 219 220 221

  TableContext table_context;
  table_context.value_type = Dense;
  table_context.pull_context.values = pull_values.data();
  table_context.num = fea_dim;
  table->Pull(table_context);
  // table->PullDense(pull_values.data(), fea_dim);
T
tangwei12 已提交
222 223 224 225 226 227 228 229
  for (int j = 0; j < fea_dim; j++) {
    auto update_val = init_values[j] - 1.0 * total_gradients[j];
    ASSERT_TRUE(abs(update_val - pull_values[j]) < 1e-5);
  }
}

}  // namespace distributed
}  // namespace paddle