-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add a the ability to trigger debugging in the interpreter without recompiling #2219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
337e6c9
Add new debugger
jroesch a063fa5
WIP
jroesch 4a0e867
Add test_debug.py
jroesch 694d46d
Add test for debugging code
jroesch 72cb372
Fix linting
jroesch 0fb114d
Add docstring
jroesch b28e88b
Update python/tvm/relay/debug.py
joshpoll 0f99bfc
Update python/tvm/relay/debug.py
joshpoll c7ffd60
Address some feedback
jroesch ba1aab2
Update src/relay/backend/interpreter.cc
joshpoll 3157c10
Address more CR
jroesch 6dc1941
Add second test
jroesch ab8e97f
Update src/relay/op/debug.cc
joshpoll 160ad3d
Fix 2.7 failure
jroesch 922f123
Make opaque
jroesch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file tvm/relay/attrs/debug.h | ||
| * \brief Auxiliary attributes for debug operators. | ||
| */ | ||
| #ifndef TVM_RELAY_ATTRS_DEBUG_H_ | ||
| #define TVM_RELAY_ATTRS_DEBUG_H_ | ||
|
|
||
| #include <tvm/attrs.h> | ||
| #include <string> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| /*! | ||
| * \brief Options for the debug operators. | ||
| */ | ||
| struct DebugAttrs : public tvm::AttrsNode<DebugAttrs> { | ||
| EnvFunc debug_func; | ||
|
|
||
| TVM_DECLARE_ATTRS(DebugAttrs, "relay.attrs.DebugAttrs") { | ||
| TVM_ATTR_FIELD(debug_func) | ||
| .describe("The function to use when debugging."); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
| #endif // TVM_RELAY_ATTRS_DEBUG_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # pylint: disable=wildcard-import, redefined-builtin, invalid-name | ||
| """The Relay IR namespace containing the IR definition and compiler.""" | ||
| from __future__ import absolute_import | ||
| from .base import NodeBase, register_relay_node | ||
| from ..api import register_func | ||
|
|
||
| @register_relay_node | ||
| class InterpreterState(NodeBase): | ||
| pass | ||
|
|
||
| # pylint: disable=unused-argument | ||
| def _debugger_init(expr, stack): | ||
| import pdb | ||
| pdb.set_trace() | ||
|
|
||
| # pylint: disable=unused-argument | ||
| @register_func("relay.debug") | ||
| def _debug(*args): | ||
| _, _, _, ist = args | ||
| print("Relay Debugger") | ||
| print(" You can manipulate the expression under evaluation with the name `expr`.") | ||
| print(" You can manipulate the call stack with the name `stack`.") | ||
| print("--------------") | ||
| print("--------------") | ||
| _debugger_init(ist.current_expr, ist.stack) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file nn.cc | ||
| * \brief Property def of nn operators. | ||
| */ | ||
|
|
||
| #include <tvm/relay/op.h> | ||
| #include <tvm/relay/attrs/debug.h> | ||
| #include <topi/elemwise.h> | ||
| #include <vector> | ||
| #include "./type_relations.h" | ||
| #include "./op_common.h" | ||
| #include "./layout.h" | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| Array<Tensor> DebugCompute(const Attrs& attrs, | ||
| const Array<Tensor>& inputs, | ||
| const Type& out_type, | ||
| const Target& target) { | ||
| return Array<Tensor>{ topi::identity(inputs[0]) }; | ||
| } | ||
|
|
||
| RELAY_REGISTER_OP("debug") | ||
| .describe(R"code(Enter the interpreter's debugger. | ||
|
|
||
| )code" TVM_ADD_FILELINE) | ||
| .set_num_inputs(1) | ||
| .add_argument("program", "Tuple", "The program to execute before debugging.") | ||
| .set_support_level(1) | ||
| .add_type_rel("Debug", IdentityRel) | ||
| .set_attr<TOpPattern>("TOpPattern", kOpaque) | ||
| .set_attr<FTVMCompute>("FTVMCompute", DebugCompute); | ||
|
|
||
| Expr MakeDebug(Expr expr, std::string name) { | ||
| auto dattrs = make_node<DebugAttrs>(); | ||
| if (name.size() > 0) { | ||
| dattrs->debug_func = EnvFunc::Get(name); | ||
| } else { | ||
| dattrs->debug_func = EnvFunc(); | ||
| } | ||
| static const Op& op = Op::Get("debug"); | ||
| return CallNode::make(op, {expr}, Attrs(dattrs), {}); | ||
| } | ||
|
|
||
| TVM_REGISTER_API("relay.op._make.debug") | ||
| .set_body([](const TVMArgs& args, TVMRetValue* rv) { | ||
| runtime::detail::unpack_call<Expr, 2>(MakeDebug, args, rv); | ||
| }); | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from tvm.relay import var, const, create_executor | ||
| from tvm.relay.op import debug | ||
|
|
||
|
|
||
| _test_debug_hit = False | ||
|
|
||
| def test_debug(): | ||
| global _test_debug_hit | ||
| ex = create_executor() | ||
| x = var('x', shape=(), dtype='int32') | ||
| _test_debug_hit = False | ||
| def did_exec(x): | ||
| global _test_debug_hit | ||
| _test_debug_hit = True | ||
| prog = debug(x, debug_func=did_exec) | ||
| result = ex.evaluate(prog, { x: const(1) }) | ||
| assert _test_debug_hit | ||
| assert result.asnumpy() == 1 | ||
|
|
||
| def test_debug_with_expr(): | ||
| global _test_debug_hit | ||
| _test_debug_hit = False | ||
| ex = create_executor() | ||
| x = var('x', shape=(), dtype='int32') | ||
| _test_debug_hit = False | ||
| def did_exec(x): | ||
| global _test_debug_hit | ||
| _test_debug_hit = True | ||
| prog = debug(x + x * x, debug_func=did_exec) | ||
| result = ex.evaluate(prog, { x: const(2) }) | ||
| assert _test_debug_hit | ||
| assert result.asnumpy() == 6 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.