-
Notifications
You must be signed in to change notification settings - Fork 131
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
WIP: Adapter to aid in debugging issues #762
Draft
skrawcz
wants to merge
2
commits into
main
Choose a base branch
from
feat_error_debug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,11 @@ | ||
def input_function() -> int: | ||
return 2 | ||
|
||
|
||
def output_function(input_function: int) -> int: | ||
return input_function + 1 | ||
|
||
|
||
def error_function(input_function: int, output_function: int, input: int) -> int: | ||
raise ValueError("This is an error") | ||
return input_function + output_function + input |
This file contains 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,100 @@ | ||
import inspect | ||
import logging | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
from hamilton import lifecycle | ||
|
||
try: | ||
import cloudpickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
template = """ | ||
try: | ||
import cloudpickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
import {module_name} # we load this for imports | ||
|
||
# let's load the inputs | ||
with open('{node_name}_inputs.pkl', 'rb') as f: | ||
inputs = pickle.load(f) | ||
|
||
# the function that errored | ||
{function_to_debug} | ||
|
||
|
||
# run the function | ||
{func_name}(**inputs) | ||
""" | ||
|
||
|
||
class NotebookErrorDebugger(lifecycle.NodeExecutionHook): | ||
|
||
def run_before_node_execution( | ||
self, | ||
*, | ||
node_name: str, | ||
node_tags: Dict[str, Any], | ||
node_kwargs: Dict[str, Any], | ||
node_return_type: type, | ||
task_id: Optional[str], | ||
run_id: str, | ||
node_input_types: Dict[str, Any], | ||
**future_kwargs: Any, | ||
): | ||
pass | ||
|
||
def run_after_node_execution( | ||
self, | ||
*, | ||
node_name: str, | ||
node_tags: Dict[str, Any], | ||
node_kwargs: Dict[str, Any], | ||
node_return_type: type, | ||
result: Any, | ||
error: Optional[Exception], | ||
success: bool, | ||
task_id: Optional[str], | ||
run_id: str, | ||
originating_function: Callable, | ||
**future_kwargs: Any, | ||
): | ||
""" | ||
This function will create the follow in the case of a failure: | ||
|
||
1. It will pickle of the inputs to the function. | ||
2. It will create a file with the following: | ||
a. it will import the module the function is from -- to cover any imports that need to exist. | ||
b. it will load the pickled inputs. | ||
c. it will have the code of the function that errored so you can debug it. | ||
d. it will then also list python version, hamilton version, and any other relevant package versions for | ||
the user to install / have. | ||
2. It will then print out where this data has been saved for the user to then debug. | ||
""" | ||
if not success: | ||
# pickle the inputs | ||
with open(f"{node_name}_inputs.pkl", "wb") as f: | ||
pickle.dump(node_kwargs, f) | ||
# create a file with the function and the inputs | ||
with open(f"{node_name}_debug.py", "w") as f: | ||
f.write( | ||
template.format( | ||
module_name=node_tags.get("module"), | ||
node_name=node_name, | ||
function_to_debug=inspect.getsource(originating_function), | ||
func_name=originating_function.__name__, | ||
) | ||
) | ||
# print out where the data has been saved | ||
message = ( | ||
f"Inputs to {node_name} have been saved to {node_name}_inputs.pkl\n" | ||
f"The function that errored has been saved to {node_name}_debug.py\n" | ||
f"Please run the function in {node_name}_debug.py to debug the error." | ||
) | ||
logger.warning(message) | ||
# TODO: create file with python requirements for pickle to work... |
This file contains 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,8 @@ | ||
if __name__ == "__main__": | ||
import example_error | ||
from notebook_debugger_plugin import NotebookErrorDebugger | ||
|
||
from hamilton import driver | ||
|
||
dr = driver.Builder().with_modules(example_error).with_adapters(NotebookErrorDebugger()).build() | ||
dr.execute(["error_function"], inputs={"input": 4}) |
This file contains 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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import abc | ||
from abc import ABC | ||
from types import ModuleType | ||
from typing import TYPE_CHECKING, Any, Collection, Dict, List, Optional, Tuple, Type, final | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Collection, | ||
Dict, | ||
List, | ||
Optional, | ||
Tuple, | ||
Type, | ||
final, | ||
) | ||
|
||
from hamilton import graph_types, node | ||
|
||
|
@@ -224,6 +235,7 @@ def run_after_node_execution( | |
success: bool, | ||
task_id: Optional[str], | ||
run_id: str, | ||
originating_function: Callable, | ||
**future_kwargs: Any, | ||
): | ||
"""Hook that is executed post node execution. | ||
|
@@ -265,6 +277,7 @@ def post_node_execute( | |
task_id=task_id, | ||
success=success, | ||
run_id=run_id, | ||
originating_function=node_.originating_functions[0], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: change name. |
||
) | ||
|
||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: add path/directory to save these to.