Skip to content

Commit

Permalink
updates the ctor of tensor, test=develop (PaddlePaddle#38946)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shixiaowei02 committed Jan 15, 2022
1 parent d13c779 commit 5c35867
Show file tree
Hide file tree
Showing 45 changed files with 175 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,27 @@ TEST(AccumulationNode, EagerTensor) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT16, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt0 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
dt0->mutable_data<paddle::platform::float16>()[0] = 10.0;
EagerTensor et0 = EagerTensor(dt0);

std::shared_ptr<pten::DenseTensor> dt1 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);

dt1->mutable_data<paddle::platform::float16>()[0] = 20.0;
EagerTensor et1 = EagerTensor(dt1);

std::shared_ptr<pten::DenseTensor> grad_dt =
std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
EagerTensor grad_et = EagerTensor(grad_dt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ TEST(AutogradMeta, MemberFunction) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 5.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ TEST(EagerTensor, Constructor) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 5.0f;
Expand Down Expand Up @@ -65,8 +66,9 @@ TEST(EagerTensor, MemberFunction) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 5.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ TEST(GradNodeInfo, GradNodeBase) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 5.0f;
Expand Down Expand Up @@ -97,8 +98,9 @@ TEST(GradNodeInfo, GradNodeBase) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 6.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class GradTestNode : public egr::GradNodeBase {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 6.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ TEST(GradTensorHolder, Constructor) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({2, 2}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
EagerTensor et = EagerTensor(dt);

Expand All @@ -52,15 +53,17 @@ TEST(GradTensorHolder, Interfaces) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt0 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
dt0->mutable_data<float>()[0] = 10.0;
EagerTensor et0 = EagerTensor(dt0);

std::shared_ptr<pten::DenseTensor> dt1 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
dt1->mutable_data<float>()[0] = 20.0;
EagerTensor et1 = EagerTensor(dt1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ TEST(TensorWrapper, Basic) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2}));
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<float>();
dt_ptr[0] = 5.0f;
Expand All @@ -51,8 +52,9 @@ TEST(TensorWrapper, Basic) {
pten::DenseTensorMeta meta2 = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2}));
std::shared_ptr<pten::DenseTensor> dt2 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta2);
auto* dt_ptr2 = dt->mutable_data<float>();
dt_ptr2[0] = 6.0f;
Expand Down
15 changes: 9 additions & 6 deletions paddle/fluid/eager/tests/task_tests/eager_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ TEST(EagerUtils, AutoGradMeta) {
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
std::shared_ptr<pten::DenseTensor> dt0 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
dt0->mutable_data<float>()[0] = 10.0;
EagerTensor et0 = EagerTensor(dt0);

std::shared_ptr<pten::DenseTensor> dt1 = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
dt1->mutable_data<float>()[0] = 20.0;
EagerTensor et1 = EagerTensor(dt1);
Expand Down Expand Up @@ -106,8 +108,9 @@ egr::EagerTensor CreateTestCPUTensor(T val,
pten::DenseTensorMeta(pten::DataType::FLOAT32, ddim);
egr::EagerTensor tensor;
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>(
std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace()),
std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace())
.get(),
meta);
auto* dt_ptr = dt->mutable_data<T>();
for (int64_t i = 0; i < dt->numel(); i++) {
Expand Down
16 changes: 3 additions & 13 deletions paddle/pten/api/lib/utils/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,15 @@ limitations under the License. */
namespace paddle {
namespace experimental {

class DefaultAllocator : public pten::deprecated::Allocator {
class DefaultAllocator : public pten::Allocator {
public:
using Allocation = pten::deprecated::Allocation;
explicit DefaultAllocator(const paddle::platform::Place& place)
: place_(place) {}

static void Delete(Allocation* allocation) {
paddle::memory::allocation::Allocator::AllocationDeleter(
allocation->CastContextWithoutCheck<paddle::memory::Allocation>());
AllocationPtr Allocate(size_t bytes_size) override {
return memory::Alloc(place_, bytes_size);
}

Allocation Allocate(size_t bytes_size) override {
paddle::memory::AllocationPtr a = memory::Alloc(place_, bytes_size);
void* ptr = a->ptr();
return Allocation(ptr, a.release(), &Delete, place_);
}

const paddle::platform::Place& place() override { return place_; }

private:
paddle::platform::Place place_;
};
Expand Down
6 changes: 2 additions & 4 deletions paddle/pten/core/dense_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ limitations under the License. */

namespace pten {

DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a,
const DenseTensorMeta& meta)
DenseTensor::DenseTensor(Allocator* a, const DenseTensorMeta& meta)
: meta_(meta),
storage_(make_intrusive<TensorStorage>(a, SizeOf(dtype()) * numel())) {}

DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a,
DenseTensorMeta&& meta)
DenseTensor::DenseTensor(Allocator* a, DenseTensorMeta&& meta)
: meta_(std::move(meta)),
storage_(make_intrusive<TensorStorage>(a, SizeOf(dtype()) * numel())) {}

Expand Down
6 changes: 2 additions & 4 deletions paddle/pten/core/dense_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ class TensorInplaceVersion {
class DenseTensor : public TensorBase,
public TypeInfoTraits<TensorBase, DenseTensor> {
public:
using Allocator = deprecated::Allocator;

/// \brief Construct a dense tensor and allocate space.
/// \param a The allocator used to allocate space.
/// \param meta The meta data of dense tensor.
DenseTensor(const std::shared_ptr<Allocator>& a, const DenseTensorMeta& meta);
DenseTensor(Allocator* a, const DenseTensorMeta& meta);

/// \brief Construct a dense tensor and allocate space.
/// \param a The allocator used to allocate space.
/// \param meta The meta data of dense tensor.
DenseTensor(const std::shared_ptr<Allocator>& a, DenseTensorMeta&& meta);
DenseTensor(Allocator* a, DenseTensorMeta&& meta);

/// \brief Use existing storage space to create dense tensor. This interface
/// can be used to deliberately create an uninitialized dense tensor.
Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/core/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace pten {

void TensorStorage::Realloc(size_t size) {
this->Clear();
data_ = paddle::memory::AllocShared(alloc_->place(), size);
data_ = alloc_->Allocate(size);
size_ = size;
}

Expand Down
19 changes: 6 additions & 13 deletions paddle/pten/core/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ class Storage : public intrusive_ref_counter<Storage> {
class TensorStorage : public Storage {
public:
using Place = paddle::platform::Place;
using Allocator = deprecated::Allocator;

explicit TensorStorage(const std::shared_ptr<Allocator>& a) : alloc_(a) {}
explicit TensorStorage(Allocator* a) : alloc_(a) {}

TensorStorage(const std::shared_ptr<Allocator>& a, size_t size)
: Storage(paddle::memory::AllocShared(a->place(), size)), alloc_(a) {
TensorStorage(Allocator* a, size_t size)
: Storage(a->Allocate(size)), alloc_(a) {
size_ = data_->size();
}

Expand All @@ -114,24 +113,18 @@ class TensorStorage : public Storage {
size_t size() const noexcept override { return size_; }

const Place& place() const override {
if (!data_ && !alloc_) {
if (!data_) {
PADDLE_THROW(paddle::platform::errors::Unimplemented(
"Unable to visit place: either data_ or alloc_ has to be initialized "
"first."));
}
if (data_) {
return data_->place();
}
return alloc_->place();
return data_->place();
}

bool OwnsMemory() const noexcept override { return true; }
const std::shared_ptr<Allocator>& allocator() const noexcept {
return alloc_;
}

private:
const std::shared_ptr<Allocator> alloc_;
Allocator* alloc_;
int64_t size_{0};
};

Expand Down
2 changes: 0 additions & 2 deletions paddle/pten/tests/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ else()
endif()

cc_test(test_pten_exception SRCS test_pten_exception.cc DEPS gtest)
cc_test(test_framework_storage SRCS test_storage.cc DEPS pten_api_utils)
cc_test(test_framework_tensor_utils SRCS test_tensor_utils.cc DEPS pten_api_utils)

cc_test(test_mean_api SRCS test_mean_api.cc DEPS pten_tensor pten_api pten_api_utils)
cc_test(test_dot_api SRCS test_dot_api.cc DEPS pten_tensor pten_api pten_api_utils)
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/tests/api/test_cast_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim;
// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, cast) {
// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
auto dense_x = std::make_shared<pten::DenseTensor>(
alloc,
alloc.get(),
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 4}),
pten::DataLayout::NCHW));
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/tests/api/test_conj_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim;
// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, conj) {
// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
auto dense_x = std::make_shared<pten::DenseTensor>(
alloc,
alloc.get(),
pten::DenseTensorMeta(pten::DataType::COMPLEX64,
framework::make_ddim({3, 10}),
pten::DataLayout::NCHW));
Expand Down
6 changes: 3 additions & 3 deletions paddle/pten/tests/api/test_dot_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ using DDim = paddle::framework::DDim;
// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, dot) {
// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
auto dense_x = std::make_shared<pten::DenseTensor>(
alloc,
alloc.get(),
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 10}),
pten::DataLayout::NCHW));
auto* dense_x_data = dense_x->mutable_data<float>();

auto dense_y = std::make_shared<pten::DenseTensor>(
alloc,
alloc.get(),
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 10}),
pten::DataLayout::NCHW));
Expand Down
Loading

0 comments on commit 5c35867

Please sign in to comment.