Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Intermediate Kernel API for refactor Tensor Lib #36914

Merged
merged 150 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
3f545c4
initial tensor design & sign kernel demo
chenwhql Jul 9, 2021
1f4ea40
add move constructor for meta & add lodtensor
chenwhql Jul 12, 2021
44bf926
add dirs & sign xpu kernel
chenwhql Jul 12, 2021
b20689d
add mean cpu&cuda kernel impl
chenwhql Jul 15, 2021
79d2a1a
move sign & mean xpu & npu kernel
chenwhql Jul 15, 2021
434136f
add selected_rows basic impl
chenwhql Jul 16, 2021
6c6ee22
refactor design, BaseTensor to DenseTensor, etc.
chenwhql Jul 27, 2021
013c3fb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Jul 27, 2021
33bba06
add scale mkldnn kernel
chenwhql Jul 28, 2021
d895a11
polish xpu & npu impl details
chenwhql Jul 28, 2021
62ebf01
fix mkldnn reuse compile failed
chenwhql Jul 29, 2021
7c09726
change tensor operation lib name
chenwhql Jul 29, 2021
7ae7f2f
resolve conflit with develop
chenwhql Jul 29, 2021
288efc2
rename util filename
chenwhql Jul 29, 2021
be3ddd5
add more comments
chenwhql Jul 29, 2021
3386c49
change TensorImplInterface to TensorInterface
chenwhql Jul 30, 2021
4ef6be5
add kernel key and factory
chenwhql Aug 4, 2021
b69066e
remove MKLDNNTensorMeta, add MKLDNNDenseTensor
chenwhql Aug 4, 2021
1d4f90e
resolve conflict with develop
chenwhql Aug 4, 2021
c732d57
change XXDeviceContext to XXContext
chenwhql Aug 5, 2021
374345f
add base kernel registrar utils & test on sign
chenwhql Aug 16, 2021
bbb6473
resolve conflict with develop
chenwhql Aug 16, 2021
0e18ff4
replace boost::any by paddle::any
chenwhql Aug 16, 2021
805896b
fix several ci failed
chenwhql Aug 17, 2021
fc4442b
fix npu compile error
chenwhql Aug 17, 2021
cefe30a
add ordered map util
chenwhql Aug 17, 2021
aa3e79b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 17, 2021
a1753a0
fix multiple ordered_map compile errors
chenwhql Aug 17, 2021
05a82e7
move dev into include dir
chenwhql Aug 18, 2021
90e9090
support sign op in static op run
chenwhql Aug 19, 2021
a94eefd
fix static op run error
chenwhql Aug 23, 2021
19da152
resolve confilt with develop
chenwhql Aug 23, 2021
021a505
fix new executor compile failed
chenwhql Aug 23, 2021
f24e45e
add dygraph branch & remove sign_op.h
chenwhql Aug 25, 2021
44acc84
fix test_infer_no_need_buffer_slots
chenwhql Aug 26, 2021
2b66ab4
fix rocm compile link error
chenwhql Aug 26, 2021
2a5ce9b
fix unitybuild error & clear glog
chenwhql Aug 26, 2021
39b7d06
fix npu compile failed
chenwhql Aug 26, 2021
d4dec61
skip quant trans test
chenwhql Aug 26, 2021
461f146
fix part windows compile problem
chenwhql Aug 26, 2021
35aee9a
Merge branch 'develop' into op2func_refactor
chenwhql Aug 26, 2021
ddfbbdd
fix xpu enforce error
chenwhql Aug 26, 2021
57bcd67
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 26, 2021
7d82352
fix inference test failed
chenwhql Aug 27, 2021
d55bb4b
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 27, 2021
d9476dd
Merge branch 'develop' into op2func_refactor
chenwhql Aug 27, 2021
193ee9d
remove ordered_map to solve quant failed
chenwhql Aug 30, 2021
f2db581
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 30, 2021
db6ff09
fix part of rcom compile faild
chenwhql Aug 30, 2021
80bf6b8
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 30, 2021
9031ab3
add more register kernels
chenwhql Aug 31, 2021
f7bbaca
revert scale kernel temporarily
chenwhql Sep 3, 2021
cea19d0
Merge branch 'develop' into op2func_refactor
chenwhql Sep 3, 2021
568bebd
fix code format error
chenwhql Sep 6, 2021
0eedc92
add new kernel registrar marco
chenwhql Sep 7, 2021
509d13e
rename top to tcmpt
chenwhql Sep 7, 2021
7146f92
revert xpu, npu, mkldnn impl & remove op def
chenwhql Sep 7, 2021
321b141
add kernel args parse functor to auto parse args
chenwhql Sep 8, 2021
57a14c6
resolve confilt with develop
chenwhql Sep 8, 2021
c3ebfea
revert some change & add scale kernels
chenwhql Sep 9, 2021
b67de9c
add op proto in dygraph kernelcontext building
chenwhql Sep 9, 2021
13c02aa
polish kernel dispatch logic & nameing rule
chenwhql Sep 10, 2021
1987ce9
fix scale kernel match error
chenwhql Sep 10, 2021
33a4c41
fix scale test failed
chenwhql Sep 10, 2021
c32fde9
add mean API and unittest
chenwhql Sep 13, 2021
a4e53ef
test mean api success
chenwhql Sep 17, 2021
1d9f33f
add branch to solve compiled error
chenwhql Sep 18, 2021
b0cf02c
skip clang format error
chenwhql Sep 18, 2021
95a612e
add mean skip rule in op_library
chenwhql Sep 18, 2021
83d6f77
add dot kernel, api and unittest (#6)
MingMingShangTian Sep 18, 2021
dad5e61
remove old kernel and add symbol link
chenwhql Sep 18, 2021
027f0b2
resolve conflit with tianyu
chenwhql Sep 18, 2021
8add5e4
fix dot compiled failed
chenwhql Sep 18, 2021
01b5ded
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 18, 2021
71a3403
add merco for module declare
chenwhql Sep 22, 2021
4663033
fix npu and xpu compile error
chenwhql Sep 22, 2021
be15b02
revert sign, mean, scale, dot kernel removing
chenwhql Sep 23, 2021
8371096
add comment for keeping old kernel impl
chenwhql Sep 23, 2021
f1f6c8e
fix mutable_data error
chenwhql Sep 23, 2021
5547b44
fix bfloat16 conflit
chenwhql Sep 24, 2021
dd3323d
fix inference undef error
chenwhql Sep 24, 2021
65e68c6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 24, 2021
caaed19
adapt to msvc compile rules
chenwhql Sep 26, 2021
c9a3f38
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 26, 2021
46b7762
polish comment for template inst
chenwhql Sep 26, 2021
4253f49
add cmake template instantiation for win
chenwhql Sep 27, 2021
4e871ea
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 28, 2021
395a50f
Merge branch 'develop' into op2func_refactor
chenwhql Sep 29, 2021
817f052
fix backend to place device id bug
chenwhql Sep 29, 2021
7f640a6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 29, 2021
bf0f99b
fix ifdef error
chenwhql Sep 29, 2021
73de891
Op2functor (#7)
MingMingShangTian Sep 30, 2021
e9b219d
fill_any_like kernel refactor (#10)
zyfncg Sep 30, 2021
9789890
skip dtype for fill_any_like
chenwhql Oct 11, 2021
9b33270
add attrs for kernel key constrcut
chenwhql Oct 11, 2021
aa6ed57
add use_pt_kernel Flags to control whether to use pt kernel (#13)
MingMingShangTian Oct 12, 2021
9db8e4a
fix mutable_data cuda place error
chenwhql Oct 12, 2021
12c1178
Merge branch 'develop' into op2func_refactor
chenwhql Oct 12, 2021
c882b5c
move high level apis into hapi
chenwhql Oct 13, 2021
e30ca2a
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 13, 2021
46ba70c
remove selectedrows adapting temporarily
chenwhql Oct 14, 2021
073aef3
Support Scalar in Tensor Compute Library (#14)
zyfncg Oct 14, 2021
06789ba
resolve conflit with yunfei
chenwhql Oct 14, 2021
3f5f789
remove mkldnn tensor & polish details
chenwhql Oct 14, 2021
2309149
use flat_hash_map and small_vector in kernel factory
chenwhql Oct 15, 2021
6ce92e5
Refactor flatten kernel (#12)
YuanRisheng Oct 15, 2021
e0322d5
Revert "use flat_hash_map and small_vector in kernel factory"
chenwhql Oct 15, 2021
d3ab655
Move cpu, cuda and other device code into kernels (#15)
zyfncg Oct 15, 2021
ddc7de8
Perfect unitests (#16)
YuanRisheng Oct 18, 2021
37791f7
replace with flat_hash_map, small_vector (#19)
MingMingShangTian Oct 18, 2021
28a6374
Perfect unitests (#20)
YuanRisheng Oct 18, 2021
e3e2b50
refactor execution adapting impl
chenwhql Oct 18, 2021
e0710fd
resolve conflits
chenwhql Oct 18, 2021
ff19bd0
fix insert conflit
chenwhql Oct 19, 2021
1dd0145
Fix CI bug of test_yolov3 (#21)
zyfncg Oct 19, 2021
b77d1ee
add the tensor base class, test=develop (#17)
Shixiaowei02 Oct 19, 2021
320b5f1
[no-verify] commit backend and tensor signature changes
chenwhql Oct 19, 2021
5b2999f
resolve conflit with xiaowei
chenwhql Oct 19, 2021
466ce03
Rename tcmpt to pten (#23)
zyfncg Oct 20, 2021
beec280
remove k of all enum var
chenwhql Oct 20, 2021
a49fd44
resolve conflit with yunfei
chenwhql Oct 20, 2021
373f9c1
Merge branch 'develop' into op2func_refactor
chenwhql Oct 20, 2021
ce210b4
remove kernel_instantiate (#26)
MingMingShangTian Oct 20, 2021
4e71d15
remove symbols and spatial_tensor
chenwhql Oct 20, 2021
04cf058
change common to functions
chenwhql Oct 20, 2021
ab8db2d
readd share tensor impl methods
chenwhql Oct 20, 2021
f1c9661
add a candidate dense tensor class, test=develop (#28)
Shixiaowei02 Oct 20, 2021
d3674e9
change all Pt to Pten
chenwhql Oct 20, 2021
4e2c0dd
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 20, 2021
bbe59bc
resolve conflit with xiaowei
chenwhql Oct 21, 2021
76a588e
Op2functor opt1 (#27)
MingMingShangTian Oct 21, 2021
fb224ab
polish kernel factory and kernel registry
chenwhql Oct 21, 2021
252fb79
fix operator test error msg mismatch
chenwhql Oct 22, 2021
19b1095
remove tensor signature and backend set member
chenwhql Oct 22, 2021
24ef6c5
move scalar and polish enforce
chenwhql Oct 22, 2021
1685b67
revert dtype layout change to fix error
chenwhql Oct 22, 2021
7b7e988
fix enum operator override error
chenwhql Oct 22, 2021
7c41b15
Add Intermediate API layer
YuanRisheng Oct 22, 2021
52fead0
add several base unittests
chenwhql Oct 22, 2021
2ff2721
add pten utils tests
chenwhql Oct 23, 2021
e3ed2c6
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Oct 23, 2021
b5c77e5
polish some details
chenwhql Oct 24, 2021
471ae40
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
YuanRisheng Oct 25, 2021
5240ac0
Dev/op2func refactor 3 (#30)
chenwhql Oct 26, 2021
5fb285c
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 26, 2021
e2731a0
Merge from op2refactor
YuanRisheng Oct 26, 2021
16e6bf1
intermediate api adapt to new dense tensor
YuanRisheng Oct 26, 2021
dfec7a0
Merge From Develop
YuanRisheng Nov 1, 2021
3b2e950
Merge branch 'develop' of github.com:YuanRisheng/Paddle into flatten_…
YuanRisheng Nov 2, 2021
aa8a3dd
add some TODO and delete include header
YuanRisheng Nov 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ limitations under the License. */
#include "paddle/fluid/framework/data_type_transform.h"
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/op_call_stack.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/transfer_scope_cache.h"
#include "paddle/fluid/framework/unused_var_check.h"
Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/imperative/prepared_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "paddle/fluid/framework/data_type_transform.h"
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/imperative/infer_shape_context.h"
#include "paddle/pten/common/scalar.h"
#include "paddle/utils/small_vector.h"
Expand Down
21 changes: 21 additions & 0 deletions paddle/pten/api/include/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,26 @@

#pragma once

#include "paddle/pten/api/include/infershape.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"
#include "paddle/pten/kernels/cpu/creation.h"
#include "paddle/pten/kernels/cuda/creation.h"

namespace pten {

// TODO(YuanRisheng) This function name should be same as User API name.
// TODO(zyfncg) Automatic code generation
template <typename T, typename ContextT>
DenseTensor FillAnyLike(const ContextT& dev_ctx,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[TODO] 后续我们需要解决这个问题,API和Kernel函数命名尽可能一致,不参照原先op的命名

const DenseTensor& x,
const Scalar& val) {
auto out_meta = UnchangedInferShape(x.meta());
const auto allocator =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[TODO] 这里后续我改为全局单例,减小开销

std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
FillAnyLike<T>(dev_ctx, x, val, &dense_out);
return dense_out;
}

} // namespace pten
19 changes: 19 additions & 0 deletions paddle/pten/api/include/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,24 @@
#pragma once

// See Note: [ How do we organize the kernel directory ]
#include "paddle/pten/api/include/infershape.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"
#include "paddle/pten/kernels/cpu/linalg.h"
#include "paddle/pten/kernels/cuda/linalg.h"

namespace pten {

template <typename T, typename ContextT>
DenseTensor Dot(const ContextT& dev_ctx,
const DenseTensor& x,
const DenseTensor& y) {
auto out_meta = DotInferShape(x.meta(), y.meta());
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
Dot<T>(dev_ctx, x, y, &dense_out);
return dense_out;
}

} // namespace pten
20 changes: 20 additions & 0 deletions paddle/pten/api/include/manipulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,25 @@
#pragma once

// See Note: [ How do we organize the kernel directory ]
#include "paddle/pten/api/include/infershape.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"
#include "paddle/pten/kernels/cpu/manipulation.h"
#include "paddle/pten/kernels/cuda/manipulation.h"

namespace pten {

template <typename T, typename ContextT>
DenseTensor Flatten(const ContextT& dev_ctx,
const DenseTensor& x,
int start_axis,
int stop_axis) {
auto out_meta = FlattenInferShape(x.meta(), start_axis, stop_axis);
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
Flatten<T>(dev_ctx, x, start_axis, stop_axis, &dense_out);
return dense_out;
}

} // namespace pten
57 changes: 57 additions & 0 deletions paddle/pten/api/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,62 @@ limitations under the License. */
#pragma once

// See Note: [ How do we organize the kernel directory ]
#include "paddle/pten/api/include/infershape.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"
#include "paddle/pten/kernels/cpu/math.h"
#include "paddle/pten/kernels/cuda/math.h"

namespace pten {

template <typename T, typename ContextT>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[TODO] 后续这里的代码也需要自动生成,需要综合考虑 @zyfncg

DenseTensor Sign(const ContextT& dev_ctx, const DenseTensor& x) {
auto out_meta = UnchangedInferShape(x.meta());
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
Sign<T>(dev_ctx, x, &dense_out);
return dense_out;
}

template <typename T, typename ContextT>
DenseTensor Mean(const ContextT& dev_ctx, const DenseTensor& x) {
auto out_meta = ReductionInferShape(x.meta());
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
Mean<T>(dev_ctx, x, &dense_out);
return dense_out;
}

template <typename T, typename ContextT>
DenseTensor Scale(const ContextT& dev_ctx,
const DenseTensor& x,
float scale,
float bias,
bool bias_after_scale) {
auto out_meta = UnchangedInferShape(x.meta());
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
Scale<T>(dev_ctx, x, scale, bias, bias_after_scale, &dense_out);
return dense_out;
}

template <typename T, typename ContextT>
DenseTensor Scale(const ContextT& dev_ctx,
const DenseTensor& x,
const DenseTensor& scale,
float bias,
bool bias_after_scale) {
auto out_meta = UnchangedInferShape(x.meta());
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
dev_ctx.GetPlace());
pten::DenseTensor dense_out(allocator, out_meta);
ScaleHost<T>(dev_ctx, x, scale, bias, bias_after_scale, &dense_out);
return dense_out;
}
} // namespace pten
1 change: 1 addition & 0 deletions paddle/pten/hapi/lib/utils/tensor_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ std::unique_ptr<pten::DenseTensor> MakePtenDenseTensor(
SetLoD(&meta.lod, src.lod());
auto shared_storage =
pten::make_intrusive<SharedStorage>(src.Holder(), src.offset());

return std::make_unique<pten::DenseTensor>(std::move(shared_storage),
std::move(meta));
}
Expand Down
5 changes: 2 additions & 3 deletions paddle/pten/kernels/cpu/manipulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ void Flatten(const CPUContext& dev_ctx,
int start_axis,
int stop_axis,
DenseTensor* out) {
auto out_meta = FlattenInferShape(x.meta(), start_axis, stop_axis);
auto out_dims = out->dims();
pten::Copy(dev_ctx, x, out);
out->set_lod(out_meta.lod);
out->Resize(out_meta.dims);
out->Resize(out_dims);
}

// TODO(yuanrisheng): this kernel is for training and xshape is a Intermediate
Expand Down
5 changes: 2 additions & 3 deletions paddle/pten/kernels/cuda/manipulation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ void Flatten(const CUDAContext& dev_ctx,
int start_axis,
int stop_axis,
DenseTensor* out) {
auto out_meta = FlattenInferShape(x.meta(), start_axis, stop_axis);
auto out_dims = out->dims();
pten::Copy(dev_ctx, x, out);
out->set_lod(out_meta.lod);
out->Resize(out_meta.dims);
out->Resize(out_dims);
}

// TODO(yuanrisheng): this kernel is for training and xshape is a Intermediate
Expand Down
1 change: 1 addition & 0 deletions paddle/pten/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cc_test(test_matmul_api SRCS test_matmul_api.cc DEPS linalg_api pten_hapi_utils)
cc_test(test_fill_api SRCS test_fill_api.cc DEPS creation_api pten_hapi_utils)
cc_test(test_copy_api SRCS test_copy_api.cc DEPS utils_cpu pten_hapi_utils)
cc_test(test_flatten_api SRCS test_flatten_api.cc DEPS utils_cpu manipulation_api pten_hapi_utils)
cc_test(test_scale_api SRCS test_scale_api.cc DEPS math_api pten_hapi_utils)
54 changes: 54 additions & 0 deletions paddle/pten/tests/test_dot_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ limitations under the License. */
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"

#include "paddle/pten/api/include/linalg.h"

PT_DECLARE_MODULE(LinalgCPU);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
Expand Down Expand Up @@ -82,3 +84,55 @@ TEST(API, dot) {
ASSERT_NEAR(expect_result[1], actual_result1, 1e-6f);
ASSERT_NEAR(expect_result[2], actual_result2, 1e-6f);
}

// TODO(YuanRisheng) This unitest should be created in other file.
// It is convenient to make compilation decoupling.
TEST(DEV_API, dot) {
Copy link
Contributor

@chenwhql chenwhql Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[TODO] 外部API的单测和内部API的单测需要分开管理,便于编译解耦

// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
pten::DenseTensor dense_x(alloc,
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 10}),
pten::DataLayout::NCHW));
auto* dense_x_data = dense_x.mutable_data<float>();

pten::DenseTensor dense_y(alloc,
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 10}),
pten::DataLayout::NCHW));
auto* dense_y_data = dense_y.mutable_data<float>();

float sum[3] = {0.0, 0.0, 0.0};
for (size_t i = 0; i < 3; ++i) {
for (size_t j = 0; j < 10; ++j) {
dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0;
dense_y_data[i * 10 + j] = (i * 10 + j) * 1.0;
sum[i] += (i * 10 + j) * (i * 10 + j) * 1.0;
}
}

paddle::platform::DeviceContextPool& pool =
paddle::platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(paddle::platform::CPUPlace());

// 2. test API
auto out = pten::Dot<float>(
*(static_cast<paddle::platform::CPUDeviceContext*>(dev_ctx)),
dense_x,
dense_y);

// 3. check result
ASSERT_EQ(out.dims().size(), 2);
ASSERT_EQ(out.dims()[0], 3);
ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32);
ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);

auto expect_result = sum;
auto actual_result0 = out.data<float>()[0];
auto actual_result1 = out.data<float>()[1];
auto actual_result2 = out.data<float>()[2];
ASSERT_NEAR(expect_result[0], actual_result0, 1e-6f);
ASSERT_NEAR(expect_result[1], actual_result1, 1e-6f);
ASSERT_NEAR(expect_result[2], actual_result2, 1e-6f);
}
37 changes: 37 additions & 0 deletions paddle/pten/tests/test_fill_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ limitations under the License. */
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"

#include "paddle/pten/api/include/creation.h"

PT_DECLARE_MODULE(CreationCPU);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
Expand Down Expand Up @@ -131,3 +133,38 @@ TEST(API, ones_like) {
ASSERT_EQ(actual_result[i], 1);
}
}

TEST(DEV_API, fill_any_like) {
// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
pten::DenseTensor dense_x(alloc,
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 2}),
pten::DataLayout::NCHW));
auto* dense_x_data = dense_x.mutable_data<float>();
dense_x_data[0] = 0;
float val = 1.0;

paddle::platform::DeviceContextPool& pool =
paddle::platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(paddle::platform::CPUPlace());

// 2. test API
auto out = pten::FillAnyLike<float>(
*(static_cast<paddle::platform::CPUDeviceContext*>(dev_ctx)),
dense_x,
val);

// 3. check result
ASSERT_EQ(out.dims().size(), 2);
ASSERT_EQ(out.dims()[0], 3);
ASSERT_EQ(out.numel(), 6);
ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32);
ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);

auto* actual_result = out.data<float>();
for (auto i = 0; i < 6; i++) {
ASSERT_NEAR(actual_result[i], val, 1e-6f);
}
}
46 changes: 46 additions & 0 deletions paddle/pten/tests/test_flatten_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ limitations under the License. */
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"

#include "paddle/pten/api/include/manipulation.h"

PT_DECLARE_MODULE(ManipulationCPU);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
Expand Down Expand Up @@ -70,3 +72,47 @@ TEST(API, flatten) {
}
ASSERT_EQ(value_equal, true);
}

TEST(DEV_API, flatten) {
// 1. create tensor
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
paddle::platform::CPUPlace());
pten::DenseTensor dense_x(
alloc,
pten::DenseTensorMeta(pten::DataType::FLOAT32,
framework::make_ddim({3, 2, 2, 3}),
pten::DataLayout::NCHW));
auto* dense_x_data = dense_x.mutable_data<float>();

for (int i = 0; i < dense_x.numel(); i++) {
dense_x_data[i] = i;
}
int start_axis = 1, stop_axis = 2;
paddle::platform::DeviceContextPool& pool =
paddle::platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(paddle::platform::CPUPlace());

// 2. test API
auto out = pten::Flatten<float>(
*(static_cast<paddle::platform::CPUDeviceContext*>(dev_ctx)),
dense_x,
start_axis,
stop_axis);

// 3. check result
std::vector<int> expect_shape = {3, 4, 3};
ASSERT_EQ(out.dims()[0], expect_shape[0]);
ASSERT_EQ(out.dims()[1], expect_shape[1]);
ASSERT_EQ(out.dims()[2], expect_shape[2]);
ASSERT_EQ(out.numel(), 36);
ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32);
ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);

bool value_equal = true;
auto* dense_out_data = out.data<float>();
for (int i = 0; i < dense_x.numel(); i++) {
if (std::abs(dense_x_data[i] - dense_out_data[i]) > 1e-6f)
value_equal = false;
}
ASSERT_EQ(value_equal, true);
}
Loading