Skip to content

Commit

Permalink
[PTen] Remove offset in storage (#38472)
Browse files Browse the repository at this point in the history
* remove offset in storage

* revert api change

* fix custom op slice bug

* fix mutable_data error
  • Loading branch information
chenwhql committed Dec 30, 2021
1 parent 3f6229c commit a504ff3
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void ScaleAPI(const egr::EagerTensor& x, float scale, float bias,
SizeOf(dense_tensor->dtype());
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
paddle::memory::Alloc(place, bytes_size), 0),
paddle::memory::Alloc(place, bytes_size)),
std::move(tensor_meta));
// Handle Device Context
const paddle::platform::Place& expected_kernel_place =
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/eager/tests/task_tests/fwd_bwd_joint_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ egr::EagerTensor hook_function(const egr::EagerTensor& t) {
paddle::framework::product(t_dense->dims()) * SizeOf(t_dense->dtype());
auto ret_dense = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
paddle::memory::Alloc(place, bytes_size), 0),
paddle::memory::Alloc(place, bytes_size)),
std::move(ret_meta));

float* t_ptr = t_dense->mutable_data<float>();
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/eager/tests/task_tests/hook_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ egr::EagerTensor hook_function(const egr::EagerTensor& t) {
paddle::framework::product(t_dense->dims()) * SizeOf(t_dense->dtype());
auto ret_dense = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
paddle::memory::Alloc(place, bytes_size), 0),
paddle::memory::Alloc(place, bytes_size)),
std::move(ret_meta));

float* t_ptr = t_dense->mutable_data<float>();
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/framework/custom_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx,
"Tensors.",
vec_true_outs.size(), outs.size()));
for (size_t j = 0; j < vec_true_outs.size(); ++j) {
experimental::MovesSharedStorage(
experimental::SharesStorage(
std::dynamic_pointer_cast<pten::DenseTensor>(outs.at(j).impl())
.get(),
vec_true_outs.at(j));
}
} else {
auto* true_out = ctx.Output<Tensor>(out_name);
experimental::MovesSharedStorage(
experimental::SharesStorage(
std::dynamic_pointer_cast<pten::DenseTensor>(outs.at(i).impl())
.get(),
true_out);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class Tensor {

const std::shared_ptr<memory::Allocation>& Holder() const { return holder_; }
size_t offset() const { return offset_; }
void set_offset(size_t offset) { offset_ = offset; }

std::shared_ptr<memory::Allocation> MoveMemoryHolder() {
return std::move(holder_);
Expand Down
3 changes: 1 addition & 2 deletions paddle/fluid/operators/reshape_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ class ReshapeKernel {
// non-inplace need move all result from pt_out to out, inplace need set
// result dims.
if (in != out) {
paddle::experimental::MovesSharedStorage(pt_out,
static_cast<Tensor *>(out));
paddle::experimental::SharesStorage(pt_out, static_cast<Tensor *>(out));
} else {
out->Resize(pt_out->dims());
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/api/include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class PADDLE_API Tensor final {
* The index number begins from begin_idx + 1.
* @return Tensor
*/
Tensor slice(const int64_t begin_idx, const int64_t end_idx) const;
Tensor slice(int64_t begin_idx, int64_t end_idx) const;

/**
* @brief Return the implemention of current Tensor.
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/api/lib/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ template PADDLE_API paddle::platform::float16 *
Tensor::data<paddle::platform::float16>();

// TODO(chenweihang): replace slice impl by API
Tensor Tensor::slice(const int64_t begin_idx, const int64_t end_idx) const {
Tensor Tensor::slice(int64_t begin_idx, int64_t end_idx) const {
if (is_dense_tensor()) {
return Tensor(std::make_shared<pten::DenseTensor>(
std::move(pten::CompatibleDenseTensorUtils::Slice(
std::dynamic_pointer_cast<pten::DenseTensor>(impl_).get(),
*(std::dynamic_pointer_cast<pten::DenseTensor>(impl_).get()),
begin_idx,
end_idx))));
} else {
Expand Down
10 changes: 2 additions & 8 deletions paddle/pten/api/lib/utils/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class ExternalStorage : public pten::Storage {
void Clear() override {
data_ = nullptr;
size_ = 0;
offset_ = 0;
}

size_t size() const noexcept override { return size_; }
Expand All @@ -57,13 +56,11 @@ class ExternalStorage : public pten::Storage {
class SharedStorage : public pten::Storage {
public:
explicit SharedStorage(
const std::shared_ptr<paddle::memory::Allocation>& allocation,
size_t offset)
const std::shared_ptr<paddle::memory::Allocation>& allocation)
: Storage(allocation) {
CHECK(allocation);
place_ = allocation->place();
size_ = allocation->size();
offset_ = offset;
}

// In order to be compatible with the original Tensor design and execution
Expand All @@ -84,7 +81,6 @@ class SharedStorage : public pten::Storage {
void Clear() override {
data_ = nullptr;
size_ = 0;
offset_ = 0;
}

size_t size() const noexcept override { return size_; }
Expand All @@ -96,12 +92,10 @@ class SharedStorage : public pten::Storage {
}

// Temporary method: For compatible with fluid Tensor and improve performance
void ResetAllocation(std::shared_ptr<paddle::memory::Allocation> allocation,
size_t offset) {
void ResetAllocation(std::shared_ptr<paddle::memory::Allocation> allocation) {
data_ = allocation;
size_ = allocation->size();
place_ = allocation->place();
offset_ = offset;
}

// Temporary method: For compatible with fluid Tensor and improve performance
Expand Down
Loading

0 comments on commit a504ff3

Please sign in to comment.