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.

cli.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. """Command module."""
  16. import argparse
  17. import os
  18. import sys
  19. import mindinsight
  20. from mindinsight.optimizer.tuner import Tuner
  21. class ConfigAction(argparse.Action):
  22. """Summary base dir action class definition."""
  23. def __call__(self, parser_in, namespace, values, option_string=None):
  24. """
  25. Inherited __call__ method from argparse.Action.
  26. Args:
  27. parser_in (ArgumentParser): Passed-in argument parser.
  28. namespace (Namespace): Namespace object to hold arguments.
  29. values (object): Argument values with type depending on argument definition.
  30. option_string (str): Option string for specific argument name.
  31. """
  32. config_path = os.path.realpath(values)
  33. if not os.path.exists(config_path):
  34. parser_in.error(f'{option_string} {config_path} not exists.')
  35. setattr(namespace, self.dest, config_path)
  36. class IterAction(argparse.Action):
  37. """Summary base dir action class definition."""
  38. def __call__(self, parser_in, namespace, values, option_string=None):
  39. """
  40. Inherited __call__ method from argparse.Action.
  41. Args:
  42. parser_in (ArgumentParser): Passed-in argument parser.
  43. namespace (Namespace): Namespace object to hold arguments.
  44. values (object): Argument values with type depending on argument definition.
  45. option_string (str): Option string for specific argument name.
  46. """
  47. iter_times = values
  48. if iter_times <= 0:
  49. parser_in.error(f'{option_string} {iter_times} should be a positive integer.')
  50. setattr(namespace, self.dest, iter_times)
  51. parser = argparse.ArgumentParser(
  52. prog='mindoptimizer',
  53. description='MindOptimizer CLI entry point (version: {})'.format(mindinsight.__version__))
  54. parser.add_argument(
  55. '--version',
  56. action='version',
  57. version='%(prog)s ({})'.format(mindinsight.__version__))
  58. parser.add_argument(
  59. '--config',
  60. type=str,
  61. action=ConfigAction,
  62. required=True,
  63. default=os.path.join(os.getcwd(), 'output'),
  64. help="Specify path for config file."
  65. )
  66. parser.add_argument(
  67. '--iter',
  68. type=int,
  69. action=IterAction,
  70. default=1,
  71. help="Optional, specify run times for the command in config file."
  72. )
  73. def cli_entry():
  74. """Cli entry."""
  75. argv = sys.argv[1:]
  76. if not argv:
  77. argv = ['-h']
  78. args = parser.parse_args(argv)
  79. else:
  80. args = parser.parse_args()
  81. tuner = Tuner(args.config)
  82. tuner.optimize(max_expr_times=args.iter)