You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mindspore/tests/ut/python/pynative_mode/test_partial.py

71 lines
1.6 KiB

# Copyright 2020 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_partial """
import mindspore.ops.composite as C
import mindspore.ops.functional as F
from mindspore import context
from mindspore.common.api import ms_function
def setup_module(module):
context.set_context(mode=context.PYNATIVE_MODE)
def myadd(x, y):
return x + y
def partial_simple_add(x):
return F.partial(myadd, x)
@ms_function
def full_simple_add(x, y):
p = partial_simple_add(x)
return p(y)
def test_full_simple_add():
print(full_simple_add(2, 5))
# partial with multitype
MULTI_ADD = C.MultitypeFuncGraph('add')
@MULTI_ADD.register("Int64", "Int64")
def add_int(x, y):
return F.scalar_add(x, y)
@MULTI_ADD.register("Float32", "Float32")
def add_float(x, y):
return F.scalar_add(x, y)
def partial_multi_add(x):
return F.partial(MULTI_ADD, x)
@ms_function
def full_multi_add(x, y, m, n):
p = partial_multi_add(x)(y)
q = partial_multi_add(m)(n)
return p, q
def test_full_multi_add():
print(full_multi_add(1, 2, 1.0, 2.0))