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.

constant.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd.All Rights Reserved.
  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. """Constant definition."""
  16. from enum import Enum, unique
  17. SEPARATOR_IN_ONNX_OP = "::"
  18. SEPARATOR_IN_SCOPE = "/"
  19. SEPARATOR_BTW_NAME_AND_ID = "_"
  20. SEPARATOR_TITLE_AND_CONTENT_IN_CONSTRUCT = "="
  21. LINK_IN_SCOPE = "-"
  22. LEFT_BUCKET = "["
  23. RIGHT_BUCKET = "]"
  24. BLANK_SYM = " "
  25. FIRST_LEVEL_INDENT = BLANK_SYM * 4
  26. SECOND_LEVEL_INDENT = BLANK_SYM * 8
  27. NEW_LINE = "\n"
  28. ONNX_TYPE_INT = 2
  29. ONNX_TYPE_INTS = 7
  30. ONNX_TYPE_FLOAT = 1
  31. ONNX_TYPE_FLOATS = 6
  32. ONNX_TYPE_STRING = 3
  33. DYNAMIC_SHAPE = -1
  34. SCALAR_WITHOUT_SHAPE = 0
  35. UNKNOWN_DIM_VAL = "unk__001"
  36. ONNX_MIN_VER = "1.8.0"
  37. TF2ONNX_MIN_VER = "1.7.1"
  38. ONNXRUNTIME_MIN_VER = "1.5.2"
  39. @unique
  40. class TemplateKeywords(Enum):
  41. """Define keywords in template message."""
  42. INIT = "init"
  43. CONSTRUCT = "construct"
  44. @unique
  45. class ExchangeMessageKeywords(Enum):
  46. """Define keywords in exchange message."""
  47. METADATA = "metadata"
  48. @unique
  49. class MetadataScope(Enum):
  50. """Define metadata scope keywords in exchange message."""
  51. SOURCE = "source"
  52. OPERATION = "operation"
  53. INPUTS = "inputs"
  54. INPUTS_SHAPE = "inputs_shape"
  55. OUTPUTS = "outputs"
  56. OUTPUTS_SHAPE = "outputs_shape"
  57. PRECURSOR = "precursor_nodes"
  58. SUCCESSOR = "successor_nodes"
  59. ATTRS = "attributes"
  60. SCOPE = "scope"
  61. @unique
  62. class VariableScope(Enum):
  63. """Define variable scope keywords in exchange message."""
  64. OPERATION = "operation"
  65. VARIABLE_NAME = "variable_name"
  66. OUTPUT_TYPE = "output_type"
  67. TSR_TYPE = "tensor"
  68. ARR_TYPE = "array"
  69. INPUTS = "inputs"
  70. ARGS = "args"
  71. WEIGHTS = "weights"
  72. TRAINABLE_PARAMS = "trainable_params"
  73. BINARY_HEADER_PYTORCH_FILE = \
  74. b'\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03.\x80\x02}q\x00(X\x10\x00\x00\x00'
  75. TENSORFLOW_MODEL_SUFFIX = "pb"
  76. BINARY_HEADER_PYTORCH_BITS = 32
  77. ARGUMENT_LENGTH_LIMIT = 512
  78. EXPECTED_NUMBER = 1
  79. MIN_SCOPE_LENGTH = 2
  80. ONNX_OPSET_VERSION = 11
  81. MODEL_INPUT_NAME = 'input.1'
  82. NO_CONVERTED_OPERATORS = [
  83. "onnx::Constant",
  84. "Constant"
  85. ]
  86. @unique
  87. class NodeType(Enum):
  88. MODULE = "module"
  89. OPERATION = "operation"
  90. CLASS = "class"
  91. FUNC = "func"
  92. INPUTS = "DataInput"
  93. @unique
  94. class InputType(Enum):
  95. TENSOR = "tensor"
  96. LIST = "list"
  97. @unique
  98. class FrameworkType(Enum):
  99. PYTORCH = 0
  100. TENSORFLOW = 1
  101. UNKNOWN = 2
  102. def get_imported_module():
  103. """
  104. Generate imported module header.
  105. Returns:
  106. str, imported module.
  107. """
  108. return f"import numpy as np{NEW_LINE}" \
  109. f"import mindspore{NEW_LINE}" \
  110. f"from mindspore import nn{NEW_LINE}" \
  111. f"from mindspore import Tensor{NEW_LINE}" \
  112. f"from mindspore.ops import operations as P{NEW_LINE * 3}"