[GitHub] [tvm] zackcquic commented on a change in pull request #8220: [DOCS] Add docs for Pass Instrument

2021-06-14 Thread GitBox


zackcquic commented on a change in pull request #8220:
URL: https://github.com/apache/tvm/pull/8220#discussion_r651393990



##
File path: docs/dev/pass_infra.rst
##
@@ -526,16 +663,93 @@ decorators and then invoke it. For more examples about 
how to customize your own
 optimization pipeline and debug Relay and tir passes, please refer to the
 `use pass infra`_ tutorial.
 
+
+.. _pass_instrument_py_frontend:
+
+Pass Instrument
+^^^
+
+A customizable framework to instrument passes is provided. ``PassInstrument`` 
classes can be registered while constructing ``PassContext``.
+
+.. code:: python
+
+@tvm._ffi.register_object("transform.PassContext")
+class PassContext(tvm.runtime.Object):
+def __init__(
+self,
+opt_level=2,
+required_pass=None,
+disabled_pass=None,
+instruments=None,
+config=None,
+):
+# ...
+
+One can implement a ``PassInstrument`` by using the ``pass_instrument`` 
decorator(`python/tvm/ir/instrument.py`_) on a class implementing following 
methods:

Review comment:
   Nit: I think it maybe better to emphasize use decorator instead of 
subclassing. 

##
File path: docs/dev/pass_infra.rst
##
@@ -526,16 +663,93 @@ decorators and then invoke it. For more examples about 
how to customize your own
 optimization pipeline and debug Relay and tir passes, please refer to the
 `use pass infra`_ tutorial.
 
+
+.. _pass_instrument_py_frontend:
+
+Pass Instrument
+^^^
+
+A customizable framework to instrument passes is provided. ``PassInstrument`` 
classes can be registered while constructing ``PassContext``.
+
+.. code:: python
+
+@tvm._ffi.register_object("transform.PassContext")
+class PassContext(tvm.runtime.Object):
+def __init__(
+self,
+opt_level=2,
+required_pass=None,
+disabled_pass=None,
+instruments=None,
+config=None,
+):
+# ...
+
+One can implement a ``PassInstrument`` by using the ``pass_instrument`` 
decorator(`python/tvm/ir/instrument.py`_) on a class implementing following 
methods:

Review comment:
   Nit: Maybe it should be emphasized to use decorator, instead of 
overriding/subclassing.

##
File path: docs/dev/pass_infra.rst
##
@@ -389,6 +396,136 @@ To allow other C++ modules to apply this pass, we declare 
a free function in
 
 TVM_DLL Pass FoldConstant();
 
+.. _pass_instrument_cpp_backend:
+
+Pass Instrument
+^^^
+
+Currently we introduce four instrument point in the life-cycle of 
``PassContext``.

Review comment:
   instrument points




-- 
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:
us...@infra.apache.org




[GitHub] [tvm] zackcquic commented on a change in pull request #8220: [DOCS] Add docs for Pass Instrument

2021-06-14 Thread GitBox


zackcquic commented on a change in pull request #8220:
URL: https://github.com/apache/tvm/pull/8220#discussion_r650028213



##
File path: docs/dev/pass_infra.rst
##
@@ -389,6 +396,103 @@ To allow other C++ modules to apply this pass, we declare 
a free function in
 
 TVM_DLL Pass FoldConstant();
 
+.. _pass_instrument_section_tag:
+
+Pass Instrument
+~~~
+
+``PassInstrument`` provides callbacks run when entering/exiting 
``PassContext`` and before/after executing passes.
+Multiple ``PassInstrument`` instances can be registed into a single 
``PassContext``.
+Instrument instances are called sequentially in the order of ``instruments`` 
argument passed to ``PassContext``.
+
+.. code:: c++
+
+namespace instrument {
+
+class PassInstrumentNode : public Object {
+ public:
+  String name;
+  virtual void EnterPassContext() const = 0;
+  virtual void ExitPassContext() const = 0;
+  virtual bool ShouldRun(const IRModule& mod, const transform::PassInfo& 
info) const = 0;
+  virtual void RunBeforePass(const IRModule& mod, const 
transform::PassInfo& info) const = 0;
+  virtual void RunAfterPass(const IRModule& mod, const 
transform::PassInfo& info) const = 0;
+  /* Other fields are omitted. */
+};
+
+class PassInstrument : public ObjectRef {
+ public:
+  TVM_DEFINE_OBJECT_REF_METHODS(PassInstrument, ObjectRef, 
PassInstrumentNode);
+};
+
+}  // namespace instrument
+
+Python interfaces are provided to implement ``PassInstrument`` quickly.
+
+Following four methods are invoked in the life-cycle of ``PassContext``.

