forked from mindspore-Ecosystem/mindspore
!38638 Support abs(Tensor) in graph mode
Merge pull request !38638 from huangbingjian/fallback_builtin
This commit is contained in:
commit
acd873e378
|
@ -106,7 +106,7 @@ _unsupported_internal_type = (
|
|||
)
|
||||
|
||||
_hybrid_type = (
|
||||
print, len, enumerate, zip, map, filter,
|
||||
print, len, enumerate, zip, map, filter, abs,
|
||||
)
|
||||
|
||||
# Unsupported python builtin type in JIT Fallback.
|
||||
|
|
|
@ -127,6 +127,7 @@ convert_object_map = {
|
|||
T.not_contains: multitype_ops.not_in_,
|
||||
|
||||
# system function
|
||||
T.abs: M.ms_abs,
|
||||
T.len: M.ms_len,
|
||||
T.bool_: M.bool_,
|
||||
T.map: C.Map(),
|
||||
|
|
|
@ -1656,6 +1656,21 @@ def hasnext(it):
|
|||
return it.__ms_hasnext__()
|
||||
|
||||
|
||||
@constexpr
|
||||
def constant_abs(x):
|
||||
"""Returns the absolute value of the constant."""
|
||||
if x is None:
|
||||
raise ValueError("For abs(), the parameter should be a constant or Tensor type.")
|
||||
return abs(x)
|
||||
|
||||
|
||||
def ms_abs(x):
|
||||
"""Implementation of `abs`."""
|
||||
if isinstance(x, Tensor):
|
||||
return abs_(x)
|
||||
return constant_abs(x)
|
||||
|
||||
|
||||
def ms_len(data):
|
||||
"""Implementation of `len`."""
|
||||
return data.__len__()
|
||||
|
|
|
@ -28,7 +28,7 @@ from operator import ( # noqa
|
|||
# support system function call
|
||||
from builtins import ( # noqa
|
||||
bool, getattr, setattr, len, iter, next, pow, range, map, zip,
|
||||
print, enumerate, isinstance, filter
|
||||
print, enumerate, isinstance, filter, abs
|
||||
)
|
||||
|
||||
# support functools
|
||||
|
@ -45,8 +45,8 @@ __all__ = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod', 'eq', 'ne', 'lt',
|
|||
'not_', 'and_', 'or_', 'xor', 'lshift', 'rshift', 'invert', 'is_', 'is_not', 'contains',
|
||||
'matmul', 'getitem', 'setitem',
|
||||
'bool', 'getattr', 'setattr', 'len', 'iter', 'next', 'pow', 'range', 'map', 'zip',
|
||||
'partial', 'print', 'enumerate', 'isinstance',
|
||||
'exp', 'log', 'sin', 'cos', 'tan', 'filter']
|
||||
'partial', 'print', 'enumerate', 'isinstance', 'filter', 'abs',
|
||||
'exp', 'log', 'sin', 'cos', 'tan']
|
||||
|
||||
|
||||
def MakeTuple(*elts): # pragma: no cover
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
# Copyright 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.
|
||||
# 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 python built-in functions in graph mode"""
|
||||
import pytest
|
||||
import numpy as np
|
||||
from mindspore import Tensor, context, nn
|
||||
|
||||
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_fallback_abs_tensor():
|
||||
"""
|
||||
Feature: JIT Fallback
|
||||
Description: Test abs(Tensor) a variable tensor in construct function in graph mode
|
||||
Expectation: No exception
|
||||
"""
|
||||
|
||||
class TestCell(nn.Cell):
|
||||
def construct(self, y):
|
||||
x = Tensor([-1, 2])
|
||||
return abs(x + y)
|
||||
|
||||
test_cell = TestCell()
|
||||
assert np.all(test_cell(Tensor([-1, 2])).asnumpy() == np.array([2, 4]))
|
|
@ -15,7 +15,6 @@
|
|||
""" test graph fallback """
|
||||
import math
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from mindspore import ms_function, context, Tensor, nn
|
||||
|
||||
|
@ -76,10 +75,10 @@ def test_fallback_abs_numpy():
|
|||
|
||||
@ms_function
|
||||
def foo():
|
||||
x = abs(np.array([1, -2, 3]))
|
||||
x = abs(np.array([1, -2, 3])) + 1
|
||||
return Tensor(x)
|
||||
|
||||
assert np.all(foo().asnumpy() == abs(np.array([-1, 2, -3])))
|
||||
assert np.all(foo().asnumpy() == abs(np.array([-1, 2, -3]))) + 1
|
||||
|
||||
|
||||
def test_fallback_abs_cell_construct_tensor():
|
||||
|
@ -98,23 +97,6 @@ def test_fallback_abs_cell_construct_tensor():
|
|||
assert np.all(test_cell().asnumpy() == np.array([1, 2]))
|
||||
|
||||
|
||||
@pytest.mark.skip("Not Supported yet not support variable")
|
||||
def test_fallback_abs_cell_variable_tensor():
|
||||
"""
|
||||
Feature: JIT Fallback
|
||||
Description: Test abs(Tensor) a variable tensor in construct function in graph mode
|
||||
Expectation: No exception
|
||||
"""
|
||||
|
||||
class TestCell(nn.Cell):
|
||||
def construct(self, y):
|
||||
x = Tensor([-1, 2])
|
||||
return abs(x + y)
|
||||
|
||||
test_cell = TestCell()
|
||||
assert np.all(test_cell(Tensor([-1, 2])).asnumpy() == np.array([2, 4]))
|
||||
|
||||
|
||||
def test_fallback_abs_cell_init_tensor():
|
||||
"""
|
||||
Feature: JIT Fallback
|
||||
|
|
Loading…
Reference in New Issue