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

[PTen]Support XPU for Flatten Kernel #36957

Merged
merged 163 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
163 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
176f1d6
Merge branch 'develop' of github.com:YuanRisheng/Paddle into flatten_…
YuanRisheng Nov 2, 2021
408a85b
Support XPU for Flatten Kernel
YuanRisheng Nov 2, 2021
6363bde
fix bugs when run kunlun ci
YuanRisheng Nov 2, 2021
8b8d114
fix compile bugs
YuanRisheng Nov 2, 2021
853b328
fix bugs for kunlun ci
YuanRisheng Nov 2, 2021
19e36b9
fix compile bugs when run kunlun
YuanRisheng Nov 2, 2021
5ca77f5
fix compile bugs in kunlun
YuanRisheng Nov 3, 2021
2ca5414
fix compile bugs in kunlun
YuanRisheng Nov 3, 2021
a88ca81
Merge From Develop
YuanRisheng Nov 3, 2021
8fc264d
fix bugs when compile
YuanRisheng Nov 3, 2021
11bd75b
fix bugs when compile
YuanRisheng Nov 3, 2021
bfb723c
fix compile bug
YuanRisheng Nov 3, 2021
890dae6
delete useless annotation
YuanRisheng Nov 3, 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
44 changes: 12 additions & 32 deletions paddle/fluid/operators/flatten_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ class FlattenOp : public framework::OperatorWithKernel {
const framework::ExecutionContext &ctx) const override {
auto input_data_type =
framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");

//#ifdef PADDLE_WITH_MKLDNN
// if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
// return framework::OpKernelType(input_data_type, ctx.GetPlace(),
// framework::DataLayout::kMKLDNN,
// framework::LibraryType::kMKLDNN);
// }
//#endif
return framework::OpKernelType(input_data_type, ctx.GetPlace());
}
};
Expand Down Expand Up @@ -157,14 +149,6 @@ class FlattenGradOp : public framework::OperatorWithKernel {
const framework::ExecutionContext &ctx) const override {
auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out"));

//#ifdef PADDLE_WITH_MKLDNN
// if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
// return framework::OpKernelType(input_data_type, ctx.GetPlace(),
// framework::DataLayout::kMKLDNN,
// framework::LibraryType::kMKLDNN);
// }
//#endif
return framework::OpKernelType(input_data_type, ctx.GetPlace());
}
};
Expand Down Expand Up @@ -227,14 +211,6 @@ class Flatten2Op : public framework::OperatorWithKernel {
const framework::ExecutionContext &ctx) const override {
auto input_data_type =
framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");

//#ifdef PADDLE_WITH_MKLDNN
// if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
// return framework::OpKernelType(input_data_type, ctx.GetPlace(),
// framework::DataLayout::kMKLDNN,
// framework::LibraryType::kMKLDNN);
// }
//#endif
return framework::OpKernelType(input_data_type, ctx.GetPlace());
}
};
Expand Down Expand Up @@ -285,14 +261,6 @@ class Flatten2GradOp : public framework::OperatorWithKernel {
const framework::ExecutionContext &ctx) const override {
auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out"));

//#ifdef PADDLE_WITH_MKLDNN
// if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
// return framework::OpKernelType(input_data_type, ctx.GetPlace(),
// framework::DataLayout::kMKLDNN,
// framework::LibraryType::kMKLDNN);
// }
//#endif
return framework::OpKernelType(input_data_type, ctx.GetPlace());
}
};
Expand Down Expand Up @@ -365,6 +333,18 @@ class FlattenContiguousRangeOp : public framework::OperatorWithKernel {

return out_shape;
}

framework::KernelSignature GetExpectedPtenKernelArgs(
const framework::ExecutionContext &ctx) const override {
if (ctx.HasOutput("XShape")) {
return framework::KernelSignature("flatten_contiguous_range.mid", {"X"},
{"start_axis", "stop_axis"},
{"Out", "XShape"});
} else {
return framework::KernelSignature("flatten_contiguous_range", {"X"},
{"start_axis", "stop_axis"}, {"Out"});
}
}
};

