selected_rows.h 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Copyright (c) 2022 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 <algorithm>
#include <memory>
#include <mutex>  // NOLINT
#include <unordered_map>
#include <utility>
#include <vector>

#include "paddle/pten/common/place.h"
#include "paddle/pten/core/ddim.h"
#include "paddle/pten/core/dense_tensor.h"
27
#include "paddle/pten/core/enforce.h"
28 29 30 31 32 33
#include "paddle/pten/core/utils/rw_lock.h"

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/framework/mixed_vector.h"
#include "paddle/fluid/memory/memcpy.h"

34 35 36
namespace egr {
class EagerTensor;
}  // namespace egr
37
namespace pten {
38 39
class SelectedRows : public TensorBase,
                     public TypeInfoTraits<TensorBase, SelectedRows> {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  /*
   * @brief We can use the SelectedRows structure to reproduce a sparse table.
   *  A sparse table is a key-value structure that the key is an `int64_t`,
   *  and the value is a Tensor which the first dimension is 0.
   *  You can use the following interface to operate the sparse table, and you
   * can find
   *  some detail information from the comments of each interface:
   *
   *  HasKey(key), whether the sparse table has the specified key.
   *  Set(key, value), set a key-value pair into the sparse table.
   *  Get(keys, value*), get value by given key list and apply it to the given
   * value pointer
   *    with the specified offset.
   *
   */
 public:
  SelectedRows(const std::vector<int64_t>& rows, const int64_t& height)
      : rows_(rows), height_(height) {
58
    value_.reset(new DenseTensor());
59 60 61 62 63
    rwlock_.reset(new RWLock);
  }

  SelectedRows() {
    height_ = 0;
64
    value_.reset(new DenseTensor());
65 66 67
    rwlock_.reset(new RWLock);
  }

68
  const DenseTensor& value() const { return *value_; }
69

70
  DenseTensor* mutable_value() { return value_.get(); }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  int64_t height() const { return height_; }

  void set_height(int64_t height) { height_ = height; }

  const paddle::framework::Vector<int64_t>& rows() const { return rows_; }

  paddle::framework::Vector<int64_t>* mutable_rows() { return &rows_; }

  void set_rows(const paddle::framework::Vector<int64_t>& rows) {
    rows_ = rows;
  }

  /*
   * @brief Get the index of key in rows
   *
   * @return -1 if the key does not exists.
   */
  int64_t Index(int64_t key) const {
    auto it = std::find(rows_.begin(), rows_.end(), key);
    if (it == rows_.end()) {
      PADDLE_THROW(paddle::platform::errors::NotFound(
          "Input id (%lld) is not in current rows table.", key));
    }
    return static_cast<int64_t>(std::distance(rows_.begin(), it));
  }

  /*
   * @brief whether has the specified key in the table.
   *
   * @return true if the key is exists.
   */
  bool HasKey(int64_t key) const;

  /*
   * @brief Get value by the key list.
   * Note!!! this interface is only used when selected_rows is used as
   * parameters
   * for distribute lookup table.
   *
   * @return a list of pair which contains the non-exists key and the index in
   * the value
   */
114 115
  void Get(const DenseTensor& ids,
           DenseTensor* value,
116 117 118
           bool auto_grown = false,
           bool is_test = false);

119 120 121 122
  void* AllocateFrom(Allocator* allocator,
                     DataType dtype,
                     size_t requested_size = 0) override;

123 124 125 126 127 128 129 130 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
  /*
   * @brief Get the index of the key from id_to_index_ map. If the key not
   * exist,
   * add the key into id_to_index_.
   *
   * Note!!! this interface is only used when selected_rows is used as
   * parameters
   * for distribute lookup table.
   *
   * @return index of the key.
   */
  int64_t AutoGrownIndex(int64_t key, bool auto_grown, bool is_test = false);

  /*
   * @brief Get the index of the key from id_to_index_ map.
   */
  inline int64_t GetIndexFromId(int64_t key) const {
    auto iter = id_to_index_.find(key);
    if (iter == id_to_index_.end()) {
      return -1;
    } else {
      return iter->second;
    }
  }

  void SyncIndex();
  /*
   * @brief Get complete Dims before
   */
  pten::framework::DDim GetCompleteDims() const {
    std::vector<int64_t> dims = vectorize(value_->dims());
    dims[0] = height_;
    return pten::framework::make_ddim(dims);
  }

158 159 160 161 162 163 164 165 166 167 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
  /// \brief Returns the name of the class for type traits.
  /// \return The name of the class.
  static const char* name() { return "SelectedRows"; }

  /// \brief Returns the number of elements contained in tensor.
  /// \return The number of elements contained in tensor.
  int64_t numel() const override { return value_->numel(); };

  /// \brief Returns the dims of the tensor.
  /// \return The dims of the tensor.
  const DDim& dims() const noexcept override {
    return value_->dims();
    // return paddle::framework::make_ddim(dims);
  }

  /// \brief Returns the data type of the tensor.
  /// \return The data type of the tensor.
  DataType dtype() const noexcept override { return value_->dtype(); }

  /// \brief Returns the data layout of the tensor.
  /// \return The data layout of the tensor.
  DataLayout layout() const noexcept override { return value_->layout(); }

  /// \brief Returns the data place of the tensor.
  /// \return The data place of the tensor.
  const Place& place() const override { return value_->place(); };

  /// \brief Test whether the metadata is valid.
  /// \return Whether the metadata is valid.
  bool valid() const noexcept override { return value_->valid(); }

  /// \brief Test whether the storage is allocated.
  /// return Whether the storage is allocated.
  bool initialized() const override { return value_->initialized(); }

193 194 195 196 197 198 199
 private:
  // Notice: rows can be duplicate. We can have {0, 4, 7, 0, 5, 7, 9} here.
  // SelectedRows are simply concated when adding together. Until a
  // SelectedRows add a Tensor, will the duplicate rows be handled.
  paddle::framework::Vector<int64_t> rows_;
  std::unordered_map<int64_t, int64_t>
      id_to_index_;  // should not be used when rows_ has duplicate member
200
  std::unique_ptr<DenseTensor> value_{nullptr};
201 202
  int64_t height_;  // height indicates the underline tensor's height
  std::unique_ptr<RWLock> rwlock_{nullptr};
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
  // TODO(jiabin): Remove this when we don't need EagerTensor support
  // SelectedRows which is expected in next version.
  /** Why we need this weird friend class?
   * In eager mode, since some of ops doesn't support C++ API for now we need to
   *use 'imperative::TraceOp' to run it.
   * So, we need to support get a SelectedRows from egr::EagerTensor's
   *framework::Variable obj and used it to reconstruct
   * a new paddle::experimental::Tensor to support framework usage. However, we
   *got 2 problems here.
   * First, we got 2 unique_ptr in SelectedRows so that we can't support
   *std::make_shared in EagerTensor's SetImplWithSelectedRows method,
   * since we have to construct a shared_ptr for paddle::experimental::Tensor's
   *impl.
   * Second, when we are trying to support move constructor for SelectedRows we
   *found that we can't get its rvalue from
   * framework::Variable because it holds an obj of target type.
   *
   *
   * The only three way to solve this problem is:
   * 1. Just like what we have done, using friend class and just copy/move each
   *member. In this way, we can avoid additional API
   * and symbols.
   * 2. Make pten::SelectedRows's member from unique_ptr to shared_ptr. However,
   *this may cause some cost of performance.
   * 3. Add some api to return or move member of framework::SelectedRows.
   *However, it's not as safe as first solution.
   * 4. Support all framework::SelectedRows related ops and make sure
   *EagerTensor never holds framework::SelectedRows.
   *
   * If anyone got better ideas, welcome to contact JiabinYang, we are open for
   *your help.
  **/
  friend class egr::EagerTensor;
236 237 238
};

}  // namespace pten