-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor API code to base.py in RunInference #21801
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6829459
refactor code from api to base
AnandInguva 8235a5b
delete api.py
AnandInguva 5ecbf64
modify imports
AnandInguva cd1118f
Add todo to mypy github issue
AnandInguva e8cd83e
Merge remote-tracking branch 'upstream/master' into api-to-base
AnandInguva 77f7691
Refactor code to reflect changes of #21777
AnandInguva 65a0483
Refactor example with KeyedModelHandler
AnandInguva 1d7f187
Merge remote-tracking branch 'upstream/master' into api-to-base
AnandInguva 55b2793
remove explicit type hints from RunInference class
AnandInguva 1576be5
Fixup : Lint
AnandInguva 0150487
remove TODO to github issue for mypy error
AnandInguva af81bac
Add mypy github issue as TODO
AnandInguva 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# mypy: ignore-errors | ||
|
||
"""An extensible run inference transform. | ||
|
||
|
@@ -32,12 +33,15 @@ | |
import pickle | ||
import sys | ||
import time | ||
from dataclasses import dataclass | ||
from typing import Any | ||
from typing import Generic | ||
from typing import Iterable | ||
from typing import List | ||
from typing import Mapping | ||
from typing import Tuple | ||
from typing import TypeVar | ||
from typing import Union | ||
|
||
import apache_beam as beam | ||
from apache_beam.utils import shared | ||
|
@@ -54,6 +58,15 @@ | |
ModelT = TypeVar('ModelT') | ||
ExampleT = TypeVar('ExampleT') | ||
PredictionT = TypeVar('PredictionT') | ||
_K = TypeVar('_K') | ||
_INPUT_TYPE = TypeVar('_INPUT_TYPE') | ||
_OUTPUT_TYPE = TypeVar('_OUTPUT_TYPE') | ||
|
||
|
||
@dataclass | ||
class PredictionResult: | ||
example: _INPUT_TYPE | ||
inference: _OUTPUT_TYPE | ||
|
||
|
||
def _to_milliseconds(time_ns: int) -> int: | ||
|
@@ -93,12 +106,27 @@ def batch_elements_kwargs(self) -> Mapping[str, Any]: | |
return {} | ||
|
||
|
||
@beam.typehints.with_input_types(Union[_INPUT_TYPE, Tuple[_K, _INPUT_TYPE]]) | ||
@beam.typehints.with_output_types(Union[PredictionResult, Tuple[_K, PredictionResult]]) # pylint: disable=line-too-long | ||
class RunInference(beam.PTransform[beam.PCollection[ExampleT], | ||
beam.PCollection[PredictionT]]): | ||
"""An extensible transform for running inferences. | ||
Args: | ||
model_handler: An implementation of ModelHandler. | ||
clock: A clock implementing get_current_time_in_microseconds. | ||
|
||
A transform that takes a PCollection of examples (or features) to be used on | ||
an ML model. It will then output inferences (or predictions) for those | ||
examples in a PCollection of PredictionResults, containing the input examples | ||
and output inferences. | ||
|
||
If examples are paired with keys, it will output a tuple | ||
(key, PredictionResult) for each (key, example) input. | ||
|
||
Models for supported frameworks can be loaded via a URI. Supported services | ||
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 in a separate PR:
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. Yep. thanks |
||
can also be used. | ||
|
||
TODO(BEAM-14046): Add and link to help documentation | ||
""" | ||
def __init__( | ||
self, | ||
|
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
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
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
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
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.
What was the error here? was it something like
Type variable is unbound
?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.
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.
Do we understand why we hit that error?
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.
it was addressed in this #17514 and commented on this issue #21441. I added a todo( link to github issue)
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.
I think #21441 describes a different problem: supporting type inference for RunInference API users. I believe some of the approaches, that it mentions, have been implemented (adding generics), and we have some type inference capabilities (that you plan to test in another test). I'd ask, what is the remaining work there?
The problem of having to disable mypy in this file seems to do with developer tooling. Do we know what causes mypy to complain? Does this problem need a separate issue?
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.
These are the errors mypy complains about.
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.
Find an issue related to this. python/mypy#7520.
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.
Looking into these errors, this is more related to mypy and Dataclass[1]. Since we are moving to use
NamedTuple
instead ofDataclass
, can we move forward with this?[1] python/mypy#7520. To solve the current error of mypy with dataclass, PredictionResult should be modified as below