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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # This is the Python adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  2. #
  3. # Copyright 2020-2021 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, CSRTensor
  21. from mindspore.ops import functional as F, composite as C
  22. from mindspore.ops.composite import multitype_ops
  23. from mindspore._c_expression import security
  24. from . import standard_method as M
  25. from . import trope as T
  26. from .namespace import CellNamespace
  27. # namespace define
  28. functional_ns = CellNamespace('mindspore.ops.functional')
  29. composite_ns = CellNamespace('mindspore.ops.composite')
  30. trope_ns = CellNamespace('mindspore._extends.parse.trope')
  31. NO_IMPLEMENT = None # not implemented
  32. SYMBOL_UNDEFINE = 0xFF # Undefined var and function
  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. # 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.lshift: NO_IMPLEMENT,
  84. T.rshift: NO_IMPLEMENT,
  85. T.and_: multitype_ops.logical_and,
  86. T.or_: multitype_ops.logical_or,
  87. T.xor: NO_IMPLEMENT,
  88. T.pos: multitype_ops.uadd,
  89. T.neg: multitype_ops.negative,
  90. T.invert: F.logical_not,
  91. T.not_: multitype_ops.logical_not,
  92. T.eq: multitype_ops.equal,
  93. T.ne: multitype_ops.not_equal,
  94. T.lt: multitype_ops.less,
  95. T.gt: multitype_ops.greater,
  96. T.le: multitype_ops.less_equal,
  97. T.ge: multitype_ops.greater_equal,
  98. T.is_: F.is_,
  99. T.is_not: F.is_not,
  100. T.contains: multitype_ops.in_,
  101. T.not_contains: multitype_ops.not_in_,
  102. # system function
  103. T.len: M.ms_len,
  104. T.bool_: M.bool_,
  105. T.map: C.Map(),
  106. T.filter: M.filter_,
  107. T.partial: F.partial,
  108. T.zip: C.zip_operation,
  109. T.enumerate: M.enumerate_,
  110. T.isinstance: M.isinstance_,
  111. # custom define operation
  112. T.iter: M.ms_iter,
  113. T.next: M.ms_next,
  114. T.hasnext: M.hasnext,
  115. T.MakeTuple: F.make_tuple,
  116. T.make_dict: F.make_dict,
  117. T.make_list: F.make_list,
  118. T.make_slice: F.make_slice,
  119. T.range: F.make_range,
  120. T.while_cond: M.while_cond,
  121. # lib function
  122. math.floor: NO_IMPLEMENT,
  123. math.trunc: NO_IMPLEMENT,
  124. math.exp: NO_IMPLEMENT,
  125. math.log: F.scalar_log,
  126. math.sin: NO_IMPLEMENT,
  127. math.cos: NO_IMPLEMENT,
  128. math.tan: NO_IMPLEMENT,
  129. # user defined
  130. RowTensor: F.make_row_tensor,
  131. SparseTensor: F.make_sparse_tensor,
  132. CSRTensor: F.make_csr_tensor
  133. }
  134. if not security.enable_security():
  135. convert_object_map[T.print] = F.print_