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_triu.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. """
  16. test nn.Triu()
  17. """
  18. import os
  19. import numpy as np
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. from mindspore import context
  23. context.set_context(mode=context.GRAPH_MODE)
  24. class TriuNet(nn.Cell):
  25. def __init__(self):
  26. super(TriuNet, self).__init__()
  27. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  28. def construct(self):
  29. triu = nn.Triu()
  30. return triu(self.value, 0)
  31. def test_triu():
  32. """
  33. Feature: None
  34. Description: test TriuNet with vm backend
  35. Expectation: None
  36. """
  37. net = TriuNet()
  38. out = net()
  39. assert np.sum(out.asnumpy()) == 26
  40. def test_triu_ge():
  41. """
  42. Feature: unify ge and vm backend
  43. Description: test TriuNet with ge backend
  44. Expectation: None
  45. """
  46. os.environ['MS_ENABLE_GE'] = "1"
  47. os.environ['MS_GE_TRAIN'] = "0"
  48. net = TriuNet()
  49. out = net()
  50. del os.environ['MS_GE_TRAIN']
  51. del os.environ['MS_ENABLE_GE']
  52. assert np.sum(out.asnumpy()) == 26
  53. def test_triu_1():
  54. class Net(nn.Cell):
  55. def __init__(self):
  56. super(Net, self).__init__()
  57. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  58. def construct(self):
  59. triu = nn.Triu()
  60. return triu(self.value, 1)
  61. net = Net()
  62. out = net()
  63. assert np.sum(out.asnumpy()) == 11
  64. def test_triu_2():
  65. class Net(nn.Cell):
  66. def __init__(self):
  67. super(Net, self).__init__()
  68. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  69. def construct(self):
  70. triu = nn.Triu()
  71. return triu(self.value, -1)
  72. net = Net()
  73. out = net()
  74. assert np.sum(out.asnumpy()) == 38
  75. def test_triu_parameter():
  76. class Net(nn.Cell):
  77. def construct(self, x):
  78. triu = nn.Triu()
  79. return triu(x, 0)
  80. net = Net()
  81. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  82. def test_triu_parameter_1():
  83. class Net(nn.Cell):
  84. def construct(self, x):
  85. triu = nn.Triu()
  86. return triu(x, 1)
  87. net = Net()
  88. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  89. def test_triu_parameter_2():
  90. class Net(nn.Cell):
  91. def construct(self, x):
  92. triu = nn.Triu()
  93. return triu(x, -1)
  94. net = Net()
  95. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))