Browse Source

support function as condition of if

tags/v1.0.0
buxue 5 years ago
parent
commit
b9c9046b93
4 changed files with 58 additions and 4 deletions
  1. +5
    -0
      mindspore/_extends/parse/standard_method.py
  2. +4
    -0
      mindspore/ccsrc/pipeline/jit/resource.cc
  3. +39
    -0
      tests/ut/python/pipeline/parse/test_if_function.py
  4. +10
    -4
      tests/ut/python/runtest.sh

+ 5
- 0
mindspore/_extends/parse/standard_method.py View File

@@ -275,6 +275,11 @@ def none_bool(x):
return False


def func_bool(x):
"""Implementation of `func_bool`."""
return True


def float_floordiv(x, y):
"""Implementation of `float_floordiv`."""
return floor(x / y)


+ 4
- 0
mindspore/ccsrc/pipeline/jit/resource.cc View File

@@ -37,6 +37,10 @@ BuiltInTypeMap &GetMethodMap() {
{
{"__bool__", std::string("none_bool")} // C.none_bool
}},
{kObjectTypeFunction,
{
{"__bool__", std::string("func_bool")} // C.str_bool
}},
{kNumberTypeBool,
{
{"__and__", prim::kPrimBoolAnd}, // P.bool_and


+ 39
- 0
tests/ut/python/pipeline/parse/test_if_function.py View File

@@ -0,0 +1,39 @@
# 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 if function"""
import numpy as np

import mindspore.nn as nn
from mindspore import Tensor
from mindspore import context

context.set_context(mode=context.GRAPH_MODE)

def test_if_function():
class Net(nn.Cell):
def __init__(self, func):
super(Net, self).__init__()
self.func = func

def construct(self, x, y):
if self.func:
return self.func(x, y)
return x - y
def add(x, y):
return x + y
net = Net(add)
x = Tensor(np.ones([1, 2, 3], np.int32))
y = Tensor(np.ones([1, 2, 3], np.int32))
net(x, y)

+ 10
- 4
tests/ut/python/runtest.sh View File

@@ -36,28 +36,33 @@ if [ $# -eq 1 ] && ([ "$1" == "stage1" ] || [ "$1" == "stage2" ] || [ "$1" ==

elif [ $1 == "stage2" ]; then
echo "run python parallel\train\ops ut"
pytest -n 4 --dist=loadfile -v $CURRPATH/parallel $CURRPATH/train $CURRPATH/ops
pytest -n 4 --dist=loadfile -v $CURRPATH/parallel $CURRPATH/train
RET=$?
if [ ${RET} -ne 0 ]; then
exit ${RET}
fi

pytest -n 2 --dist=loadfile -v $CURRPATH/ops

elif [ $1 == "stage3" ]; then
echo "run other ut"
pytest --ignore=$CURRPATH/dataset --ignore=$CURRPATH/parallel --ignore=$CURRPATH/train --ignore=$CURRPATH/ops --ignore=$CURRPATH/pynative_mode $IGNORE_EXEC $CURRPATH

RET=$?
if [ ${RET} -ne 0 ]; then
exit ${RET}
fi

pytest $CURRPATH/pynative_mode
fi
else
echo "run all python ut"
pytest $CURRPATH/dataset

RET=$?
if [ ${RET} -ne 0 ]; then
exit ${RET}
fi
pytest -n 4 --dist=loadfile -v $CURRPATH/parallel $CURRPATH/train $CURRPATH/ops

pytest -n 4 --dist=loadfile -v $CURRPATH/parallel $CURRPATH/train $CURRPATH/ops
RET=$?
if [ ${RET} -ne 0 ]; then
exit ${RET}
@@ -68,6 +73,7 @@ else
if [ ${RET} -ne 0 ]; then
exit ${RET}
fi

pytest $CURRPATH/pynative_mode
fi



Loading…
Cancel
Save