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_while_mindir.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import os
  15. import numpy as np
  16. import pytest
  17. import mindspore.nn as nn
  18. from mindspore import context, ms_function
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.train.serialization import export, load
  21. class SingleWhileNet(nn.Cell):
  22. def construct(self, x, y):
  23. x += 1
  24. while x < y:
  25. x += 1
  26. y += 2 * x
  27. return y
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_ascend_training
  30. @pytest.mark.platform_arm_ascend_training
  31. @pytest.mark.env_onecard
  32. def test_single_while():
  33. context.set_context(mode=context.GRAPH_MODE)
  34. network = SingleWhileNet()
  35. x = Tensor(np.array([1]).astype(np.float32))
  36. y = Tensor(np.array([2]).astype(np.float32))
  37. origin_out = network(x, y)
  38. file_name = "while_net"
  39. export(network, x, y, file_name=file_name, file_format='MINDIR')
  40. mindir_name = file_name + ".mindir"
  41. assert os.path.exists(mindir_name)
  42. graph = load(mindir_name)
  43. loaded_net = nn.GraphCell(graph)
  44. outputs_after_load = loaded_net(x, y)
  45. assert origin_out == outputs_after_load
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_ascend_training
  48. @pytest.mark.platform_arm_ascend_training
  49. @pytest.mark.env_onecard
  50. def test_ms_function_while():
  51. context.set_context(mode=context.PYNATIVE_MODE)
  52. network = SingleWhileNet()
  53. x = Tensor(np.array([1]).astype(np.float32))
  54. y = Tensor(np.array([2]).astype(np.float32))
  55. origin_out = network(x, y)
  56. file_name = "while_net"
  57. export(network, x, y, file_name=file_name, file_format='MINDIR')
  58. mindir_name = file_name + ".mindir"
  59. assert os.path.exists(mindir_name)
  60. graph = load(mindir_name)
  61. loaded_net = nn.GraphCell(graph)
  62. @ms_function
  63. def run_graph(x, y):
  64. outputs = loaded_net(x, y)
  65. return outputs
  66. outputs_after_load = run_graph(x, y)
  67. assert origin_out == outputs_after_load
  68. class SingleWhileInlineNet(nn.Cell):
  69. def construct(self, x, y):
  70. x += 1
  71. while x < y:
  72. x += 1
  73. y += x
  74. return y
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_ascend_training
  77. @pytest.mark.platform_arm_ascend_training
  78. @pytest.mark.env_onecard
  79. def test_single_while_inline_export():
  80. context.set_context(mode=context.GRAPH_MODE)
  81. network = SingleWhileInlineNet()
  82. x = Tensor(np.array([1]).astype(np.float32))
  83. y = Tensor(np.array([2]).astype(np.float32))
  84. file_name = "while_inline_net"
  85. export(network, x, y, file_name=file_name, file_format='MINDIR')
  86. mindir_name = file_name + ".mindir"
  87. assert os.path.exists(mindir_name)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_ascend_training
  90. @pytest.mark.platform_arm_ascend_training
  91. @pytest.mark.env_onecard
  92. def test_single_while_inline_load():
  93. context.set_context(mode=context.GRAPH_MODE)
  94. network = SingleWhileInlineNet()
  95. x = Tensor(np.array([1]).astype(np.float32))
  96. y = Tensor(np.array([2]).astype(np.float32))
  97. file_name = "while_inline_net"
  98. export(network, x, y, file_name=file_name, file_format='MINDIR')
  99. mindir_name = file_name + ".mindir"
  100. assert os.path.exists(mindir_name)
  101. load(mindir_name)
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_ascend_training
  104. @pytest.mark.platform_arm_ascend_training
  105. @pytest.mark.env_onecard
  106. def test_single_while_inline():
  107. context.set_context(mode=context.GRAPH_MODE)
  108. network = SingleWhileInlineNet()
  109. x = Tensor(np.array([1]).astype(np.float32))
  110. y = Tensor(np.array([2]).astype(np.float32))
  111. origin_out = network(x, y)
  112. file_name = "while_inline_net"
  113. export(network, x, y, file_name=file_name, file_format='MINDIR')
  114. mindir_name = file_name + ".mindir"
  115. assert os.path.exists(mindir_name)
  116. graph = load(mindir_name)
  117. loaded_net = nn.GraphCell(graph)
  118. outputs_after_load = loaded_net(x, y)
  119. assert origin_out == outputs_after_load