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_bert.py 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 bert cell """
  16. import numpy as np
  17. import pytest
  18. from mindspore.model_zoo.Bert_NEZHA import BertConfig, BertModel
  19. from ....dataset_mock import MindData
  20. def map_bert(record):
  21. target_data = {'input_ids': None, 'input_mask': None,
  22. 'segment_ids': None, 'next_sentence_labels': None,
  23. 'masked_lm_positions': None, 'masked_lm_ids': None,
  24. 'masked_lm_weights': None}
  25. sample = dt.parse_single_example(record, target_data)
  26. return sample['input_ids'], sample['input_mask'], sample['segment_ids'], \
  27. sample['next_sentence_labels'], sample['masked_lm_positions'], \
  28. sample['masked_lm_ids'], sample['masked_lm_weights']
  29. def test_bert_model():
  30. # test for config.hidden_size % config.num_attention_heads != 0
  31. config_error = BertConfig(32, hidden_size=512, num_attention_heads=10)
  32. with pytest.raises(ValueError):
  33. BertModel(config_error, True)
  34. def get_dataset(batch_size=1):
  35. dataset_types = (np.int32, np.int32, np.int32, np.int32, np.int32, np.int32, np.int32)
  36. dataset_shapes = ((batch_size, 128), (batch_size, 128), (batch_size, 128), (batch_size, 1),
  37. (batch_size, 20), (batch_size, 20), (batch_size, 20))
  38. dataset = MindData(size=2, batch_size=batch_size,
  39. np_types=dataset_types,
  40. output_shapes=dataset_shapes,
  41. input_indexs=(0, 1))
  42. return dataset