Skip to content
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

[BEAM-14044] Allow ModelLoader to forward BatchElements args #17527

Merged
merged 8 commits into from
May 25, 2022

Conversation

zwestrick
Copy link
Contributor

Adds a method to ModelLoader to allow overriding of arguments to beam.BatchElements. The main goal here is to set a max batch size (e.g., 1) when the caller has particular requirements.

@asf-ci
Copy link

asf-ci commented May 2, 2022

Can one of the admins verify this patch?

2 similar comments
@asf-ci
Copy link

asf-ci commented May 2, 2022

Can one of the admins verify this patch?

@asf-ci
Copy link

asf-ci commented May 2, 2022

Can one of the admins verify this patch?

@github-actions github-actions bot added the python label May 2, 2022
Copy link
Contributor

@ryanthompson591 ryanthompson591 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be possible to add a unit test. LMK if you need some help with that.

Probably a good consistent unit test to add would be to set min and max size to 2, and then expect sizes of 2 in a mock/fake. I suggest 2 because I think default might be 1.

@@ -80,6 +80,10 @@ def load_model(self) -> T:
def get_inference_runner(self) -> InferenceRunner:
"""Returns an implementation of InferenceRunner for this model."""
raise NotImplementedError(type(self))

def batch_elements_kwargs(self) -> Mapping[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are going to need to import Mapping

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@zwestrick zwestrick changed the title Updates ml/inference ModelLoader to allow defining arguments to BatchElements [BEAM-14044] Allow ModelLoader to forward BatchElements args May 2, 2022
@zwestrick
Copy link
Contributor Author

cc @TheNeuralBit

@codecov
Copy link

codecov bot commented May 2, 2022

Codecov Report

Merging #17527 (117da9f) into master (2ef576f) will increase coverage by 0.13%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #17527      +/-   ##
==========================================
+ Coverage   73.81%   73.94%   +0.13%     
==========================================
  Files         690      694       +4     
  Lines       90944    91527     +583     
==========================================
+ Hits        67129    67683     +554     
- Misses      22595    22624      +29     
  Partials     1220     1220              
Flag Coverage Δ
python 83.75% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
sdks/python/apache_beam/ml/inference/base.py 92.70% <100.00%> (+0.16%) ⬆️
...s/interactive/dataproc/dataproc_cluster_manager.py 84.21% <0.00%> (-3.70%) ⬇️
...ache_beam/runners/dataflow/test_dataflow_runner.py 29.62% <0.00%> (-3.03%) ⬇️
sdks/python/apache_beam/coders/row_coder.py 94.49% <0.00%> (-2.51%) ⬇️
sdks/python/apache_beam/utils/retry.py 83.05% <0.00%> (-2.39%) ⬇️
sdks/python/apache_beam/internal/gcp/auth.py 78.66% <0.00%> (-2.37%) ⬇️
sdks/python/apache_beam/runners/common.py 87.94% <0.00%> (-2.33%) ⬇️
...python/apache_beam/runners/worker/worker_status.py 78.26% <0.00%> (-1.45%) ⬇️
sdks/python/apache_beam/io/gcp/gcsfilesystem.py 88.65% <0.00%> (-1.35%) ⬇️
...ive/messaging/interactive_environment_inspector.py 96.66% <0.00%> (-1.21%) ⬇️
... and 60 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ef576f...117da9f. Read the comment docs.

@zwestrick
Copy link
Contributor Author

I think it should be possible to add a unit test. LMK if you need some help with that.

Probably a good consistent unit test to add would be to set min and max size to 2, and then expect sizes of 2 in a mock/fake. I suggest 2 because I think default might be 1.

Done, added a test that batches 100 elements so that we're very far outside the range of random initialization

@ryanthompson591
Copy link
Contributor

R: @TheNeuralBit

You will need to look through the tests and linter that are failing and fix those issues. If you look at the bottom of the comments you section can see all the tests that are failing.

@zwestrick
Copy link
Contributor Author

R: @TheNeuralBit

You will need to look through the tests and linter that are failing and fix those issues. If you look at the bottom of the comments you section can see all the tests that are failing.

Done. It looks like the remaining test failure is likely flaky

Copy link
Contributor

@ryanthompson591 ryanthompson591 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, those tests that are failing are just flaky.

@ryanthompson591
Copy link
Contributor

R: @TheNeuralBit

@ryanthompson591
Copy link
Contributor

R: @yeandy

return FakeInferenceRunnerNeedsBigBatch()

def batch_elements_kwargs(self):
return {'min_batch_size': 9999}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is to be able to set a max batch size of 1, I think it might also be useful to explicitly test the case in which batch_elements_kwargs returns {max_batch_size=1}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes the test much more difficult to write, since the actual batch size chosen in that case would be nondeterministic, so it's difficult to distinguish between size 1 batches due to randomness and size 1 batches due to forwarding the correct args.

@TheNeuralBit
Copy link
Member

Run Python PreCommit

Copy link
Member

@TheNeuralBit TheNeuralBit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like with the API, selecting batch_elements_kwargs is up to implementers of ModelLoader/InferenceRunner.

What if the implementer wants to enable users to set the values as appropriate for their model? Then each implementation would need to decide on a way to expose it, right?

@zwestrick
Copy link
Contributor Author

It seems like with the API, selecting batch_elements_kwargs is up to implementers of ModelLoader/InferenceRunner.

What if the implementer wants to enable users to set the values as appropriate for their model? Then each implementation would need to decide on a way to expose it, right?

Yes, although the motivating context for this is that we have a particular ModelLoader (in TFX-BSL) for handling pre-batched inputs, and would like a way to limit subsequent batching for all users of that ModelLoader. The goal isn't really to expose knobs on a per-model basis, although you might want to (e.g., if a model is very big).

@TheNeuralBit
Copy link
Member

It seems like with the API, selecting batch_elements_kwargs is up to implementers of ModelLoader/InferenceRunner.
What if the implementer wants to enable users to set the values as appropriate for their model? Then each implementation would need to decide on a way to expose it, right?

Yes, although the motivating context for this is that we have a particular ModelLoader (in TFX-BSL) for handling pre-batched inputs, and would like a way to limit subsequent batching for all users of that ModelLoader. The goal isn't really to expose knobs on a per-model basis, although you might want to (e.g., if a model is very big).

Got it, thanks. I suppose we can re-visit some shared way to expose this to end users if the implementations start doing it independently.

@TheNeuralBit TheNeuralBit merged commit 0a6fa95 into apache:master May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants