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_lstm.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. """ test lstm """
  16. import pytest
  17. import mindspore.context as context
  18. from mindspore import nn
  19. from ..ut_filter import run_on_gpu
  20. from ....ops_common import convert
  21. class LstmTestNet(nn.Cell):
  22. """ LstmTestNet definition """
  23. def __init__(self, input_size, hidden_size, num_layers, has_bias, batch_first, bidirectional):
  24. super(LstmTestNet, self).__init__()
  25. self.lstm = nn.LSTM(input_size=input_size,
  26. hidden_size=hidden_size,
  27. num_layers=num_layers,
  28. has_bias=has_bias,
  29. batch_first=batch_first,
  30. bidirectional=bidirectional,
  31. dropout=0.0)
  32. def construct(self, inp, h0, c0):
  33. return self.lstm(inp, (h0, c0))
  34. test_case_cell_ops = [
  35. ('lstm1_with_bias', {
  36. 'cell': LstmTestNet(10, 12, 2, has_bias=True, batch_first=False, bidirectional=False),
  37. 'input_shape': [[5, 3, 10], [2, 3, 12], [2, 3, 12]],
  38. 'output_shape': [[5, 3, 12], [2, 3, 12], [2, 3, 12]]}),
  39. ('lstm2_without_bias', {
  40. 'cell': LstmTestNet(10, 12, 2, has_bias=False, batch_first=False, bidirectional=False),
  41. 'input_shape': [[5, 3, 10], [2, 3, 12], [2, 3, 12]],
  42. 'output_shape': [[5, 3, 12], [2, 3, 12], [2, 3, 12]]}),
  43. ('lstm3_with_bias_bidirectional', {
  44. 'cell': LstmTestNet(10, 12, 2, has_bias=True, batch_first=False, bidirectional=True),
  45. 'input_shape': [[5, 3, 10], [4, 3, 12], [4, 3, 12]],
  46. 'output_shape': [[5, 3, 24], [4, 3, 12], [4, 3, 12]]}),
  47. ('lstm4_without_bias_bidirectional', {
  48. 'cell': LstmTestNet(10, 12, 2, has_bias=False, batch_first=False, bidirectional=True),
  49. 'input_shape': [[5, 3, 10], [4, 3, 12], [4, 3, 12]],
  50. 'output_shape': [[5, 3, 24], [4, 3, 12], [4, 3, 12]]}),
  51. ('lstm5_with_bias_batch_first', {
  52. 'cell': LstmTestNet(10, 12, 2, has_bias=True, batch_first=True, bidirectional=False),
  53. 'input_shape': [[3, 5, 10], [2, 3, 12], [2, 3, 12]],
  54. 'output_shape': [[3, 5, 12], [2, 3, 12], [2, 3, 12]]}),
  55. ('lstm6_without_bias_batch_first', {
  56. 'cell': LstmTestNet(10, 12, 2, has_bias=False, batch_first=True, bidirectional=False),
  57. 'input_shape': [[3, 5, 10], [2, 3, 12], [2, 3, 12]],
  58. 'output_shape': [[3, 5, 12], [2, 3, 12], [2, 3, 12]]}),
  59. ('lstm7_with_bias_bidirectional_batch_first', {
  60. 'cell': LstmTestNet(10, 12, 2, has_bias=True, batch_first=True, bidirectional=True),
  61. 'input_shape': [[3, 5, 10], [4, 3, 12], [4, 3, 12]],
  62. 'output_shape': [[3, 5, 24], [4, 3, 12], [4, 3, 12]]}),
  63. ('lstm8_without_bias_bidirectional_batch_first', {
  64. 'cell': LstmTestNet(10, 12, 2, has_bias=False, batch_first=True, bidirectional=True),
  65. 'input_shape': [[3, 5, 10], [4, 3, 12], [4, 3, 12]],
  66. 'output_shape': [[3, 5, 24], [4, 3, 12], [4, 3, 12]]}),
  67. ]
  68. # use -k to select certain testcast
  69. # pytest tests/python/ops/test_lstm.py::test_compile -k lstm_with_bias
  70. @pytest.mark.parametrize('args', test_case_cell_ops, ids=lambda x: x[0])
  71. def test_compile(args):
  72. config = args[1]
  73. shapes = config['input_shape']
  74. net = config['cell']
  75. net.set_train()
  76. inputs = [convert(shp) for shp in shapes]
  77. out = net(*inputs)
  78. print(f"out: {out}")
  79. @run_on_gpu
  80. @pytest.mark.parametrize('args', test_case_cell_ops, ids=lambda x: x[0])
  81. def test_execute(args):
  82. """ test_execute """
  83. config = args[1]
  84. shapes = config['input_shape']
  85. net = config['cell']
  86. net.set_train()
  87. inputs = [convert(shp) for shp in shapes]
  88. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  89. # pylint: disable=unused-variable
  90. ret, (hn, cn) = net(*inputs)
  91. print(f'result: {shapes[0]} --> {ret.asnumpy().shape}, expected: {config["output_shape"][0]}')