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.

resources.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # This is the Python adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  2. #
  3. # Copyright 2020 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # ============================================================================
  17. """Resources for ast tree parse."""
  18. import ast
  19. import math
  20. from mindspore.ops.composite import multitype_ops
  21. from mindspore.ops import functional as F, composite as C
  22. from . import standard_method as M
  23. from . import trope as T
  24. from .namespace import CellNamespace
  25. # namespace define
  26. functional_ns = CellNamespace('mindspore.ops.functional')
  27. composite_ns = CellNamespace('mindspore.ops.composite')
  28. trope_ns = CellNamespace('mindspore._extends.parse.trope')
  29. NO_IMPLEMENT = None # not implemented
  30. SYMBOL_UNDEFINE = 0xFF # Undefined var and function
  31. # ops map: {op.type:(Namespace, symbol)}
  32. # Some space set aside for readability of code
  33. parse_object_map = {
  34. # ast grammar
  35. ast.Add: (trope_ns, 'add'),
  36. ast.Sub: (trope_ns, 'sub'),
  37. ast.Mult: (trope_ns, 'mul'),
  38. ast.Div: (trope_ns, 'truediv'),
  39. ast.FloorDiv: (trope_ns, 'floordiv'),
  40. ast.Mod: (trope_ns, 'mod'),
  41. ast.Pow: (trope_ns, 'pow'),
  42. ast.MatMult: (trope_ns, 'matmul'),
  43. ast.LShift: (trope_ns, 'lshift'),
  44. ast.RShift: (trope_ns, 'rshift'),
  45. ast.And: (trope_ns, 'and_'),
  46. ast.Or: (trope_ns, 'or_'),
  47. ast.BitXor: (trope_ns, 'xor'),
  48. ast.UAdd: (trope_ns, 'pos'),
  49. ast.USub: (trope_ns, 'neg'),
  50. ast.Invert: (trope_ns, 'invert'),
  51. ast.Not: (trope_ns, 'not_'),
  52. ast.Eq: (trope_ns, 'eq'),
  53. ast.NotEq: (trope_ns, 'ne'),
  54. ast.Lt: (trope_ns, 'lt'),
  55. ast.Gt: (trope_ns, 'gt'),
  56. ast.LtE: (trope_ns, 'le'),
  57. ast.GtE: (trope_ns, 'ge'),
  58. ast.Is: (trope_ns, 'is_'),
  59. ast.IsNot: (trope_ns, 'is_not'),
  60. ast.In: (trope_ns, 'contains'),
  61. ast.NotIn: (trope_ns, 'not_contains'),
  62. # operation symbol type
  63. 'getitem': (composite_ns, 'getitem'),
  64. 'ms_iter': (composite_ns, 'ms_iter'),
  65. 'ms_next': (composite_ns, 'ms_next'),
  66. 'hasnext': (composite_ns, 'hasnext'),
  67. # undefined type
  68. SYMBOL_UNDEFINE: (None, 'undefine'),
  69. }
  70. # convert map: {obj:(Namespace, symbol)}
  71. # Escape an object to another object, eg: system function(len,xxx)
  72. # Some space set aside for readability of code
  73. convert_object_map = {
  74. T.add: multitype_ops.add,
  75. T.sub: multitype_ops.sub,
  76. T.mul: multitype_ops.mul,
  77. T.truediv: multitype_ops.div,
  78. T.getitem: multitype_ops.getitem,
  79. T.setitem: multitype_ops.setitem,
  80. T.floordiv: multitype_ops.floordiv,
  81. T.mod: multitype_ops.mod,
  82. T.pow: multitype_ops.pow_,
  83. T.matmul: F.dot,
  84. T.lshift: NO_IMPLEMENT,
  85. T.rshift: NO_IMPLEMENT,
  86. T.and_: multitype_ops.logical_and,
  87. T.or_: multitype_ops.logical_or,
  88. T.xor: NO_IMPLEMENT,
  89. T.pos: multitype_ops.uadd,
  90. T.neg: multitype_ops.negative,
  91. T.invert: NO_IMPLEMENT,
  92. T.not_: multitype_ops.logical_not,
  93. T.eq: multitype_ops.equal,
  94. T.ne: multitype_ops.not_equal,
  95. T.lt: multitype_ops.less,
  96. T.gt: multitype_ops.greater,
  97. T.le: multitype_ops.less_equal,
  98. T.ge: multitype_ops.greater_equal,
  99. T.is_: F.is_,
  100. T.is_not: F.is_not,
  101. T.contains: F.in_dict,
  102. T.not_contains: F.not_in_dict,
  103. # system function
  104. T.len: M.ms_len,
  105. T.bool: M.bool_,
  106. T.map: C.HyperMap(),
  107. T.partial: F.partial,
  108. T.zip: C.zip_operation,
  109. T.print: F.print_,
  110. # custom define operation
  111. T.iter: M.ms_iter,
  112. T.next: M.ms_next,
  113. T.hasnext: M.hasnext,
  114. T.make_tuple: F.make_tuple,
  115. T.make_dict: F.make_dict,
  116. T.make_list: F.make_list,
  117. T.make_slice: F.make_slice,
  118. T.range: F.make_range,
  119. # lib function
  120. math.floor: NO_IMPLEMENT,
  121. math.trunc: NO_IMPLEMENT,
  122. math.exp: NO_IMPLEMENT,
  123. math.log: F.scalar_log,
  124. math.sin: NO_IMPLEMENT,
  125. math.cos: NO_IMPLEMENT,
  126. math.tan: NO_IMPLEMENT,
  127. }