You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/11/03 14:04:16 UTC

[GitHub] [incubator-tvm] areusch commented on a change in pull request #6779: [WIP] Object Schema

areusch commented on a change in pull request #6779:
URL: https://github.com/apache/incubator-tvm/pull/6779#discussion_r516259261



##########
File path: schema/python/schema/parser.py
##########
@@ -0,0 +1,123 @@
+from .registry import register, lookup
+from .ir import ObjectDef, ObjectRefDef, FieldDef
+from .codegen import generate
+from . import typing as ty
+
+import re
+import ast
+import inspect
+
+
+# root class
+class Object:
+    pass
+
+class ObjectRef:
+    pass
+
+_ObjectBaseDef = ObjectDef("Object", "ObjectRef", base=None)
+register(_ObjectBaseDef)
+
+_ObjectRefBaseDef = ObjectRefDef("ObjectRef", base=None,
+    internal=_ObjectBaseDef)
+register(_ObjectRefBaseDef)
+
+
+class Parser(ast.NodeVisitor):
+    def __init__(self):
+        self.type_key = None
+        self.default_visit_attrs = True
+        self.default_sequal_reduce = True
+        self.default_shash_reduce = True
+        self.fields = []
+
+    def visit_Assign(self, node):
+        target = node.targets[0]
+        value = node.value
+        if target.id == 'type_key':
+            self.type_key = value.s
+        elif target.id == 'default_visit_attrs':
+            self.default_visit_attrs = value.value
+        elif target.id == 'default_sequal_reduce':
+            self.default_sequal_reduce = value.value
+        elif target.id == 'default_shash_reduce':
+            self.default_shash_reduce = value.value
+        self.generic_visit(node)
+
+    def visit_AnnAssign(self, node):
+        name = node.target.id
+        type_name = node.annotation.attr
+        type_ = ty.get_type(type_name)
+        self.fields.append(FieldDef(name, type_))
+        self.generic_visit(node)
+
+
+def parse_comment(text):
+    ret = {}
+    lines = text.split('\n')
+    block_delimiter = [0]
+    for lnum, line in enumerate(lines):
+        result = re.match(r"\s*-+", line)
+        if result:
+            block_delimiter.append(lnum - 1)
+    block_delimiter.append(len(lines))
+    ret['Root'] = lines[block_delimiter[0]: block_delimiter[1]]
+    for idx in range(1, len(block_delimiter) - 1):
+        key = lines[block_delimiter[idx]].strip()
+        ret[key] = lines[block_delimiter[idx]+2: block_delimiter[idx+1]]
+
+    for key in ret:
+        lines = [line.strip() for line in ret[key]]
+        # remove space lines at beginning and the end
+        while lines and not lines[0]:
+            lines.pop(0)
+        while lines and not lines[-1]:
+            lines.pop()
+        ret[key] = lines
+
+    return ret
+
+from json import JSONEncoder
+
+
+def declare(cls):
+    print('\n\n')

Review comment:
       is the plan to delete these prints because they are debug, or ?

##########
File path: schema/python/schema/parser.py
##########
@@ -0,0 +1,123 @@
+from .registry import register, lookup
+from .ir import ObjectDef, ObjectRefDef, FieldDef
+from .codegen import generate
+from . import typing as ty
+
+import re
+import ast
+import inspect
+
+
+# root class
+class Object:
+    pass
+
+class ObjectRef:
+    pass
+
+_ObjectBaseDef = ObjectDef("Object", "ObjectRef", base=None)
+register(_ObjectBaseDef)
+
+_ObjectRefBaseDef = ObjectRefDef("ObjectRef", base=None,
+    internal=_ObjectBaseDef)
+register(_ObjectRefBaseDef)
+
+
+class Parser(ast.NodeVisitor):

Review comment:
       it does seem like Python AST leaves open multiple ways to declare class members. we'll need to continue extending this parser as they are added. any reason to keep the definitions in Python? e.g. here is a perfectly legal way to add some annotations:
   
   ```
   class PrimExprNode(BaseExprNode):
     type_key = 'PrimExprNode'
   
   for field in dir(RelayExprNode):
     setattr(PrimExprNode, field, ty.int64_t)
   ```
   
   it'll be hard to catch uses of this without using AST, but it'll also be confusing to debug from a user perspective.

##########
File path: schema/python/schema/parser.py
##########
@@ -0,0 +1,123 @@
+from .registry import register, lookup
+from .ir import ObjectDef, ObjectRefDef, FieldDef
+from .codegen import generate
+from . import typing as ty
+
+import re
+import ast
+import inspect
+
+
+# root class
+class Object:
+    pass
+
+class ObjectRef:
+    pass
+
+_ObjectBaseDef = ObjectDef("Object", "ObjectRef", base=None)
+register(_ObjectBaseDef)
+
+_ObjectRefBaseDef = ObjectRefDef("ObjectRef", base=None,
+    internal=_ObjectBaseDef)
+register(_ObjectRefBaseDef)
+
+
+class Parser(ast.NodeVisitor):
+    def __init__(self):
+        self.type_key = None
+        self.default_visit_attrs = True
+        self.default_sequal_reduce = True
+        self.default_shash_reduce = True
+        self.fields = []
+
+    def visit_Assign(self, node):
+        target = node.targets[0]
+        value = node.value
+        if target.id == 'type_key':
+            self.type_key = value.s
+        elif target.id == 'default_visit_attrs':
+            self.default_visit_attrs = value.value
+        elif target.id == 'default_sequal_reduce':
+            self.default_sequal_reduce = value.value
+        elif target.id == 'default_shash_reduce':
+            self.default_shash_reduce = value.value
+        self.generic_visit(node)
+
+    def visit_AnnAssign(self, node):
+        name = node.target.id
+        type_name = node.annotation.attr
+        type_ = ty.get_type(type_name)
+        self.fields.append(FieldDef(name, type_))
+        self.generic_visit(node)
+
+
+def parse_comment(text):
+    ret = {}
+    lines = text.split('\n')
+    block_delimiter = [0]
+    for lnum, line in enumerate(lines):
+        result = re.match(r"\s*-+", line)
+        if result:
+            block_delimiter.append(lnum - 1)
+    block_delimiter.append(len(lines))
+    ret['Root'] = lines[block_delimiter[0]: block_delimiter[1]]
+    for idx in range(1, len(block_delimiter) - 1):
+        key = lines[block_delimiter[idx]].strip()
+        ret[key] = lines[block_delimiter[idx]+2: block_delimiter[idx+1]]
+
+    for key in ret:
+        lines = [line.strip() for line in ret[key]]
+        # remove space lines at beginning and the end
+        while lines and not lines[0]:
+            lines.pop(0)
+        while lines and not lines[-1]:
+            lines.pop()
+        ret[key] = lines
+
+    return ret
+
+from json import JSONEncoder

Review comment:
       move to the top of the file




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org