ctr_accessor_test.cc 9.5 KB
Newer Older
Z
zhaocaibei123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 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. */

15
#include "paddle/fluid/distributed/ps/table/ctr_accessor.h"
Z
zhaocaibei123 已提交
16 17 18 19 20
#include <cmath>
#include <iostream>
#include "gtest/gtest.h"
#include "paddle/fluid/distributed/common/registerer.h"
#include "paddle/fluid/distributed/ps.pb.h"
21
#include "paddle/fluid/distributed/ps/table/sparse_sgd_rule.h"
Z
zhaocaibei123 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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

namespace paddle {
namespace distributed {
REGISTER_PSCORE_CLASS(SparseValueSGDRule, SparseAdaGradSGDRule);
REGISTER_PSCORE_CLASS(SparseValueSGDRule, StdAdaGradSGDRule);
REGISTER_PSCORE_CLASS(SparseValueSGDRule, SparseAdamSGDRule);
REGISTER_PSCORE_CLASS(SparseValueSGDRule, SparseNaiveSGDRule);

TableAccessorParameter gen_param() {
  TableAccessorParameter param;
  param.set_accessor_class("CtrCommonAccessor");
  param.set_fea_dim(11);
  param.set_embedx_dim(8);
  param.mutable_ctr_accessor_param()->set_nonclk_coeff(0.2);
  param.mutable_ctr_accessor_param()->set_click_coeff(1);
  param.mutable_ctr_accessor_param()->set_base_threshold(0.5);
  param.mutable_ctr_accessor_param()->set_delta_threshold(0.2);
  param.mutable_ctr_accessor_param()->set_delta_keep_days(16);
  param.mutable_ctr_accessor_param()->set_show_click_decay_rate(0.99);
  /*
  param.mutable_embed_sgd_param()->set_name("naive");
  auto* naive_param = param.mutable_embed_sgd_param()->mutable_naive();
  naive_param->set_learning_rate(0.1);
  naive_param->set_initial_range(0.3);
  naive_param->add_weight_bounds(-10.0);
  naive_param->add_weight_bounds(10.0);
  */
  param.mutable_embed_sgd_param()->set_name("StdAdaGradSGDRule");
  auto* adagrad_param = param.mutable_embed_sgd_param()->mutable_adagrad();
  adagrad_param->set_learning_rate(0.1);
  adagrad_param->set_initial_range(0.3);
  adagrad_param->set_initial_g2sum(0.0);
  adagrad_param->add_weight_bounds(-10.0);
  adagrad_param->add_weight_bounds(10.0);

  param.mutable_embedx_sgd_param()->set_name("SparseNaiveSGDRule");
  auto* naive_param = param.mutable_embedx_sgd_param()->mutable_naive();
  naive_param->set_learning_rate(0.1);
  naive_param->set_initial_range(0.3);
  naive_param->add_weight_bounds(-10.0);
  naive_param->add_weight_bounds(10.0);

  return std::move(param);
}

TEST(downpour_feature_value_accessor_test, test_shrink) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
70 71
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
72 73 74 75

  VLOG(3) << "size of struct: " << acc->common_feature_value.embed_sgd_dim
          << " " << acc->common_feature_value.embedx_dim << " "
          << acc->common_feature_value.embedx_sgd_dim << " "
76
          << acc->common_feature_value.Dim() << "\n";
Z
zhaocaibei123 已提交
77

78 79
  float* value = new float[acc->GetAccessorInfo().dim];
  for (auto i = 0u; i < acc->GetAccessorInfo().dim; ++i) {
Z
zhaocaibei123 已提交
80 81
    value[i] = i * 1.0;
  }
82
  ASSERT_TRUE(!acc->Shrink(value));
Z
zhaocaibei123 已提交
83 84 85 86 87

