ctr_double_accessor.cc 15.7 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/ctr_double_accessor.h"
#include <gflags/gflags.h>
#include "glog/logging.h"
#include "paddle/fluid/string/string_helper.h"

namespace paddle {
namespace distributed {

23
int DownpourCtrDoubleAccessor::Initialize() {
Y
yaoxuefeng 已提交
24 25
  auto name = _config.embed_sgd_param().name();
  _embed_sgd_rule = CREATE_PSCORE_CLASS(SparseValueSGDRule, name);
26
  _embed_sgd_rule->LoadConfig(_config.embed_sgd_param(), 1);
Y
yaoxuefeng 已提交
27 28 29

  name = _config.embedx_sgd_param().name();
  _embedx_sgd_rule = CREATE_PSCORE_CLASS(SparseValueSGDRule, name);
30 31
  _embedx_sgd_rule->LoadConfig(_config.embedx_sgd_param(),
                               _config.embedx_dim());
Y
yaoxuefeng 已提交
32 33 34 35 36

  _show_click_decay_rate = _config.ctr_accessor_param().show_click_decay_rate();
  _ssd_unseenday_threshold =
      _config.ctr_accessor_param().ssd_unseenday_threshold();

37
  InitAccessorInfo();
Y
yaoxuefeng 已提交
38 39 40
  return 0;
}

41
void DownpourCtrDoubleAccessor::InitAccessorInfo() {
Y
yaoxuefeng 已提交
42
  auto embedx_dim = _config.embedx_dim();
43 44 45 46 47 48 49
  _accessor_info.dim = DownpourCtrDoubleFeatureValue::Dim(embedx_dim);
  _accessor_info.size = DownpourCtrDoubleFeatureValue::Size(embedx_dim);
  _accessor_info.select_dim = 3 + embedx_dim;
  _accessor_info.select_size = _accessor_info.select_dim * sizeof(float);
  _accessor_info.update_dim = 4 + embedx_dim;
  _accessor_info.update_size = _accessor_info.update_dim * sizeof(float);
  _accessor_info.mf_size = (embedx_dim + 1) * sizeof(float);
Y
yaoxuefeng 已提交
50
}
51

52
bool DownpourCtrDoubleAccessor::Shrink(float* value) {
Y
yaoxuefeng 已提交
53 54 55 56 57 58 59 60 61
  // 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
62 63
  DownpourCtrDoubleFeatureValue::Show(value) *= _show_click_decay_rate;
  DownpourCtrDoubleFeatureValue::Click(value) *= _show_click_decay_rate;
Y
yaoxuefeng 已提交
64
  // shrink after
65 66 67
  auto score = ShowClickScore(DownpourCtrDoubleFeatureValue::Show(value),
                              DownpourCtrDoubleFeatureValue::Click(value));
  auto unseen_days = DownpourCtrDoubleFeatureValue::UnseenDays(value);
Y
yaoxuefeng 已提交
68 69 70 71 72 73
  if (score < delete_threshold || unseen_days > delete_after_unseen_days) {
    return true;
  }
  return false;
}
bool DownpourCtrDoubleAccessor::save_ssd(float* value) {
74
  if (DownpourCtrDoubleFeatureValue::UnseenDays(value) >
Y
yaoxuefeng 已提交
75 76 77 78 79 80 81 82 83
      _ssd_unseenday_threshold) {
    return true;
  }
  return false;
}
// bool DownpourCtrDoubleAccessor::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();
84
//     if (ShowClickScore(DownpourCtrDoubleFeatureValue::Show(value),
85
//     DownpourCtrDoubleFeatureValue::Click(value)) >= base_threshold
86
//         && DownpourCtrDoubleFeatureValue::UnseenDays(value) <=
Y
yaoxuefeng 已提交
87
//         delta_keep_days) {
88
//         return DownpourCtrDoubleFeatureValue::Show(value) >
Y
yaoxuefeng 已提交
89 90 91 92
//         global_cache_threshold;
//     }
//     return false;
// }
93
bool DownpourCtrDoubleAccessor::Save(float* value, int param) {
Y
yaoxuefeng 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // 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: {
112 113
      if (ShowClickScore(DownpourCtrDoubleFeatureValue::Show(value),
                         DownpourCtrDoubleFeatureValue::Click(value)) >=
Y
yaoxuefeng 已提交
114
              base_threshold &&
115 116
          DownpourCtrDoubleFeatureValue::DeltaScore(value) >= delta_threshold &&
          DownpourCtrDoubleFeatureValue::UnseenDays(value) <= delta_keep_days) {
Y
yaoxuefeng 已提交
117 118
        // do this after save, because it must not be modified when retry
        if (param == 2) {
119
          DownpourCtrDoubleFeatureValue::DeltaScore(value) = 0;
Y
yaoxuefeng 已提交
120 121 122 123 124 125 126 127
        }
        return true;
      } else {
        return false;
      }
    }
    // already decayed in shrink
    case 3: {
128 129
      // DownpourCtrFeatureValue::Show(value) *= _show_click_decay_rate;
      // DownpourCtrFeatureValue::Click(value) *= _show_click_decay_rate;
Y
yaoxuefeng 已提交
130
      // do this after save, because it must not be modified when retry
131
      // DownpourCtrDoubleFeatureValue::UnseenDays(value)++;
Y
yaoxuefeng 已提交
132 133 134 135 136 137 138
      return true;
    }
    default:
      return true;
  };
}

