!33955 add Ascend dynamic shape testcases for IndexAdd

Merge pull request !33955 from looop5/index_add_d_dynamic
This commit is contained in:
i-robot 2022-05-11 03:14:48 +00:00 committed by Gitee
commit ddced156b0
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 104 additions and 12 deletions

View File

@ -0,0 +1,35 @@
mindspore.ops.IndexAdd
======================
.. py:class:: mindspore.ops.IndexAdd(axis, use_lock=True, check_index_bound=True)
将Tensor `y` 加到Parameter `x` 的指定 `axis` 轴的指定 `indices` 位置。要求 `axis` 轴的取值范围
为[0, len(x.dim) - 1] `indices` 中元素的取值范围为[0, x.shape[axis] - 1]。
**参数:**
- **axis** (int) - 指定沿哪根轴相加。
- **use_lock** (bool) - 是否对参数更新过程加锁保护。如果为True在更新参数 `x` 的值时使用原子操作以实现加锁保护,如果为
False `x` 的值可能会不可预测。默认值True。
- **check_index_bound** (bool) - True表示检查 `indices` 边界False表示不检查。默认值True。
**输入:**
- **x** (Parameter) - 被加的Parameter。
- **indices** (Tensor) - 指定Tensor `y` 加到 `x``axis` 轴的指定下标位置要求数据类型为int32。
要求 `indices` shape的维度为一维并且 `indices` shape的大小与 `y` shape在 `axis` 轴上的大小一致。 `indices` 中元素
取值范围为[0, b)其中b的值为 `x` shape在 `axis` 轴上的大小。
- **y** (Tensor) - 与 `x` 加的Tensor。
**输出:**
Tensorshape和数据类型与输入 `x` 相同。
**异常:**
- **TypeError** - `x` 的类型不是Parameter。
- **TypeError** - `indices` 或者 `y` 的类型不是Tensor。
- **ValueError** - `axis` 的值超出 `x` shape的维度范围。
- **ValueError** - `x` shape的维度和 `y` shape的维度不一致。
- **ValueError** - `indices` shape的维度不是一维或者 `indices` shape的大小与 `y` shape在 `axis` 轴上的大小不一致。
- **ValueError** - 除 `axis` 轴外,`x` shape和 `y` shape的大小不一致。

View File

@ -3,17 +3,20 @@ mindspore.ops.index_add
.. py:function:: mindspore.ops.index_add(x, indices, y, axis, use_lock=True, check_index_bound=True)
将Tensor y加到Parameter x的指定axis轴的指定indices位置。要求axis轴的取值范围为[0, len(x.dim) - 1]indices元素的取值范围
为[0, x.shape[axis] - 1]。
将Tensor `y` 加到Parameter `x` 的指定 `axis` 轴的指定 `indices` 位置。要求 `axis`的取值范围
为[0, len(x.dim) - 1] `indices` 中元素的取值范围为[0, x.shape[axis] - 1]。
**参数:**
- **x** (Parameter) - 被加Parameter。
- **indices** (Tensor) - 指定Tensor y加到 `x` 的指定axis轴的指定indices位置。
- **x** (Parameter) - 被加的Parameter。
- **indices** (Tensor) - 指定Tensor `y` 加到 `x``axis` 轴的指定下标位置要求数据类型为int32。
要求 `indices` shape的维度为一维并且 `indices` shape的大小与 `y` shape在 `axis` 轴上的大小一致。 `indices` 中元素
取值范围为[0, b)其中b的值为 `x` shape在 `axis` 轴上的大小。
- **y** (Tensor) - 与 `x` 加的Tensor。
- **axis** (int) - 指定Tensor y加到 `x` 的指定axis轴。
- **use_lock** (bool) - 计算时使用锁。默认值True。
- **check_index_bound** (bool) - indices边界检查。默认值True。
- **axis** (int) - 指定沿哪根轴相加。
- **use_lock** (bool) - 是否对参数更新过程加锁保护。如果为True在更新参数 `x` 的值时使用原子操作以实现加锁保护,如果为
False `x` 的值可能会不可预测。默认值True。
- **check_index_bound** (bool) - True表示检查 `indices` 边界False表示不检查。默认值True。
**返回:**
@ -21,6 +24,7 @@ mindspore.ops.index_add
**异常:**
- **TypeError** - `x` 的类型不是Parameter。
- **TypeError** - `indices` 或者 `y` 的类型不是Tensor。
- **ValueError** - `axis` 的值超出 `x` shape的维度范围。
- **ValueError** - `x` shape的维度和 `y` shape的维度不一致。

