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.

generate_model.py 2.5 kB

5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import random
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.common.dtype as mstype
  19. import mindspore.dataset as de
  20. from mindspore import Tensor, context
  21. from mindspore.ops import operations as P
  22. from mindspore.train.serialization import export
  23. from tests.st.networks.models.bert.src.bert_model import BertModel, BertConfig
  24. bert_net_cfg = BertConfig(
  25. batch_size=2,
  26. seq_length=32,
  27. vocab_size=21128,
  28. hidden_size=768,
  29. num_hidden_layers=12,
  30. num_attention_heads=12,
  31. intermediate_size=3072,
  32. hidden_act="gelu",
  33. hidden_dropout_prob=0.1,
  34. attention_probs_dropout_prob=0.1,
  35. max_position_embeddings=512,
  36. type_vocab_size=2,
  37. initializer_range=0.02,
  38. use_relative_positions=False,
  39. input_mask_from_dataset=True,
  40. token_type_ids_from_dataset=True,
  41. dtype=mstype.float32,
  42. compute_type=mstype.float16
  43. )
  44. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  45. random.seed(1)
  46. np.random.seed(1)
  47. de.config.set_seed(1)
  48. class AddNet(nn.Cell):
  49. def __init__(self):
  50. super(AddNet, self).__init__()
  51. self.add = P.TensorAdd()
  52. def construct(self, x_, y_):
  53. return self.add(x_, y_)
  54. def export_add_model():
  55. net = AddNet()
  56. x = np.ones(4).astype(np.float32)
  57. y = np.ones(4).astype(np.float32)
  58. export(net, Tensor(x), Tensor(y), file_name='add.pb', file_format='MINDIR')
  59. def export_bert_model():
  60. net = BertModel(bert_net_cfg, False)
  61. input_ids = np.random.randint(0, 1000, size=(2, 32), dtype=np.int32)
  62. segment_ids = np.zeros((2, 32), dtype=np.int32)
  63. input_mask = np.zeros((2, 32), dtype=np.int32)
  64. export(net, Tensor(input_ids), Tensor(segment_ids), Tensor(input_mask), file_name='bert.pb', file_format='MINDIR')
  65. if __name__ == '__main__':
  66. export_add_model()
  67. export_bert_model()