未验证 提交 83541fd4 编写于 作者: Q Qi Li 提交者: GitHub

[NPU] fix set_value, test=develop (#36272)

* [NPU] fix set_value, test=develop

* fix typo, test=develop

* fix typo, test=develop
上级 11061325
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -10,382 +13,177 @@ See the License for the specific language governing permissions and ...@@ -10,382 +13,177 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/set_value_op.h" #include "paddle/fluid/operators/set_value_op.h"
#include "paddle/fluid/operators/assign_value_op.h"
#include "paddle/fluid/operators/npu_op_runner.h" #include "paddle/fluid/operators/npu_op_runner.h"
#include "paddle/fluid/operators/slice_utils.h"
#include "paddle/fluid/operators/utils.h"
#include "paddle/fluid/platform/enforce.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
template <typename DeviceContext, typename T>
class SetValueNPUKernel : public framework::OpKernel<T> {
private:
using Vector_Int64 = std::vector<int64_t>;
void GetNPUStartEndSteps(const Vector_Int64& start, const Vector_Int64& end,
const Vector_Int64& steps, const Vector_Int64& axes,
const framework::DDim& in_dim,
std::vector<std::vector<int64_t>>& output) const {
int rank = in_dim.size();
for (int i = 0; i < rank; ++i) {
int axis_size = in_dim[i];
auto iter = find(axes.begin(), axes.end(), i);
if (iter != axes.end()) {
int idx = iter - axes.begin();
output[0].push_back(start[idx]); // set as the same as raw input
output[1].push_back(end[idx]);
output[2].push_back(steps[idx]);
} else {
output[0].push_back(0); // begin 0
output[1].push_back(axis_size); // end = last one
output[2].push_back(1); // step = 1
}
}
}
inline std::vector<int> MininumPadNumberMakeSureLastDimGT8( using NPUDeviceContext = platform::NPUDeviceContext;
const std::vector<std::vector<int64_t>>& npu_slice) const {
int rank = npu_slice[0].size();
int last_dim_start = npu_slice[0][rank - 1];
int last_dim_end = npu_slice[1][rank - 1];
int last_dim_step = npu_slice[2][rank - 1];
int min_end = last_dim_start + last_dim_step * min_last_dim_value_;
int raw_last_dim_len = (last_dim_end - last_dim_start) / last_dim_step;
return std::vector<int>({std::max(0, min_end - last_dim_end),
min_last_dim_value_ - raw_last_dim_len});
}
inline void TileTensor(const framework::ExecutionContext* ctx, template <typename T>
const Tensor* input, Tensor* output) const { class SetValueNPUKernel : public framework::OpKernel<T> {
VLOG(4) << "start to tile tensor function, which calls the npu operator " public:
"TileWithAxis"; void Compute(const framework::ExecutionContext& ctx) const {
// UNSQUEEZE last dim + TILE last dim * min_last_dim_value_ auto* in = ctx.Input<Tensor>("Input");
Tensor reshape_tensor; auto* value_tensor = ctx.Input<Tensor>("ValueTensor");
auto reshape_dims = framework::vectorize<int>(input->dims()); auto* out = ctx.Output<Tensor>("Out");
reshape_dims.push_back(1);
reshape_tensor.ShareDataWith(*input);
reshape_tensor.Resize(framework::make_ddim(reshape_dims));
auto output_dims = framework::vectorize<int>(input->dims());
output_dims.push_back(min_last_dim_value_);
output->mutable_data<T>(framework::make_ddim(output_dims), ctx->GetPlace());
framework::NPUAttributeMap attr; auto starts_tensor_list = ctx.MultiInput<Tensor>("StartsTensorList");
attr["axis"] = static_cast<int>(reshape_dims.size() - 1); auto ends_tensor_list = ctx.MultiInput<Tensor>("EndsTensorList");
attr["tiles"] = min_last_dim_value_; auto steps_tensor_list = ctx.MultiInput<Tensor>("StepsTensorList");
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner("TileWithAxis", {reshape_tensor}, {*output}, attr).Run(stream);
}
inline void BroadcastToD(const framework::ExecutionContext* ctx, auto axes = ctx.Attr<std::vector<int64_t>>("axes");
const Tensor* input, auto starts = ctx.Attr<std::vector<int64_t>>("starts");
const std::vector<int64_t>* shape, auto ends = ctx.Attr<std::vector<int64_t>>("ends");
Tensor* output) const { auto steps = ctx.Attr<std::vector<int64_t>>("steps");
VLOG(4) << "Start BroadCast To"; auto shape = ctx.Attr<std::vector<int64_t>>("shape");
auto new_shape = std::vector<int32_t>(shape->begin(), shape->end()); auto decrease_axes = ctx.Attr<std::vector<int64_t>>("decrease_axes");
output->mutable_data<T>(framework::make_ddim(new_shape), ctx->GetPlace()); auto none_axes = ctx.Attr<std::vector<int64_t>>("none_axes");
framework::NPUAttributeMap attr;
attr["shape"] = new_shape;
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner("BroadcastToD", {*input}, {*output}, attr).Run(stream);
}
inline void CropTensor(const framework::ExecutionContext* ctx, if (!starts_tensor_list.empty()) {
const Tensor* input, Tensor* output) const { starts = GetDataFromTensorList<int64_t>(starts_tensor_list);
auto out_dims = output->dims();
auto in_dims = input->dims();
int rank = in_dims.size();
in_dims[rank - 1] = 1;
output->Resize(in_dims); // unsqueeze output -> [..., 1]
framework::NPUAttributeMap attr;
attr["axis"] = 0;
attr["offsets"] = std::vector<int>(rank, 0);
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner("Crop", {*input, *output}, {*output}, attr).Run(stream);
output->Resize(out_dims); // restore it
} }
if (!ends_tensor_list.empty()) {
void SliceAssignNPU(const framework::ExecutionContext* ctx, ends = GetDataFromTensorList<int64_t>(ends_tensor_list);
const Tensor* value_tensor, Vector_Int64& start,
Vector_Int64& end, Vector_Int64& steps,
Vector_Int64& axes, Tensor* assigned_tensor) const {
// must ensure assigned_tensor and value_tensor have the same shape
// not support steps < 0
// output is also the assigned_tensor.
VLOG(4) << "start function SliceAssignND";
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
for (size_t i = 0; i < steps.size(); ++i) {
PADDLE_ENFORCE_GT(steps[i], 0,
platform::errors::InvalidArgument(
"Currently NPU set_value operator doesn't support "
"negative steps, but got %d as step",
steps[i]));
} }
std::vector<std::vector<int64_t>> npu_slice(3); if (!steps_tensor_list.empty()) {
GetNPUStartEndSteps(start, end, steps, axes, assigned_tensor->dims(), steps = GetDataFromTensorList<int64_t>(steps_tensor_list);
npu_slice);
auto tile_numbers = MininumPadNumberMakeSureLastDimGT8(npu_slice);
int assigned_tensor_tile_number = tile_numbers[0];
int value_tensor_tile_number = tile_numbers[1];
VLOG(4) << "tile number is : " << assigned_tensor_tile_number << " "
<< value_tensor_tile_number;
Tensor tiled_assigned_tns, tiled_value_tns;
if (assigned_tensor_tile_number > 0) {
TileTensor(ctx, assigned_tensor, &tiled_assigned_tns);
TileTensor(ctx, value_tensor, &tiled_value_tns);
// output have different shape, so use a tmp variable before_crop_output;
// add last dim = min_last_dim_value_ in slice
npu_slice[0].push_back(0);
npu_slice[1].push_back(min_last_dim_value_);
npu_slice[2].push_back(1);
} }
framework::NPUAttributeMap attr_input; auto in_dims = in->dims();
attr_input["begin"] = CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps);
std::vector<int>(npu_slice[0].begin(), npu_slice[0].end()); auto slice_dims = GetSliceDims(in_dims, axes, starts, ends, &steps);
attr_input["end"] = auto decrease_slice_dims = GetDecreasedDims(slice_dims, decrease_axes);
std::vector<int>(npu_slice[1].begin(), npu_slice[1].end());
attr_input["strides"] =
std::vector<int>(npu_slice[2].begin(), npu_slice[2].end());
attr_input["begin_mask"] = 0;
attr_input["end_mask"] = 0;
attr_input["ellipsis_mask"] = 0;
attr_input["new_axis_mask"] = 0;
attr_input["shrink_axis_mask"] = 0;
if (assigned_tensor_tile_number > 0) {
NpuOpRunner("StridedSliceAssignD", {tiled_assigned_tns, tiled_value_tns},
{tiled_assigned_tns}, attr_input)
.Run(stream); // Remember, set output = input, and this op will
// change the input value.
} else {
NpuOpRunner("StridedSliceAssignD", {*assigned_tensor, *value_tensor},
{*assigned_tensor}, attr_input)
.Run(stream);
}
if (assigned_tensor_tile_number > 0) {
CropTensor(ctx, &tiled_assigned_tns /*initialzied*/,
assigned_tensor /*initalized*/);
}
}
void ModifyAxesAccordingNoneAxes(const Vector_Int64& none_axes, auto slice_dims_for_assign = decrease_slice_dims;
Vector_Int64& axes_to_modify) const { if (!none_axes.empty()) {
if (none_axes.empty()) return; std::vector<int64_t> slice_dims_with_none;
auto none_axes_copy = none_axes;
sort(none_axes_copy.begin(), none_axes_copy.end());
for (size_t i = 0; i < axes_to_modify.size(); ++i) {
int axis = axes_to_modify[i];
auto upper =
upper_bound(none_axes_copy.begin(), none_axes_copy.end(), axis);
// Example: none_axes = [1,3,4,5,7]
// axis = 4
// find the element number less or equal than 4, which is
// 3(1,3,4)
// axis becomes 4 + 3 = 7 ;
axes_to_modify[i] = axis + (upper - none_axes_copy.begin());
}
}
void UnsqueezeAccordingNoneAxes(const Vector_Int64& none_axes, size_t none_axes_cur = 0, decrease_axes_cur = 0;
Vector_Int64& slice_dims) const { for (int i = 0; i < slice_dims.size(); ++i) {
// note : axes will change, because new axes inserted.
// sum array to modify the axes. because more simply
if (none_axes.empty()) return;
Vector_Int64 slice_dims_with_none;
size_t none_axes_cur = 0;
for (size_t i = 0; i < slice_dims.size(); ++i) {
while (none_axes_cur < none_axes.size() && while (none_axes_cur < none_axes.size() &&
none_axes[none_axes_cur] <= static_cast<int>(i)) { none_axes[none_axes_cur] <= i) {
slice_dims_with_none.push_back(1); slice_dims_with_none.push_back(1);
none_axes_cur++; none_axes_cur++;
} }
if (decrease_axes_cur < decrease_axes.size() &&
decrease_axes[decrease_axes_cur] == i) {
decrease_axes_cur++;
} else {
slice_dims_with_none.push_back(slice_dims[i]); slice_dims_with_none.push_back(slice_dims[i]);
} }
// if the none_axes.size() > slice_dims.size(), append 1 after last dim }
while (none_axes_cur < none_axes.size()) { while (none_axes_cur < none_axes.size()) {
slice_dims_with_none.push_back(1); slice_dims_with_none.push_back(1);
none_axes_cur++; none_axes_cur++;
} }
slice_dims = slice_dims_with_none;
}
void ModiftyDimsAccordingNoneAndDecrease(Vector_Int64& slice_dim, slice_dims_for_assign = framework::make_ddim(slice_dims_with_none);
Vector_Int64& value_dim, }
Vector_Int64& axes,
Vector_Int64& none_axes,
Vector_Int64& dec_axes) const {
// change the value of slice_dim, value_dim, start, end, steps, axes by none
// and decrease axes
// after change, this values can be passed to SliceAssignNPU() directly.
// Modity Slice Dim TensorCopy(*in, ctx.GetPlace(), out);
UnsqueezeAccordingNoneAxes(none_axes, slice_dim);
ModifyAxesAccordingNoneAxes(none_axes, dec_axes);
ModifyAxesAccordingNoneAxes(none_axes, axes);
// Modity Value Dim by new slice dim
auto slice_dim_reverse = slice_dim;
auto value_dim_reverse = value_dim;
std::reverse(slice_dim_reverse.begin(), slice_dim_reverse.end());
std::reverse(value_dim_reverse.begin(), value_dim_reverse.end());
Vector_Int64 new_value_dim; auto starts_indices = std::vector<int64_t>(in_dims.size(), 0);
PADDLE_ENFORCE_GE( auto ends_indices = std::vector<int64_t>(in_dims.size(), 0);
slice_dim.size(), value_dim.size(), auto strides_indices = std::vector<int64_t>(in_dims.size(), 0);
platform::errors::InvalidArgument("The size of expanded slice_dim(%d) "
"must greater than the value_dim(%d)",
slice_dim.size(), value_dim.size()));
size_t value_cur = 0; for (int i = 0; i < in_dims.size(); ++i) {
size_t rank = slice_dim.size(); starts_indices[i] = 0;
for (size_t i = 0; i < rank; ++i) { ends_indices[i] = slice_dims[i];
auto& xsize = slice_dim_reverse[i]; strides_indices[i] = 1;
if (value_cur >= value_dim_reverse.size()) {
new_value_dim.push_back(1);
continue;
}
auto& vsize = value_dim_reverse[value_cur];
auto it = find(dec_axes.begin(), dec_axes.end(), rank - 1 - i);
if (it != dec_axes.end()) {
// found, insert one dim ;
PADDLE_ENFORCE_EQ(xsize, 1, platform::errors::InvalidArgument(
"The dims refered by decrease axes is "
"not equal to 1, some wrongs happen"));
new_value_dim.push_back(1);
continue;
}
if (xsize == vsize || vsize == 1) {
new_value_dim.push_back(vsize);
++value_cur;
continue;
} }
PADDLE_THROW(platform::errors::InvalidArgument( for (size_t i = 0; i < axes.size(); i++) {
"The shape of value_tensor can't be broadcast to value tensor, " int axis_index = axes[i];
"please check input")); starts_indices[axis_index] = starts[i];
} ends_indices[axis_index] = ends[i];
for (; value_cur < value_dim_reverse.size(); ++value_cur) { strides_indices[axis_index] = steps[i];
if (value_dim_reverse[value_cur] != 1) {
PADDLE_THROW(platform::errors::InvalidArgument(
"The shape of value_tensor can't be broadcast to value tensor, "
"please check input"));
}
}
std::reverse(new_value_dim.begin(), new_value_dim.end());
value_dim = new_value_dim;
return;
} }
public: int64_t stride_step = framework::product(in_dims);
void Compute(const framework::ExecutionContext& ctx) const override { std::vector<int64_t> index_indices(1, 0);
VLOG(2) << "Start Set Value Npu Kernel"; for (size_t i = 0; i < strides_indices.size(); ++i) {
auto* in = ctx.Input<framework::LoDTensor>("Input"); auto index_size = index_indices.size();
auto* out = ctx.Output<framework::LoDTensor>("Out"); stride_step /= in_dims[i];
auto* value_tensor = ctx.Input<framework::LoDTensor>("ValueTensor"); for (size_t j = 0; j < index_size; ++j) {
auto starts_tensor_list = auto start_index = *index_indices.begin();
ctx.MultiInput<framework::Tensor>("StartsTensorList"); if (strides_indices[i] > 0) {
auto ends_tensor_list = ctx.MultiInput<framework::Tensor>("EndsTensorList"); for (int64_t k = starts_indices[i]; k < ends_indices[i];
auto steps_tensor_list = k += strides_indices[i]) {
ctx.MultiInput<framework::Tensor>("StepsTensorList"); index_indices.push_back(start_index + k * stride_step);
auto axes = ctx.Attr<std::vector<int64_t>>("axes");
auto starts = ctx.Attr<std::vector<int64_t>>("starts");
auto ends = ctx.Attr<std::vector<int64_t>>("ends");
auto steps = ctx.Attr<std::vector<int64_t>>("steps");
auto shape = ctx.Attr<std::vector<int64_t>>("shape");
auto decrease_axes = ctx.Attr<std::vector<int64_t>>("decrease_axes");
auto none_axes = ctx.Attr<std::vector<int64_t>>("none_axes");
auto dtype = in->type();
if (dtype == framework::proto::VarType::FP64 ||
dtype == framework::proto::VarType::INT64 ||
dtype == framework::proto::VarType::BOOL) {
auto value_type_name = GetValueName(dtype);
PADDLE_THROW(platform::errors::InvalidArgument(
"The NPU setvalue kernel currently only support FLOAT32 and INT32, "
"but got type: %s",
value_type_name.data()));
} }
} else {
if (!starts_tensor_list.empty()) { for (int64_t k = starts_indices[i]; k > ends_indices[i];
starts = GetDataFromTensorList<int64_t>(starts_tensor_list); k += strides_indices[i]) {
index_indices.push_back(start_index + k * stride_step);
} }
if (!ends_tensor_list.empty()) {
ends = GetDataFromTensorList<int64_t>(ends_tensor_list);
} }
if (!steps_tensor_list.empty()) { index_indices.erase(index_indices.begin());
steps = GetDataFromTensorList<int64_t>(steps_tensor_list); }
} }
auto in_dims = in->dims(); PADDLE_ENFORCE_EQ(
CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps); static_cast<int64_t>(index_indices.size()),
auto slice_dims = GetSliceDims(in_dims, axes, starts, ends, &steps); framework::product(slice_dims_for_assign),
auto place = ctx.GetPlace(); platform::errors::InvalidArgument(
"OP(set_value) error index indices and value update not match "));
// aforementioned code is copyed directly from CPU kernel.
// (@xiongkun03) the following is redesigned by xiongkun. because NPU can do
// step slice assignment. so we deal with all none_axes and decrease_axes
// here.
// 1. we insert 1 into assigned_tensor_shape according to none_axes;
// 2. we insert 1 into value_tensor_shape(value tensor) according to
// decrease_axes;
// 3. we reshape back the assigned_tensor. and return it.
// note : we use a tmp_value_tensor as value_tns. it shares data with
// value_tensor;
// I believe the logic is more simple than cpu logic.
TensorCopy(*in, place, out);
Tensor value_t(dtype);
if (value_tensor == nullptr) { Tensor value_t(in->type());
if (value_tensor != nullptr) {
value_t.ShareDataWith(*value_tensor);
} else {
auto value_dims = framework::make_ddim(shape); auto value_dims = framework::make_ddim(shape);
value_t.mutable_data<T>(value_dims, place); CheckIsDimsMatch(slice_dims_for_assign, value_dims);
auto value_name = GetValueName(dtype);
value_t.mutable_data<T>(value_dims, ctx.GetPlace());
auto value_name = GetValueName(in->type());
CopyVecotorToTensor<T>(value_name.c_str(), &value_t, ctx); CopyVecotorToTensor<T>(value_name.c_str(), &value_t, ctx);
value_t.Resize(value_dims); value_t.Resize(value_dims);
} }
const Tensor* value_tensor_ptr = auto stream = ctx.template device_context<NPUDeviceContext>().stream();
(value_tensor == nullptr) ? &value_t : value_tensor;
auto value_dims_vec = framework::vectorize(value_tensor_ptr->dims());
auto slice_dims_vec = framework::vectorize(slice_dims);
auto in_dims_vec = framework::vectorize(in_dims);
UnsqueezeAccordingNoneAxes(none_axes, in_dims_vec); Tensor value_temp(in->type());
ModiftyDimsAccordingNoneAndDecrease(slice_dims_vec, value_dims_vec, axes, if (slice_dims_for_assign == value_t.dims()) {
none_axes, value_temp.ShareDataWith(value_t);
decrease_axes); // Modify and Check } else {
value_temp.Resize(slice_dims_for_assign);
value_temp.mutable_data<T>(ctx.GetPlace());
NpuOpRunner runner_brd;
runner_brd.SetType("BroadcastTo")
.AddInput(value_t)
.AddInput(framework::vectorize(slice_dims_for_assign))
.AddOutput(value_temp)
.Run(stream);
}
Tensor reshaped_value_tensor, broadcast_value_tensor; int64_t input_numel = framework::product(in_dims);
reshaped_value_tensor.ShareDataWith(*value_tensor_ptr); int64_t index_numel = index_indices.size();
reshaped_value_tensor.Resize(framework::make_ddim(value_dims_vec));
BroadcastToD(&ctx, &reshaped_value_tensor, &slice_dims_vec, Tensor in_temp, out_temp, val_temp;
&broadcast_value_tensor /*inner function initialized*/); in_temp.ShareDataWith(*in);
out_temp.ShareDataWith(*out);
val_temp.ShareDataWith(value_temp);
in_temp.Resize(framework::make_ddim({input_numel}));
out_temp.Resize(framework::make_ddim({input_numel}));
val_temp.Resize(framework::make_ddim({index_numel}));
out->Resize(framework::make_ddim(in_dims_vec)); NpuOpRunner runner;
SliceAssignNPU(&ctx, &broadcast_value_tensor, starts, ends, steps, axes, runner.SetType("ScatterUpdate")
out); .AddInput(in_temp)
out->Resize(in_dims); // Reshape Back .AddInput(std::move(index_indices))
.AddInput(val_temp)
.AddOutput(out_temp)
.Run(stream);
} }
private:
const int min_last_dim_value_ =
32 / sizeof(T); // 16 for float16 , 8 for float32
}; };
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
namespace ops = paddle::operators; namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_NPU_KERNEL( REGISTER_OP_NPU_KERNEL(set_value, ops::SetValueNPUKernel<int>,
set_value, ops::SetValueNPUKernel<paddle::platform::NPUDeviceContext, int>, #ifdef PADDLE_WITH_ASCEND_INT64
ops::SetValueNPUKernel<paddle::platform::NPUDeviceContext, float>) ops::SetValueNPUKernel<int64_t>,
#endif
ops::SetValueNPUKernel<float>)
...@@ -23,13 +23,15 @@ import paddle ...@@ -23,13 +23,15 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import core from paddle.fluid import core
SEED = 2021
class TestSetValueBase(unittest.TestCase): class TestSetValueBase(unittest.TestCase):
def set_input(self): def set_npu(self):
self.__class__.use_npu = True
self.place = paddle.NPUPlace(0)
def setUp(self):
paddle.enable_static()
self.set_npu() self.set_npu()
paddle.device.set_device('npu')
self.set_dtype() self.set_dtype()
self.set_value() self.set_value()
self.set_shape() self.set_shape()
...@@ -51,9 +53,6 @@ class TestSetValueBase(unittest.TestCase): ...@@ -51,9 +53,6 @@ class TestSetValueBase(unittest.TestCase):
def _get_answer(self): def _get_answer(self):
self.data[0, 0] = self.value self.data[0, 0] = self.value
def set_npu(self):
self.__class__.use_npu = True
class TestSetValueApi(TestSetValueBase): class TestSetValueApi(TestSetValueBase):
def _run_static(self): def _run_static(self):
...@@ -62,13 +61,13 @@ class TestSetValueApi(TestSetValueBase): ...@@ -62,13 +61,13 @@ class TestSetValueApi(TestSetValueBase):
x = paddle.ones(shape=self.shape, dtype=self.dtype) x = paddle.ones(shape=self.shape, dtype=self.dtype)
self._call_setitem(x) self._call_setitem(x)
exe = paddle.static.Executor(paddle.NPUPlace(0)) exe = paddle.static.Executor(self.place)
out = exe.run(self.program, fetch_list=[x]) out = exe.run(self.program, fetch_list=[x])
paddle.disable_static() paddle.disable_static()
return out return out
def _run_dynamic(self): def _run_dynamic(self):
paddle.disable_static(paddle.NPUPlace(0)) paddle.disable_static(self.place)
x = paddle.ones(shape=self.shape, dtype=self.dtype) x = paddle.ones(shape=self.shape, dtype=self.dtype)
self._call_setitem(x) self._call_setitem(x)
out = x.numpy() out = x.numpy()
...@@ -76,7 +75,6 @@ class TestSetValueApi(TestSetValueBase): ...@@ -76,7 +75,6 @@ class TestSetValueApi(TestSetValueBase):
return out return out
def test_api(self): def test_api(self):
self.set_input()
static_out = self._run_static() static_out = self._run_static()
dynamic_out = self._run_dynamic() dynamic_out = self._run_dynamic()
self._get_answer() self._get_answer()
...@@ -134,23 +132,22 @@ class TestSetValueItemSlice4(TestSetValueApi): ...@@ -134,23 +132,22 @@ class TestSetValueItemSlice4(TestSetValueApi):
self.data[0:, 1:2, :] = self.value self.data[0:, 1:2, :] = self.value
""" FIXEME : it seams that NPU don't support while operator ??? # TODO(qili93): Fix this after NPU support while_loop
class TestSetValueItemSliceInWhile(TestSetValueApi): # class TestSetValueItemSliceInWhile(TestSetValueApi):
def _call_setitem(self, x): # def _call_setitem(self, x):
def cond(i, x): # def cond(i, x):
return i < 1 # return i < 1
def body(i, x): # def body(i, x):
x[i] = self.value # x[i] = self.value
i = i + 1 # i = i + 1
return i, x # return i, x
with paddle.static.device_guard("npu"):
i = paddle.zeros(shape=(1, ), dtype='int32')
i, x = paddle.fluid.layers.while_loop(cond, body, [i, x])
def _get_answer(self): # i = paddle.zeros(shape=(1, ), dtype='int32')
self.data[0] = self.value # i, x = paddle.fluid.layers.while_loop(cond, body, [i, x])
"""
# def _get_answer(self):
# self.data[0] = self.value
# 1.2.2 step > 1 # 1.2.2 step > 1
...@@ -192,6 +189,60 @@ class TestSetValueItemSliceStep4(TestSetValueApi): ...@@ -192,6 +189,60 @@ class TestSetValueItemSliceStep4(TestSetValueApi):
self.data[0:, 1:2:2, :] = self.value self.data[0:, 1:2:2, :] = self.value
# 1.2.3 step < 0
class TestSetValueItemSliceNegetiveStep(TestSetValueApi):
def set_shape(self):
self.shape = [5, 2]
def set_value(self):
self.value = np.array([3, 4])
def _call_setitem(self, x):
x[5:2:-1] = self.value
def _get_answer(self):
self.data[5:2:-1] = self.value
class TestSetValueItemSliceNegetiveStep2(TestSetValueApi):
def set_shape(self):
self.shape = [5]
def set_value(self):
self.value = np.array([3, 4])
def _call_setitem(self, x):
x[1::-1] = self.value
def _get_answer(self):
self.data[1::-1] = self.value
class TestSetValueItemSliceNegetiveStep3(TestSetValueApi):
def set_shape(self):
self.shape = [3]
def set_value(self):
self.value = np.array([3, 4, 5])
def _call_setitem(self, x):
x[::-1] = self.value
def _get_answer(self):
self.data[::-1] = self.value
class TestSetValueItemSliceNegetiveStep4(TestSetValueApi):
def set_shape(self):
self.shape = [3, 4, 5]
def _call_setitem(self, x):
x[2:0:-1, 0:2, ::-1] = self.value
def _get_answer(self):
self.data[2:0:-1, 0:2, ::-1] = self.value
# 1.3 item is Ellipsis # 1.3 item is Ellipsis
...@@ -277,6 +328,19 @@ class TestSetValueItemTensor5(TestSetValueApi): ...@@ -277,6 +328,19 @@ class TestSetValueItemTensor5(TestSetValueApi):
self.data[0:, 1:2:2, :] = self.value self.data[0:, 1:2:2, :] = self.value
class TestSetValueItemTensor6(TestSetValueApi):
def set_shape(self):
self.shape = [3, 4, 5]
def _call_setitem(self, x):
minus1 = paddle.full([1], -1, dtype="int32")
zero = paddle.full([1], 0, dtype="int32")
x[2:zero:minus1, 0:2, 10:-6:minus1] = self.value
def _get_answer(self):
self.data[2:0:-1, 0:2, ::-1] = self.value
# 1.5 item is None # 1.5 item is None
class TestSetValueItemNone1(TestSetValueApi): class TestSetValueItemNone1(TestSetValueApi):
def _call_setitem(self, x): def _call_setitem(self, x):
...@@ -350,133 +414,99 @@ class TestSetValueItemNone9(TestSetValueApi): ...@@ -350,133 +414,99 @@ class TestSetValueItemNone9(TestSetValueApi):
self.data[None, :, 1, ..., None] = np.zeros(self.shape)[0, 0, :, None] self.data[None, :, 1, ..., None] = np.zeros(self.shape)[0, 0, :, None]
""" FIXME : current NPU set_value don't support negative step !!! # 1.5 item is list or Tensor of bol
@xiongkun03 class TestSetValueItemBool1(TestSetValueApi):
def _call_setitem(self, x):
x[[True, False]] = self.value
class TestSetValueItemTensor6(TestSetValueApi): def _get_answer(self):
def set_shape(self): self.data[[True, False]] = self.value
self.shape = [3, 4, 5]
class TestSetValueItemBool2(TestSetValueApi):
def _call_setitem(self, x): def _call_setitem(self, x):
minus1 = paddle.full([1], -1, dtype="int32") x[[False, False]] = self.value
zero = paddle.full([1], 0, dtype="int32")
x[2:zero:minus1, 0:2, 10:-6:minus1] = self.value
def _get_answer(self): def _get_answer(self):
self.data[2:0:-1, 0:2, ::-1] = self.value self.data[[False, False]] = self.value
"""
# 2. Test different type of value: int, float, numpy.ndarray, Tensor
# 2.1 value is int32, int64, float32, float64, bool
class TestSetValueItemBool3(TestSetValueApi):
def _call_setitem(self, x):
x[[False, True]] = np.zeros(self.shape[2])
def create_test_value_int32(parent): def _get_answer(self):
class TestValueInt(parent): self.data[[False, True]] = np.zeros(self.shape[2])
def set_value(self):
self.value = 7
def set_dtype(self):
self.dtype = "int32"
cls_name = "{0}_{1}".format(parent.__name__, "ValueInt32")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
class TestSetValueItemBool4(TestSetValueApi):
def _call_setitem(self, x):
idx = paddle.assign(np.array([False, True]))
x[idx] = np.zeros(self.shape[2])
create_test_value_int32(TestSetValueItemInt) def _get_answer(self):
create_test_value_int32(TestSetValueItemSlice) self.data[np.array([False, True])] = np.zeros(self.shape[2])
create_test_value_int32(TestSetValueItemSlice2)
create_test_value_int32(TestSetValueItemSlice3)
create_test_value_int32(TestSetValueItemSlice4)
def create_test_value_numpy_fp32(parent): class TestSetValueItemBool5(TestSetValueApi):
class TestValueInt(parent): def _call_setitem(self, x):
def set_value(self): idx = paddle.assign(
self.value = np.array([1]) np.array([[False, True, False], [True, True, False]]))
x[idx] = self.value
def set_dtype(self): def _get_answer(self):
self.dtype = "float32" self.data[np.array([[False, True, False], [True, True, False]
])] = self.value
cls_name = "{0}_{1}".format(parent.__name__, "ValueNumpyFp32")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
class TestSetValueItemBool6(TestSetValueApi):
def _call_setitem(self, x):
x[0, ...] = 0
x[x > 0] = self.value
create_test_value_numpy_fp32(TestSetValueItemInt) def _get_answer(self):
create_test_value_numpy_fp32(TestSetValueItemSlice) self.data[0, ...] = 0
create_test_value_numpy_fp32(TestSetValueItemSlice2) self.data[self.data > 0] = self.value
create_test_value_numpy_fp32(TestSetValueItemSlice3)
create_test_value_numpy_fp32(TestSetValueItemSlice4)
def create_test_value_numpy_fp64(parent): def create_test_value_int32(parent):
class TestValueInt(parent): class TestValueInt(parent):
def set_value(self): def set_value(self):
self.value = np.array([2**127]).astype("float64") self.value = 7
def set_dtype(self):
self.dtype = "float64"
cls_name = "{0}_{1}".format(parent.__name__, "ValueNumpyFp64")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
create_test_value_numpy_fp64(TestSetValueItemInt)
create_test_value_numpy_fp64(TestSetValueItemSlice)
create_test_value_numpy_fp64(TestSetValueItemSlice2)
create_test_value_numpy_fp64(TestSetValueItemSlice3)
create_test_value_numpy_fp64(TestSetValueItemSlice4)
# 2.3 value is a Paddle Tensor (int32, int64, float32, float64, bool)
def create_test_value_tensor_int32(parent):
class TestValueInt(parent):
def set_dtype(self): def set_dtype(self):
self.dtype = "int32" self.dtype = "int32"
def _call_setitem(self, x): cls_name = "{0}_{1}".format(parent.__name__, "ValueInt32")
value = paddle.full(shape=[1], fill_value=3, dtype=self.dtype)
x[0, 1] = value
def _get_answer(self):
self.data[0, 1] = 3
cls_name = "{0}_{1}".format(parent.__name__, "ValueTensorInt32")
TestValueInt.__name__ = cls_name TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt globals()[cls_name] = TestValueInt
create_test_value_tensor_int32(TestSetValueItemInt) create_test_value_int32(TestSetValueItemInt)
create_test_value_tensor_int32(TestSetValueItemSlice) create_test_value_int32(TestSetValueItemSlice)
create_test_value_tensor_int32(TestSetValueItemSlice2) create_test_value_int32(TestSetValueItemSlice2)
create_test_value_tensor_int32(TestSetValueItemSlice3) create_test_value_int32(TestSetValueItemSlice3)
create_test_value_tensor_int32(TestSetValueItemSlice4) create_test_value_int32(TestSetValueItemSlice4)
def create_test_value_tensor_int64(parent): def create_test_value_int64(parent):
class TestValueInt(parent): class TestValueInt(parent):
def set_value(self):
self.value = 7
def set_dtype(self): def set_dtype(self):
self.dtype = "int64" self.dtype = "int64"
def _call_setitem(self, x): cls_name = "{0}_{1}".format(parent.__name__, "ValueInt64")
value = paddle.full(shape=[1], fill_value=3, dtype=self.dtype)
x[0, 1] = value
def _get_answer(self):
self.data[0, 1] = 3
cls_name = "{0}_{1}".format(parent.__name__, "ValueTensorInt64")
TestValueInt.__name__ = cls_name TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt globals()[cls_name] = TestValueInt
create_test_value_tensor_int64(TestSetValueItemInt) create_test_value_int64(TestSetValueItemInt)
create_test_value_tensor_int64(TestSetValueItemSlice) create_test_value_int64(TestSetValueItemSlice)
create_test_value_tensor_int64(TestSetValueItemSlice2) create_test_value_int64(TestSetValueItemSlice2)
create_test_value_tensor_int64(TestSetValueItemSlice3) create_test_value_int64(TestSetValueItemSlice3)
create_test_value_tensor_int64(TestSetValueItemSlice4) create_test_value_int64(TestSetValueItemSlice4)
def create_test_value_tensor_fp32(parent): def create_test_value_tensor_fp32(parent):
...@@ -503,30 +533,6 @@ create_test_value_tensor_fp32(TestSetValueItemSlice3) ...@@ -503,30 +533,6 @@ create_test_value_tensor_fp32(TestSetValueItemSlice3)
create_test_value_tensor_fp32(TestSetValueItemSlice4) create_test_value_tensor_fp32(TestSetValueItemSlice4)
def create_test_value_tensor_fp64(parent):
class TestValueInt(parent):
def set_dtype(self):
self.dtype = "float64"
def _call_setitem(self, x):
value = paddle.full(shape=[1], fill_value=3, dtype=self.dtype)
x[0, 1] = value
def _get_answer(self):
self.data[0, 1] = 3
cls_name = "{0}_{1}".format(parent.__name__, "ValueTensorFp64")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
create_test_value_tensor_fp64(TestSetValueItemInt)
create_test_value_tensor_fp64(TestSetValueItemSlice)
create_test_value_tensor_fp64(TestSetValueItemSlice2)
create_test_value_tensor_fp64(TestSetValueItemSlice3)
create_test_value_tensor_fp64(TestSetValueItemSlice4)
# 3. Test different shape of value # 3. Test different shape of value
class TestSetValueValueShape1(TestSetValueApi): class TestSetValueValueShape1(TestSetValueApi):
def set_value(self): def set_value(self):
...@@ -589,59 +595,5 @@ class TestSetValueValueShape5(TestSetValueApi): ...@@ -589,59 +595,5 @@ class TestSetValueValueShape5(TestSetValueApi):
self.data[:, 0] = self.value self.data[:, 0] = self.value
# 4. Test error
class TestError(TestSetValueBase):
def _value_type_error(self):
with self.assertRaisesRegexp(
TypeError,
"Only support to assign an integer, float, numpy.ndarray or paddle.Tensor"
):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
value = [1]
x[0] = value
def _dtype_error(self):
with self.assertRaisesRegexp(
TypeError,
"When assign a numpy.ndarray, integer or float to a paddle.Tensor, "
):
y = paddle.ones(shape=self.shape, dtype="float16")
y[0] = 1
def _step_error(self):
with self.assertRaisesRegexp(ValueError, "step can not be 0"):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
x[0:1:0] = self.value
def _ellipsis_error(self):
with self.assertRaisesRegexp(
IndexError, "An index can only have a single ellipsis"):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
x[..., ...] = self.value
with self.assertRaisesRegexp(ValueError, "the start or end is None"):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
one = paddle.ones([1])
x[::one] = self.value
def _broadcast_mismatch(self):
program = paddle.static.Program()
with paddle.static.program_guard(program):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
value = np.array([3, 4, 5, 6, 7])
x[0] = value
exe = paddle.static.Executor(paddle.CPUPlace())
with self.assertRaises(ValueError):
exe.run(program)
def test_error(self):
self.set_input()
paddle.enable_static()
with paddle.static.program_guard(self.program):
self._value_type_error()
self._dtype_error()
self._step_error()
self._broadcast_mismatch()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册