tkonolige commented on a change in pull request #8220: URL: https://github.com/apache/tvm/pull/8220#discussion_r648494489
########## File path: docs/dev/pass_infra.rst ########## @@ -109,7 +109,8 @@ configure the compilation options, including optimization level and required/disabled passes, etc. For instance, we may have a configuration which performs all passes at ``opt_level=3`` with some disabled passes using ``disabled_pass=xx`` provided by ``PassContext``. Now we could glob all passes -at ``opt_level=3`` and exclude those in the disabled pass list. +at ``opt_level=3`` and exclude those in the disabled pass list. ``PassContext`` +also provides pass-instruments mechanism, which will be introduced latter. Review comment: ```suggestion also provides a way to instrument all passes. See section [fill in the section here]. ``` ########## 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. Review comment: ```suggestion Note that ``InstrumentBeforePass()`` returns a boolean indicating whether or not the pass should be run. ``` ########## 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`_). Review comment: I think pseudo code of how these functions are called would be useful here. ########## 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: Review comment: ```suggestion One can implement a ``PassInstrument`` by using the ``pass_instrument`` decorator(`python/tvm/ir/instrument.py`_) on a class implementing following methods: ``` ########## 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``. Review comment: ```suggestion * This callback is run when entering the ``PassContext``. ``` What does "entering a PassContext" mean? Is this when you do `with PassContext():`? ########## 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: Is this the same as the order in which they are registered? ########## 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 <https://github.com/chiwwang>`_ + +As more and more passes are implemented, it becomes interesting to instrument Review comment: ```suggestion As more and more passes are implemented, it becomes useful to 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``. Review comment: ```suggestion * This callback is run when exiting the ``PassContext``. ``` ########## 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 <https://github.com/chiwwang>`_ + +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` +""" +import tvm +from tvm import relay +from tvm.contrib.download import download_testdata +from tvm.relay.build_module import bind_params_by_name +from tvm.ir.instrument import ( + PassTimingInstrument, + pass_instrument, +) + +############################################################################### +# Create An Example Relay Program +# ------------------------------- +# We create a Relay program from a Pytorch model. +# Here we pick up ``mobilenet_v2`` from torchvision. +import torch +import torchvision + +model_name = "mobilenet_v2" +model = getattr(torchvision.models, model_name)(pretrained=True) +model = model.eval() + +input_shape = [1, 3, 224, 224] +input_data = torch.randn(input_shape) +scripted_model = torch.jit.trace(model, input_data).eval() + +shape_list = [("input0", input_shape)] +relay_mod, relay_params = relay.frontend.from_pytorch(scripted_model, shape_list) Review comment: Importing a pytorch model seems to unnecessarily complicate this tutorial. Why not use `relay.testing.resnet`? ########## File path: docs/dev/pass_infra.rst ########## @@ -123,16 +124,23 @@ Python APIs to create a compilation pipeline using pass context. class PassContextNode : public Object { public: - ErrorReporter err_reporter; Review comment: What happened to the error reporter? ########## File path: python/tvm/ir/instrument.py ########## @@ -91,13 +88,15 @@ def pass_instrument(pi_cls=None): Parameters ---------- - pi_class : + pi_class : class + Instrument class. See example below. Examples -------- - The following code block decorates a pass instrument class. + The following code block show how to decorate a pass instrument class. Review comment: ```suggestion The following code block shows how to decorate a pass instrument class. ``` ########## 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. Review comment: ```suggestion * This callback is run before a pass is executed. It returns a boolean indicating if the pass should be run. ``` -- 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