Review comment:
   Currently we introduces four instrument point in the life-cycle of 
``PassContext``. (introduce in python frontend maybe better)
   Include the text graph in 
https://github.com/apache/tvm/blob/d69011dea6a09960b38d36f679888a6e29c24240/include/tvm/ir/instrument.h#L52
 maybe helpful to understand quickly (move this to python frontend, too?) 

##
File path: docs/dev/pass_infra.rst
##
@@ -389,6 +396,103 @@ To allow other C++ modules to apply this pass, we declare 
a free function in
 
 TVM_DLL Pass FoldConstant();
 
+.. _pass_instrument_section_tag:
+
+Pass Instrument
+~~~
+
+``PassInstrument`` provides callbacks run when entering/exiting 
``PassContext`` and before/after executing passes.
+Multiple ``PassInstrument`` instances can be registed into a single 
``PassContext``.
+Instrument instances are called sequentially in the order of ``instruments`` 
argument passed to ``PassContext``.
+
+.. code:: c++
+
+namespace instrument {
+
+class PassInstrumentNode : public Object {
+ public:
+  String name;
+  virtual void EnterPassContext() const = 0;
+  virtual void ExitPassContext() const = 0;
+  virtual bool ShouldRun(const IRModule& mod, const transform::PassInfo& 
info) const = 0;
+  virtual void RunBeforePass(const IRModule& mod, const 
transform::PassInfo& info) const = 0;
+  virtual void RunAfterPass(const IRModule& mod, const 
transform::PassInfo& info) const = 0;
+  /* Other fields are omitted. */
+};
+
+class PassInstrument : public ObjectRef {
+ public:
+  TVM_DEFINE_OBJECT_REF_METHODS(PassInstrument, ObjectRef, 
PassInstrumentNode);
+};
+
+}  // namespace instrument
+
+Python interfaces are provided to implement ``PassInstrument`` quickly.
+
+Following four methods are invoked in the life-cycle of ``PassContext``.
+
+.. code:: c++
+
+TVM_DLL void InstrumentEnterPassContext();
+TVM_DLL void InstrumentExitPassContext();
+TVM_DLL bool InstrumentBeforePass(const IRModule& mod, const PassInfo& 
info) const;
+TVM_DLL void InstrumentAfterPass(const IRModule& mod, const PassInfo& 
info) const;
+
+``InstrumentEnterPassContext`` is called immediately when the scope
+of the ``PassContext`` instance is entered.
+
+``InstrumentExitPassContext`` is called when the scope of ``PassContextNode``
+is being leaved, or exceptions occur during the execution of passes.
+This method is also called when instruments is being overriden by 
``override_instruments`` in ::py:class:`tvm.transform.PassContext`.
+
+``InstrumentBeforePass`` is called before pass-execution.
+``InstrumentAfterPass`` is called after pass-executioon if the pass should be 
run. The behavir is like:
+
+.. code:: c++
+
+  if (pass_ctx.InstrumentBeforePass(ir_module, pass_info)) {
+new_ir_module = run_pass(ir_module, pass_ctx);
+pass_ctx.InstrumentAfterPass(new_ir_module, pass_info);
+return new_ir_module;
+  }
+
+Here is a brief introduction of each methods. See (`src/ir/transform.cc`_) for 
more details.
+
+- ``InstrumentEnterPassContext``
+
+  * ``EnterPassContext()`` is executed in the order of ``instruments`` passed 
to the ``PassContext``.
+  * When an exception raises, ``PassContext`` disable the pass instrumentation
+by clearing all registered ``PassInstrument`` instances.
+  * Then 