class FlattenContiguousRangeOpMaker : public FlattenOpMaker {
Expand Down
18 changes: 12 additions & 6 deletions paddle/fluid/operators/flatten_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ limitations under the License. */
#pragma once
#include <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/pooling.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/pten/include/core.h"
#include "paddle/pten/include/manipulation.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -122,13 +125,16 @@ class FlattenContiguousRangeKernel : public framework::OpKernel<T> {
void Compute(const framework::ExecutionContext &context) const override {
auto *in = context.Input<framework::LoDTensor>("X");
auto *out = context.Output<framework::LoDTensor>("Out");
auto out_dims = out->dims();

out->mutable_data(context.GetPlace(), in->type());
framework::TensorCopy(
*in, context.GetPlace(),
context.template device_context<platform::DeviceContext>(), out);
out->Resize(out_dims);
auto &start_axis = context.Attr<int>("start_axis");
auto &stop_axis = context.Attr<int>("stop_axis");
auto &dev_ctx = context.device_context<DeviceContext>();

auto pt_x = paddle::experimental::MakePtenDenseTensor(*in);
auto pt_out = paddle::experimental::MakePtenDenseTensor(*out);

// call new kernel
pten::Flatten<T>(dev_ctx, *pt_x.get(), start_axis, stop_axis, pt_out.get());
}
};

Expand Down
4 changes: 3 additions & 1 deletion paddle/pten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ set(PTEN_DEPS ${PTEN_DEPS} unary binary)
if(WITH_GPU OR WITH_ROCM)
set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda creation_cuda manipulation_cuda)
endif()

if(WITH_XPU)
set(PTEN_DEPS ${PTEN_DEPS} manipulation_xpu)
endif()
cc_library(pten SRCS all.cc DEPS ${PTEN_DEPS})
1 change: 1 addition & 0 deletions paddle/pten/include/manipulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "paddle/pten/include/infershape.h"
#include "paddle/pten/kernels/cpu/manipulation.h"
#include "paddle/pten/kernels/cuda/manipulation.h"
#include "paddle/pten/kernels/xpu/manipulation.h"