View File

@ -1,5 +1,5 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -60,6 +60,7 @@ std::string OpTilingCalculateAdapter::GetRealOpType(const std::string &op_type)
{"ParallelResizeBilinearGrad", "SyncResizeBilinearV2Grad"},
{"CeLU", "CeluV2"},
{"TransposeNOD", "Transpose"},
{"IndexAdd", "InplaceIndexAdd"},
};
auto iter = kOpTypeMap.find(op_type);
if (iter == kOpTypeMap.end()) {

View File

@ -149,14 +149,16 @@ def index_add(x, indices, y, axis, use_lock=True, check_index_bound=True):
Args:
x (Parameter): The input Parameter to add to.
indices (Tensor): Add the value of `x` and `y` along the dimension of the `axis` according to the
indices (Tensor): Add the value of `x` and `y` along the dimension of the `axis` according to the
specified index value, with data type int32.
The `indices` must be 1D with the same size as the size of `y` in the `axis` dimension. The values
of `indices` should be in [0, b), where the b is the size of `x` in the `axis` dimension.
y (Tensor): The input tensor with the value to add. Must have same data type as `x`.
The shape must be the same as `x` except the `axis` th dimension.
axis (int): The dimension along which to index.
use_lock (bool): If true, use lock mode. If false, don't use lock mode. Default: True.
use_lock (bool): Whether to enable a lock to protect the updating process of variable tensors.
If true, when updating the value of `x`, this process will be protected by a lock by using atomic operation.
If false, the result may be unpredictable. Default: True.
check_index_bound (bool): If true, check index boundary. If false, don't check index boundary. Default: True.
Returns:

View File

@ -5082,12 +5082,14 @@ class IndexAdd(Primitive):
Args:
axis (int): The dimension along which to index.
use_lock (bool): If true, use lock mode. If false, don't use lock mode. Default: True.
use_lock (bool): Whether to enable a lock to protect the updating process of variable tensors.
If true, when updating the value of `x`, this process will be protected by a lock by using atomic operation.
If false, the result may be unpredictable. Default: True.
check_index_bound (bool): If true, check index boundary. If false, don't check index boundary. Default: True.
Inputs:
- **x** (Parameter) - The input Parameter to add to.
- **indices** (Tensor) - Add the value of `x` and `y` along the dimension of the `axis` according to the
- **indices** (Tensor) - Add the value of `x` and `y` along the dimension of the `axis` according to the
specified index value, with data type int32.
The `indices` must be 1D with the same size as the size of `y` in the `axis` dimension. The values
of `indices` should be in [0, b), where the b is the size of `x` in the `axis` dimension.

View File

@ -243,3 +243,51 @@ def test_index_add_grad_uint8():
index_add_grad_with_type(np.uint8)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
index_add_grad_with_type(np.uint8)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_index_add_dynamic_y():
"""
Feature: test IndexAdd dynamic shape.
Description: input y is dynamic shape.
Expectation: the result match with numpy result
"""
x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float32)
y = np.ones((2, 2, 4), dtype=np.float32)
idx = np.array([0, 2]).astype(np.int32)
axis = 1
expect = np.copy(x)
expect[:, idx, :] = expect[:, idx, :] + y
y_dyn = Tensor(shape=[2, None, 4], dtype=mindspore.float32)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
net = NetIndexAdd(x, axis)
net.set_inputs(Tensor(idx), y_dyn)
output = net(Tensor(idx), Tensor(y))
assert (output.asnumpy() == expect).all()
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_index_add_dynamic_indices():
"""
Feature: test IndexAdd dynamic shape.
Description: input indices is dynamic shape.
Expectation: the result match with numpy result
"""
x = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(np.float32)
y = np.ones((2, 2, 4), dtype=np.float32)
idx = np.array([0, 2]).astype(np.int32)
axis = 1
expect = np.copy(x)
expect[:, idx, :] = expect[:, idx, :] + y
idx_dyn = Tensor(shape=[None], dtype=mindspore.int32)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
net = NetIndexAdd(x, axis)
net.set_inputs(idx_dyn, Tensor(y))
output = net(Tensor(idx), Tensor(y))
assert (output.asnumpy() == expect).all()