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/st/ops/cpu/test_range_op.py

63 lines
1.7 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.
# ============================================================================
import numpy as np
import pytest
import mindspore.context as context
import mindspore.nn as nn
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
class OpNetWrapper(nn.Cell):
def __init__(self, op):
super(OpNetWrapper, self).__init__()
self.op = op
def construct(self, *inputs):
return self.op(*inputs)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_int():
op = nn.Range(0, 100, 10)
op_wrapper = OpNetWrapper(op)
outputs = op_wrapper()
print(outputs)
assert outputs.shape == (10,)
assert np.allclose(outputs.asnumpy(), range(0, 100, 10))
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_float():
op = nn.Range(10., 100., 20.)
op_wrapper = OpNetWrapper(op)
outputs = op_wrapper()
print(outputs)
assert outputs.shape == (5,)
assert np.allclose(outputs.asnumpy(), [10., 30., 50., 70., 90.])
if __name__ == '__main__':
test_int()
test_float()