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 5.2 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 import RowTensor, SparseTensor
  21. from mindspore.ops.composite import multitype_ops
  22. from mindspore.ops import functional as F, composite as C
  23. from . import standard_method as M
  24. from . import trope as T
  25. from .namespace import CellNamespace
  26. # namespace define
  27. functional_ns = CellNamespace('mindspore.ops.functional')
  28. composite_ns = CellNamespace('mindspore.ops.composite')
  29. trope_ns = CellNamespace('mindspore._extends.parse.trope')
  30. NO_IMPLEMENT = None # not implemented
  31. SYMBOL_UNDEFINE = 0xFF # Undefined var and function
  32. # ops map: {op.type:(Namespace, symbol)}
  33. # Some space set aside for readability of code
  34. parse_object_map = {
  35. # ast grammar
  36. ast.Add: (trope_ns, 'add'),
  37. ast.Sub: (trope_ns, 'sub'),
  38. ast.Mult: (trope_ns, 'mul'),
  39. ast.Div: (trope_ns, 'truediv'),
  40. ast.FloorDiv: (trope_ns, 'floordiv'),
  41. ast.Mod: (trope_ns, 'mod'),
  42. ast.Pow: (trope_ns, 'pow'),
  43. ast.MatMult: (trope_ns, 'matmul'),
  44. ast.LShift: (trope_ns, 'lshift'),
  45. ast.RShift: (trope_ns, 'rshift'),
  46. ast.And: (trope_ns, 'and_'),
  47. ast.Or: (trope_ns, 'or_'),
  48. ast.BitXor: (trope_ns, 'xor'),
  49. ast.UAdd: (trope_ns, 'pos'),
  50. ast.USub: (trope_ns, 'neg'),
  51. ast.Invert: (trope_ns, 'invert'),
  52. ast.Not: (trope_ns, 'not_'),
  53. ast.Eq: (trope_ns, 'eq'),
  54. ast.NotEq: (trope_ns, 'ne'),
  55. ast.Lt: (trope_ns, 'lt'),
  56. ast.Gt: (trope_ns, 'gt'),
  57. ast.LtE: (trope_ns, 'le'),
  58. ast.GtE: (trope_ns, 'ge'),
  59. ast.Is: (trope_ns, 'is_'),
  60. ast.IsNot: (trope_ns, 'is_not'),
  61. ast.In: (trope_ns, 'contains'),
  62. ast.NotIn: (trope_ns, 'not_contains'),
  63. # operation symbol type
  64. 'getitem': (composite_ns, 'getitem'),
  65. 'ms_iter': (composite_ns, 'ms_iter'),
  66. 'ms_next': (composite_ns, 'ms_next'),
  67. 'hasnext': (composite_ns, 'hasnext'),
  68. # undefined type
  69. SYMBOL_UNDEFINE: (None, 'undefine'),
  70. }
  71. # convert map: {obj:(Namespace, symbol)}
  72. # Escape an object to another object, eg: system function(len,xxx)
  73. # Some space set aside for readability of code
  74. convert_object_map = {
  75. T.add: multitype_ops.add,
  76. T.sub: multitype_ops.sub,
  77. T.mul: multitype_ops.mul,
  78. T.truediv: multitype_ops.div,
  79. T.getitem: multitype_ops.getitem,
  80. T.setitem: multitype_ops.setitem,
  81. T.floordiv: multitype_ops.floordiv,
  82. T.mod: multitype_ops.mod,
  83. T.pow: multitype_ops.pow_,
  84. T.matmul: F.dot,
  85. T.lshift: NO_IMPLEMENT,
  86. T.rshift: NO_IMPLEMENT,
  87. T.and_: multitype_ops.logical_and,
  88. T.or_: multitype_ops.logical_or,
  89. T.xor: NO_IMPLEMENT,
  90. T.pos: multitype_ops.uadd,
  91. T.neg: multitype_ops.negative,
  92. T.invert: NO_IMPLEMENT,
  93. T.not_: multitype_ops.logical_not,
  94. T.eq: multitype_ops.equal,
  95. T.ne: multitype_ops.not_equal,
  96. T.lt: multitype_ops.less,
  97. T.gt: multitype_ops.greater,
  98. T.le: multitype_ops.less_equal,
  99. T.ge: multitype_ops.greater_equal,
  100. T.is_: F.is_,
  101. T.is_not: F.is_not,
  102. T.contains: multitype_ops.in_,
  103. T.not_contains: F.not_in_dict,
  104. # system function
  105. T.len: M.ms_len,
  106. T.bool: M.bool_,
  107. T.map: C.Map(),
  108. T.partial: F.partial,
  109. T.zip: C.zip_operation,
  110. T.print: F.print_,
  111. T.enumerate: M.enumerate_,
  112. T.isinstance: M.isinstance_,
  113. # custom define operation
  114. T.iter: M.ms_iter,
  115. T.next: M.ms_next,
  116. T.hasnext: M.hasnext,
  117. T.make_tuple: F.make_tuple,
  118. T.make_dict: F.make_dict,
  119. T.make_list: F.make_list,
  120. T.make_slice: F.make_slice,
  121. T.range: F.make_range,
  122. T.while_cond: M.while_cond,
  123. # lib function
  124. math.floor: NO_IMPLEMENT,
  125. math.trunc: NO_IMPLEMENT,
  126. math.exp: NO_IMPLEMENT,
  127. math.log: F.scalar_log,
  128. math.sin: NO_IMPLEMENT,
  129. math.cos: NO_IMPLEMENT,
  130. math.tan: NO_IMPLEMENT,
  131. # user defined
  132. RowTensor: F.make_row_tensor,
  133. SparseTensor: F.make_sparse_tensor,
  134. }