139
void DownpourCtrDoubleAccessor::UpdateStatAfterSave(float* value, int param) {
Y
yaoxuefeng 已提交
140 141 142 143 144 145 146 147
  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: {
148 149
      if (ShowClickScore(DownpourCtrDoubleFeatureValue::Show(value),
                         DownpourCtrDoubleFeatureValue::Click(value)) >=
Y
yaoxuefeng 已提交
150
              base_threshold &&
151 152 153
          DownpourCtrDoubleFeatureValue::DeltaScore(value) >= delta_threshold &&
          DownpourCtrDoubleFeatureValue::UnseenDays(value) <= delta_keep_days) {
        DownpourCtrDoubleFeatureValue::DeltaScore(value) = 0;
Y
yaoxuefeng 已提交
154 155 156 157
      }
    }
      return;
    case 3: {
158
      DownpourCtrDoubleFeatureValue::UnseenDays(value)++;
Y
yaoxuefeng 已提交
159 160 161 162 163 164 165
    }
      return;
    default:
      return;
  };
}

166
int32_t DownpourCtrDoubleAccessor::Create(float** values, size_t num) {
Y
yaoxuefeng 已提交
167 168 169
  auto embedx_dim = _config.embedx_dim();
  for (size_t value_item = 0; value_item < num; ++value_item) {
    float* value = values[value_item];
170 171
    value[DownpourCtrDoubleFeatureValue::UnseenDaysIndex()] = 0;
    value[DownpourCtrDoubleFeatureValue::DeltaScoreIndex()] = 0;
172 173 174
    *(double*)(value + DownpourCtrDoubleFeatureValue::ShowIndex()) = 0;
    *(double*)(value + DownpourCtrDoubleFeatureValue::ClickIndex()) = 0;
    value[DownpourCtrDoubleFeatureValue::SlotIndex()] = -1;
175 176 177 178 179 180
    _embed_sgd_rule->InitValue(
        value + DownpourCtrDoubleFeatureValue::EmbedWIndex(),
        value + DownpourCtrDoubleFeatureValue::EmbedG2SumIndex());
    _embedx_sgd_rule->InitValue(
        value + DownpourCtrDoubleFeatureValue::EmbedxWIndex(),
        value + DownpourCtrDoubleFeatureValue::EmbedxG2SumIndex(), false);
Y
yaoxuefeng 已提交
181 182 183
  }
  return 0;
}
184
bool DownpourCtrDoubleAccessor::NeedExtendMF(float* value) {
Y
yaoxuefeng 已提交
185
  auto show =
186
      ((double*)(value + DownpourCtrDoubleFeatureValue::ShowIndex()))[0];
Y
yaoxuefeng 已提交
187
  auto click =
188
      ((double*)(value + DownpourCtrDoubleFeatureValue::ClickIndex()))[0];
Y
yaoxuefeng 已提交
189 190 191 192 193 194 195
  // float score = (show - click) * _config.ctr_accessor_param().nonclk_coeff()
  auto 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();
}
// from DownpourCtrFeatureValue to DownpourCtrPullValue
196
int32_t DownpourCtrDoubleAccessor::Select(float** select_values,
Y
yaoxuefeng 已提交
197 198 199 200 201
                                          const float** values, 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]);
202 203 204 205
    select_value[DownpourCtrDoublePullValue::ShowIndex()] =
        (float)*(double*)(value + DownpourCtrDoubleFeatureValue::ShowIndex());
    select_value[DownpourCtrDoublePullValue::ClickIndex()] =
        (float)*(double*)(value + DownpourCtrDoubleFeatureValue::ClickIndex());
