未验证 提交 b20f771f 编写于 作者: F freeliuzc 提交者: GitHub

[phi] move crop_tensor kernel from fluid to phi (#44574)

* move crop_tensor from fluid to phi

* delete fluid header files

* fix crop_tensor_op dygraph_mode bug

* modify header files, add out tensor check
上级 4b7fe610
...@@ -12,11 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,11 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/crop_tensor_op.h" #include "paddle/fluid/framework/op_registry.h"
#include <memory> // TODO(freeliuzc): Delete old infershape
#include <string> // New infershape has already in unary.h and backward.h
#include <vector>
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -297,8 +296,8 @@ class CropTensorGradOpMaker : public framework::SingleGradOpMaker<T> { ...@@ -297,8 +296,8 @@ class CropTensorGradOpMaker : public framework::SingleGradOpMaker<T> {
protected: protected:
void Apply(GradOpPtr<T> op) const override { void Apply(GradOpPtr<T> op) const override {
op->SetType("crop_tensor_grad"); op->SetType("crop_tensor_grad");
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetInput("X", this->Input("X")); op->SetInput("X", this->Input("X"));
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
if (this->HasInput("OffsetsTensor")) { if (this->HasInput("OffsetsTensor")) {
op->SetInput("OffsetsTensor", this->Input("OffsetsTensor")); op->SetInput("OffsetsTensor", this->Input("OffsetsTensor"));
} }
...@@ -314,32 +313,10 @@ class CropTensorGradOpMaker : public framework::SingleGradOpMaker<T> { ...@@ -314,32 +313,10 @@ class CropTensorGradOpMaker : public framework::SingleGradOpMaker<T> {
} // namespace paddle } // namespace paddle
namespace ops = paddle::operators; namespace ops = paddle::operators;
REGISTER_OPERATOR(crop_tensor, REGISTER_OPERATOR(crop_tensor,
ops::CropTensorOp, ops::CropTensorOp,
ops::CropTensorOpMaker, ops::CropTensorOpMaker,
ops::CropTensorGradOpMaker<paddle::framework::OpDesc>, ops::CropTensorGradOpMaker<paddle::framework::OpDesc>,
ops::CropTensorGradOpMaker<paddle::imperative::OpBase>); ops::CropTensorGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(crop_tensor_grad, ops::CropTensorOpGrad); REGISTER_OPERATOR(crop_tensor_grad, ops::CropTensorOpGrad);
REGISTER_OP_CPU_KERNEL(crop_tensor,
ops::CropTensorKernel<phi::CPUContext, float>,
ops::CropTensorKernel<phi::CPUContext, double>,
ops::CropTensorKernel<phi::CPUContext, int>,
ops::CropTensorKernel<phi::CPUContext, int64_t>);
REGISTER_OP_CPU_KERNEL(crop_tensor_grad,
ops::CropTensorGradKernel<phi::CPUContext, float>,
ops::CropTensorGradKernel<phi::CPUContext, double>,
ops::CropTensorGradKernel<phi::CPUContext, int>,
ops::CropTensorGradKernel<phi::CPUContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(
crop_tensor,
ops::CropTensorKernel<paddle::platform::CUDADeviceContext, float>,
ops::CropTensorKernel<paddle::platform::CUDADeviceContext, double>,
ops::CropTensorKernel<paddle::platform::CUDADeviceContext, int>,
ops::CropTensorKernel<paddle::platform::CUDADeviceContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(
crop_tensor_grad,
ops::CropTensorGradKernel<paddle::platform::CUDADeviceContext, float>,
ops::CropTensorGradKernel<paddle::platform::CUDADeviceContext, double>,
ops::CropTensorGradKernel<paddle::platform::CUDADeviceContext, int>,
ops::CropTensorGradKernel<paddle::platform::CUDADeviceContext, int64_t>);
/* Copyright (c) 2019 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 <utility>
#include <vector>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/eigen/eigen_function.h"
#include "paddle/fluid/operators/strided_memcpy.h"
namespace paddle {
namespace operators { // Internal
template <typename T,
size_t D,
int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
using framework::Tensor;
inline std::vector<int> get_new_data(
const std::vector<const Tensor*>& list_new_tensor) {
// get tensor from
std::vector<int> vec_new_data;
for (size_t i = 0; i < list_new_tensor.size(); ++i) {
auto tensor = list_new_tensor[i];
PADDLE_ENFORCE_EQ(
tensor->dims(),
phi::make_ddim({1}),
platform::errors::InvalidArgument(
"The tensor's shape in list of Op(crop_tensor) should be [1], "
"but the value received is %d.",
tensor->dims()));
if (platform::is_gpu_place(tensor->place())) {
framework::Tensor temp;
paddle::framework::TensorCopySync(*tensor, platform::CPUPlace(), &temp);
vec_new_data.push_back(static_cast<int32_t>(*temp.data<int32_t>()));
} else {
vec_new_data.push_back(static_cast<int32_t>(*tensor->data<int32_t>()));
}
}
return vec_new_data;
}
static framework::DDim ValidateShape(const std::vector<int> shape,
const std::vector<int> offsets,
const framework::DDim& in_dims) {
auto in_dim_size = in_dims.size();
auto shape_size = shape.size();
PADDLE_ENFORCE_EQ(
in_dim_size,
shape_size,
platform::errors::InvalidArgument(
"The number of elements (%d) for shape of Op(crop_tensor) should be "
"equal to the number of dimensions (%d) of the input tensor.",
shape_size,
in_dim_size));
std::vector<int64_t> output_shape(shape.size(), 0);
for (size_t i = 0; i < shape.size(); ++i) {
if (shape[i] <= 0 && in_dims[i] > 0) {
PADDLE_ENFORCE_NE(shape[i],
0,
platform::errors::InvalidArgument(
"The value (%d) of the %uth element for shape of "
"Op(crop_tensor) should not be zero.",
shape[i],
i));
PADDLE_ENFORCE_EQ(shape[i],
-1,
platform::errors::InvalidArgument(
"When the value (%d) of the %uth "
"element for shape of Op(crop_tensor)"
" is negative, only -1 is supported.",
shape[i],
i));
output_shape[i] = in_dims[i] - offsets[i];
} else {
output_shape[i] = static_cast<int64_t>(shape[i]);
}
}
return phi::make_ddim(output_shape);
}
static std::vector<int> GetShape(const framework::ExecutionContext& ctx) {
std::vector<int> res;
int rank = ctx.Input<Tensor>("X")->dims().size();
auto list_new_shape_tensor = ctx.MultiInput<framework::Tensor>("ShapeTensor");
if (list_new_shape_tensor.size() > 0) {
// have offsets tensor list
PADDLE_ENFORCE_EQ(
list_new_shape_tensor.size(),
rank,
platform::errors::InvalidArgument(
"The number of tensors (%d) for the input ShapeTensor of "
"Op(crop_tensor) must be equal to the number of "
"dimensions (%d) of the input.",
list_new_shape_tensor.size(),
rank));
res = get_new_data(list_new_shape_tensor);
return res;
}
auto* shape_tensor = ctx.HasInput("Shape")
? ctx.Input<framework::LoDTensor>("Shape")
: nullptr;
if (shape_tensor) {
auto* shape_data = shape_tensor->data<int>();
framework::Tensor cpu_shape_tensor;
if (platform::is_gpu_place(shape_tensor->place())) {
paddle::framework::TensorCopySync(
*shape_tensor, platform::CPUPlace(), &cpu_shape_tensor);
shape_data = cpu_shape_tensor.data<int>();
}
res = std::vector<int>(shape_data, shape_data + shape_tensor->numel());
}
return res;
}
static std::vector<int> GetOffsets(const framework::ExecutionContext& ctx) {
std::vector<int> res;
int rank = ctx.Input<Tensor>("X")->dims().size();
auto list_new_offsets_tensor =
ctx.MultiInput<framework::Tensor>("OffsetsTensor");
if (list_new_offsets_tensor.size() > 0) {
// have offsets tensor list
res = get_new_data(list_new_offsets_tensor);
return res;
}
if (ctx.HasInput("Offsets")) {
const auto* offsets_tensor = ctx.Input<Tensor>("Offsets");
PADDLE_ENFORCE_EQ(offsets_tensor->dims().size(),
1,
platform::errors::InvalidArgument(
"The number of dimensions of input 'Offsets' must "
"be 1, but the value received is: %d.",
offsets_tensor->dims().size()));
PADDLE_ENFORCE_EQ(rank,
offsets_tensor->dims()[0],
platform::errors::InvalidArgument(
"The number of elements (%d) for "
"input 'Offsets' must be equal to "
"the number of dimensions (%d) of the input tensor.",
offsets_tensor->dims()[0],
rank));
const int* offsets_data;
framework::Tensor cpu_tmp_tensor;
if (platform::is_cpu_place(offsets_tensor->place())) {
offsets_data = offsets_tensor->data<int>();
} else {
framework::TensorCopySync(
*offsets_tensor, platform::CPUPlace(), &cpu_tmp_tensor);
offsets_data = cpu_tmp_tensor.data<int>();
}
res = std::vector<int>(offsets_data, offsets_data + rank);
} else {
res = ctx.Attr<std::vector<int>>("offsets");
PADDLE_ENFORCE_EQ(
rank,
static_cast<int>(res.size()),
platform::errors::InvalidArgument("The number of elements (%d) for "
"input 'Offsets' must be equal to "
"the number of dimensions (%d) "
"of the input tensor.",
static_cast<int>(res.size()),
rank));
}
return res;
}
template <typename DeviceContext, typename T, size_t D>
void CropTensorFunction(const framework::ExecutionContext& context) {
auto* x = context.Input<Tensor>("X");
auto* out = context.Output<Tensor>("Out");
auto x_dims = x->dims();
auto out_dims = out->dims();
// get shape from Input(ShapeTensor) of Input(Shape)
std::vector<int> shape = GetShape(context);
// out_dims set by arrt(shape)
if (shape.size() == 0) {
for (int i = 0; i < out_dims.size(); ++i) {
shape.push_back(out_dims[i]);
}
}
auto offsets = GetOffsets(context);
out_dims = ValidateShape(shape, offsets, x->dims());
out->mutable_data<T>(out_dims, context.GetPlace());
for (size_t i = 0; i < offsets.size(); ++i) {
PADDLE_ENFORCE_LE(offsets[i] + shape[i],
x_dims[i],
platform::errors::InvalidArgument(
"The sum of the %uth elements of "
"offsets (%d) and shape (%d) of Op(crop_tensor) "
"should be less than or "
"equal to the size of %uth dimension of the input.",
i,
offsets[i],
shape[i],
i));
}
auto x_tensor = EigenTensor<T, D>::From(*x);
auto out_tensor = EigenTensor<T, D>::From(*out);
Eigen::DSizes<Eigen::DenseIndex, D> e_offsets;
Eigen::DSizes<Eigen::DenseIndex, D> e_shape;
for (size_t i = 0; i < D; ++i) {
e_offsets[i] = offsets[i];
e_shape[i] = out->dims()[i];
}
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
EigenSlice<std::decay_t<decltype(place)>, T, D>::Eval(
place, out_tensor, x_tensor, e_offsets, e_shape);
}
template <typename DeviceContext, typename T>
class CropTensorKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
int rank = context.Input<Tensor>("X")->dims().size();
PADDLE_ENFORCE_GE(
rank,
1,
platform::errors::InvalidArgument(
"The number of dimensions of the input 'x' for "
"Op(crop_tensor) must be greater than or equal to 1, but the "
"value received is %d.",
rank));
PADDLE_ENFORCE_LE(
rank,
6,
platform::errors::InvalidArgument(
"The number of dimensions of the input 'x' for "
"Op(crop_tensor) must be less than or equal to 6, but the "
"value received is %d.",
rank));
switch (rank) {
case 1:
CropTensorFunction<DeviceContext, T, 1>(context);
break;
case 2:
CropTensorFunction<DeviceContext, T, 2>(context);
break;
case 3:
CropTensorFunction<DeviceContext, T, 3>(context);
break;
case 4:
CropTensorFunction<DeviceContext, T, 4>(context);
break;
case 5:
CropTensorFunction<DeviceContext, T, 5>(context);
break;
case 6:
CropTensorFunction<DeviceContext, T, 6>(context);
break;
}
}
};
template <typename DeviceContext, typename T, size_t D>
void CropTensorGradFunction(const framework::ExecutionContext& context) {
auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
auto* x = context.Input<Tensor>("X");
if (d_x != nullptr) {
auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
d_x->mutable_data<T>(x->dims(), context.GetPlace());
auto offsets = GetOffsets(context);
Eigen::array<std::pair<int64_t, int64_t>, D> paddings;
for (size_t i = 0; i < D; ++i) {
paddings[i].first = offsets[i];
paddings[i].second = d_x->dims()[i] - d_out->dims()[i] - offsets[i];
}
auto d_x_tensor = EigenTensor<T, D>::From(*d_x);
auto d_out_tensor = EigenTensor<T, D>::From(*d_out);
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
EigenPad<std::decay_t<decltype(place)>, T, D>::Eval(
place, d_x_tensor, d_out_tensor, paddings, static_cast<T>(0));
}
}
template <typename DeviceContext, typename T>
class CropTensorGradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
size_t rank =
context.Input<Tensor>(framework::GradVarName("Out"))->dims().size();
PADDLE_ENFORCE_GE(
rank,
1,
platform::errors::InvalidArgument(
"The number of dimensions of the input 'Out@GRAD' for "
"Op(crop_tensor_grad) must be greater than or equal to 1, but the "
"value received is %d.",
rank));
PADDLE_ENFORCE_LE(
rank,
6,
platform::errors::InvalidArgument(
"The number of dimensions of the input 'Out@GRAD' for "
"Op(crop_tensor_grad) must be less than or equal to 6, but the "
"value received is %d.",
rank));
switch (rank) {
case 1:
CropTensorGradFunction<DeviceContext, T, 1>(context);
break;
case 2:
CropTensorGradFunction<DeviceContext, T, 2>(context);
break;
case 3:
CropTensorGradFunction<DeviceContext, T, 3>(context);
break;
case 4:
CropTensorGradFunction<DeviceContext, T, 4>(context);
break;
case 5:
CropTensorGradFunction<DeviceContext, T, 5>(context);
break;
case 6:
CropTensorGradFunction<DeviceContext, T, 6>(context);
break;
}
}
};
} // namespace operators
} // namespace paddle
...@@ -513,6 +513,16 @@ ...@@ -513,6 +513,16 @@
func : cosh func : cosh
backward : cosh_grad backward : cosh_grad
- api : crop_tensor
args : (Tensor x, IntArray shape, IntArray offsets)
output : Tensor(out)
infer_meta :
func : CropTensorInferMeta
kernel :
func : crop_tensor
data_type : x
backward : crop_tensor_grad
# Part of python API paddle.nn.functional.cross_entropy # Part of python API paddle.nn.functional.cross_entropy
- api : cross_entropy_with_softmax - api : cross_entropy_with_softmax
args : (Tensor input, Tensor label, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis) args : (Tensor input, Tensor label, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis)
......
...@@ -481,6 +481,16 @@ ...@@ -481,6 +481,16 @@
func : cosh_grad func : cosh_grad
inplace : (out_grad -> x_grad) inplace : (out_grad -> x_grad)
- backward_api : crop_tensor_grad
forward : crop_tensor (Tensor x, IntArray shape, IntArray offsets) -> Tensor(out)
args : (Tensor x, Tensor out_grad, IntArray offsets)
output : Tensor(x_grad)
infer_meta :
func : CropTensorGradInferMeta
kernel :
func : crop_tensor_grad
data_type : x
- backward_api : cross_entropy_with_softmax_grad - backward_api : cross_entropy_with_softmax_grad
forward : cross_entropy_with_softmax (Tensor input, Tensor label, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis) -> Tensor(softmax), Tensor(loss) forward : cross_entropy_with_softmax (Tensor input, Tensor label, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis) -> Tensor(softmax), Tensor(loss)
args : (Tensor label, Tensor softmax, Tensor loss_grad, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis) args : (Tensor label, Tensor softmax, Tensor loss_grad, bool soft_label, bool use_softmax, bool numeric_stable_mode, int ignore_index, int axis)
......
...@@ -156,6 +156,18 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x, ...@@ -156,6 +156,18 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
} }
} }
void CropTensorGradInferMeta(const MetaTensor& out_grad,
const MetaTensor& x,
const IntArray& offsets,
MetaTensor* x_grad) {
auto x_dims = x.dims();
if (x_grad != nullptr) {
x_grad->set_dims(x_dims);
x_grad->set_dtype(x.dtype());
}
}
void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label, void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
const MetaTensor& softmax, const MetaTensor& softmax,
const MetaTensor& loss_grad, const MetaTensor& loss_grad,
......
...@@ -89,6 +89,11 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x, ...@@ -89,6 +89,11 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
MetaTensor* dfilter, MetaTensor* dfilter,
MetaTensor* ddout); MetaTensor* ddout);
void CropTensorGradInferMeta(const MetaTensor& out_grad,
const MetaTensor& x,
const IntArray& offsets,
MetaTensor* x_grad);
void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label, void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
const MetaTensor& softmax, const MetaTensor& softmax,
const MetaTensor& loss_grad, const MetaTensor& loss_grad,
......
...@@ -300,6 +300,47 @@ void CumInferMeta(const MetaTensor& x, ...@@ -300,6 +300,47 @@ void CumInferMeta(const MetaTensor& x,
out->share_lod(x); out->share_lod(x);
} }
void CropTensorInferMeta(const MetaTensor& x,
const IntArray& shape,
const IntArray& offsets,
MetaTensor* out,
MetaConfig config) {
PADDLE_ENFORCE_NE(
out,
nullptr,
errors::InvalidArgument("CropTensor should have output tensor out."));
auto x_dim = x.dims();
auto shape_dims = shape.GetData();
auto offsets_vec = offsets.GetData();
PADDLE_ENFORCE_EQ(shape_dims.size(),
x_dim.size(),
errors::InvalidArgument(
"The number of elements (%d) of attribute 'shape' for "
"CropTensor must be equal to the number of "
"dimensions (%d) of the input.",
shape_dims.size(),
x_dim.size()));
if (config.is_runtime) {
out->share_lod(x);
}
auto out_dims = std::vector<int64_t>(shape.size(), -1);
for (size_t i = 0; i < shape_dims.size(); ++i) {
if (shape_dims[i] > 0) {
out_dims[i] = static_cast<int64_t>(shape_dims[i]);
} else {
if (shape_dims[i] == -1 && offsets_vec[i] != -1 && x_dim[i] != -1) {
out_dims[i] = x_dim[i] - static_cast<int64_t>(offsets_vec[i]);
}
}
}
out->set_dims(phi::make_ddim(out_dims));
out->set_dtype(x.dtype());
}
void DiagEmbedInferMeta( void DiagEmbedInferMeta(
const MetaTensor& x, int offset, int dim1, int dim2, MetaTensor* out) { const MetaTensor& x, int offset, int dim1, int dim2, MetaTensor* out) {
auto x_dims = x.dims(); auto x_dims = x.dims();
......
...@@ -66,6 +66,12 @@ void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out); ...@@ -66,6 +66,12 @@ void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out);
void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out); void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out);
void CropTensorInferMeta(const MetaTensor& x,
const IntArray& shape,
const IntArray& offsets,
MetaTensor* out,
MetaConfig config = MetaConfig());
void CumInferMeta(const MetaTensor& x, void CumInferMeta(const MetaTensor& x,
int axis, int axis,
bool flatten, bool flatten,
......
// 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/phi/kernels/crop_tensor_grad_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/crop_tensor_grad_kernel_impl.h"
PD_REGISTER_KERNEL(crop_tensor_grad,
CPU,
ALL_LAYOUT,
phi::CropTensorGradKernel,
float,
double,
int,
int64_t) {}
// 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/phi/kernels/crop_tensor_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/crop_tensor_kernel_impl.h"
PD_REGISTER_KERNEL(crop_tensor,
CPU,
ALL_LAYOUT,
phi::CropTensorKernel,
float,
double,
int,
int64_t) {}
// 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 "paddle/phi/common/int_array.h"
#include "paddle/phi/core/dense_tensor.h"
namespace phi {
template <typename T, typename Context>
void CropTensorGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& out_grad,
const IntArray& offsets,
DenseTensor* x_grad);
} // namespace phi
// 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 "paddle/phi/common/int_array.h"
#include "paddle/phi/core/dense_tensor.h"
namespace phi {
template <typename T, typename Context>
void CropTensorKernel(const Context& dev_ctx,
const DenseTensor& x,
const IntArray& shape,
const IntArray& offsets,
DenseTensor* out);
} // namespace phi
// 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/phi/kernels/crop_tensor_grad_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/crop_tensor_grad_kernel_impl.h"
PD_REGISTER_KERNEL(crop_tensor_grad,
GPU,
ALL_LAYOUT,
phi::CropTensorGradKernel,
float,
double,
int,
int64_t) {}
// 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/phi/kernels/crop_tensor_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/crop_tensor_kernel_impl.h"
PD_REGISTER_KERNEL(crop_tensor,
GPU,
ALL_LAYOUT,
phi::CropTensorKernel,
float,
double,
int,
int64_t) {}
// 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 "paddle/phi/kernels/crop_tensor_grad_kernel.h"
#include <vector>
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"
namespace phi {
template <typename Context, typename T, size_t D>
void CropTensorGradFunction(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& out_grad,
const IntArray& offsets,
DenseTensor* x_grad) {
if (x_grad != nullptr) {
x_grad->Resize(x.dims());
dev_ctx.template Alloc<T>(x_grad);
auto offsets_vec = offsets.GetData();
std::array<std::pair<int64_t, int64_t>, D> paddings;
for (size_t i = 0; i < D; ++i) {
paddings[i].first = offsets_vec[i];
paddings[i].second =
x_grad->dims()[i] - out_grad.dims()[i] - offsets_vec[i];
}
auto x_grad_tensor = EigenTensor<T, D>::From(*x_grad);
auto out_grad_tensor = EigenTensor<T, D>::From(out_grad);
auto& place = *dev_ctx.eigen_device();
funcs::EigenPad<std::decay_t<decltype(place)>, T, D>::Eval(
place, x_grad_tensor, out_grad_tensor, paddings, static_cast<T>(0));
}
}
template <typename T, typename Context>
void CropTensorGradKernel(const Context& dev_ctx,
const DenseTensor& out_grad,
const DenseTensor& x,
const IntArray& offsets,
DenseTensor* x_grad) {
size_t rank = out_grad.dims().size();
PADDLE_ENFORCE_GE(
rank,
1,
errors::InvalidArgument(
"The number of dimensions of the input 'Out@GRAD' for "
"Op(crop_tensor_grad) must be greater than or equal to 1, but the "
"value received is %d.",
rank));
PADDLE_ENFORCE_LE(
rank,
6,
errors::InvalidArgument(
"The number of dimensions of the input 'Out@GRAD' for "
"Op(crop_tensor_grad) must be less than or equal to 6, but the "
"value received is %d.",
rank));
switch (rank) {
case 1:
CropTensorGradFunction<Context, T, 1>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
case 2:
CropTensorGradFunction<Context, T, 2>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
case 3:
CropTensorGradFunction<Context, T, 3>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
case 4:
CropTensorGradFunction<Context, T, 4>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
case 5:
CropTensorGradFunction<Context, T, 5>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
case 6:
CropTensorGradFunction<Context, T, 6>(
dev_ctx, out_grad, x, offsets, x_grad);
break;
}
}
} // namespace phi
// 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 "paddle/phi/kernels/crop_tensor_kernel.h"
#include <utility>
#include <vector>
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"
namespace phi {
static phi::DDim ValidateShape(const std::vector<int64_t>& shape,
const std::vector<int64_t>& offsets,
const phi::DDim& in_dims) {
auto in_dim_size = in_dims.size();
auto shape_size = shape.size();
PADDLE_ENFORCE_EQ(
in_dim_size,
shape_size,
errors::InvalidArgument(
"The number of elements (%d) for shape of Op(crop_tensor) should be "
"equal to the number of dimensions (%d) of the input tensor.",
shape_size,
in_dim_size));
std::vector<int64_t> output_shape(shape.size(), 0);
for (size_t i = 0; i < shape.size(); ++i) {
if (shape[i] <= 0 && in_dims[i] > 0) {
PADDLE_ENFORCE_NE(shape[i],
0,
errors::InvalidArgument(
"The value (%d) of the %uth element for shape of "
"Op(crop_tensor) should not be zero.",
shape[i],
i));
PADDLE_ENFORCE_EQ(
shape[i],
-1,
errors::InvalidArgument("When the value (%d) of the %uth "
"element for shape of Op(crop_tensor)"
" is negative, only -1 is supported.",
shape[i],
i));
output_shape[i] = in_dims[i] - offsets[i];
} else {
output_shape[i] = static_cast<int64_t>(shape[i]);
}
}
return phi::make_ddim(output_shape);
}
template <typename Context, typename T, size_t D>
void CropTensorFunction(const Context& dev_ctx,
const DenseTensor& x,
const IntArray& shape,
const IntArray& offsets,
DenseTensor* out) {
auto x_dims = x.dims();
auto rank = x.dims().size();
auto out_dims = out->dims();
auto shape_vec = shape.GetData();
if (shape_vec.size() == 0) {
for (int i = 0; i < out_dims.size(); ++i) {
shape_vec.push_back(out_dims[i]);
}
}
auto offsets_vec = offsets.GetData();
PADDLE_ENFORCE_EQ(
rank,
static_cast<int>(offsets_vec.size()),
errors::InvalidArgument("The number of elements (%d) for "
"input 'Offsets' must be equal to "
"the number of dimensions (%d) "
"of the input tensor.",
static_cast<int>(offsets_vec.size()),
rank));
out_dims = ValidateShape(shape_vec, offsets_vec, x.dims());
out->Resize(out_dims);
dev_ctx.template Alloc<T>(out);
for (size_t i = 0; i < offsets_vec.size(); ++i) {
PADDLE_ENFORCE_LE(offsets_vec[i] + shape_vec[i],
x_dims[i],
errors::InvalidArgument(
"The sum of the %uth elements of "
"offsets (%d) and shape (%d) of Op(crop_tensor) "
"should be less than or "
"equal to the size of %uth dimension of the input.",
i,
offsets_vec[i],
shape_vec[i],
i));
}
auto x_tensor = EigenTensor<T, D>::From(x);
auto out_tensor = EigenTensor<T, D>::From(*out);
Eigen::DSizes<Eigen::DenseIndex, D> e_offsets;
Eigen::DSizes<Eigen::DenseIndex, D> e_shape;
for (size_t i = 0; i < D; ++i) {
e_offsets[i] = offsets_vec[i];
e_shape[i] = out->dims()[i];
}
auto& place = *dev_ctx.eigen_device();
phi::funcs::EigenSlice<std::decay_t<decltype(place)>, T, D>::Eval(
place, out_tensor, x_tensor, e_offsets, e_shape);
}
template <typename T, typename Context>
void CropTensorKernel(const Context& dev_ctx,
const DenseTensor& x,
const IntArray& shape,
const IntArray& offsets,
DenseTensor* out) {
int rank = x.dims().size();
PADDLE_ENFORCE_GE(
rank,
1,
errors::InvalidArgument(
"The number of dimensions of the input 'x' for "
"Op(crop_tensor) must be greater than or equal to 1, but the "
"value received is %d.",
rank));
PADDLE_ENFORCE_LE(
rank,
6,
errors::InvalidArgument(
"The number of dimensions of the input 'x' for "
"Op(crop_tensor) must be less than or equal to 6, but the "
"value received is %d.",
rank));
switch (rank) {
case 1:
CropTensorFunction<Context, T, 1>(dev_ctx, x, shape, offsets, out);
break;
case 2:
CropTensorFunction<Context, T, 2>(dev_ctx, x, shape, offsets, out);
break;
case 3:
CropTensorFunction<Context, T, 3>(dev_ctx, x, shape, offsets, out);
break;
case 4:
CropTensorFunction<Context, T, 4>(dev_ctx, x, shape, offsets, out);
break;
case 5:
CropTensorFunction<Context, T, 5>(dev_ctx, x, shape, offsets, out);
break;
case 6:
CropTensorFunction<Context, T, 6>(dev_ctx, x, shape, offsets, out);
break;
}
}
} // namespace phi
// 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/phi/core/compat/op_utils.h"
namespace phi {
KernelSignature CropTensorOpArgumentMapping(const ArgumentMappingContext& ctx) {
if (ctx.InputSize("ShapeTensor") > 0) {
if (ctx.InputSize("OffsetsTensor") > 0) {
return KernelSignature(
"crop_tensor", {"X"}, {"ShapeTensor", "OffsetsTensor"}, {"Out"});
} else if (ctx.HasInput("Offsets")) {
return KernelSignature(
"crop_tensor", {"X"}, {"ShapeTensor", "Offsets"}, {"Out"});
} else {
return KernelSignature(
"crop_tensor", {"X"}, {"ShapeTensor", "offsets"}, {"Out"});
}
} else if (ctx.HasInput("Shape")) {
if (ctx.InputSize("OffsetsTensor") > 0) {
return KernelSignature(
"crop_tensor", {"X"}, {"Shape", "OffsetsTensor"}, {"Out"});
} else if (ctx.HasInput("Offsets")) {
return KernelSignature(
"crop_tensor", {"X"}, {"Shape", "Offsets"}, {"Out"});
} else {
return KernelSignature(
"crop_tensor", {"X"}, {"Shape", "offsets"}, {"Out"});
}
} else {
if (ctx.InputSize("OffsetsTensor") > 0) {
return KernelSignature(
"crop_tensor", {"X"}, {"shape", "OffsetsTensor"}, {"Out"});
} else if (ctx.HasInput("Offsets")) {
return KernelSignature(
"crop_tensor", {"X"}, {"shape", "Offsets"}, {"Out"});
} else {
return KernelSignature(
"crop_tensor", {"X"}, {"shape", "offsets"}, {"Out"});
}
}
}
KernelSignature CropTensorGradOpArgumentMapping(
const ArgumentMappingContext& ctx) {
if (ctx.InputSize("OffsetsTensor") > 0) {
return KernelSignature(
"crop_tensor_grad", {"X", "Out@GRAD"}, {"OffsetsTensor"}, {"X@GRAD"});
} else if (ctx.HasInput("Offsets")) {
return KernelSignature(
"crop_tensor_grad", {"X", "Out@GRAD"}, {"Offsets"}, {"X@GRAD"});
} else {
return KernelSignature(
"crop_tensor_grad", {"X", "Out@GRAD"}, {"offsets"}, {"X@GRAD"});
}
}
} // namespace phi
PD_REGISTER_ARG_MAPPING_FN(crop_tensor, phi::CropTensorOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(crop_tensor_grad,
phi::CropTensorGradOpArgumentMapping);
...@@ -51,6 +51,7 @@ class TestCropTensorOp(OpTest): ...@@ -51,6 +51,7 @@ class TestCropTensorOp(OpTest):
self.offset_by_input = False self.offset_by_input = False
self.unk_dim_idx = -1 self.unk_dim_idx = -1
self.attrs = {} self.attrs = {}
self.python_api = paddle.crop
self.initTestCase() self.initTestCase()
if self.shape_by_input: if self.shape_by_input:
...@@ -146,6 +147,7 @@ class TestCropTensorOpTensorAttr(OpTest): ...@@ -146,6 +147,7 @@ class TestCropTensorOpTensorAttr(OpTest):
self.OffsetsTensor = False self.OffsetsTensor = False
self.ShapeTensor = True self.ShapeTensor = True
self.attrs = {} self.attrs = {}
self.python_api = paddle.crop
self.initTestCase() self.initTestCase()
if self.ShapeTensor: if self.ShapeTensor:
......
...@@ -640,6 +640,7 @@ def crop(x, shape=None, offsets=None, name=None): ...@@ -640,6 +640,7 @@ def crop(x, shape=None, offsets=None, name=None):
# if offsets = [1, 1], out = [[5,6], [8,9]] # if offsets = [1, 1], out = [[5,6], [8,9]]
""" """
helper = LayerHelper('crop_tensor', **locals()) helper = LayerHelper('crop_tensor', **locals())
check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'], check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'],
'crop_tensor') 'crop_tensor')
...@@ -650,6 +651,9 @@ def crop(x, shape=None, offsets=None, name=None): ...@@ -650,6 +651,9 @@ def crop(x, shape=None, offsets=None, name=None):
if offsets is None: if offsets is None:
offsets = [0] * len(x.shape) offsets = [0] * len(x.shape)
if in_dygraph_mode():
return _C_ops.final_state_crop_tensor(x, shape, offsets)
out = helper.create_variable_for_type_inference(x.dtype) out = helper.create_variable_for_type_inference(x.dtype)
ipts = {'X': x} ipts = {'X': x}
attrs = {} attrs = {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册