namespace pten {

Expand Down
2 changes: 2 additions & 0 deletions paddle/pten/kernels/xpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cc_library(utils_xpu SRCS utils.cc DEPS dense_tensor kernel_context kernel_factory memory convert_utils)
cc_library(manipulation_xpu SRCS manipulation.cc DEPS dense_tensor kernel_context kernel_factory utils_xpu unary)
82 changes: 82 additions & 0 deletions paddle/pten/kernels/xpu/manipulation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2021 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/kernels/xpu/manipulation.h"
#include "paddle/pten/infershape/unary.h"
#include "paddle/pten/kernels/xpu/utils.h"

namespace pten {

template <typename T>
void Flatten(const XPUContext& dev_ctx,
const DenseTensor& x,
int start_axis,
int stop_axis,
DenseTensor* out) {
auto out_dims = out->dims();
pten::Copy(dev_ctx, x, out);
out->Resize(out_dims);
}

// TODO(yuanrisheng): this kernel is for training and xshape is a Intermediate
// Output Tensor,
// is there a more flexible way to deal with this case?
template <typename T>
void FlattenWithXShape(const XPUContext& dev_ctx,
const DenseTensor& x,
int start_axis,
int stop_axis,
DenseTensor* out,
DenseTensor* xshape) {
Flatten<T>(dev_ctx, x, start_axis, stop_axis, out);
const auto& in_dims = x.dims();
std::vector<int64_t> xshape_dims(in_dims.size() + 1);
xshape_dims[0] = 0;
for (int i = 0; i < in_dims.size(); ++i) {
xshape_dims[i + 1] = in_dims[i];
}
xshape->Resize(paddle::framework::make_ddim(xshape_dims));
xshape->set_lod(x.lod());
}

} // namespace pten

// TODO(chenweihang): replace by better impl
PT_REGISTER_MODULE(ManipulationXPU);

// TODO(yuanrisheng): "flatten_contiguous_range" is compatible with old kernel
// architecture, kernel_name should be "flatten".
PT_REGISTER_KERNEL("flatten_contiguous_range",
XPU,
ANY,
pten::Flatten,
float,
paddle::platform::float16,
double,
uint8_t,
int8_t,
int,
int64_t) {}

PT_REGISTER_KERNEL("flatten_contiguous_range.mid",
XPU,
ANY,
pten::FlattenWithXShape,
float,
paddle::platform::float16,
double,
uint8_t,
int8_t,
int,
int64_t) {}
38 changes: 38 additions & 0 deletions paddle/pten/kernels/xpu/manipulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2021 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

#ifdef PADDLE_WITH_XPU

#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_registry.h"

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

namespace pten {

using XPUContext = paddle::platform::XPUDeviceContext;

template <typename T>
void Flatten(const XPUContext& dev_ctx,
const DenseTensor& x,
int start_axis,
int stop_axis,
DenseTensor* out);

} // namespace pten

#endif
81 changes: 81 additions & 0 deletions paddle/pten/kernels/xpu/utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Copyright (c) 2021 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/kernels/xpu/utils.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/pten/common/data_type.h"
#include "paddle/pten/core/convert_utils.h"

namespace pten {

void Copy(const XPUDeviceContext& dev_ctx,
const DenseTensor& src,
DenseTensor* dst) {
auto* src_ptr = src.data();
auto* dst_ptr = dst->mutable_data();
const auto& src_place = src.place();
const auto& dst_place = dst->place();

if (src_ptr == dst_ptr && src_place == dst_place) {
VLOG(3) << "Skip copy the same data async from " << src_place << " to "
<< dst_place;
return;
}
VLOG(4) << "src:" << src_ptr << ", dst:" << dst_ptr;

VLOG(3) << "TensorCopy " << src.dims() << " from " << src.place() << " to "
<< dst_place;
dst->Resize(src.dims());
CHECK(dst->layout() == src.layout());
auto size = src.numel() * paddle::framework::SizeOfType(
TransToProtoVarType(src.data_type()));

if (paddle::platform::is_xpu_place(src_place) && // NOLINT
paddle::platform::is_cpu_place(dst_place)) {
paddle::memory::Copy(BOOST_GET_CONST(paddle::platform::CPUPlace, dst_place),
dst_ptr,
BOOST_GET_CONST(paddle::platform::XPUPlace, src_place),
src_ptr,
size);
} else if (paddle::platform::is_cpu_place(src_place) &&
paddle::platform::is_xpu_place(dst_place)) {
paddle::memory::Copy(BOOST_GET_CONST(paddle::platform::XPUPlace, dst_place),
dst_ptr,
BOOST_GET_CONST(paddle::platform::CPUPlace, src_place),
src_ptr,
size);
} else if (paddle::platform::is_xpu_place(src_place) &&
paddle::platform::is_xpu_place(dst_place)) {
if (src_ptr == dst_ptr) {
VLOG(3) << "Skip copy the same data async from " << src_place << " to "
<< dst_place;
return;
}
paddle::memory::Copy(BOOST_GET_CONST(paddle::platform::XPUPlace, dst_place),
dst_ptr,
BOOST_GET_CONST(paddle::platform::XPUPlace, src_place),
src_ptr,
size);
} else {
PADDLE_THROW(paddle::platform::errors::Unimplemented(
"Copy from %s to %s is not supported.", src_place, dst_place));
}
}

} // namespace pten

// TODO(chenweihang): replace by better impl
PT_REGISTER_MODULE(UtilsXPU);

PT_REGISTER_KERNEL_WITH_NO_TYPE("copy", XPU, ANY, pten::Copy) {}
34 changes: 34 additions & 0 deletions paddle/pten/kernels/xpu/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) 2021 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

#ifdef PADDLE_WITH_XPU

#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_registry.h"

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/device_context.h"
namespace pten {

using XPUDeviceContext = paddle::platform::XPUDeviceContext;

void Copy(const XPUDeviceContext& dev_ctx,
const DenseTensor& src,
DenseTensor* dst);

} // namespace pten

#endif