未验证 提交 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.
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.
......@@ -10,382 +13,177 @@ See the License for the specific language governing permissions and
limitations under the License. */
#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/slice_utils.h"
#include "paddle/fluid/operators/utils.h"
#include "paddle/fluid/platform/enforce.h"
namespace paddle {
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(
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});
}
using NPUDeviceContext = platform::NPUDeviceContext;
inline void TileTensor(const framework::ExecutionContext* ctx,
const Tensor* input, Tensor* output) const {
VLOG(4) << "start to tile tensor function, which calls the npu operator "
"TileWithAxis";
// UNSQUEEZE last dim + TILE last dim * min_last_dim_value_
Tensor reshape_tensor;
auto reshape_dims = framework::vectorize<int>(input->dims());
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());
template <typename T>
class SetValueNPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const {
auto* in = ctx.Input<Tensor>("Input");
auto* value_tensor = ctx.Input<Tensor>("ValueTensor");
auto* out = ctx.Output<Tensor>("Out");
framework::NPUAttributeMap attr;
attr["axis"] = static_cast<int>(reshape_dims.size() - 1);
attr["tiles"] = min_last_dim_value_;
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner("TileWithAxis", {reshape_tensor}, {*output}, attr).Run(stream);
}
auto starts_tensor_list = ctx.MultiInput<Tensor>("StartsTensorList");
auto ends_tensor_list = ctx.MultiInput<Tensor>("EndsTensorList");
auto steps_tensor_list = ctx.MultiInput<Tensor>("StepsTensorList");
inline void BroadcastToD(const framework::ExecutionContext* ctx,
const Tensor* input,
const std::vector<int64_t>* shape,
Tensor* output) const {
VLOG(4) << "Start BroadCast To";
auto new_shape = std::vector<int32_t>(shape->begin(), shape->end());
output->mutable_data<T>(framework::make_ddim(new_shape), ctx->GetPlace());
framework::NPUAttributeMap attr;
attr["shape"] = new_shape;
auto stream =
ctx->template device_context<paddle::platform::NPUDeviceContext>()
.stream();
NpuOpRunner("BroadcastToD", {*input}, {*output}, attr).Run(stream);
}
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");
inline void CropTensor(const framework::ExecutionContext* ctx,
const Tensor* input, Tensor* output) const {
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 (!starts_tensor_list.empty()) {
starts = GetDataFromTensorList<int64_t>(starts_tensor_list);
}
void SliceAssignNPU(const framework::ExecutionContext* ctx,
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]));
if (!ends_tensor_list.empty()) {
ends = GetDataFromTensorList<int64_t>(ends_tensor_list);
}
std::vector<std::vector<int64_t>> npu_slice(3);
GetNPUStartEndSteps(start, end, steps, axes, assigned_tensor->dims(),
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);
if (!steps_tensor_list.empty()) {
steps = GetDataFromTensorList<int64_t>(steps_tensor_list);
}
framework::NPUAttributeMap attr_input;
attr_input["begin"] =
std::vector<int>(npu_slice[0].begin(), npu_slice[0].end());
attr_input["end"] =
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*/);
}
}
auto in_dims = in->dims();
CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps);
auto slice_dims = GetSliceDims(in_dims, axes, starts, ends, &steps);
auto decrease_slice_dims = GetDecreasedDims(slice_dims, decrease_axes);
void ModifyAxesAccordingNoneAxes(const Vector_Int64& none_axes,
Vector_Int64& axes_to_modify) const {
if (none_axes.empty()) return;
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());
}
}
auto slice_dims_for_assign = decrease_slice_dims;
if (!none_axes.empty()) {
std::vector<int64_t> slice_dims_with_none;
void UnsqueezeAccordingNoneAxes(const Vector_Int64& none_axes,
Vector_Int64& slice_dims) const {
// 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) {
size_t none_axes_cur = 0, decrease_axes_cur = 0;
for (int i = 0; i < slice_dims.size(); ++i) {
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);
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]);
}
// if the none_axes.size() > slice_dims.size(), append 1 after last dim
}
while (none_axes_cur < none_axes.size()) {
slice_dims_with_none.push_back(1);
none_axes_cur++;
}
slice_dims = slice_dims_with_none;
}
void ModiftyDimsAccordingNoneAndDecrease(Vector_Int64& slice_dim,
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.
slice_dims_for_assign = framework::make_ddim(slice_dims_with_none);
}
// Modity Slice Dim
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());
TensorCopy(*in, ctx.GetPlace(), out);
Vector_Int64 new_value_dim;
PADDLE_ENFORCE_GE(
slice_dim.size(), value_dim.size(),
platform::errors::InvalidArgument("The size of expanded slice_dim(%d) "
"must greater than the value_dim(%d)",
slice_dim.size(), value_dim.size()));
auto starts_indices = std::vector<int64_t>(in_dims.size(), 0);
auto ends_indices = std::vector<int64_t>(in_dims.size(), 0);
auto strides_indices = std::vector<int64_t>(in_dims.size(), 0);
size_t value_cur = 0;
size_t rank = slice_dim.size();
for (size_t i = 0; i < rank; ++i) {
auto& xsize = slice_dim_reverse[i];
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;
for (int i = 0; i < in_dims.size(); ++i) {
starts_indices[i] = 0;
ends_indices[i] = slice_dims[i];
strides_indices[i] = 1;
}
PADDLE_THROW(platform::errors::InvalidArgument(
"The shape of value_tensor can't be broadcast to value tensor, "
"please check input"));
}
for (; value_cur < value_dim_reverse.size(); ++value_cur) {
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;
for (size_t i = 0; i < axes.size(); i++) {
int axis_index = axes[i];
starts_indices[axis_index] = starts[i];
ends_indices[axis_index] = ends[i];
strides_indices[axis_index] = steps[i];
}
public:
void Compute(const framework::ExecutionContext& ctx) const override {
VLOG(2) << "Start Set Value Npu Kernel";
auto* in = ctx.Input<framework::LoDTensor>("Input");
auto* out = ctx.Output<framework::LoDTensor>("Out");
auto* value_tensor = ctx.Input<framework::LoDTensor>("ValueTensor");
auto starts_tensor_list =
ctx.MultiInput<framework::Tensor>("StartsTensorList");
auto ends_tensor_list = ctx.MultiInput<framework::Tensor>("EndsTensorList");
auto steps_tensor_list =
ctx.MultiInput<framework::Tensor>("StepsTensorList");
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()));
int64_t stride_step = framework::product(in_dims);
std::vector<int64_t> index_indices(1, 0);
for (size_t i = 0; i < strides_indices.size(); ++i) {
auto index_size = index_indices.size();
stride_step /= in_dims[i];
for (size_t j = 0; j < index_size; ++j) {
auto start_index = *index_indices.begin();
if (strides_indices[i] > 0) {
for (int64_t k = starts_indices[i]; k < ends_indices[i];
k += strides_indices[i]) {
index_indices.push_back(start_index + k * stride_step);
}
if (!starts_tensor_list.empty()) {
starts = GetDataFromTensorList<int64_t>(starts_tensor_list);
} else {
for (int64_t k = starts_indices[i]; k > ends_indices[i];
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()) {
steps = GetDataFromTensorList<int64_t>(steps_tensor_list);
index_indices.erase(index_indices.begin());
}
}
auto in_dims = in->dims();
CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps);
auto slice_dims = GetSliceDims(in_dims, axes, starts, ends, &steps);
auto place = ctx.GetPlace();
// 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);
PADDLE_ENFORCE_EQ(
static_cast<int64_t>(index_indices.size()),
framework::product(slice_dims_for_assign),
platform::errors::InvalidArgument(
"OP(set_value) error index indices and value update not match "));
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);
value_t.mutable_data<T>(value_dims, place);
auto value_name = GetValueName(dtype);
CheckIsDimsMatch(slice_dims_for_assign, value_dims);
value_t.mutable_data<T>(value_dims, ctx.GetPlace());
auto value_name = GetValueName(in->type());
CopyVecotorToTensor<T>(value_name.c_str(), &value_t, ctx);
value_t.Resize(value_dims);
}
const Tensor* value_tensor_ptr =
(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);
auto stream = ctx.template device_context<NPUDeviceContext>().stream();
UnsqueezeAccordingNoneAxes(none_axes, in_dims_vec);
ModiftyDimsAccordingNoneAndDecrease(slice_dims_vec, value_dims_vec, axes,
none_axes,
decrease_axes); // Modify and Check
Tensor value_temp(in->type());
if (slice_dims_for_assign == value_t.dims()) {
value_temp.ShareDataWith(value_t);
} 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;
reshaped_value_tensor.ShareDataWith(*value_tensor_ptr);
reshaped_value_tensor.Resize(framework::make_ddim(value_dims_vec));
int64_t input_numel = framework::product(in_dims);
int64_t index_numel = index_indices.size();
BroadcastToD(&ctx, &reshaped_value_tensor, &slice_dims_vec,
&broadcast_value_tensor /*inner function initialized*/);
Tensor in_temp, out_temp, val_temp;
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));
SliceAssignNPU(&ctx, &broadcast_value_tensor, starts, ends, steps, axes,
out);
out->Resize(in_dims); // Reshape Back
NpuOpRunner runner;
runner.SetType("ScatterUpdate")
.AddInput(in_temp)
.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 paddle
namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_NPU_KERNEL(
set_value, ops::SetValueNPUKernel<paddle::platform::NPUDeviceContext, int>,
ops::SetValueNPUKernel<paddle::platform::NPUDeviceContext, float>)
REGISTER_OP_NPU_KERNEL(set_value, ops::SetValueNPUKernel<int>,
#ifdef PADDLE_WITH_ASCEND_INT64
ops::SetValueNPUKernel<int64_t>,
#endif
ops::SetValueNPUKernel<float>)
......@@ -23,13 +23,15 @@ import paddle
import paddle.fluid as fluid
from paddle.fluid import core
SEED = 2021
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()
paddle.device.set_device('npu')
self.set_dtype()
self.set_value()
self.set_shape()
......@@ -51,9 +53,6 @@ class TestSetValueBase(unittest.TestCase):
def _get_answer(self):
self.data[0, 0] = self.value
def set_npu(self):
self.__class__.use_npu = True
class TestSetValueApi(TestSetValueBase):
def _run_static(self):
......@@ -62,13 +61,13 @@ class TestSetValueApi(TestSetValueBase):
x = paddle.ones(shape=self.shape, dtype=self.dtype)
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])
paddle.disable_static()
return out
def _run_dynamic(self):
paddle.disable_static(paddle.NPUPlace(0))
paddle.disable_static(self.place)
x = paddle.ones(shape=self.shape, dtype=self.dtype)
self._call_setitem(x)
out = x.numpy()
......@@ -76,7 +75,6 @@ class TestSetValueApi(TestSetValueBase):
return out
def test_api(self):
self.set_input()
static_out = self._run_static()
dynamic_out = self._run_dynamic()
self._get_answer()
......@@ -134,23 +132,22 @@ class TestSetValueItemSlice4(TestSetValueApi):
self.data[0:, 1:2, :] = self.value
""" FIXEME : it seams that NPU don't support while operator ???
class TestSetValueItemSliceInWhile(TestSetValueApi):
def _call_setitem(self, x):
def cond(i, x):
return i < 1
# TODO(qili93): Fix this after NPU support while_loop
# class TestSetValueItemSliceInWhile(TestSetValueApi):
# def _call_setitem(self, x):
# def cond(i, x):
# return i < 1
def body(i, x):
x[i] = self.value
i = i + 1
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 body(i, x):
# x[i] = self.value
# i = i + 1
# return i, x
def _get_answer(self):
self.data[0] = self.value
"""
# i = paddle.zeros(shape=(1, ), dtype='int32')
# 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
......@@ -192,6 +189,60 @@ class TestSetValueItemSliceStep4(TestSetValueApi):
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
......@@ -277,6 +328,19 @@ class TestSetValueItemTensor5(TestSetValueApi):
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
class TestSetValueItemNone1(TestSetValueApi):
def _call_setitem(self, x):
......@@ -350,133 +414,99 @@ class TestSetValueItemNone9(TestSetValueApi):
self.data[None, :, 1, ..., None] = np.zeros(self.shape)[0, 0, :, None]
""" FIXME : current NPU set_value don't support negative step !!!
@xiongkun03
# 1.5 item is list or Tensor of bol
class TestSetValueItemBool1(TestSetValueApi):
def _call_setitem(self, x):
x[[True, False]] = self.value
class TestSetValueItemTensor6(TestSetValueApi):
def set_shape(self):
self.shape = [3, 4, 5]
def _get_answer(self):
self.data[[True, False]] = self.value
class TestSetValueItemBool2(TestSetValueApi):
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
x[[False, False]] = self.value
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):
class TestValueInt(parent):
def set_value(self):
self.value = 7
def _get_answer(self):
self.data[[False, True]] = np.zeros(self.shape[2])
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)
create_test_value_int32(TestSetValueItemSlice)
create_test_value_int32(TestSetValueItemSlice2)
create_test_value_int32(TestSetValueItemSlice3)
create_test_value_int32(TestSetValueItemSlice4)
def _get_answer(self):
self.data[np.array([False, True])] = np.zeros(self.shape[2])
def create_test_value_numpy_fp32(parent):
class TestValueInt(parent):
def set_value(self):
self.value = np.array([1])
class TestSetValueItemBool5(TestSetValueApi):
def _call_setitem(self, x):
idx = paddle.assign(
np.array([[False, True, False], [True, True, False]]))
x[idx] = self.value
def set_dtype(self):
self.dtype = "float32"
def _get_answer(self):
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)
create_test_value_numpy_fp32(TestSetValueItemSlice)
create_test_value_numpy_fp32(TestSetValueItemSlice2)
create_test_value_numpy_fp32(TestSetValueItemSlice3)
create_test_value_numpy_fp32(TestSetValueItemSlice4)
def _get_answer(self):
self.data[0, ...] = 0
self.data[self.data > 0] = self.value
def create_test_value_numpy_fp64(parent):
def create_test_value_int32(parent):
class TestValueInt(parent):
def set_value(self):
self.value = np.array([2**127]).astype("float64")
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)
self.value = 7
# 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):
self.dtype = "int32"
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__, "ValueTensorInt32")
cls_name = "{0}_{1}".format(parent.__name__, "ValueInt32")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
create_test_value_tensor_int32(TestSetValueItemInt)
create_test_value_tensor_int32(TestSetValueItemSlice)
create_test_value_tensor_int32(TestSetValueItemSlice2)
create_test_value_tensor_int32(TestSetValueItemSlice3)
create_test_value_tensor_int32(TestSetValueItemSlice4)
create_test_value_int32(TestSetValueItemInt)
create_test_value_int32(TestSetValueItemSlice)
create_test_value_int32(TestSetValueItemSlice2)
create_test_value_int32(TestSetValueItemSlice3)
create_test_value_int32(TestSetValueItemSlice4)
def create_test_value_tensor_int64(parent):
def create_test_value_int64(parent):
class TestValueInt(parent):
def set_value(self):
self.value = 7
def set_dtype(self):
self.dtype = "int64"
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__, "ValueTensorInt64")
cls_name = "{0}_{1}".format(parent.__name__, "ValueInt64")
TestValueInt.__name__ = cls_name
globals()[cls_name] = TestValueInt
create_test_value_tensor_int64(TestSetValueItemInt)
create_test_value_tensor_int64(TestSetValueItemSlice)
create_test_value_tensor_int64(TestSetValueItemSlice2)
create_test_value_tensor_int64(TestSetValueItemSlice3)
create_test_value_tensor_int64(TestSetValueItemSlice4)
create_test_value_int64(TestSetValueItemInt)
create_test_value_int64(TestSetValueItemSlice)
create_test_value_int64(TestSetValueItemSlice2)
create_test_value_int64(TestSetValueItemSlice3)
create_test_value_int64(TestSetValueItemSlice4)
def create_test_value_tensor_fp32(parent):
......@@ -503,30 +533,6 @@ create_test_value_tensor_fp32(TestSetValueItemSlice3)
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
class TestSetValueValueShape1(TestSetValueApi):
def set_value(self):
......@@ -589,59 +595,5 @@ class TestSetValueValueShape5(TestSetValueApi):
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__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册