zackcquic commented on a change in pull request #7952: URL: https://github.com/apache/tvm/pull/7952#discussion_r634817444
########## File path: src/ir/instrument.cc ########## @@ -0,0 +1,310 @@ +/* + * 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. + */ + +/*! + * \file src/ir/instrument.cc + * \brief Infrastructure for instrumentation. + */ +#include <dmlc/thread_local.h> +#include <tvm/ir/instrument.h> +#include <tvm/ir/transform.h> +#include <tvm/node/repr_printer.h> +#include <tvm/runtime/registry.h> + +#include <stack> + +namespace tvm { +namespace instrument { + +/*! + * \brief A named PassInstrument implementation + * \sa NamedPassInstrument + */ +class NamedPassInstrumentNode : public PassInstrumentNode { + public: + /*! \brief Name of this pass instrument object. */ + String name; + + /*! \brief Callback for instrumentation environment set up. */ + runtime::TypedPackedFunc<void()> set_up_callback; + /*! \brief Callback for instrumentation environment clean up. */ + runtime::TypedPackedFunc<void()> tear_down_callback; + + /*! \brief Callback to run before a pass. */ + runtime::TypedPackedFunc<bool(const IRModule&, const transform::PassInfo&)> + run_before_pass_callback; + /*! \brief Callback to run after a pass. */ + runtime::TypedPackedFunc<void(const IRModule&, const transform::PassInfo&)> + run_after_pass_callback; + + void VisitAttrs(AttrVisitor* v) { v->Visit("name", &name); } + + /*! \brief Set up environment for instrumentation. */ + void SetUp() const final; + + /*! \brief Clean up instrumentation environment. */ + void TearDown() const final; + + /*! + * \brief Instrument before pass run, determine whether to run the pass or not. + * \param mod The module that an optimization pass runs on. + * \param info The pass information. + * + * \return true to run the pass; false to skip the pass. + */ + bool RunBeforePass(const IRModule& mod, const transform::PassInfo& info) const final; + + /*! + * \brief Instrument after pass run. + * + * \param mod The module that an optimization pass runs on. + * \param info The pass information. + */ + void RunAfterPass(const IRModule& mod, const transform::PassInfo& info) const final; + + static constexpr const char* _type_key = "instrument.NamedPassInstrument"; + TVM_DECLARE_FINAL_OBJECT_INFO(NamedPassInstrumentNode, PassInstrumentNode); +}; + +/*! + * \brief Managed reference class for NamedPassInstrumentNode + * \sa NamedPassInstrumentNode + */ +class NamedPassInstrument : public PassInstrument { + public: + /*! + * \brief Constructor + * \param name Name for this instrumentation. + */ + TVM_DLL NamedPassInstrument(String name); Review comment: Done ########## File path: python/tvm/ir/transform.py ########## @@ -65,12 +65,20 @@ class PassContext(tvm.runtime.Object): disabled_pass : Optional[Union[List[str], Set[str], Tuple[str]]] The list of passes that are disabled. + instruments : Optional[Union[List[PassInstrument], Set[PassInstrument], Tuple[PassInstrument]]] Review comment: Done -- 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: [email protected]