[GitHub] [tvm] zackcquic commented on a change in pull request #8220: [DOCS] Add docs for Pass Instrument

2021-06-09 Thread GitBox


zackcquic commented on a change in pull request #8220:
URL: https://github.com/apache/tvm/pull/8220#discussion_r648118036



##
File path: docs/api/python/ir.rst
##
@@ -23,6 +23,14 @@ tvm.ir
:autosummary:
 
 
+tvm.ir.instrument

Review comment:
   Use tvm.instrument

##
File path: docs/dev/pass_infra.rst
##
@@ -526,6 +579,51 @@ decorators and then invoke it. For more examples about how 
to customize your own
 optimization pipeline and debug Relay and tir passes, please refer to the
 `use pass infra`_ tutorial.
 
+Pass Instrument
+^^^
+
+A customizable framework to instrument passes is provided. ``PassInstrument`` 
classes can be registered while constructing ``PassContext``.
+
+.. code:: python
+
+@tvm._ffi.register_object("transform.PassContext")
+class PassContext(tvm.runtime.Object):
+def __init__(
+self,
+opt_level=2,
+required_pass=None,
+disabled_pass=None,
+instruments=None,
+config=None,
+):
+# ...
+
+One can implement a ``PassInstrument`` by ``pass_instrument`` 
decorator(`python/tvm/ir/instrument.py`_) with a class implementing following 
methods:
+
+- ``enter_pass_ctx``
+
+  * This callback is run at the moement of entering ``PassContext``.
+
+- ``exit_pass_ctx``
+
+  * This callback is run at the moement of exiting ``PassContext``.
+
+- ``should_run``
+
+  * This callback is run before a pass is executed, returning a boolean 
indicating if the pass should be run.
+  * If a pass is listed as required, this callback will not be executed for 
that pass.

Review comment:
   This callback will not be executed for that pass. -> should_run will not 
have effect and not be executed.

##
File path: docs/dev/pass_infra.rst
##
@@ -389,6 +397,51 @@ To allow other C++ modules to apply this pass, we declare 
a free function in
 
 TVM_DLL Pass FoldConstant();
 
+Pass Instrument
+~~~
+
+To instrument passes, four methods are introduced to ``PassContext``.
+
+.. code:: c++
+
+TVM_DLL void InstrumentEnterPassContext();
+TVM_DLL void InstrumentExitPassContext();
+TVM_DLL bool InstrumentBeforePass(const IRModule& mod, const PassInfo& 
info) const;
+TVM_DLL void InstrumentAfterPass(const IRModule& mod, const PassInfo& 
info) const;
+
+The first two methods are called respectively in entering/exiting context 
scope. The latter two are called while passes is being 
applied(`src/ir/transform.cc`_).
+
+Note that ``InstrumentBeforePass()`` return a boolean indicating this pass 
should
+be run or not.
+
+``PassInstrument`` provides callbacks run by these methods. Multiple
+``PassInstrument`` instances can be registed into a single ``PassContext``.
+They are called sequentially in the order of ``instruments`` member.
+

Review comment:
   Add exception situations.

##
File path: tutorials/dev/use_pass_instrument.py
##
@@ -0,0 +1,206 @@
+# 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=line-too-long
+"""
+.. _tutorial-use-pass-instrument:
+
+How to Use TVM Pass Instrument
+==
+**Author**: `Chi-Wei Wang `_
+
+As more and more passes are implemented, it becomes interesting to instrument
+passes execution, analyze per-pass effects and observe various events.
+We have extended :py:class:`tvm.transform.PassContext` to accept a list of
+instrument classes. Also a decorator 
:py:func:`tvm.ir.instrument.pass_instrument` is provided to easily implement 
instrument classes.
+
+This tutorial demostrates how developers can use ``PassContext`` to instrument
+passes. For more details, please refer to the :ref:`pass-infra`

Review comment:
   How about remove "For more details,  ... " ?

##
File path: tutorials/dev/use_pass_instrument.py
##
@@ -0,0 +1,206 @@
+# 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