You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_range_op.py 1.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  20. class OpNetWrapper(nn.Cell):
  21. def __init__(self, op):
  22. super(OpNetWrapper, self).__init__()
  23. self.op = op
  24. def construct(self, *inputs):
  25. return self.op(*inputs)
  26. @pytest.mark.level0
  27. @pytest.mark.platform_x86_cpu
  28. @pytest.mark.env_onecard
  29. def test_int():
  30. op = nn.Range(0, 100, 10)
  31. op_wrapper = OpNetWrapper(op)
  32. outputs = op_wrapper()
  33. print(outputs)
  34. assert outputs.shape == (10,)
  35. assert np.allclose(outputs.asnumpy(), range(0, 100, 10))
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_cpu
  38. @pytest.mark.env_onecard
  39. def test_float():
  40. op = nn.Range(10., 100., 20.)
  41. op_wrapper = OpNetWrapper(op)
  42. outputs = op_wrapper()
  43. print(outputs)
  44. assert outputs.shape == (5,)
  45. assert np.allclose(outputs.asnumpy(), [10., 30., 50., 70., 90.])
  46. if __name__ == '__main__':
  47. test_int()
  48. test_float()