  // set unseen_days too long
  value[1] = 1000;
  // set delta score too small
  value[2] = 0.001;
88
  ASSERT_TRUE(acc->Shrink(value));
Z
zhaocaibei123 已提交
89 90 91 92 93
}

TEST(downpour_feature_value_accessor_test, test_save) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
94 95
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
96

97 98
  float* value = new float[acc->GetAccessorInfo().dim];
  for (auto i = 0u; i < acc->GetAccessorInfo().dim; ++i) {
Z
zhaocaibei123 已提交
99 100 101 102
    value[i] = i * 1.0;
  }

  // save all feature
103
  ASSERT_TRUE(acc->Save(value, 0));
Z
zhaocaibei123 已提交
104 105

  // save delta feature
106
  ASSERT_TRUE(acc->Save(value, 1));
Z
zhaocaibei123 已提交
107 108

  // save base feature with time decay
109
  ASSERT_TRUE(acc->Save(value, 2));
Z
zhaocaibei123 已提交
110 111

  VLOG(3) << "test_save:";
112
  for (auto i = 0u; i < acc->GetAccessorInfo().dim; ++i) {
Z
zhaocaibei123 已提交
113 114 115 116 117 118 119
    VLOG(3) << value[i];
  }
}

TEST(downpour_feature_value_accessor_test, test_create) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
120 121
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
122 123 124 125 126 127 128 129

  const int field_size = 7 + 8;
  const int item_size = 10;

  float** value = new float*[item_size];
  for (auto i = 0u; i < item_size; ++i) {
    value[i] = new float[field_size];
  }
130
  ASSERT_EQ(acc->Create(value, item_size), 0);
Z
zhaocaibei123 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143

  for (auto i = 0u; i < item_size; ++i) {
    for (auto j = 0u; j < field_size; ++j) {
      VLOG(3) << value[i][j] << " ";
      // ASSERT_FLOAT_EQ(value[i][j], 0);
    }
    VLOG(3) << "\n";
  }
}

TEST(downpour_feature_value_accessor_test, test_update) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
144 145
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
146

147
  VLOG(3) << "dim: " << acc->common_feature_value.Dim() << "\n";
148
  VLOG(3) << "update_dim: " << acc->GetAccessorInfo().update_dim << "\n";
