You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

48 lines
2.4 KiB

  1. # Copyright 2021 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. """export script."""
  16. import argparse
  17. import numpy as np
  18. from mindspore import context
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net, export
  21. from src.seq2seq import Seq2Seq
  22. from src.gru_for_infer import GRUInferCell
  23. from src.config import config
  24. parser = argparse.ArgumentParser(description='export')
  25. parser.add_argument("--device_target", type=str, default="Ascend",
  26. help="device where the code will be implemented, default is Ascend")
  27. parser.add_argument('--device_id', type=int, default=0, help='device id of GPU or Ascend, default is 0')
  28. parser.add_argument('--file_name', type=str, default="gru", help='output file name.')
  29. parser.add_argument("--file_format", type=str, choices=["AIR", "MINDIR"], default="MINDIR", help="file format.")
  30. parser.add_argument('--ckpt_file', type=str, required=True, help='ckpt file path')
  31. args = parser.parse_args()
  32. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, reserve_class_name_in_scope=False, \
  33. device_id=args.device_id, save_graphs=False)
  34. if __name__ == "__main__":
  35. network = Seq2Seq(config, is_training=False)
  36. network = GRUInferCell(network)
  37. network.set_train(False)
  38. if args.ckpt_file != "":
  39. parameter_dict = load_checkpoint(args.ckpt_file)
  40. load_param_into_net(network, parameter_dict)
  41. source_ids = Tensor(np.random.uniform(0.0, 1e5, size=[config.eval_batch_size, config.max_length]).astype(np.int32))
  42. target_ids = Tensor(np.random.uniform(0.0, 1e5, size=[config.eval_batch_size, config.max_length]).astype(np.int32))
  43. export(network, source_ids, target_ids, file_name=args.file_name, file_format=args.file_format)