ctr_accessor.h 7.2 KB
Newer Older
Z
zhaocaibei123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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.

#pragma once
#include <stdint.h>
#include <stdio.h>
#include <vector>
#include "paddle/fluid/distributed/common/registerer.h"
#include "paddle/fluid/distributed/ps.pb.h"
21 22
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/sparse_sgd_rule.h"
Z
zhaocaibei123 已提交
23 24 25 26

namespace paddle {
namespace distributed {

27
// DownpourUnitAccessor
Z
zhaocaibei123 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class CtrCommonAccessor : public ValueAccessor {
 public:
  struct CtrCommonFeatureValue {
    /*
       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;
       */

43 44 45 46
    int Dim() { return 6 + embed_sgd_dim + embedx_sgd_dim + embedx_dim; }
    int DimSize(size_t dim, int embedx_dim) { return sizeof(float); }
    int Size() { return Dim() * sizeof(float); }
    int SlotIndex() { return 0; }
47 48 49
    int UnseenDaysIndex() { return SlotIndex() + 1; }
    int DeltaScoreIndex() { return UnseenDaysIndex() + 1; }
    int ShowIndex() { return DeltaScoreIndex() + 1; }
50
    int ClickIndex() { return ShowIndex() + 1; }
51 52 53 54
    int EmbedWIndex() { return ClickIndex() + 1; }
    int EmbedG2SumIndex() { return EmbedWIndex() + 1; }
    int EmbedxWIndex() { return EmbedG2SumIndex() + embed_sgd_dim; }
    int EmbedxG2SumIndex() { return EmbedxWIndex() + embedx_dim; }
Z
zhaocaibei123 已提交
55

56 57
    float& UnseenDays(float* val) { return val[UnseenDaysIndex()]; }
    float& DeltaScore(float* val) { return val[DeltaScoreIndex()]; }
58 59 60
    float& Show(float* val) { return val[ShowIndex()]; }
    float& Click(float* val) { return val[ClickIndex()]; }
    float& Slot(float* val) { return val[SlotIndex()]; }
61 62 63 64
    float& EmbedW(float* val) { return val[EmbedWIndex()]; }
    float& EmbedG2Sum(float* val) { return val[EmbedG2SumIndex()]; }
    float& EmbedxW(float* val) { return val[EmbedxWIndex()]; }
    float& EmbedxG2Sum(float* val) { return val[EmbedxG2SumIndex()]; }
Z
zhaocaibei123 已提交
65

Z
zhaocaibei123 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    int embed_sgd_dim;
    int embedx_dim;
    int embedx_sgd_dim;
  };

  struct CtrCommonPushValue {
    /*
       float slot;
       float show;
       float click;
       float embed_g;
       std::vector<float> embedx_g;
       */

80
    static int Dim(int embedx_dim) { return 4 + embedx_dim; }
Z
zhaocaibei123 已提交
81

82 83 84 85 86
    static int DimSize(int dim, int embedx_dim) { return sizeof(float); }
    static int Size(int embedx_dim) { return Dim(embedx_dim) * sizeof(float); }
    static int SlotIndex() { return 0; }
    static int ShowIndex() { return CtrCommonPushValue::SlotIndex() + 1; }
    static int ClickIndex() { return CtrCommonPushValue::ShowIndex() + 1; }
87 88
    static int EmbedGIndex() { return CtrCommonPushValue::ClickIndex() + 1; }
    static int EmbedxGIndex() { return CtrCommonPushValue::EmbedGIndex() + 1; }
89 90
    static float& Slot(float* val) {
      return val[CtrCommonPushValue::SlotIndex()];
Z
zhaocaibei123 已提交
91
    }
92 93
    static float& Show(float* val) {
      return val[CtrCommonPushValue::ShowIndex()];
Z
zhaocaibei123 已提交
94
    }
95 96
    static float& Click(float* val) {
      return val[CtrCommonPushValue::ClickIndex()];
Z
zhaocaibei123 已提交
97
    }
98
    static float& EmbedG(float* val) {
99
      return val[CtrCommonPushValue::EmbedGIndex()];
Z
zhaocaibei123 已提交
100
    }
101
    static float* EmbedxG(float* val) {
102
      return val + CtrCommonPushValue::EmbedxGIndex();
Z
zhaocaibei123 已提交
103 104 105 106 107
    }
  };

  struct CtrCommonPullValue {
    /*
108 109
       float show;
       float click;
Z
zhaocaibei123 已提交
110 111 112 113
       float embed_w;
       std::vector<float> embedx_w;
       */

114 115 116 117 118
    static int Dim(int embedx_dim) { return 3 + embedx_dim; }
    static int DimSize(size_t dim) { return sizeof(float); }
    static int Size(int embedx_dim) { return Dim(embedx_dim) * sizeof(float); }
    static int ShowIndex() { return 0; }
    static int ClickIndex() { return 1; }
119 120
    static int EmbedWIndex() { return 2; }
    static int EmbedxWIndex() { return 3; }
121 122
    static float& Show(float* val) {
      return val[CtrCommonPullValue::ShowIndex()];
123
    }
124 125
    static float& Click(float* val) {
      return val[CtrCommonPullValue::ClickIndex()];
126
    }
127
    static float& EmbedW(float* val) {
128
      return val[CtrCommonPullValue::EmbedWIndex()];
Z
zhaocaibei123 已提交
129
    }
130
    static float* EmbedxW(float* val) {
131
      return val + CtrCommonPullValue::EmbedxWIndex();
Z
zhaocaibei123 已提交
132 133 134 135
    }
  };
  CtrCommonAccessor() {}
  virtual ~CtrCommonAccessor() {}
136 137 138
  virtual int Initialize();
  // 初始化AccessorInfo
  virtual void InitAccessorInfo();
Z
zhaocaibei123 已提交
139
  // 判断该value是否进行shrink
140
  virtual bool Shrink(float* value);
Z
zhaocaibei123 已提交
141 142
  // 判断该value是否保存到ssd
  // virtual bool save_ssd(float* value);
143 144
  virtual bool NeedExtendMF(float* value);
  virtual bool HasMF(size_t size);
Z
zhaocaibei123 已提交
145 146 147 148 149
  // 判断该value是否在save阶段dump,
  // param作为参数用于标识save阶段,如downpour的xbox与batch_model
  // param = 0, save all feature
  // param = 1, save delta feature
  // param = 2, save xbox base feature
150
  bool Save(float* value, int param) override;
Z
zhaocaibei123 已提交
151
  // update delta_score and unseen_days after save
152
  void UpdateStatAfterSave(float* value, int param) override;
Z
zhaocaibei123 已提交
153 154
  // keys不存在时,为values生成随机值
  // 要求value的内存由外部调用者分配完毕
155
  virtual int32_t Create(float** value, size_t num);
Z
zhaocaibei123 已提交
156
  // 从values中选取到select_values中
157
  virtual int32_t Select(float** select_values, const float** values,
Z
zhaocaibei123 已提交
158 159
                         size_t num);
  // 将update_values聚合到一起
160
  virtual int32_t Merge(float** update_values,
Z
zhaocaibei123 已提交
161 162
                        const float** other_update_values, size_t num);
  // 将update_values聚合到一起,通过it.next判定是否进入下一个key
163
  // virtual int32_t Merge(float** update_values, iterator it);
Z
zhaocaibei123 已提交
164
  // 将update_values更新应用到values中
165
  virtual int32_t Update(float** values, const float** update_values,
Z
zhaocaibei123 已提交
166 167
                         size_t num);

168 169 170
  std::string ParseToString(const float* value, int param) override;
  int32_t ParseFromString(const std::string& str, float* v) override;
  virtual bool CreateValue(int type, const float* value);
Z
zhaocaibei123 已提交
171 172

  // 这个接口目前只用来取show
173
  float GetField(float* value, const std::string& name) override {
Z
zhaocaibei123 已提交
174 175
    // CHECK(name == "show");
    if (name == "show") {
176
      return common_feature_value.Show(value);
Z
zhaocaibei123 已提交
177 178 179 180 181
    }
    return 0.0;
  }

 private:
182
  // float ShowClickScore(float show, float click);
Z
zhaocaibei123 已提交
183 184 185 186 187 188 189 190 191 192

  // SparseValueSGDRule* _embed_sgd_rule;
  // SparseValueSGDRule* _embedx_sgd_rule;
  // CtrCommonFeatureValue common_feature_value;
  float _show_click_decay_rate;
  int32_t _ssd_unseenday_threshold;

 public:  // TODO(zhaocaibei123): it should be private, but we make it public
          // for unit test
  CtrCommonFeatureValue common_feature_value;
193
  float ShowClickScore(float show, float click);
Z
zhaocaibei123 已提交
194 195 196 197 198
  SparseValueSGDRule* _embed_sgd_rule;
  SparseValueSGDRule* _embedx_sgd_rule;
};
}  // namespace distributed
}  // namespace paddle