add kernel for unsqueeze_op and Add unsqueezed op test, test=develop (#19436)
* add kernel for unsqueeze_op, test=develop * add kernel for unsqueeze_op, test=develop * add kernel for unsqueeze_op, test=developnew_fix
parent
a7691603a5
commit
5f627488db
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,45 @@
|
||||
/* 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. */
|
||||
|
||||
#include "paddle/fluid/operators/unsqueeze_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
unsqueeze, ops::UnsqueezeKernel<paddle::platform::CUDADeviceContext, float>,
|
||||
ops::UnsqueezeKernel<paddle::platform::CUDADeviceContext, double>,
|
||||
ops::UnsqueezeKernel<paddle::platform::CUDADeviceContext, int>,
|
||||
ops::UnsqueezeKernel<paddle::platform::CUDADeviceContext, int8_t>,
|
||||
ops::UnsqueezeKernel<paddle::platform::CUDADeviceContext, int64_t>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
unsqueeze_grad,
|
||||
ops::UnsqueezeGradKernel<paddle::platform::CUDADeviceContext, float>,
|
||||
ops::UnsqueezeGradKernel<paddle::platform::CUDADeviceContext, double>,
|
||||
ops::UnsqueezeGradKernel<paddle::platform::CUDADeviceContext, int>,
|
||||
ops::UnsqueezeGradKernel<paddle::platform::CUDADeviceContext, int8_t>,
|
||||
ops::UnsqueezeGradKernel<paddle::platform::CUDADeviceContext, int64_t>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
unsqueeze2,
|
||||
ops::Unsqueeze2Kernel<paddle::platform::CUDADeviceContext, float>,
|
||||
ops::Unsqueeze2Kernel<paddle::platform::CUDADeviceContext, double>,
|
||||
ops::Unsqueeze2Kernel<paddle::platform::CUDADeviceContext, int>,
|
||||
ops::Unsqueeze2Kernel<paddle::platform::CUDADeviceContext, int8_t>,
|
||||
ops::Unsqueeze2Kernel<paddle::platform::CUDADeviceContext, int64_t>);
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
unsqueeze2_grad,
|
||||
ops::Unsqueeze2GradKernel<paddle::platform::CUDADeviceContext, float>,
|
||||
ops::Unsqueeze2GradKernel<paddle::platform::CUDADeviceContext, double>,
|
||||
ops::Unsqueeze2GradKernel<paddle::platform::CUDADeviceContext, int>,
|
||||
ops::Unsqueeze2GradKernel<paddle::platform::CUDADeviceContext, int8_t>,
|
||||
ops::Unsqueeze2GradKernel<paddle::platform::CUDADeviceContext, int64_t>);
|
@ -0,0 +1,137 @@
|
||||
/* 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 <vector>
|
||||
#include "paddle/fluid/framework/op_registry.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"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class UnsqueezeKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &context) const override {
|
||||
auto &axes = context.Attr<std::vector<int>>("axes");
|
||||
auto *in = context.Input<framework::LoDTensor>("X");
|
||||
auto *out = context.Output<framework::LoDTensor>("Out");
|
||||
auto x_dims = in->dims();
|
||||
auto out_dims = GetOutputShape(axes, x_dims);
|
||||
|
||||
out->mutable_data(context.GetPlace(), in->type());
|
||||
framework::TensorCopy(
|
||||
*in, context.GetPlace(),
|
||||
context.template device_context<platform::DeviceContext>(), out);
|
||||
out->Resize(out_dims);
|
||||
}
|
||||
|
||||
static framework::DDim GetOutputShape(const std::vector<int> unsqz_dims,
|
||||
const framework::DDim &in_dims) {
|
||||
int output_size = in_dims.size() + static_cast<int>(unsqz_dims.size());
|
||||
int cur_output_size = in_dims.size();
|
||||
std::vector<int64_t> output_shape(output_size, 0);
|
||||
|
||||
// Validity Check: rank range.
|
||||
PADDLE_ENFORCE_LE(output_size, 6,
|
||||
"The output tensor's rank should be less than 6.");
|
||||
|
||||
for (int axis : unsqz_dims) {
|
||||
int cur = axis < 0 ? axis + cur_output_size + 1 : axis;
|
||||
// Vaildity Check: the axis bound
|
||||
PADDLE_ENFORCE_GE(cur, 0);
|
||||
PADDLE_ENFORCE_LE(cur, cur_output_size);
|
||||
// Move old axis, and insert new axis
|
||||
for (int i = cur_output_size; i >= cur; --i) {
|
||||
if (output_shape[i] == 1) {
|
||||
// Move axis
|
||||
output_shape[i + 1] = 1;
|
||||
output_shape[i] = 0;
|
||||
}
|
||||
}
|
||||
output_shape[cur] = 1;
|
||||
// Add the output size.
|
||||
cur_output_size++;
|
||||
}
|
||||
|
||||
// Make output shape
|
||||
for (int in_idx = 0, out_idx = 0; out_idx < output_size; ++out_idx) {
|
||||
if (output_shape[out_idx] == 0) {
|
||||
output_shape[out_idx] = in_dims[in_idx++];
|
||||
}
|
||||
}
|
||||
|
||||
return framework::make_ddim(output_shape);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class UnsqueezeGradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *d_out =
|
||||
ctx.Input<framework::LoDTensor>(framework::GradVarName("Out"));
|
||||
auto *d_x = ctx.Output<framework::LoDTensor>(framework::GradVarName("X"));
|
||||
auto in_dims = ctx.Input<framework::LoDTensor>("X")->dims();
|
||||
|
||||
d_x->mutable_data(ctx.GetPlace(), d_out->type());
|
||||
framework::TensorCopySync(*d_out, ctx.GetPlace(), d_x);
|
||||
d_x->Resize(in_dims);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class Unsqueeze2Kernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &context) const override {
|
||||
auto *out = context.Output<framework::LoDTensor>("Out");
|
||||
auto *in = context.Input<framework::LoDTensor>("X");
|
||||
|
||||
auto &axes = context.Attr<std::vector<int>>("axes");
|
||||
|
||||
auto x_dims = in->dims();
|
||||
auto out_dims =
|
||||
UnsqueezeKernel<DeviceContext, T>::GetOutputShape(axes, x_dims);
|
||||
|
||||
out->mutable_data(context.GetPlace(), in->type());
|
||||
framework::TensorCopy(
|
||||
*in, context.GetPlace(),
|
||||
context.template device_context<platform::DeviceContext>(), out);
|
||||
out->Resize(out_dims);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class Unsqueeze2GradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *d_out =
|
||||
ctx.Input<framework::LoDTensor>(framework::GradVarName("Out"));
|
||||
auto *d_x = ctx.Output<framework::LoDTensor>(framework::GradVarName("X"));
|
||||
// auto in_dims = d_x->dims();
|
||||
|
||||
auto xshape_dims = ctx.Input<framework::LoDTensor>("XShape")->dims();
|
||||
auto x_dims = framework::slice_ddim(xshape_dims, 1, xshape_dims.size());
|
||||
|
||||
d_x->mutable_data(ctx.GetPlace(), d_out->type());
|
||||
framework::TensorCopySync(*d_out, ctx.GetPlace(), d_x);
|
||||
d_x->Resize(x_dims);
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,83 @@
|
||||
# Copyright (c) 2018 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.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
# Correct: General.
|
||||
class TestUnsqueezeOp(OpTest):
|
||||
def setUp(self):
|
||||
self.init_test_case()
|
||||
self.op_type = "unsqueeze2"
|
||||
self.inputs = {"X": np.random.random(self.ori_shape).astype("float32")}
|
||||
self.init_attrs()
|
||||
self.outputs = {
|
||||
"Out": self.inputs["X"].reshape(self.new_shape),
|
||||
"XShape": np.random.random(self.ori_shape).astype("float32")
|
||||
}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output(no_check_set=["XShape"])
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(["X"], "Out")
|
||||
|
||||
def init_test_case(self):
|
||||
self.ori_shape = (3, 5)
|
||||
self.axes = (1, 2)
|
||||
self.new_shape = (3, 1, 1, 5)
|
||||
|
||||
def init_attrs(self):
|
||||
self.attrs = {"axes": self.axes}
|
||||
|
||||
|
||||
# Correct: Single input index.
|
||||
class TestUnsqueezeOp1(TestUnsqueezeOp):
|
||||
def init_test_case(self):
|
||||
self.ori_shape = (3, 5)
|
||||
self.axes = (-1, )
|
||||
self.new_shape = (3, 5, 1)
|
||||
|
||||
|
||||
# Correct: Mixed input axis.
|
||||
class TestUnsqueezeOp2(TestUnsqueezeOp):
|
||||
def init_test_case(self):
|
||||
self.ori_shape = (3, 5)
|
||||
self.axes = (0, -1)
|
||||
self.new_shape = (1, 3, 5, 1)
|
||||
|
||||
|
||||
# Correct: There is duplicated axis.
|
||||
class TestUnsqueezeOp3(TestUnsqueezeOp):
|
||||
def init_test_case(self):
|
||||
self.ori_shape = (3, 2, 5)
|
||||
self.axes = (0, 3, 3)
|
||||
self.new_shape = (1, 3, 2, 1, 1, 5)
|
||||
|
||||
|
||||
# Correct: Reversed axes.
|
||||
class TestUnsqueezeOp4(TestUnsqueezeOp):
|
||||
def init_test_case(self):
|
||||
self.ori_shape = (3, 2, 5)
|
||||
self.axes = (3, 1, 1)
|
||||
self.new_shape = (3, 1, 1, 2, 5, 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
Reference in new issue