selected_rows.h 5.5 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
/* 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>

J
Jiabin Yang 已提交
24 25
#include "paddle/pten/core/selected_rows_impl.h"

26
namespace pten {
27 28
class SelectedRows : public TensorBase,
                     public TypeInfoTraits<TensorBase, SelectedRows> {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /*
   * @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)
J
Jiabin Yang 已提交
46
      : impl_(std::make_shared<pten::SelectedRowsImpl>(rows, height)) {}
47

J
Jiabin Yang 已提交
48
  SelectedRows() : impl_(std::make_shared<pten::SelectedRowsImpl>()) {}
49

J
Jiabin Yang 已提交
50
  const DenseTensor& value() const { return impl_->value(); }
51

J
Jiabin Yang 已提交
52
  DenseTensor* mutable_value() { return impl_->mutable_value(); }
53

J
Jiabin Yang 已提交
54
  int64_t height() const { return impl_->height(); }
55

J
Jiabin Yang 已提交
56
  void set_height(int64_t height) { impl_->set_height(height); }
57

J
Jiabin Yang 已提交
58 59 60
  const paddle::framework::Vector<int64_t>& rows() const {
    return impl_->rows();
  }
61

J
Jiabin Yang 已提交
62 63 64
  paddle::framework::Vector<int64_t>* mutable_rows() {
    return impl_->mutable_rows();
  }
65 66

  void set_rows(const paddle::framework::Vector<int64_t>& rows) {
J
Jiabin Yang 已提交
67
    impl_->set_rows(rows);
68 69 70 71 72 73 74
  }

  /*
   * @brief Get the index of key in rows
   *
   * @return -1 if the key does not exists.
   */
J
Jiabin Yang 已提交
75
  int64_t Index(int64_t key) const { return impl_->Index(key); }
76 77 78 79 80 81

  /*
   * @brief whether has the specified key in the table.
   *
   * @return true if the key is exists.
   */
J
Jiabin Yang 已提交
82
  bool HasKey(int64_t key) const { return impl_->HasKey(key); }
83 84 85 86 87 88 89 90 91 92

  /*
   * @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
   */
93 94
  void Get(const DenseTensor& ids,
           DenseTensor* value,
95
           bool auto_grown = false,
J
Jiabin Yang 已提交
96 97 98
           bool is_test = false) {
    impl_->Get(ids, value, auto_grown, is_test);
  }
99

100 101
  void* AllocateFrom(Allocator* allocator,
                     DataType dtype,
J
Jiabin Yang 已提交
102 103 104
                     size_t requested_size = 0) override {
    return impl_->AllocateFrom(allocator, dtype, requested_size);
  }
105

106 107 108 109 110 111 112 113 114 115 116
  /*
   * @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.
   */
J
Jiabin Yang 已提交
117 118 119
  int64_t AutoGrownIndex(int64_t key, bool auto_grown, bool is_test = false) {
    return impl_->AutoGrownIndex(key, auto_grown, is_test);
  }
120 121 122 123 124

  /*
   * @brief Get the index of the key from id_to_index_ map.
   */
  inline int64_t GetIndexFromId(int64_t key) const {
J
Jiabin Yang 已提交
125
    return impl_->GetIndexFromId(key);
126 127
  }

J
Jiabin Yang 已提交
128
  void SyncIndex() { impl_->SyncIndex(); }
129 130 131 132
  /*
   * @brief Get complete Dims before
   */
  pten::framework::DDim GetCompleteDims() const {
J
Jiabin Yang 已提交
133
    return impl_->GetCompleteDims();
134 135
  }

136 137 138 139 140 141
  /// \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.
J
Jiabin Yang 已提交
142
  int64_t numel() const override { return impl_->numel(); };
143 144 145 146

  /// \brief Returns the dims of the tensor.
  /// \return The dims of the tensor.
  const DDim& dims() const noexcept override {
J
Jiabin Yang 已提交
147
    return impl_->dims();
148 149 150 151 152
    // return paddle::framework::make_ddim(dims);
  }

  /// \brief Returns the data type of the tensor.
  /// \return The data type of the tensor.
J
Jiabin Yang 已提交
153
  DataType dtype() const noexcept override { return impl_->dtype(); }
154 155 156

  /// \brief Returns the data layout of the tensor.
  /// \return The data layout of the tensor.
J
Jiabin Yang 已提交
157
  DataLayout layout() const noexcept override { return impl_->layout(); }
158 159 160

  /// \brief Returns the data place of the tensor.
  /// \return The data place of the tensor.
J
Jiabin Yang 已提交
161
  const Place& place() const override { return impl_->place(); };
162 163 164

  /// \brief Test whether the metadata is valid.
  /// \return Whether the metadata is valid.
J
Jiabin Yang 已提交
165
  bool valid() const noexcept override { return impl_->valid(); }
166 167 168

  /// \brief Test whether the storage is allocated.
  /// return Whether the storage is allocated.
J
Jiabin Yang 已提交
169
  bool initialized() const override { return impl_->initialized(); }
170

171
 private:
J
Jiabin Yang 已提交
172
  std::shared_ptr<pten::SelectedRowsImpl> impl_{nullptr};
173 174 175
};

}  // namespace pten