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.2 kB

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