tree_wrapper.h 4.6 KB
Newer Older
M
bug fix  
malin10 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

M
malin10 已提交
15 16 17 18 19
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
M
bug fix  
malin10 已提交
20
#include <utility>
M
malin10 已提交
21 22 23 24 25 26 27 28
#include <vector>

#include "paddle/fluid/framework/data_feed.h"

namespace paddle {
namespace framework {

struct Node {
M
bug fix  
malin10 已提交
29 30
  Node() : parent_node(NULL), id(0), height(0) {}
  ~Node() {}
M
malin10 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  std::vector<Node*> sub_nodes;
  // uint32_t sub_node_num;
  Node* parent_node;
  uint64_t id;
  std::vector<float> embedding;
  int16_t height;  //层级
};

class Tree {
 public:
  Tree() : _nodes(NULL), _head(NULL) {}
  ~Tree() {
    if (_nodes) {
      delete[] _nodes;
      _nodes = NULL;
    }
  }

  void print_tree();
  int dump_tree(const uint64_t table_id, int fea_value_dim,
                const std::string tree_path);
M
bug fix  
malin10 已提交
52
  bool trace_back(uint64_t id, std::vector<std::pair<uint64_t, uint32_t>>* ids);
M
malin10 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  int load(std::string path);
  Node* get_node();
  size_t get_total_node_num();

 private:
  // tree data info
  Node* _nodes{nullptr};
  // head pointer
  Node* _head{nullptr};
  // total number of nodes
  size_t _total_node_num{0};
  // leaf node map
  std::unordered_map<uint64_t, Node*> _leaf_node_map;
  // version
  std::string _version{""};
  //树的高度
  int16_t _tree_height{0};
};

using TreePtr = std::shared_ptr<Tree>;
class TreeWrapper {
 public:
  virtual ~TreeWrapper() {}
  TreeWrapper() {}

  // TreeWrapper singleton
  static std::shared_ptr<TreeWrapper> GetInstance() {
    if (NULL == s_instance_) {
      s_instance_.reset(new paddle::framework::TreeWrapper());
    }
    return s_instance_;
  }

  void clear() { tree_map.clear(); }

  void insert(std::string name, std::string tree_path) {
    if (tree_map.find(name) != tree_map.end()) {
      return;
    }
M
bug fix  
malin10 已提交
92 93
    TreePtr tree = std::make_shared<Tree>();
    tree->load(tree_path);
M
malin10 已提交
94 95 96 97 98 99 100 101 102 103 104 105
    tree_map.insert(std::pair<std::string, TreePtr>{name, tree});
  }

  void dump(std::string name, const uint64_t table_id, int fea_value_dim,
            const std::string tree_path) {
    if (tree_map.find(name) == tree_map.end()) {
      return;
    }
    tree_map.at(name)->dump_tree(table_id, fea_value_dim, tree_path);
  }

  void sample(const uint16_t sample_slot, const uint64_t type_slot,
M
bug fix  
malin10 已提交
106 107 108 109
              const std::vector<Record>& src_datas,
              std::vector<Record>* sample_results) {
    sample_results->clear();
    auto debug_idx = 0;
M
malin10 已提交
110
    for (auto& data : src_datas) {
M
bug fix  
malin10 已提交
111 112 113 114
      if (debug_idx == 0) {
        VLOG(0) << "src record";
        data.Print();
      }
M
malin10 已提交
115
      uint64_t sample_feasign_idx = -1, type_feasign_idx = -1;
M
bug fix  
malin10 已提交
116
      for (uint64_t i = 0; i < data.uint64_feasigns_.size(); i++) {
M
malin10 已提交
117 118 119
        if (data.uint64_feasigns_[i].slot() == sample_slot) {
          sample_feasign_idx = i;
        }
M
bug fix  
malin10 已提交
120
        if (data.uint64_feasigns_[i].slot() == type_slot) {
M
malin10 已提交
121 122 123 124 125
          type_feasign_idx = i;
        }
      }
      if (sample_feasign_idx > 0) {
        std::vector<std::pair<uint64_t, uint32_t>> trace_ids;
M
bug fix  
malin10 已提交
126 127 128 129
        for (std::unordered_map<std::string, TreePtr>::iterator ite =
                 tree_map.begin();
             ite != tree_map.end(); ite++) {
          bool in_tree = ite->second->trace_back(
M
malin10 已提交
130
              data.uint64_feasigns_[sample_feasign_idx].sign().uint64_feasign_,
M
bug fix  
malin10 已提交
131
              &trace_ids);
M
malin10 已提交
132 133 134 135 136 137
          if (in_tree) {
            break;
          } else {
            PADDLE_ENFORCE_EQ(trace_ids.size(), 0, "");
          }
        }
M
bug fix  
malin10 已提交
138
        for (uint64_t i = 0; i < trace_ids.size(); i++) {
M
malin10 已提交
139 140 141 142 143 144 145
          Record instance(data);
          instance.uint64_feasigns_[sample_feasign_idx].sign().uint64_feasign_ =
              trace_ids[i].first;
          if (type_feasign_idx > 0)
            instance.uint64_feasigns_[type_feasign_idx]
                .sign()
                .uint64_feasign_ += trace_ids[i].second * 100;
M
bug fix  
malin10 已提交
146 147 148 149 150
          if (debug_idx == 0) {
            VLOG(0) << "sample results:" << i;
            instance.Print();
          }
          sample_results->push_back(instance);
M
malin10 已提交
151 152
        }
      }
M
bug fix  
malin10 已提交
153
      debug_idx += 1;
M
malin10 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
    }
    return;
  }

 public:
  std::unordered_map<std::string, TreePtr> tree_map;

 private:
  static std::shared_ptr<TreeWrapper> s_instance_;
};

}  // end namespace framework
}  // end namespace paddle