Skip to content

Commit

Permalink
[Move selected_rows PR #2] Added Selected_Rows and rw_lock to Pten (P…
Browse files Browse the repository at this point in the history
…addlePaddle#39087)

* Renamed selected_rows.* -> selected_rows_utils.*

* Added selected_rows and rw_lock to pten

* Removed useless header

* Renamed the unit test target to fix CI

* Use pten::framework::DDim

* Set selceted_rows_test properties timeout

* Polish code to pten style

Co-authored-by: Chen Weihang <chenweihang@baidu.com>
  • Loading branch information
veyron95 and chenwhql authored Jan 22, 2022
1 parent 09f6f17 commit ff7f9d0
Show file tree
Hide file tree
Showing 7 changed files with 756 additions and 1 deletion.
3 changes: 2 additions & 1 deletion paddle/pten/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ cc_library(lod_utils SRCS lod_utils.cc DEPS enforce mixed_vector)
cc_library(dense_tensor SRCS dense_tensor.cc DEPS convert_utils tensor_meta tensor_base)
cc_library(pten_device_context SRCS device_context.cc DEPS tensor_base )


cc_library(meta_tensor SRCS meta_tensor.cc DEPS tensor_base tensor_meta dense_tensor)

cc_test(unroll_array_ops_test SRCS unroll_array_ops_test.cc)
Expand All @@ -28,6 +27,8 @@ elseif(WITH_ROCM)
hip_test(dim_test SRCS dim_test.cu DEPS ddim)
endif()

cc_library(selected_rows SRCS selected_rows.cc DEPS dense_tensor mixed_vector enforce ddim)

# Will remove once we implemented MKLDNN_Tensor
if(WITH_MKLDNN)
add_dependencies(dense_tensor mkldnn)
Expand Down
208 changes: 208 additions & 0 deletions paddle/pten/core/selected_rows.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/* 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. */

#include "paddle/pten/core/selected_rows.h"

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

namespace pten {

struct ReAllocateVisitor {
ReAllocateVisitor(const pten::framework::DDim& dims,
pten::DenseTensor* tensor)
: dims_(dims), tensor_(tensor) {}

template <typename T>
void operator()() const {
pten::DenseTensor cpu_tensor;
paddle::platform::CPUPlace cpu;
T* ptr = cpu_tensor.mutable_data<T>(dims_, cpu);
const T* old_ptr =
tensor_->memory_size() == 0 ? nullptr : tensor_->data<T>();
if (old_ptr != nullptr) {
std::copy(old_ptr, old_ptr + tensor_->numel(), ptr);
}
tensor_->ShareDataWith(cpu_tensor);
}

pten::framework::DDim dims_;
pten::DenseTensor* tensor_;
};

struct TensorCopyVisitor {
TensorCopyVisitor(pten::DenseTensor* dst,
int64_t dst_offset,
const pten::DenseTensor src,
int64_t src_offset,
int64_t size)
: dst_(dst),
dst_offset_(dst_offset),
src_(src),
src_offset_(src_offset),
size_(size) {}

template <typename T>
void apply() const {
// TODO(Yancey1989): support other place
paddle::platform::CPUPlace cpu;
paddle::memory::Copy(cpu,
dst_->mutable_data<T>(cpu) + dst_offset_,
cpu,
src_.data<T>() + src_offset_,
size_ * sizeof(T));
}

pten::DenseTensor* dst_;
int64_t dst_offset_;
pten::DenseTensor src_;
int64_t src_offset_;
int64_t size_;
};

struct TensorFillVisitor {
TensorFillVisitor(pten::DenseTensor* dst,
int64_t dst_offset,
int64_t size,
float value)
: dst_(dst), dst_offset_(dst_offset), size_(size) {}

template <typename T>
void apply() const {
// TODO(qiao): support other place
paddle::platform::CPUPlace cpu;
auto* tensor_data = dst_->mutable_data<T>(cpu);
auto* start = tensor_data + dst_offset_;
auto* end = start + size_;
std::fill(start, end, static_cast<T>(0.0));
}

pten::DenseTensor* dst_;
int64_t dst_offset_;
int64_t size_;
};

bool SelectedRows::HasKey(int64_t key) const {
return std::find(rows_.begin(), rows_.end(), key) == rows_.end() ? false
: true;
}

int64_t SelectedRows::AutoGrownIndex(int64_t key,
bool auto_grown,
bool is_test) {
if (is_test) {
auto iter = id_to_index_.find(key);
if (iter == id_to_index_.end()) {
return -1;
} else {
return iter->second;
}
}

rwlock_->RDLock();
auto iter = id_to_index_.find(key);
if (iter == id_to_index_.end()) {
rwlock_->UNLock();
PADDLE_ENFORCE_EQ(auto_grown,
true,
paddle::platform::errors::NotFound(
"Input key(%lld) is not found.", key));
rwlock_->WRLock();
auto map_size = id_to_index_.size();
auto vector_size = rows_.size();
if (map_size != vector_size) {
rwlock_->UNLock();
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
"Row map size(%zu) should be equal to rows size(%zu).",
map_size,
vector_size));
}
auto write_iter = id_to_index_.find(key);
if (write_iter == id_to_index_.end()) {
int row_num = rows_.size();
if (row_num == value_->dims()[0]) {
rwlock_->UNLock();
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
"Selected rows is full, then length exceed the length of first "
"dimension (%d).",
row_num));
}
// key logic to put a key into id_to_index_
rows_.push_back(key);
auto index = static_cast<int64_t>(rows_.size() - 1);
id_to_index_[key] = index;
rwlock_->UNLock();
return index;
} else {
auto index = write_iter->second;
rwlock_->UNLock();
return index;
}
} else {
auto index = iter->second;
rwlock_->UNLock();
return index;
}
}

