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/03/25 09:26:32 UTC

[GitHub] [incubator-tvm] manupa-arm commented on a change in pull request #5030: [RELAY] Added a AnnotatedRegion utility class

manupa-arm commented on a change in pull request #5030: [RELAY] Added a AnnotatedRegion utility class
URL: https://github.com/apache/incubator-tvm/pull/5030#discussion_r397711380
 
 

 ##########
 File path: tests/python/relay/test_annotated_regions.py
 ##########
 @@ -0,0 +1,121 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=no-else-return, unidiomatic-typecheck, invalid-name
+from tvm import relay
+from tvm.relay.op.annotation import compiler_begin, compiler_end
+
+
+def check_region(region_set, args, nodes, rets):
+    region = region_set.get_region(args[0])
+    assert region
+    assert set(args) == set(region.args)
+    assert set(nodes) == set(region.nodes)
+    assert set(rets) == set(region.rets)
+
+
+def test_region_set_creator_diamond():
+    data = relay.var('data', shape=(10, 10))
+    cb_1 = compiler_begin(data, 'test_target')
+    O_1 = relay.abs(cb_1)
+    ce_1 = compiler_end(O_1, 'test_target')
+    ce_2 = compiler_end(O_1, 'test_target')
+    cb_2 = compiler_begin(ce_1, 'test_target')
+    O_2 = relay.nn.relu(cb_2)
+    ce_3 = compiler_end(O_2, 'test_target')
+    cb_d = compiler_begin(ce_2, "default")
+    X = relay.tanh(cb_d)
+    ce_d = compiler_end(X, 'default')
+    cb_3 = compiler_begin(ce_3, 'test_target')
+    cb_4 = compiler_begin(ce_d, 'test_target')
+    O_3 = relay.add(cb_3, cb_4)
+    ce_4 = compiler_end(O_3, 'test_target')
+    diamond = relay.Function([data], ce_4)
+
+    region_set = relay.analysis.AnnotatedRegionSet(diamond,
+                                                   relay.op.get("annotation.compiler_begin"),
+                                                   relay.op.get("annotation.compiler_end"))
+    assert len(region_set) == 4
+    check_region(
+        region_set,
+        [cb_1],
+        [cb_1, O_1, ce_1, ce_2],
+        [ce_1, ce_2],
+    )
+    check_region(
+        region_set,
+        [cb_2],
+        [cb_2, O_2, ce_3],
+        [ce_3],
+    )
+    check_region(
+        region_set,
+        [cb_d],
+        [cb_d, X, ce_d],
+        [ce_d],
+    )
+    check_region(
+        region_set,
+        [cb_3, cb_4],
+        [cb_3, cb_4, O_3, ce_4],
+        [ce_4],
+    )
+
+
+def test_region_set_creator_merged():
+    data = relay.var('data', shape=(10, 10))
+    cb_1 = compiler_begin(data, 'test_target')
+    O_1 = relay.abs(cb_1)
+    ce_2 = compiler_end(O_1, 'test_target')
+    O_2 = relay.nn.relu(O_1)
+    ce_3 = compiler_end(O_2, 'test_target')
+    cb_d = compiler_begin(ce_2, "default")
+    X = relay.tanh(cb_d)
+    ce_d = compiler_end(X, 'default')
+    cb_3 = compiler_begin(ce_3, 'test_target')
+    cb_4 = compiler_begin(ce_d, 'test_target')
+    O_3 = relay.add(cb_3, cb_4)
+    ce_4 = compiler_end(O_3, 'test_target')
+    merged = relay.Function([data], ce_4)
+
+    region_set = relay.analysis.AnnotatedRegionSet(merged,
 
 Review comment:
   @jwfromm To clarify further,
   
   I think pass provides a mechanism to obtain annotated region (as a data structure --  a collection of nodes with ins and outs) that are bound (i.e. all the incoming and outgoing edges, to the region from the rest of the graph, are bound ) by annotation ops.
   
   One important use case of it is to highlight regions that are off-loaded to different backend compilers. In which case,  all the ins and outs (which are annotation ops) should have the 'compiler" attribute as @mbaret  mentions in the previous comment.
   
   However, it is not limited to backend compiler partitioning. I see this being useful in future region specific optimizations that a user might want to do without needing to pass additional objects (C++/Python); just through passing the IR modules with annotations. I am also using this in my recent [PR](https://github.com/apache/incubator-tvm/pull/5143).

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


With regards,
Apache Git Services