-
Notifications
You must be signed in to change notification settings - Fork 61
Add context to handler functions #103
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 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb84927
Add context to handler functions
d4372c9
Add context to validate_and_initialize_user_module unit tests
1e54f98
Fix formatting issues
ad06a7f
Update comments in handler_service.py
778408f
Include unit tests for handler service with and without context
sachanub 129fa2f
Add logic to check for custom handler functions
sachanub 4c5911f
Modify logic to identify extra arguments in initialize stage
sachanub 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
8 changes: 4 additions & 4 deletions
8
tests/resources/model_input_predict_output_fn/code/inference.py
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
def model_fn(model_dir): | ||
def model_fn(model_dir, context=None): | ||
return "model" | ||
|
||
|
||
def input_fn(data, content_type): | ||
def input_fn(data, content_type, context=None): | ||
return "data" | ||
|
||
|
||
def predict_fn(data, model): | ||
def predict_fn(data, model, context=None): | ||
return "output" | ||
|
||
|
||
def output_fn(prediction, accept): | ||
def output_fn(prediction, accept, context=None): | ||
return prediction |
4 changes: 2 additions & 2 deletions
4
tests/resources/model_transform_fn/code/inference_tranform_fn.py
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import os | ||
|
||
|
||
def model_fn(model_dir): | ||
def model_fn(model_dir, context=None): | ||
return f"Loading {os.path.basename(__file__)}" | ||
|
||
|
||
def transform_fn(a, b, c, d): | ||
def transform_fn(a, b, c, d, context=None): | ||
return f"output {b}" |
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
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.
Is that needed? What overhead does it add to an request/function call?.
Why don't we just past
self.context
in thehandle
method in linesagemaker-huggingface-inference-toolkit/src/sagemaker_huggingface_inference_toolkit/handler_service.py
Line 234 in 44e3dec
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @philschmid . Thank you for your review. Maybe I am not understanding your point correctly, but I don't think we can directly pass
self.context
in thehandle
method. If we do that, don't we risk breaking existing customers who are not usingcontext
as an input argument? With the above mentionedrun_handler_function
, we should be able to support both customers who do not want to usecontext
as well as customers who want to usecontext
. Please correct me if I misunderstood. Thanks!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.
Why would it break it? you mean if they have a custom
inference.py
that defines atransform_fn
method?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.
Yes, we might affect those who define a custom
transform_fn
right?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.
In
handler_service.py
, we are trying to import user defined functions frominference.py
If existence, it will be used to overwrite
self.load
,self.preprocess
,self.predict
,self.postprocess
andself.transform_fn
In this PR, we introduce additional parameter
context
for default handlers. However, for existing user scripts, they don't have this parameter when they implement customized handler function. We have to be careful when we call these functions. That's why @sachanub is adding a helper function to determine when to call functions with the new parameter.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.
Is that something we could deprecate and remove with a v3 of the toolkit? Feels like very error prone or a big overhead to parse and add args like this for every incoming request.
What do you think of adding a check to see if there is a
inference.py
provided and if not we are usingself-transform
directly? Most customer deploy models using the "zero-code" deployment feature, where you provide aMODEL_ID
andTASK
and don't need andinference.py
script.