Enable list getitem with bool index
This commit is contained in:
parent
59b92c0c29
commit
6cabd733b6
|
@ -189,6 +189,7 @@
|
|||
"mindspore/tests/st/pynative/parser/test_parser_construct.py" "bad-super-call"
|
||||
"mindspore/tests/st/gradient/test_grad_return_type.py" "unused-variable"
|
||||
"mindspore/tests/ut/python/optimizer/test_auto_grad.py" "broad-except"
|
||||
"mindspore/tests/st/syntax/simple_expression/test_sequence_getitem.py" "simplifiable-if-statement"
|
||||
"mindspore/tests/st/fallback/control_flow/test_fallback_100_if_after_if.py" "unused-variable"
|
||||
"mindspore/tests/st/numpy_native/test_array_ops.py" "useless-super-delegation"
|
||||
"mindspore/tests/ut/python/mindir/test_mindir_export.py" "no-else-return"
|
||||
|
|
|
@ -118,6 +118,11 @@ def _tuple_getitem_by_number(data, number_index):
|
|||
Outputs:
|
||||
Type, is the same as the element type of data.
|
||||
"""
|
||||
if isinstance(number_index, bool):
|
||||
if number_index:
|
||||
number_index = 1
|
||||
else:
|
||||
number_index = 0
|
||||
return F.tuple_getitem(data, number_index)
|
||||
|
||||
|
||||
|
@ -174,6 +179,11 @@ def _list_getitem_by_number(data, number_index):
|
|||
Outputs:
|
||||
Type is the same as the element type of data.
|
||||
"""
|
||||
if isinstance(number_index, bool):
|
||||
if number_index:
|
||||
number_index = 1
|
||||
else:
|
||||
number_index = 0
|
||||
return F.list_getitem(data, number_index)
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,156 @@
|
|||
# Copyright 2023 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.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test graph mode sequence getitem """
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
from mindspore import context, jit
|
||||
from mindspore.common.tensor import Tensor
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tuple_getitem_with_constant_bool_index():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo():
|
||||
m = (1, 2, 3, 4)
|
||||
return m[True], m[False]
|
||||
|
||||
ret1, ret2 = foo()
|
||||
assert ret1 == 2
|
||||
assert ret2 == 1
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tuple_getitem_with_constant_bool_index_2():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo(x):
|
||||
m = (x, x+1, x+2, x+3)
|
||||
return m[True], m[False]
|
||||
|
||||
ret1, ret2 = foo(Tensor([1, 2, 3, 4]))
|
||||
assert np.all(ret1.asnumpy() == np.array([2, 3, 4, 5]))
|
||||
assert np.all(ret2.asnumpy() == np.array([1, 2, 3, 4]))
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tuple_getitem_with_variable_bool_index():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo(x):
|
||||
if x > 0:
|
||||
index = True
|
||||
else:
|
||||
index = False
|
||||
m = (x, x+1, x+2, x+3)
|
||||
return m[index], m[not index]
|
||||
|
||||
ret1, ret2 = foo(Tensor([1]))
|
||||
assert np.all(ret1.asnumpy() == np.array([2]))
|
||||
assert np.all(ret2.asnumpy() == np.array([1]))
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_list_getitem_with_constant_bool_index():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo():
|
||||
m = [1, 2, 3, 4]
|
||||
return m[True], m[False]
|
||||
|
||||
ret1, ret2 = foo()
|
||||
assert ret1 == 2
|
||||
assert ret2 == 1
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_list_getitem_with_constant_bool_index_2():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo(x):
|
||||
m = [x, x+1, x+2, x+3]
|
||||
return m[True], m[False]
|
||||
|
||||
ret1, ret2 = foo(Tensor([1, 2, 3, 4]))
|
||||
assert np.all(ret1.asnumpy() == np.array([2, 3, 4, 5]))
|
||||
assert np.all(ret2.asnumpy() == np.array([1, 2, 3, 4]))
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.platform_arm_ascend_training
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_list_getitem_with_variable_bool_index():
|
||||
"""
|
||||
Feature: sequence getitem with bool index
|
||||
Description: sequence getitem
|
||||
Expectation: No exception
|
||||
"""
|
||||
@jit
|
||||
def foo(x):
|
||||
if x > 0:
|
||||
index = True
|
||||
else:
|
||||
index = False
|
||||
m = [x, x+1, x+2, x+3]
|
||||
return m[index], m[not index]
|
||||
|
||||
ret1, ret2 = foo(Tensor([1]))
|
||||
assert np.all(ret1.asnumpy() == np.array([2]))
|
||||
assert np.all(ret2.asnumpy() == np.array([1]))
|
Loading…
Reference in New Issue