downpour_ctr_accessor.cc 17.8 KB
Newer Older
Y
yaoxuefeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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.

#include "paddle/fluid/distributed/ps/table/downpour_ctr_accessor.h"
#include <gflags/gflags.h>
#include "glog/logging.h"
#include "paddle/fluid/string/string_helper.h"

namespace paddle {
namespace distributed {

23
int DownpourCtrAccessor::Initialize() {
Y
yaoxuefeng 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  auto name = _config.embed_sgd_param().name();
  _embed_sgd_rule = CREATE_PSCORE_CLASS(SparseValueSGDRule, name);
  _embed_sgd_rule->load_config(_config.embed_sgd_param(), 1);

  name = _config.embedx_sgd_param().name();
  _embedx_sgd_rule = CREATE_PSCORE_CLASS(SparseValueSGDRule, name);
  _embedx_sgd_rule->load_config(_config.embedx_sgd_param(),
                                _config.embedx_dim());

  _show_click_decay_rate = _config.ctr_accessor_param().show_click_decay_rate();
  _ssd_unseenday_threshold =
      _config.ctr_accessor_param().ssd_unseenday_threshold();
  set_time_decay_rates();
  return 0;
}

40
void DownpourCtrAccessor::SetTableInfo(AccessorInfo& info) {
41 42 43 44 45 46 47
  info.dim = Dim();
  info.size = Size();
  info.select_dim = SelectDim();
  info.select_size = SelectSize();
  info.update_dim = UpdateDim();
  info.update_size = UpdateSize();
  info.mf_size = MFSize();
Y
yaoxuefeng 已提交
48 49
}

50 51 52
size_t DownpourCtrAccessor::GetTableInfo(InfoKey key) {
  switch (key) {
    case DIM:
53
      return Dim();
54
    case SIZE:
55
      return Size();
56
    case SELECT_DIM:
57
      return SelectDim();
58
    case SELECT_SIZE:
59
      return SelectSize();
60
    case UPDATE_DIM:
61
      return UpdateDim();
62
    case UPDATE_SIZE:
63
      return UpdateSize();
64
    case MF_SIZE:
65 66 67
      return MFSize();
    default:
      return 0;
68 69 70 71
  }
  return 0;
}

72
size_t DownpourCtrAccessor::Dim() {
Y
yaoxuefeng 已提交
73
  auto embedx_dim = _config.embedx_dim();
74
  return DownpourCtrFeatureValue::Dim(embedx_dim);
Y
yaoxuefeng 已提交
75 76
}

77
size_t DownpourCtrAccessor::DimSize(size_t dim) {
Y
yaoxuefeng 已提交
78
  auto embedx_dim = _config.embedx_dim();
79
  return DownpourCtrFeatureValue::DimSize(dim, embedx_dim);
Y
yaoxuefeng 已提交
80 81
}

82
size_t DownpourCtrAccessor::Size() {
Y
yaoxuefeng 已提交
83
  auto embedx_dim = _config.embedx_dim();
84
  return DownpourCtrFeatureValue::Size(embedx_dim);
Y
yaoxuefeng 已提交
85 86
}

87
size_t DownpourCtrAccessor::MFSize() {
Y
yaoxuefeng 已提交
88 89 90 91
  return (_config.embedx_dim() + 1) * sizeof(float);  // embedx embedx_g2sum
}

// pull value
92
size_t DownpourCtrAccessor::SelectDim() {
Y
yaoxuefeng 已提交
93 94 95 96
  auto embedx_dim = _config.embedx_dim();
  return 3 + embedx_dim;
}

97
size_t DownpourCtrAccessor::SelectDimSize(size_t dim) { return sizeof(float); }
Y
yaoxuefeng 已提交
98

99
size_t DownpourCtrAccessor::SelectSize() { return SelectDim() * sizeof(float); }
Y
yaoxuefeng 已提交
100 101

// push value
102
size_t DownpourCtrAccessor::UpdateDim() {
Y
yaoxuefeng 已提交
103 104 105 106
  auto embedx_dim = _config.embedx_dim();
  return 4 + embedx_dim;
}

107
size_t DownpourCtrAccessor::UpdateDimSize(size_t dim) { return sizeof(float); }
Y
yaoxuefeng 已提交
108

109
size_t DownpourCtrAccessor::UpdateSize() { return UpdateDim() * sizeof(float); }
Y
yaoxuefeng 已提交
110

111
bool DownpourCtrAccessor::Shrink(float* value) {
Y
yaoxuefeng 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  // auto base_threshold = _config.ctr_accessor_param().base_threshold();
  // auto delta_threshold = _config.ctr_accessor_param().delta_threshold();
  // auto delete_threshold = _config.ctr_accessor_param().delete_threshold();
  auto base_threshold = _config.ctr_accessor_param().base_threshold();
  auto delta_threshold = _config.ctr_accessor_param().delta_threshold();
  auto delete_after_unseen_days =
      _config.ctr_accessor_param().delete_after_unseen_days();
  auto delete_threshold = _config.ctr_accessor_param().delete_threshold();

  // time_decay first
  auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
  int16_t day_diff = _day_id - unseen_days;
  if (day_diff < 0 || day_diff > delete_after_unseen_days) {
    return true;
  }
  auto show_right =
128
      DownpourCtrFeatureValue::Show(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
129
  auto click_right =
130
      DownpourCtrFeatureValue::Click(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

  // shrink after
  auto score = show_click_score(show_right, click_right);
  if (score < delete_threshold) {
    return true;
  }
  return false;
}

void DownpourCtrAccessor::set_day_id(int day_id) { _day_id = day_id; }

int DownpourCtrAccessor::get_day_id() { return _day_id; }

bool DownpourCtrAccessor::save_ssd(float* value) {
  if (_day_id == 0) {
    return true;
  }
  auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
  if (unseen_days == 0) {
    return false;
  }
  // for the origin load (eg. unseen_days = 0-15)
  if (unseen_days < _config.ctr_accessor_param().delta_keep_days()) {
    unseen_days = _day_id - unseen_days;
  }
  int16_t day_diff = _day_id - unseen_days;
  if (day_diff > _ssd_unseenday_threshold) {
    return true;
  }
  return false;
}

// bool DownpourCtrAccessor::save_cache(
//         float* value, int param, double global_cache_threshold) {
//     auto base_threshold = _config.ctr_accessor_param().base_threshold();
//     auto delta_keep_days = _config.ctr_accessor_param().delta_keep_days();
//     auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
//     int16_t day_diff = _day_id - unseen_days;
169 170
//     if (show_click_score(DownpourCtrFeatureValue::Show(value),
//     DownpourCtrFeatureValue::Click(value)) >= base_threshold
Y
yaoxuefeng 已提交
171
//         && day_diff <= delta_keep_days) {
172
//         return DownpourCtrFeatureValue::Show(value) > global_cache_threshold;
Y
yaoxuefeng 已提交
173 174 175 176
//     }
//     return false;
// }

177
bool DownpourCtrAccessor::Save(float* value, int param) {
Y
yaoxuefeng 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  // auto base_threshold = _config.ctr_accessor_param().base_threshold();
  // auto delta_threshold = _config.ctr_accessor_param().delta_threshold();
  // auto delta_keep_days = _config.ctr_accessor_param().delta_keep_days();
  auto base_threshold = _config.ctr_accessor_param().base_threshold();
  auto delta_threshold = _config.ctr_accessor_param().delta_threshold();
  auto delta_keep_days = _config.ctr_accessor_param().delta_keep_days();
  if (param == 2) {
    delta_threshold = 0;
  }
  switch (param) {
    // save all
    case 0: {
      return true;
    }
    // save xbox delta
    case 1:
    // save xbox base
    case 2: {
      auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
      int16_t day_diff = _day_id - unseen_days;

      auto show_right =
200
          DownpourCtrFeatureValue::Show(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
201
      auto click_right =
202
          DownpourCtrFeatureValue::Click(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

      if (show_click_score(show_right, click_right) >= base_threshold &&
          DownpourCtrFeatureValue::delta_score(value) >= delta_threshold &&
          day_diff <= delta_keep_days) {
        // do this after save, because it must not be modified when retry
        if (param == 2) {
          DownpourCtrFeatureValue::delta_score(value) = 0;
        }
        return true;
      } else {
        return false;
      }
    }
    // already decayed in shrink
    case 3: {
218 219
      // DownpourCtrFeatureValue::Show(value) *= _show_click_decay_rate;
      // DownpourCtrFeatureValue::Click(value) *= _show_click_decay_rate;
Y
yaoxuefeng 已提交
220 221 222 223 224 225 226 227 228
      // do this after save, because it must not be modified when retry
      // DownpourCtrFeatureValue::unseen_days(value)++;
      return true;
    }
    default:
      return true;
  };
}

229
void DownpourCtrAccessor::UpdateStatAfterSave(float* value, int param) {
Y
yaoxuefeng 已提交
230 231 232 233 234 235 236 237 238 239 240
  auto base_threshold = _config.ctr_accessor_param().base_threshold();
  auto delta_threshold = _config.ctr_accessor_param().delta_threshold();
  auto delta_keep_days = _config.ctr_accessor_param().delta_keep_days();
  if (param == 2) {
    delta_threshold = 0;
  }
  switch (param) {
    case 1: {
      auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
      int16_t day_diff = _day_id - unseen_days;
      auto show_right =
241
          DownpourCtrFeatureValue::Show(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
242
      auto click_right =
243
          DownpourCtrFeatureValue::Click(value) * _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

      if (show_click_score(show_right, click_right) >= base_threshold &&
          DownpourCtrFeatureValue::delta_score(value) >= delta_threshold &&
          day_diff <= delta_keep_days) {
        DownpourCtrFeatureValue::delta_score(value) = 0;
      }
    }
      return;
    //  case 3:
    //     {
    //         DownpourCtrFeatureValue::unseen_days(value)++;
    //     }
    //     return;
    default:
      return;
  };
}

262
int32_t DownpourCtrAccessor::Create(float** values, size_t num) {
Y
yaoxuefeng 已提交
263 264 265 266 267
  auto embedx_dim = _config.embedx_dim();
  for (size_t value_item = 0; value_item < num; ++value_item) {
    float* value = values[value_item];
    value[DownpourCtrFeatureValue::unseen_days_index()] = 0;
    value[DownpourCtrFeatureValue::delta_score_index()] = 0;
268 269 270
    value[DownpourCtrFeatureValue::ShowIndex()] = 0;
    value[DownpourCtrFeatureValue::ClickIndex()] = 0;
    value[DownpourCtrFeatureValue::SlotIndex()] = -1;
Y
yaoxuefeng 已提交
271
    _embed_sgd_rule->init_value(
272
        value + DownpourCtrFeatureValue::Embed_W_Index(),
Y
yaoxuefeng 已提交
273 274
        value + DownpourCtrFeatureValue::embed_g2sum_index(), true);
    _embedx_sgd_rule->init_value(
275
        value + DownpourCtrFeatureValue::Embedx_W_Index(),
Y
yaoxuefeng 已提交
276 277 278 279 280
        value + DownpourCtrFeatureValue::embedx_g2sum_index());
  }
  return 0;
}

281 282 283
bool DownpourCtrAccessor::NeedExtendMF(float* value) {
  float show = value[DownpourCtrFeatureValue::ShowIndex()];
  float click = value[DownpourCtrFeatureValue::ClickIndex()];
Y
yaoxuefeng 已提交
284 285 286 287 288 289 290
  // float score = (show - click) * _config.ctr_accessor_param().nonclk_coeff()
  float score = (show - click) * _config.ctr_accessor_param().nonclk_coeff() +
                click * _config.ctr_accessor_param().click_coeff();
  //+ click * _config.ctr_accessor_param().click_coeff();
  return score >= _config.embedx_threshold();
}

291
bool DownpourCtrAccessor::HasMF(size_t size) {
Y
yaoxuefeng 已提交
292 293 294 295
  return size > DownpourCtrFeatureValue::embedx_g2sum_index();
}

// from DownpourCtrFeatureValue to DownpourCtrPullValue
296
int32_t DownpourCtrAccessor::Select(float** select_values, const float** values,
Y
yaoxuefeng 已提交
297 298 299 300 301
                                    size_t num) {
  auto embedx_dim = _config.embedx_dim();
  for (size_t value_item = 0; value_item < num; ++value_item) {
    float* select_value = select_values[value_item];
    float* value = const_cast<float*>(values[value_item]);
302 303 304 305 306 307 308 309
    select_value[DownpourCtrPullValue::ShowIndex()] =
        value[DownpourCtrFeatureValue::ShowIndex()];
    select_value[DownpourCtrPullValue::ClickIndex()] =
        value[DownpourCtrFeatureValue::ClickIndex()];
    select_value[DownpourCtrPullValue::Embed_W_Index()] =
        value[DownpourCtrFeatureValue::Embed_W_Index()];
    memcpy(select_value + DownpourCtrPullValue::Embedx_W_Index(),
           value + DownpourCtrFeatureValue::Embedx_W_Index(),
Y
yaoxuefeng 已提交
310 311 312 313 314 315 316 317
           embedx_dim * sizeof(float));
  }
  return 0;
}

// from DownpourCtrPushValue to DownpourCtrPushValue
// first dim: item
// second dim: field num
318
int32_t DownpourCtrAccessor::Merge(float** update_values,
Y
yaoxuefeng 已提交
319 320 321
                                   const float** other_update_values,
                                   size_t num) {
  auto embedx_dim = _config.embedx_dim();
322
  size_t total_dim = DownpourCtrPushValue::Dim(embedx_dim);
Y
yaoxuefeng 已提交
323 324 325 326
  for (size_t value_item = 0; value_item < num; ++value_item) {
    float* update_value = update_values[value_item];
    const float* other_update_value = other_update_values[value_item];
    for (auto i = 0u; i < total_dim; ++i) {
327
      if (i != DownpourCtrPushValue::SlotIndex()) {
Y
yaoxuefeng 已提交
328 329 330 331 332 333 334 335 336 337
        update_value[i] += other_update_value[i];
      }
    }
  }
  return 0;
}

// from DownpourCtrPushValue to DownpourCtrFeatureValue
// first dim: item
// second dim: field num
338
int32_t DownpourCtrAccessor::Update(float** update_values,
Y
yaoxuefeng 已提交
339 340 341 342 343
                                    const float** push_values, size_t num) {
  auto embedx_dim = _config.embedx_dim();
  for (size_t value_item = 0; value_item < num; ++value_item) {
    float* update_value = update_values[value_item];
    const float* push_value = push_values[value_item];
344 345 346 347 348 349
    float push_show = push_value[DownpourCtrPushValue::ShowIndex()];
    float push_click = push_value[DownpourCtrPushValue::ClickIndex()];
    float slot = push_value[DownpourCtrPushValue::SlotIndex()];
    update_value[DownpourCtrFeatureValue::ShowIndex()] += push_show;
    update_value[DownpourCtrFeatureValue::ClickIndex()] += push_click;
    update_value[DownpourCtrFeatureValue::SlotIndex()] = slot;
Y
yaoxuefeng 已提交
350 351 352 353 354 355 356
    update_value[DownpourCtrFeatureValue::delta_score_index()] +=
        (push_show - push_click) * _config.ctr_accessor_param().nonclk_coeff() +
        push_click * _config.ctr_accessor_param().click_coeff();
    //(push_show - push_click) * _config.ctr_accessor_param().nonclk_coeff() +
    // push_click * _config.ctr_accessor_param().click_coeff();
    update_value[DownpourCtrFeatureValue::unseen_days_index()] = 0;
    _embed_sgd_rule->update_value(
357
        update_value + DownpourCtrFeatureValue::Embed_W_Index(),
Y
yaoxuefeng 已提交
358
        update_value + DownpourCtrFeatureValue::embed_g2sum_index(),
359
        push_value + DownpourCtrPushValue::Embed_G_Index(), push_show);
Y
yaoxuefeng 已提交
360
    _embedx_sgd_rule->update_value(
361
        update_value + DownpourCtrFeatureValue::Embedx_W_Index(),
Y
yaoxuefeng 已提交
362
        update_value + DownpourCtrFeatureValue::embedx_g2sum_index(),
363
        push_value + DownpourCtrPushValue::Embedx_G_Index(), push_show);
Y
yaoxuefeng 已提交
364 365 366 367
  }
  return 0;
}

368
bool DownpourCtrAccessor::CreateValue(int stage, const float* value) {
Y
yaoxuefeng 已提交
369 370 371 372 373
  // stage == 0, pull
  // stage == 1, push
  if (stage == 0) {
    return true;
  } else if (stage == 1) {
374 375
    auto show = DownpourCtrPushValue::Show(const_cast<float*>(value));
    auto click = DownpourCtrPushValue::Click(const_cast<float*>(value));
Y
yaoxuefeng 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    auto score = show_click_score(show, click);
    if (score <= 0) {
      return false;
    }
    if (score >= 1) {
      return true;
    }
    return local_uniform_real_distribution<float>()(local_random_engine()) <
           score;
  } else {
    return true;
  }
}

float DownpourCtrAccessor::show_click_score(float show, float click) {
  // auto nonclk_coeff = _config.ctr_accessor_param().nonclk_coeff();
  // auto click_coeff = _config.ctr_accessor_param().click_coeff();
  auto nonclk_coeff = _config.ctr_accessor_param().nonclk_coeff();
  auto click_coeff = _config.ctr_accessor_param().click_coeff();
  return (show - click) * nonclk_coeff + click * click_coeff;
}

398
std::string DownpourCtrAccessor::ParseToString(const float* v, int param_size) {
Y
yaoxuefeng 已提交
399 400 401 402 403
  thread_local std::ostringstream os;
  os.clear();
  os.str("");
  os << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " "
     << v[5] << " " << v[6];
404 405
  auto show = DownpourCtrFeatureValue::Show(const_cast<float*>(v));
  auto click = DownpourCtrFeatureValue::Click(const_cast<float*>(v));
Y
yaoxuefeng 已提交
406 407 408 409 410 411 412 413 414 415
  auto score = show_click_score(show, click);
  if (score >= _config.embedx_threshold() && param_size > 7) {
    os << " " << v[7];
    for (auto i = 0; i < _config.embedx_dim(); ++i) {
      os << " " << v[8 + i];
    }
  }
  return os.str();
}

416
int DownpourCtrAccessor::ParseFromString(const std::string& str, float* value) {
Y
yaoxuefeng 已提交
417
  int embedx_dim = _config.embedx_dim();
418
  float data_buff[Dim()];
Y
yaoxuefeng 已提交
419 420 421
  float* data_buff_ptr = data_buff;

  _embedx_sgd_rule->init_value(
422
      data_buff_ptr + DownpourCtrFeatureValue::Embedx_W_Index(),
Y
yaoxuefeng 已提交
423 424 425 426 427
      data_buff_ptr + DownpourCtrFeatureValue::embedx_g2sum_index());

  auto str_len = paddle::string::str_to_float(str.data(), data_buff_ptr);
  CHECK(str_len >= 6) << "expect more than 6 real:" << str_len;
  // no slot, embedx
428
  int value_dim = Dim();
Y
yaoxuefeng 已提交
429
  int embedx_g2sum_index = DownpourCtrFeatureValue::embedx_g2sum_index();
430
  value[DownpourCtrFeatureValue::SlotIndex()] = -1;
Y
yaoxuefeng 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  // other case
  if (str_len == (value_dim - 1)) {
    memcpy(value, data_buff_ptr, (embedx_g2sum_index - 1) * sizeof(float));
    memcpy(value + embedx_g2sum_index, data_buff_ptr + embedx_g2sum_index - 1,
           (embedx_dim + 1) * sizeof(float));
  } else {
    memcpy(value, data_buff_ptr, str_len * sizeof(float));
  }
  if (str_len == (value_dim - 1) || str_len == 6) {
    str_len += 1;
  }
  return str_len;
}

void DownpourCtrAccessor::set_time_decay_rates() {
  //根据unseen_days的天数来初始化_time_decay_rates大小和对应的衰减率
  auto delete_after_unseen_days =
      _config.ctr_accessor_param().delete_after_unseen_days();
  _time_decay_rates.assign(delete_after_unseen_days + 1, 0.0);
  for (int i = 0; i <= delete_after_unseen_days; ++i) {
    _time_decay_rates[i] = pow(_show_click_decay_rate, i);
  }
}

void DownpourCtrAccessor::update_time_decay(float* value,
                                            bool is_update_seen_day) {
  // 根据day_id 来进行show click 衰减和unseen_day 更新;unseen_day
  // 为上次出现的dayid
  if (_day_id == 0) {
    return;
  }
  auto unseen_days = DownpourCtrFeatureValue::unseen_days(value);
  if (unseen_days == 0) {
    DownpourCtrFeatureValue::unseen_days(value) = _day_id;
    return;
  }
  // for the origin load (unseenday = 0 -15)
  if (unseen_days < _config.ctr_accessor_param().delete_after_unseen_days()) {
    // pull
    if (is_update_seen_day) {
      DownpourCtrFeatureValue::unseen_days(value) = _day_id;
      return;
      // save 舍弃原始的unseenday,都变为上一天出现,保证show/click不被重复decay
    } else {
      DownpourCtrFeatureValue::unseen_days(value) = _day_id - 1;
    }
  }
  int16_t day_diff = _day_id - unseen_days;
  if (day_diff < 0) {
    DownpourCtrFeatureValue::unseen_days(value) = _day_id;
    return;
  }
  if (day_diff >= _config.ctr_accessor_param().delete_after_unseen_days()) {
    return;
  }
486 487
  DownpourCtrFeatureValue::Show(value) *= _time_decay_rates[day_diff];
  DownpourCtrFeatureValue::Click(value) *= _time_decay_rates[day_diff];
Y
yaoxuefeng 已提交
488 489 490 491 492 493 494
  if (is_update_seen_day) {
    DownpourCtrFeatureValue::unseen_days(value) = _day_id;
  }
}

}  // namespace distributed
}  // namespace paddle