206 207 208 209
    select_value[DownpourCtrDoublePullValue::EmbedWIndex()] =
        value[DownpourCtrDoubleFeatureValue::EmbedWIndex()];
    memcpy(select_value + DownpourCtrDoublePullValue::EmbedxWIndex(),
           value + DownpourCtrDoubleFeatureValue::EmbedxWIndex(),
Y
yaoxuefeng 已提交
210 211 212 213 214 215 216
           embedx_dim * sizeof(float));
  }
  return 0;
}
// from DownpourCtrPushValue to DownpourCtrPushValue
// first dim: item
// second dim: field num
217
int32_t DownpourCtrDoubleAccessor::Merge(float** update_values,
Y
yaoxuefeng 已提交
218 219 220
                                         const float** other_update_values,
                                         size_t num) {
  auto embedx_dim = _config.embedx_dim();
221
  size_t total_dim = DownpourCtrDoublePushValue::Dim(embedx_dim);
Y
yaoxuefeng 已提交
222 223 224
  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];
225 226 227 228
    /**(double*)(update_value + DownpourCtrDoublePushValue::ShowIndex()) +=
    *(double*)(other_update_value + DownpourCtrDoublePushValue::ShowIndex());
    *(double*)(update_value + DownpourCtrDoublePushValue::ClickIndex()) +=
    *(double*)(other_update_value + DownpourCtrDoublePushValue::ClickIndex());
Y
yaoxuefeng 已提交
229 230 231 232
    for (auto i = 3u; i < total_dim; ++i) {
        update_value[i] += other_update_value[i];
    }*/
    for (auto i = 0u; i < total_dim; ++i) {
233
      if (i != DownpourCtrDoublePushValue::SlotIndex()) {
Y
yaoxuefeng 已提交
234 235 236 237 238 239 240 241 242
        update_value[i] += other_update_value[i];
      }
    }
  }
  return 0;
}
// from DownpourCtrPushValue to DownpourCtrFeatureValue
// first dim: item
// second dim: field num
243
int32_t DownpourCtrDoubleAccessor::Update(float** update_values,
Y
yaoxuefeng 已提交
244 245 246 247 248 249
                                          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];
250 251 252 253
    float push_show = push_value[DownpourCtrDoublePushValue::ShowIndex()];
    float push_click = push_value[DownpourCtrDoublePushValue::ClickIndex()];
    float slot = push_value[DownpourCtrDoublePushValue::SlotIndex()];
    *(double*)(update_value + DownpourCtrDoubleFeatureValue::ShowIndex()) +=
Y
yaoxuefeng 已提交
254
        (double)push_show;
255
    *(double*)(update_value + DownpourCtrDoubleFeatureValue::ClickIndex()) +=
Y
yaoxuefeng 已提交
256
        (double)push_click;
257
    update_value[DownpourCtrDoubleFeatureValue::SlotIndex()] = slot;
258
    update_value[DownpourCtrDoubleFeatureValue::DeltaScoreIndex()] +=