Z
zhaocaibei123 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  const int field_size = 7 + 8;
  const int item_size = 10;

  float** value = new float*[item_size];
  for (auto i = 0u; i < item_size; ++i) {
    value[i] = new float[field_size];

    for (auto j = 0u; j < field_size; ++j) {
      value[i][j] = 0;
    }
  }

  typedef const float* const_float_ptr;
  const_float_ptr* grad = new const_float_ptr[item_size];
  for (auto i = 0u; i < item_size; ++i) {
165 166
    float* p = new float[acc->GetAccessorInfo().update_dim];
    for (auto j = 0u; j < acc->GetAccessorInfo().update_dim; ++j) {
167
      p[j] = i + 1;
Z
zhaocaibei123 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    }
    grad[i] = p;
  }

  struct DownpourSparseValueTest {
    float slot;
    float unseen_days;
    float delta_score;
    float show;
    float click;
    float embed_w;
    std::vector<float> embed_g2sum;
    std::vector<float> embedx_w;
    std::vector<float> embedx_g2sum;

    void to_array(float* ptr, size_t dim) {
      ptr[0] = slot;
      ptr[1] = unseen_days;
      ptr[2] = delta_score;
      ptr[3] = show;
      ptr[4] = click;
      ptr[5] = embed_w;
      int idx = 6;
      for (auto j = 0u; j < 1; ++j) {
        ptr[idx + j] = embed_g2sum[j];
      }
      idx += 1;
      for (auto j = 0u; j < 8; ++j) {
        ptr[idx + j] = embedx_w[j];
      }
      idx += 8;
      for (auto j = 0u; j < 0; ++j) {
        ptr[idx + j] = embedx_g2sum[j];
      }
    }
  };
  struct DownpourSparsePushValueTest {
    float slot;
    float show;
    float click;
    float embed_g;
    std::vector<float> embedx_g;
  };
  std::vector<float*> exp_value;
  for (auto i = 0u; i < item_size; ++i) {
    DownpourSparseValueTest v;
    v.slot = value[i][0];
    v.unseen_days = value[i][1];
    v.delta_score = value[i][2];
    v.show = value[i][3];
    v.click = value[i][4];
    v.embed_w = value[i][5];

    int idx = 6;
    for (auto j = 0u; j < acc->common_feature_value.embed_sgd_dim; ++j) {
      v.embed_g2sum.push_back(value[i][idx + j]);
    }
    idx += acc->common_feature_value.embed_sgd_dim;
    for (auto j = 0u; j < acc->common_feature_value.embedx_dim; ++j) {
      v.embedx_w.push_back(value[i][idx + j]);
    }
    idx += acc->common_feature_value.embedx_dim;
    for (auto j = 0u; j < acc->common_feature_value.embedx_sgd_dim; ++j) {
      v.embedx_g2sum.push_back(value[i][idx + j]);
    }

    DownpourSparsePushValueTest push_v;
    push_v.slot = grad[i][0];
    push_v.show = grad[i][1];
    push_v.click = grad[i][2];
    push_v.embed_g = grad[i][3];
    for (auto j = 0; j < parameter.embedx_dim(); ++j) {
      push_v.embedx_g.push_back(grad[i][4 + j]);
    }

    v.slot = push_v.slot;
    v.unseen_days = 0;
    v.show += push_v.show;
    v.click += push_v.click;
247
    v.delta_score += acc->ShowClickScore(push_v.show, push_v.click);
Z
zhaocaibei123 已提交
248

249
    acc->_embed_sgd_rule->UpdateValue(&v.embed_w, &v.embed_g2sum[0],
250
                                      &push_v.embed_g, push_v.show);
251
    acc->_embedx_sgd_rule->UpdateValue(&v.embedx_w[0], &v.embedx_g2sum[0],
252
                                       &push_v.embedx_g[0], push_v.show);
Z
zhaocaibei123 已提交
253

254
    float* ptr = new float[acc->GetAccessorInfo().dim];
Z
zhaocaibei123 已提交
255 256 257
    v.to_array(ptr, parameter.embedx_dim());
    exp_value.push_back(ptr);
  }
258
  acc->Update(value, grad, item_size);
Z
zhaocaibei123 已提交
259 260

  for (auto i = 0u; i < item_size; ++i) {
261
    for (auto j = 0u; j < acc->GetAccessorInfo().dim; ++j) {
Z
zhaocaibei123 已提交
262 263 264 265 266 267 268 269 270
      VLOG(3) << value[i][j] << ":" << exp_value[i][j] << " ";
      ASSERT_FLOAT_EQ(value[i][j], exp_value[i][j]);
    }
  }
}

TEST(downpour_feature_value_accessor_test, test_show_click_score) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
271 272
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
273 274 275

  float show = 10;
  float click = 6;
276
  ASSERT_FLOAT_EQ(acc->ShowClickScore(show, click), 6.8);
Z
zhaocaibei123 已提交
277 278 279 280 281
}

TEST(downpour_feature_value_accessor_test, test_string_related) {
  TableAccessorParameter parameter = gen_param();
  CtrCommonAccessor* acc = new CtrCommonAccessor();
282 283
  ASSERT_EQ(acc->Configure(parameter), 0);
  ASSERT_EQ(acc->Initialize(), 0);
Z
zhaocaibei123 已提交
284 285 286 287 288 289 290

  const int field_size = 15;
  float* value = new float[field_size];
  for (auto i = 0u; i < field_size; ++i) {
    value[i] = i;
  }

291
  auto str = acc->ParseToString(value, 0);
Z
zhaocaibei123 已提交
292 293 294 295

  VLOG(3) << str << std::endl;

  str = "0 1 2 3 4 5 6";
296
  ASSERT_NE(acc->ParseFromString(str, value), 0);
Z
zhaocaibei123 已提交
297 298 299 300 301 302 303 304
  // make sure init_zero=true

  for (auto i = 7; i < 15; ++i) {
    ASSERT_FLOAT_EQ(value[i], 0);
  }
}
}  // namespace distributed
}  // namespace paddle