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.py 2.5 kB

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2019 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. from mindspore import Tensor
  17. from mindspore.common.api import ms_function
  18. from mindspore.ops import operations as P
  19. def test_nest_range_transpose():
  20. batch_size = 2
  21. num_layers = 5
  22. batch_tuple = tuple(Tensor(np.array(np.ones((2, 3)) * 0.01)) for i in range(batch_size))
  23. layers_tuple = tuple(Tensor(np.array(np.ones((3, 4)) * 0.02)) for i in range(num_layers))
  24. transpose1 = P.Transpose()
  25. @ms_function()
  26. def invoke_range():
  27. out1 = ()
  28. for m in range(num_layers):
  29. out1 += (transpose1(layers_tuple[m], (1, 0)),)
  30. # Both for loop will the same range symbol as phi node, when range primitive is converted
  31. # to DoSigature MetaFuncGraph, that MetaFuncGraph will take 2 and 5 as argument, so there is
  32. # 2 entries in that MetaFuncGraphEvaluator, that will make Specialier try to use AnyValue to
  33. # FindGeneralized for S-make_range MetaFuncGraph but it will fail as AnyValue is not constant.
  34. for i in range(batch_size):
  35. out1 += (transpose1(batch_tuple[i], (1, 0)),)
  36. for j in range(num_layers):
  37. out1 += (transpose1(layers_tuple[j], (1, 0)),)
  38. return out1
  39. print(invoke_range())
  40. def test_nest_range_simple():
  41. batch_size = 2
  42. num_layers = 5
  43. batch_tuple = tuple(Tensor(np.array(np.ones((2, 3)) * 0.01)) for i in range(batch_size))
  44. layers_tuple = tuple(Tensor(np.array(np.ones((3, 4)) * 0.02)) for i in range(num_layers))
  45. @ms_function()
  46. def invoke_range():
  47. out1 = ()
  48. for m in range(num_layers):
  49. out1 += (layers_tuple[m],)
  50. for i in range(batch_size):
  51. out1 += (batch_tuple[i],)
  52. for j in range(num_layers):
  53. out1 += (layers_tuple[j],)
  54. return out1
  55. print(invoke_range())