Y
yaoxuefeng 已提交
259 260 261 262
        (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();
263 264 265 266 267 268 269 270 271
    update_value[DownpourCtrDoubleFeatureValue::UnseenDaysIndex()] = 0;
    _embed_sgd_rule->UpdateValue(
        update_value + DownpourCtrDoubleFeatureValue::EmbedWIndex(),
        update_value + DownpourCtrDoubleFeatureValue::EmbedG2SumIndex(),
        push_value + DownpourCtrDoublePushValue::EmbedGIndex(), push_show);
    _embedx_sgd_rule->UpdateValue(
        update_value + DownpourCtrDoubleFeatureValue::EmbedxWIndex(),
        update_value + DownpourCtrDoubleFeatureValue::EmbedxG2SumIndex(),
        push_value + DownpourCtrDoublePushValue::EmbedxGIndex(), push_show);
Y
yaoxuefeng 已提交
272 273 274
  }
  return 0;
}
275
bool DownpourCtrDoubleAccessor::CreateValue(int stage, const float* value) {
Y
yaoxuefeng 已提交
276 277 278 279 280
  // stage == 0, pull
  // stage == 1, push
  if (stage == 0) {
    return true;
  } else if (stage == 1) {
281 282
    auto show = DownpourCtrDoublePushValue::Show(const_cast<float*>(value));
    auto click = DownpourCtrDoublePushValue::Click(const_cast<float*>(value));
283
    auto score = ShowClickScore(show, click);
Y
yaoxuefeng 已提交
284 285 286 287 288 289 290 291 292 293 294 295
    if (score <= 0) {
      return false;
    }
    if (score >= 1) {
      return true;
    }
    return local_uniform_real_distribution<float>()(local_random_engine()) <
           score;
  } else {
    return true;
  }
}
296
double DownpourCtrDoubleAccessor::ShowClickScore(double show, double click) {
Y
yaoxuefeng 已提交
297 298 299 300 301 302
  // 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;
}
303 304
std::string DownpourCtrDoubleAccessor::ParseToString(const float* v,
                                                     int param_size) {
Y
yaoxuefeng 已提交
305 306 307 308 309 310
  thread_local std::ostringstream os;
  os.clear();
  os.str("");
  os << v[0] << " " << v[1] << " " << (float)((double*)(v + 2))[0] << " "
     << (float)((double*)(v + 4))[0] << " " << v[6] << " " << v[7] << " "
     << v[8];
311 312
  auto show = DownpourCtrDoubleFeatureValue::Show(const_cast<float*>(v));
  auto click = DownpourCtrDoubleFeatureValue::Click(const_cast<float*>(v));
313
  auto score = ShowClickScore(show, click);
Y
yaoxuefeng 已提交
314 315 316 317 318 319 320 321
  if (score >= _config.embedx_threshold() && param_size > 9) {
    os << " " << v[9];
    for (auto i = 0; i < _config.embedx_dim(); ++i) {
      os << " " << v[10 + i];
    }
  }
  return os.str();
}
322 323
int DownpourCtrDoubleAccessor::ParseFromString(const std::string& str,
                                               float* value) {
Y
yaoxuefeng 已提交
324
  int embedx_dim = _config.embedx_dim();
325
  float data_buff[_accessor_info.dim + 2];
Y
yaoxuefeng 已提交
326
  float* data_buff_ptr = data_buff;
327 328 329
  _embedx_sgd_rule->InitValue(
      data_buff_ptr + DownpourCtrDoubleFeatureValue::EmbedxWIndex(),
      data_buff_ptr + DownpourCtrDoubleFeatureValue::EmbedxG2SumIndex());
Y
yaoxuefeng 已提交
330 331
  auto str_len = paddle::string::str_to_float(str.data(), data_buff_ptr);
  CHECK(str_len >= 6) << "expect more than 6 real:" << str_len;
332 333
  int show_index = DownpourCtrDoubleFeatureValue::ShowIndex();
  int click_index = DownpourCtrDoubleFeatureValue::ClickIndex();
334
  int embed_w_index = DownpourCtrDoubleFeatureValue::EmbedWIndex();
Y
yaoxuefeng 已提交
335
  // no slot, embedx
336 337
  int value_dim = _accessor_info.dim;
  int embedx_g2sum_index = DownpourCtrDoubleFeatureValue::EmbedxG2SumIndex();
338
  value[DownpourCtrDoubleFeatureValue::SlotIndex()] = -1;
Y
yaoxuefeng 已提交
339 340 341 342 343 344 345 346
  // other case
  if (str_len == (value_dim - 1)) {
    // copy unseen_days..delta_score
    memcpy(value, data_buff_ptr, show_index * sizeof(float));
    // copy show & click
    *(double*)(value + show_index) = (double)data_buff_ptr[2];
    *(double*)(value + click_index) = (double)data_buff_ptr[3];
    // copy others
347 348
    value[DownpourCtrDoubleFeatureValue::EmbedWIndex()] = data_buff_ptr[4];
    value[DownpourCtrDoubleFeatureValue::EmbedG2SumIndex()] = data_buff_ptr[5];
Y
yaoxuefeng 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    memcpy(value + embedx_g2sum_index, data_buff_ptr + 6,
           (embedx_dim + 1) * sizeof(float));
  } else {
    // copy unseen_days..delta_score
    memcpy(value, data_buff_ptr, show_index * sizeof(float));
    // copy show & click
    *(double*)(value + show_index) = (double)data_buff_ptr[2];
    *(double*)(value + click_index) = (double)data_buff_ptr[3];
    // copy embed_w..embedx_w
    memcpy(value + embed_w_index, data_buff_ptr + 4,
           (str_len - 4) * sizeof(float));
  }
  if (str_len == (value_dim - 1) || str_len == 6) {
    str_len += 1;
  }
  return str_len + 2;
}

}  // namespace distributed
}  // namespace paddle