From 074065e5de113d548bb3552e26f73fe67627aec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=96=E9=B1=BC=E7=9A=84=E5=93=B2=E5=AD=A6?= Date: Fri, 4 Dec 2020 18:25:42 +0800 Subject: [PATCH] fix expand/uniform_random && concat/transpose to new api on xpu (#29280) * fix expand && concat/transpose to new api * update uniform_random_op * update xpu_header --- paddle/fluid/operators/concat_op_xpu.cc | 127 ++++++++++-------- .../fluid/operators/deformable_conv_op_xpu.cc | 2 - paddle/fluid/operators/expand_op.h | 16 ++- paddle/fluid/operators/transpose_op_xpu.cc | 100 +------------- .../fluid/operators/uniform_random_op_xpu.cc | 51 +++++-- paddle/fluid/platform/xpu_header.h | 1 + .../tests/unittests/xpu/test_concat_op_xpu.py | 98 +------------- .../unittests/xpu/test_transpose_op_xpu.py | 116 +--------------- 8 files changed, 150 insertions(+), 361 deletions(-) diff --git a/paddle/fluid/operators/concat_op_xpu.cc b/paddle/fluid/operators/concat_op_xpu.cc index 9c9c72c7f6..0558f09a17 100644 --- a/paddle/fluid/operators/concat_op_xpu.cc +++ b/paddle/fluid/operators/concat_op_xpu.cc @@ -11,18 +11,12 @@ 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. */ - +#ifdef PADDLE_WITH_XPU #include "paddle/fluid/operators/concat_op.h" - #include #include #include - -#ifdef PADDLE_WITH_MKLDNN -#include -#endif - -#ifdef PADDLE_WITH_XPU +#include "paddle/fluid/platform/xpu_header.h" namespace paddle { namespace operators { @@ -32,8 +26,8 @@ template class ConcatXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - auto ins = ctx.MultiInput("X"); - framework::Tensor* out = ctx.Output("Out"); + auto ins = ctx.MultiInput("X"); + framework::LoDTensor* out = ctx.Output("Out"); int axis = ctx.Attr("axis"); PADDLE_ENFORCE_NE(ins[0], nullptr, platform::errors::InvalidArgument( "The input should not be null.")); @@ -47,6 +41,7 @@ class ConcatXPUKernel : public framework::OpKernel { PADDLE_ENFORCE_LT(axis, ins[0]->dims().size(), platform::errors::InvalidArgument( "concat: axis shoud < ins[0]->dims()!")); + auto place = ctx.GetPlace(); out->mutable_data(place); std::vector choose_idx; @@ -57,43 +52,54 @@ class ConcatXPUKernel : public framework::OpKernel { n++; } } - PADDLE_ENFORCE_LE(n, 8, platform::errors::InvalidArgument( - "XPU only surpport at most 8 tensors for now")); PADDLE_ENFORCE_GT( n, 0, platform::errors::InvalidArgument("No tensor need concat?")); - int h = 1; - int w_except_axis = 1; - for (int i = 0; i < axis; ++i) { - h *= (ins[choose_idx[0]]->dims())[i]; - } - for (int i = axis + 1; i < ins[0]->dims().size(); ++i) { - w_except_axis *= (ins[choose_idx[0]]->dims())[i]; - } - for (int i = 1; i < n; ++i) { - int hh = 1; - int ww = 1; - for (int j = 0; j < axis; ++j) { - hh *= (ins[choose_idx[i]]->dims())[j]; + + // If axis is 0, the lod of the output is not the same as inputs. + if (axis == 0 && ins[0]->lod().size() > 0) { + size_t lod_size_0 = ins[0]->lod().size(); + size_t lod_size = lod_size_0; + for (size_t i = 1; i < ins.size(); ++i) { + if (ins[i]->lod().size() > 0) { + PADDLE_ENFORCE_EQ( + ins[i]->lod().size(), lod_size_0, + platform::errors::Unimplemented( + "The lod level of all input LoDTensors should be same. " + "Maybe different lod level of input LoDTensors can concat," + "it is not supported currently. The lod level of %dth input " + "is %d and first input is %d.", + i, ins[i]->lod().size(), lod_size_0)); + } else { + lod_size = 0; + break; + } } - for (int j = axis + 1; j < ins[i]->dims().size(); ++j) { - ww *= (ins[choose_idx[i]]->dims())[j]; + if (lod_size) { + auto* out_lod = out->mutable_lod(); + for (size_t i = 1; i < ins.size(); ++i) { + auto in_lod = ConvertToLengthBasedLoD(ins[i]->lod()); + AppendLoD(out_lod, in_lod); + } } - PADDLE_ENFORCE_EQ(hh, h, platform::errors::InvalidArgument( - "concat: h should be eual!")); - PADDLE_ENFORCE_EQ(ww, w_except_axis, - platform::errors::InvalidArgument( - "concat: w should be eual except for axis!")); } + + auto input_dims = ins[0]->dims(); + std::vector> xdims_list(n); + for (int i = 0; i < n; ++i) { + std::vector tmp_dims(input_dims.size()); + for (int j = 0; j < input_dims.size(); ++j) { + tmp_dims[j] = ins[i]->dims()[j]; + } + xdims_list[i] = tmp_dims; + } + auto& dev_ctx = ctx.template device_context(); - std::unique_ptr in_w_host(new int[n]); - std::unique_ptr ptrs(new const float*[n]); + std::vector ptrs; for (int i = 0; i < n; ++i) { - ptrs[i] = ins[choose_idx[i]]->data(); - in_w_host[i] = w_except_axis * (ins[choose_idx[i]]->dims())[axis]; + ptrs.push_back(ins[choose_idx[i]]->data()); } - int r = - xpu::concat(dev_ctx.x_context(), h, (const int*)in_w_host.get(), - n, (const float**)ptrs.get(), out->data()); + int r = xpu::concat(dev_ctx.x_context(), ptrs, out->data(), + xdims_list, axis); PADDLE_ENFORCE_EQ( r, XPU_SUCCESS, platform::errors::External( @@ -102,6 +108,7 @@ class ConcatXPUKernel : public framework::OpKernel { r)); } }; + template class ConcatGradXPUKernel : public framework::OpKernel { public: @@ -132,13 +139,15 @@ class ConcatGradXPUKernel : public framework::OpKernel { static_cast(ins[0]->dims().size())); // get output tensor that the name is not kEmptyVarName std::vector outputs; + std::vector choose_idx; + int n = 0; for (size_t j = 0; j < outs.size(); ++j) { if (out_var_names[j] != framework::kEmptyVarName && outs[j]->numel() != 0UL) { outs[j]->mutable_data(ctx.GetPlace()); outputs.push_back(outs[j]); - } else { - outputs.push_back(nullptr); + choose_idx.push_back(j); + n++; } } PADDLE_ENFORCE_GE(axis, 0, platform::errors::InvalidArgument( @@ -146,23 +155,31 @@ class ConcatGradXPUKernel : public framework::OpKernel { PADDLE_ENFORCE_LT(axis, out_grad->dims().size(), platform::errors::InvalidArgument( "concat_grad: axis shoud < ins[0]->dims()!")); - auto out_grad_stride = framework::stride_numel(out_grad->dims()); - int n = outputs.size(); - PADDLE_ENFORCE_LE(n, 16, - platform::errors::InvalidArgument( - "XPU only surpport at most 16 tensors for now")); - int h = out_grad_stride[0] / out_grad_stride[axis]; - auto& dev_ctx = ctx.template device_context(); - std::unique_ptr in_w_host(new int[n]); - std::unique_ptr ptrs(new float*[n]); + + auto input_dims = ins[0]->dims(); + std::vector split_list(n); + std::vector xdims_list(input_dims.size()); + int total_length = 0; + for (int i = 0; i < n; ++i) { + split_list[i] = ins[i]->dims()[axis]; + total_length += ins[i]->dims()[axis]; + } + for (int i = 0; i < input_dims.size(); ++i) { + if (i == axis) { + continue; + } + xdims_list[i] = input_dims[i]; + } + xdims_list[axis] = total_length; + + std::vector ptrs(n); for (int i = 0; i < n; ++i) { - auto out_stride = framework::stride_numel(outputs[i]->dims()); ptrs[i] = outputs[i]->data(); - in_w_host[i] = out_stride[axis]; } - int r = xpu::concat_grad(dev_ctx.x_context(), h, in_w_host.get(), n, - reinterpret_cast(ptrs.get()), - out_grad->data()); + + auto& dev_ctx = ctx.template device_context(); + int r = xpu::split(dev_ctx.x_context(), out_grad->data(), ptrs, + xdims_list, split_list, axis); PADDLE_ENFORCE_EQ( r, XPU_SUCCESS, platform::errors::External( diff --git a/paddle/fluid/operators/deformable_conv_op_xpu.cc b/paddle/fluid/operators/deformable_conv_op_xpu.cc index 8dc5e59ee9..18bab83b0e 100644 --- a/paddle/fluid/operators/deformable_conv_op_xpu.cc +++ b/paddle/fluid/operators/deformable_conv_op_xpu.cc @@ -17,8 +17,6 @@ limitations under the License. */ #include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/platform/xpu_header.h" -#include "xpu/refactor/math.h" -#include "xpu/refactor/nn.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/expand_op.h b/paddle/fluid/operators/expand_op.h index b3ff4ee198..8b79a1feb8 100644 --- a/paddle/fluid/operators/expand_op.h +++ b/paddle/fluid/operators/expand_op.h @@ -56,6 +56,12 @@ inline std::vector get_expand_times( TensorCopySync(*expand_tensor, platform::CPUPlace(), &cpu_expand_tensor); expand_data = cpu_expand_tensor.data(); } +#ifdef PADDLE_WITH_XPU + if (platform::is_xpu_place(expand_tensor->place())) { + TensorCopySync(*expand_tensor, platform::CPUPlace(), &cpu_expand_tensor); + expand_data = cpu_expand_tensor.data(); + } +#endif auto vec_epxand_times = std::vector(expand_data, expand_data + expand_tensor->numel()); return vec_epxand_times; @@ -72,7 +78,15 @@ inline std::vector get_expand_times( framework::Tensor temp; TensorCopySync(*tensor, platform::CPUPlace(), &temp); vec_epxand_times.push_back(*temp.data()); - } else { + } +#ifdef PADDLE_WITH_XPU + else if (platform::is_xpu_place(tensor->place())) { // NOLINT + framework::Tensor temp; + TensorCopySync(*tensor, platform::CPUPlace(), &temp); + vec_epxand_times.push_back(*temp.data()); + } +#endif + else { // NOLINT vec_epxand_times.push_back(*tensor->data()); } } diff --git a/paddle/fluid/operators/transpose_op_xpu.cc b/paddle/fluid/operators/transpose_op_xpu.cc index c7ecf2ebfa..2748c07f9e 100644 --- a/paddle/fluid/operators/transpose_op_xpu.cc +++ b/paddle/fluid/operators/transpose_op_xpu.cc @@ -17,105 +17,27 @@ limitations under the License. */ #include #include #include +#include "paddle/fluid/platform/xpu_header.h" namespace paddle { namespace operators { using framework::Tensor; -bool XPUSupported(int ndims, const std::vector& axis) { - /* - * XPU currently support: - * permute = {0, 2, 1}, permute = {1, 0}, - * permute = {0, 2, 1, 3}, permute = {1, 0, 2}, - * permute = {0, 2, 3, 1} - */ - bool is_supported = false; - std::vector permute_10(2, 0); - std::vector permute_102(3, 0); - std::vector permute_021(3, 0); - std::vector permute_210(3, 0); - std::vector permute_0213(4, 0); - std::vector permute_0231(4, 0); - std::vector permute_0312(4, 0); - std::vector permute_3201(4, 0); - permute_10[0] = 1; - permute_102[0] = 1; - permute_102[2] = 2; - permute_021[1] = 2; - permute_021[2] = 1; - permute_210[0] = 2; - permute_210[1] = 1; - permute_0213[1] = 2; - permute_0213[2] = 1; - permute_0213[3] = 3; - permute_0231[1] = 2; - permute_0231[2] = 3; - permute_0231[3] = 1; - permute_0312[1] = 3; - permute_0312[2] = 1; - permute_0312[3] = 2; - permute_3201[0] = 3; - permute_3201[1] = 2; - permute_3201[3] = 1; - switch (ndims) { - case 2: - if (axis == permute_10) { - is_supported = true; - } - break; - case 3: - if ((axis == permute_021) || (axis == permute_102) || - (axis == permute_210)) { - is_supported = true; - } - break; - case 4: - if ((axis == permute_0213) || (axis == permute_0231) || - (axis == permute_0312) || (axis == permute_3201)) { - is_supported = true; - } - break; - default: - PADDLE_THROW(platform::errors::Unimplemented( - "Tensors with rank only 2, 3 and 4 are supported on XPU")); - } - return is_supported; -} - template class TransposeXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto x = context.Input("X"); auto out = context.Output("Out"); + // axis is permute auto axis = context.Attr>("axis"); int ndims = axis.size(); const auto x_dims = x->dims(); - const T* x_data = x->data(); T* y_data = out->mutable_data(context.GetPlace()); - if (!XPUSupported(ndims, axis)) { - VLOG(0) << "XPU does not support the permute, try to do on cpu"; - framework::Tensor x_cpu; - framework::Tensor out_cpu; - auto x_cpu_data = x_cpu.mutable_data(x->dims(), platform::CPUPlace()); - auto out_cpu_data = - out_cpu.mutable_data(out->dims(), platform::CPUPlace()); - memory::Copy(platform::CPUPlace(), reinterpret_cast(x_cpu_data), - BOOST_GET_CONST(platform::XPUPlace, context.GetPlace()), - (const void*)x_data, x->numel() * sizeof(T)); - - const platform::CPUDeviceContext* cpu_dev_ctx = - static_cast( - platform::DeviceContextPool::Instance().Get( - platform::CPUPlace())); - TransCompute(ndims, *cpu_dev_ctx, x_cpu, - &out_cpu, axis); - memory::Copy(BOOST_GET_CONST(platform::XPUPlace, context.GetPlace()), - reinterpret_cast(y_data), platform::CPUPlace(), - (const void*)out_cpu_data, out->numel() * sizeof(T)); + if (out->numel() == 0) { return; } @@ -123,10 +45,9 @@ class TransposeXPUKernel : public framework::OpKernel { for (int i = 0; i < ndims; ++i) { x_shape_host[i] = x_dims[i]; } - int* permute_host = axis.data(); auto& dev_ctx = context.template device_context(); - int r = xpu::transpose(dev_ctx.x_context(), x_data, y_data, - x_shape_host.data(), permute_host, ndims); + int r = xpu::transpose(dev_ctx.x_context(), x_data, y_data, x_shape_host, + axis); PADDLE_ENFORCE_EQ( r, xpu::Error_t::SUCCESS, platform::errors::External("XPU kernel error! error code=%d", r)); @@ -151,20 +72,13 @@ class TransposeGradXPUKernel : public framework::OpKernel { } int ndims = axis.size(); - if (!XPUSupported(ndims, reversed_axis)) { - PADDLE_THROW( - platform::errors::Unimplemented("XPU does not support the permute")); - } - std::vector out_shape_host(ndims, 0); for (int i = 0; i < ndims; ++i) { out_shape_host[i] = out_grad->dims()[i]; } - int* permute_host = reversed_axis.data(); auto& dev_ctx = context.template device_context(); - int r = xpu::transpose(dev_ctx.x_context(), out_grad->data(), - x_grad->data(), out_shape_host.data(), - permute_host, ndims); + int r = xpu::transpose(dev_ctx.x_context(), out_grad->data(), + x_grad->data(), out_shape_host, reversed_axis); PADDLE_ENFORCE_EQ( r, xpu::Error_t::SUCCESS, platform::errors::External("XPU kernel error! error code=%d", r)); diff --git a/paddle/fluid/operators/uniform_random_op_xpu.cc b/paddle/fluid/operators/uniform_random_op_xpu.cc index 507bd7e9ea..d8b82ad5f8 100644 --- a/paddle/fluid/operators/uniform_random_op_xpu.cc +++ b/paddle/fluid/operators/uniform_random_op_xpu.cc @@ -29,37 +29,68 @@ class XPUUniformRandomKernel : public framework::OpKernel { void Compute(const framework::ExecutionContext &ctx) const override { framework::Tensor *tensor = nullptr; auto out_var = ctx.OutputVar("Out"); - if (out_var->IsType()) { - tensor = out_var->GetMutable(); - } else if (out_var->IsType()) { - auto shape = ctx.Attr>("shape"); + std::vector new_shape; + auto list_new_shape_tensor = + ctx.MultiInput("ShapeTensorList"); + if (list_new_shape_tensor.size() > 0 || ctx.HasInput("ShapeTensor")) { + if (ctx.HasInput("ShapeTensor")) { + auto *shape_tensor = ctx.Input("ShapeTensor"); + new_shape = GetNewDataFromShapeTensor(shape_tensor); + } else if (list_new_shape_tensor.size() > 0) { + new_shape = GetNewDataFromShapeTensorList(list_new_shape_tensor); + } + } + + if (out_var->IsType()) { auto *selected_rows = out_var->GetMutable(); tensor = selected_rows->mutable_value(); + auto shape = ctx.Attr>("shape"); + if (!new_shape.empty()) shape = new_shape; tensor->Resize(framework::make_ddim(shape)); selected_rows->mutable_rows()->reserve(shape[0]); + } else if (out_var->IsType()) { + tensor = out_var->GetMutable(); + if (!new_shape.empty()) tensor->Resize(framework::make_ddim(new_shape)); } else { PADDLE_THROW(platform::errors::InvalidArgument( - "Expected type of Output(out) in uniform_random_op must be " - "LoDTensor, " - "SelectedRows. But got unsupport type: %s.", + "Expected type of Output(out) in uniform_random_op must be Tensor, " + "SelectedRows. But got " + "unsupport type: %s.", framework::ToTypeName(out_var->Type()))); } T *data = tensor->mutable_data(ctx.GetPlace()); int64_t size = tensor->numel(); + std::unique_ptr data_cpu(new T[size]); std::uniform_real_distribution dist( static_cast(ctx.Attr("min")), static_cast(ctx.Attr("max"))); unsigned int seed = static_cast(ctx.Attr("seed")); - // TODO(pangyoki): implement GetXPURandomEngine to set different seeds on - // corresponding XPU device. auto engine = framework::GetCPURandomEngine(seed); - std::unique_ptr data_cpu(new T[size]); for (int64_t i = 0; i < size; ++i) { data_cpu[i] = dist(*engine); } + unsigned int diag_num = + static_cast(ctx.Attr("diag_num")); + unsigned int diag_step = + static_cast(ctx.Attr("diag_step")); + auto diag_val = static_cast(ctx.Attr("diag_val")); + if (diag_num > 0) { + PADDLE_ENFORCE_GT( + size, (diag_num - 1) * (diag_step + 1), + platform::errors::InvalidArgument( + "ShapeInvalid: the diagonal's elements is equal (num-1) " + "* (step-1) with num %d, step %d," + "It should be smaller than %d, but received %d", + diag_num, diag_step, (diag_num - 1) * (diag_step + 1), size)); + for (int64_t i = 0; i < diag_num; ++i) { + int64_t pos = i * diag_step + i; + data_cpu[pos] = diag_val; + } + } + memory::Copy(BOOST_GET_CONST(platform::XPUPlace, ctx.GetPlace()), data, platform::CPUPlace(), reinterpret_cast(data_cpu.get()), size * sizeof(T)); diff --git a/paddle/fluid/platform/xpu_header.h b/paddle/fluid/platform/xpu_header.h index bce82b897f..98bd019ad9 100644 --- a/paddle/fluid/platform/xpu_header.h +++ b/paddle/fluid/platform/xpu_header.h @@ -21,6 +21,7 @@ #include "paddle/fluid/platform/errors.h" #include "xpu/api.h" +#include "xpu/refactor/math.h" #include "xpu/refactor/nn.h" #include "xpu/runtime.h" #include "xpu/runtime_ex.h" diff --git a/python/paddle/fluid/tests/unittests/xpu/test_concat_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_concat_op_xpu.py index bb5d7134a1..b1a5e422ac 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_concat_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_concat_op_xpu.py @@ -19,16 +19,20 @@ import sys sys.path.append("..") import unittest import numpy as np + from op_test import OpTest, skip_check_grad_ci +from op_test_xpu import XPUOpTest import paddle.fluid as fluid from paddle.fluid import compiler, Program, program_guard, core import paddle -class TestConcatOp(OpTest): +class TestConcatOp(XPUOpTest): def setUp(self): self.op_type = "concat" self.dtype = self.get_dtype() + self.use_xpu = True + self.use_mkldnn = False self.init_test_data() self.inputs = {'X': [('x0', self.x0), ('x1', self.x1), ('x2', self.x2)]} self.attrs = {'axis': self.axis} @@ -44,7 +48,7 @@ class TestConcatOp(OpTest): } def get_dtype(self): - return "float64" + return "float32" def test_check_output(self): if paddle.is_compiled_with_xpu(): @@ -131,7 +135,7 @@ class TestConcatOp6(TestConcatOp): def test_check_output(self): if paddle.is_compiled_with_xpu(): place = paddle.XPUPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_dygraph=False) def test_check_grad(self): if paddle.is_compiled_with_xpu(): @@ -147,94 +151,6 @@ class TestConcatOp6(TestConcatOp): self.axis = 0 -class TestConcatOpError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - # The input type of concat_op should be list. - x1 = fluid.layers.data(shape=[4], dtype='int32', name='x1') - fluid.layers.concat(x1) - # The item in input must be Variable. - x2 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace()) - x3 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace()) - self.assertRaises(TypeError, fluid.layers.concat, [x2]) - # The input dtype of concat_op must be float16, float32, float64, int32, int64. - x4 = fluid.layers.data(shape=[4], dtype='uint8', name='x4') - x5 = fluid.layers.data(shape=[4], dtype='uint8', name='x5') - self.assertRaises(TypeError, fluid.layers.concat, [x4, x5]) - x6 = fluid.layers.data(shape=[4], dtype='float16', name='x6') - x7 = fluid.layers.data(shape=[4], dtype='float16', name='x7') - x8 = fluid.layers.data(shape=[4], dtype='float32', name='x8') - fluid.layers.concat([x6, x7]) - - # The type of axis in concat_op should be int or Variable. - def test_axis_type(): - fluid.layers.concat([x6, x7], 3.2) - - self.assertRaises(TypeError, test_axis_type) - - def test_input_same_dtype(): - fluid.layers.concat([x7, x8]) - - self.assertRaises(TypeError, test_input_same_dtype) - - -class TestConcatAPI(unittest.TestCase): - def test_fluid_api(self): - x_1 = fluid.data(shape=[None, 1, 4, 5], dtype='float32', name='x_1') - fluid.layers.concat([x_1, x_1], 0) - - input_2 = np.random.random([2, 1, 4, 5]).astype("float32") - input_3 = np.random.random([2, 2, 4, 5]).astype("float32") - x_2 = fluid.data(shape=[2, 1, 4, 5], dtype='float32', name='x_2') - x_3 = fluid.data(shape=[2, 2, 4, 5], dtype='float32', name='x_3') - positive_1_int32 = fluid.layers.fill_constant([1], "float32", 1) - positive_1_int64 = fluid.layers.fill_constant([1], "float32", 1) - out_1 = fluid.layers.concat(input=[x_2, x_3], axis=1) - out_2 = fluid.layers.concat(input=[x_2, x_3], axis=1) - out_3 = fluid.layers.concat(input=[x_2, x_3], axis=1) - - exe = fluid.Executor(place=fluid.XPUPlace(0)) - [res_1, res_2, res_3] = exe.run( - fluid.default_main_program(), - feed={"x_1": input_2, - "x_2": input_2, - "x_3": input_3}, - fetch_list=[out_1, out_2, out_3]) - assert np.array_equal(res_1, np.concatenate((input_2, input_3), axis=1)) - assert np.array_equal(res_2, np.concatenate((input_2, input_3), axis=1)) - assert np.array_equal(res_3, np.concatenate((input_2, input_3), axis=1)) - - def test_errors(self): - with program_guard(Program(), Program()): - # The item in input must be Variable. - x2 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.XPUPlace(0)) - x3 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.XPUPlace(0)) - self.assertRaises(TypeError, paddle.concat, [x2]) - # The input dtype of concat_op must be float32. - x4 = fluid.data(shape=[4], dtype='uint8', name='x4') - x5 = fluid.data(shape=[4], dtype='uint8', name='x5') - self.assertRaises(TypeError, fluid.layers.concat, [x4, x5]) - - # The type of axis in concat_op should be int or Variable. - x6 = fluid.layers.data(shape=[4], dtype='float16', name='x6') - x7 = fluid.layers.data(shape=[4], dtype='float16', name='x7') - x8 = fluid.layers.data(shape=[4], dtype='float32', name='x8') - - def test_axis_type(): - paddle.concat([x6, x7], 3.2) - - self.assertRaises(TypeError, test_axis_type) - - def test_input_same_dtype(): - paddle.concat([x7, x8]) - - self.assertRaises(TypeError, test_input_same_dtype) - - if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_transpose_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_transpose_op_xpu.py index c191e5f0b2..41df4481e2 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_transpose_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_transpose_op_xpu.py @@ -19,24 +19,27 @@ import numpy as np import sys sys.path.append("..") -from op_test import OpTest +from op_test_xpu import OpTest, XPUOpTest import paddle +import paddle.fluid.core as core import paddle.fluid as fluid from paddle.fluid import compiler, Program, program_guard -class TestXPUTransposeOp(OpTest): +class TestXPUTransposeOp(XPUOpTest): def setUp(self): self.init_op_type() self.initTestCase() - self.inputs = {'X': np.random.random(self.shape).astype("float64")} + self.use_xpu = True + self.use_mkldnn = False + self.inputs = {'X': np.random.random(self.shape).astype("float32")} self.attrs = { 'axis': list(self.axis), 'use_mkldnn': False, 'use_xpu': True } self.outputs = { - 'XShape': np.random.random(self.shape).astype("float64"), + 'XShape': np.random.random(self.shape).astype("float32"), 'Out': self.inputs['X'].transpose(self.axis) } @@ -121,110 +124,5 @@ class TestCase9(TestXPUTransposeOp): self.axis = (6, 1, 3, 5, 0, 2, 4, 7) -class TestTransposeOpError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - x = fluid.layers.data(name='x', shape=[10, 5, 3], dtype='float64') - - def test_x_Variable_check(): - # the Input(x)'s type must be Variable - fluid.layers.transpose("not_variable", perm=[1, 0, 2]) - - self.assertRaises(TypeError, test_x_Variable_check) - - def test_x_dtype_check(): - # the Input(x)'s dtype must be one of [float16, float32, float64, int32, int64] - x1 = fluid.layers.data( - name='x1', shape=[10, 5, 3], dtype='bool') - fluid.layers.transpose(x1, perm=[1, 0, 2]) - - self.assertRaises(TypeError, test_x_dtype_check) - - def test_perm_list_check(): - # Input(perm)'s type must be list - fluid.layers.transpose(x, perm="[1, 0, 2]") - - self.assertRaises(TypeError, test_perm_list_check) - - def test_perm_length_and_x_dim_check(): - # Input(perm) is the permutation of dimensions of Input(input) - # its length should be equal to dimensions of Input(input) - fluid.layers.transpose(x, perm=[1, 0, 2, 3, 4]) - - self.assertRaises(ValueError, test_perm_length_and_x_dim_check) - - def test_each_elem_value_check(): - # Each element in Input(perm) should be less than Input(x)'s dimension - fluid.layers.transpose(x, perm=[3, 5, 7]) - - self.assertRaises(ValueError, test_each_elem_value_check) - - -class TestTAPI(unittest.TestCase): - def test_out(self): - with fluid.program_guard(fluid.Program()): - data = fluid.data(shape=[10], dtype="float64", name="data") - data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - data_np = np.random.random([10]).astype("float64") - result, = exe.run(feed={"data": data_np}, fetch_list=[data_t]) - expected_result = np.transpose(data_np) - self.assertEqual((result == expected_result).all(), True) - - with fluid.program_guard(fluid.Program()): - data = fluid.data(shape=[10, 5], dtype="float64", name="data") - data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - data_np = np.random.random([10, 5]).astype("float64") - result, = exe.run(feed={"data": data_np}, fetch_list=[data_t]) - expected_result = np.transpose(data_np) - self.assertEqual((result == expected_result).all(), True) - - with fluid.program_guard(fluid.Program()): - data = fluid.data(shape=[1, 5], dtype="float64", name="data") - data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - data_np = np.random.random([1, 5]).astype("float64") - result, = exe.run(feed={"data": data_np}, fetch_list=[data_t]) - expected_result = np.transpose(data_np) - self.assertEqual((result == expected_result).all(), True) - - with fluid.dygraph.guard(): - np_x = np.random.random([10]).astype("float64") - data = fluid.dygraph.to_variable(np_x) - z = paddle.t(data) - np_z = z.numpy() - z_expected = np.array(np.transpose(np_x)) - self.assertEqual((np_z == z_expected).all(), True) - - with fluid.dygraph.guard(): - np_x = np.random.random([10, 5]).astype("float64") - data = fluid.dygraph.to_variable(np_x) - z = paddle.t(data) - np_z = z.numpy() - z_expected = np.array(np.transpose(np_x)) - self.assertEqual((np_z == z_expected).all(), True) - - with fluid.dygraph.guard(): - np_x = np.random.random([1, 5]).astype("float64") - data = fluid.dygraph.to_variable(np_x) - z = paddle.t(data) - np_z = z.numpy() - z_expected = np.array(np.transpose(np_x)) - self.assertEqual((np_z == z_expected).all(), True) - - def test_errors(self): - with fluid.program_guard(fluid.Program()): - x = fluid.data(name='x', shape=[10, 5, 3], dtype='float64') - - def test_x_dimension_check(): - paddle.t(x) - - self.assertRaises(ValueError, test_x_dimension_check) - - if __name__ == "__main__": unittest.main() -- GitLab