void SelectedRows::SyncIndex() {
rwlock_->WRLock();
id_to_index_.clear();
for (size_t i = 0; i < rows_.size(); ++i) {
id_to_index_[rows_[i]] = i;
}
rwlock_->UNLock();
}

void SelectedRows::Get(const pten::DenseTensor& ids,
pten::DenseTensor* value,
bool auto_grown,
bool is_test) {
PADDLE_ENFORCE_EQ(value->IsInitialized(),
true,
paddle::platform::errors::InvalidArgument(
"The value tensor is not initialized."));
if (ids.numel() == 0) {
VLOG(3) << "keys is empty, please check data!";
} else {
int64_t value_width = value_->numel() / value_->dims()[0];
PADDLE_ENFORCE_EQ(
value_width,
value->numel() / value->dims()[0],
paddle::platform::errors::InvalidArgument(
"Output tensor should have the same shape with table "
"except the first dimmension, excepted value width not counting "
"the first dimension is %d, actual value width is %d.",
value_width,
value->numel() / value->dims()[0]));
for (int i = 0; i < ids.numel(); ++i) {
auto id = ids.data<int64_t>()[i];
int64_t index = AutoGrownIndex(id, auto_grown, is_test);
if (index < 0) {
VLOG(5) << "id " << id << " not in the table, return 0";
paddle::framework::VisitDataType(
value_->type(),
TensorFillVisitor(value, i * value_width, value_width, 0.0));
} else {
paddle::framework::VisitDataType(value_->type(),
TensorCopyVisitor(value,
i * value_width,
*value_.get(),
index * value_width,
value_width));
}
}
}
}
} // namespace pten
164 changes: 164 additions & 0 deletions paddle/pten/core/selected_rows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* 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"
#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"
#include "paddle/fluid/platform/enforce.h"

namespace pten {
class SelectedRows {
/*
* @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) {
value_.reset(new pten::DenseTensor());
rwlock_.reset(new RWLock);
}

SelectedRows() {
height_ = 0;
value_.reset(new pten::DenseTensor());
rwlock_.reset(new RWLock);
}

const pten::Place& place() const { return value_->place(); }

const pten::DenseTensor& value() const { return *value_; }

pten::DenseTensor* mutable_value() { return value_.get(); }

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
*/
void Get(const pten::DenseTensor& ids,
pten::DenseTensor* value,
bool auto_grown = false,
bool is_test = false);

/*
* @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);
}

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
std::unique_ptr<pten::DenseTensor> value_{nullptr};
int64_t height_; // height indicates the underline tensor's height
std::unique_ptr<RWLock> rwlock_{nullptr};
};

} // namespace pten
Loading

0 comments on commit ff7f9d0

Please sign in to comment.