You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "damccorm (via GitHub)" <gi...@apache.org> on 2023/07/21 18:22:35 UTC
[GitHub] [beam] damccorm opened a new pull request, #27603: Add model manager class to hold many models at once
damccorm opened a new pull request, #27603:
URL: https://github.com/apache/beam/pull/27603
Step 1 of https://docs.google.com/document/d/1kj3FyWRbJu1KhViX07Z0Gk0MU0842jhYRhI-DMhhcv4/edit
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281169576
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
starting from py 3.7 dictionary order is guaranteed to be insertion order. defaultdict should work.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] github-actions[bot] commented on pull request #27603: Add model manager class to hold many models at once
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27603:
URL: https://github.com/apache/beam/pull/27603#issuecomment-1648150090
Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
R: @tvalentyn for label python.
Available commands:
- `stop reviewer notifications` - opt out of the automated review tooling
- `remind me after tests pass` - tag the comment author after tests pass
- `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281147101
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
I like the idea of relying on dictionary order, that requires an ordereddict though, so I can't use the defaultdict, right? I updated to do this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1280936684
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
Review Comment:
I wonder if we might have side-effect if these tests run concurrently due to multi-process-sharing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on PR #27603:
URL: https://github.com/apache/beam/pull/27603#issuecomment-1664264661
Thanks! Changes applied, I'll merge once checks go green
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281192968
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ model2 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag2).acquire()
+ model3 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag3).acquire()
+ self.assertEqual(2, model2.predict(10))
+ self.assertEqual(3, model3.predict(10))
+
+ def test_model_manager_evicts_models(self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=2)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
+
+ def test_model_manager_evicts_correct_num_of_models_after_being_incremented(
+ self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=1)
+ mm.increment_max_models(1)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
Review Comment:
That's actually basically what https://github.com/apache/beam/blob/8054afac894303d3fba61599b93861656b1158e3/sdks/python/apache_beam/utils/multi_process_shared_test.py#L136 does (always_proxy forces a new process to be spun up for the object). I agree that doing this kind of testing makes more sense in multi_process_shared_test.py though, if there are additional cases you'd like to see I'm happy to add some
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] codecov[bot] commented on pull request #27603: Add model manager class to hold many models at once
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #27603:
URL: https://github.com/apache/beam/pull/27603#issuecomment-1646105586
## [Codecov](https://app.codecov.io/gh/apache/beam/pull/27603?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
> Merging [#27603](https://app.codecov.io/gh/apache/beam/pull/27603?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (bb8e344) into [master](https://app.codecov.io/gh/apache/beam/commit/b54bf522faead146b1c186e90dfe01a64163876e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (b54bf52) will **decrease** coverage by `0.01%`.
> The diff coverage is `88.23%`.
```diff
@@ Coverage Diff @@
## master #27603 +/- ##
==========================================
- Coverage 71.12% 71.11% -0.01%
==========================================
Files 860 860
Lines 104573 104608 +35
==========================================
+ Hits 74378 74395 +17
- Misses 28638 28656 +18
Partials 1557 1557
```
| Flag | Coverage Δ | |
|---|---|---|
| python | `80.32% <88.23%> (-0.02%)` | :arrow_down: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://app.codecov.io/gh/apache/beam/pull/27603?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [sdks/python/apache\_beam/io/gcp/bigquery.py](https://app.codecov.io/gh/apache/beam/pull/27603?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vaW8vZ2NwL2JpZ3F1ZXJ5LnB5) | `70.46% <0.00%> (-0.16%)` | :arrow_down: |
| [sdks/python/apache\_beam/ml/inference/base.py](https://app.codecov.io/gh/apache/beam/pull/27603?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vbWwvaW5mZXJlbmNlL2Jhc2UucHk=) | `94.33% <90.90%> (-0.31%)` | :arrow_down: |
... and [7 files with indirect coverage changes](https://app.codecov.io/gh/apache/beam/pull/27603/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on PR #27603:
URL: https://github.com/apache/beam/pull/27603#issuecomment-1664328561
Run Python_Integration PreCommit
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281147278
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
I added a check for this in the test.
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ model2 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag2).acquire()
+ model3 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag3).acquire()
+ self.assertEqual(2, model2.predict(10))
+ self.assertEqual(3, model3.predict(10))
+
+ def test_model_manager_evicts_models(self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=2)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
+
+ def test_model_manager_evicts_correct_num_of_models_after_being_incremented(
+ self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=1)
+ mm.increment_max_models(1)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
Review Comment:
Do we have an easy mechanism for doing this? If so, I'm on board, I'm not sure I see the utility in doing so though, what advantage do you see it getting us?
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
Review Comment:
We shouldn't because of the randomness introduced when we get the tag (the uuid bit); each model reference to a model will have a unique tag created for it, regardless of the key it represents. I also just tried running the suite 1000 times and it succeeded each time.
Note that this is actually an important property if we have multiple model managers operating at once (e.g. in a pipeline that does multiple sets of sequential inference)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1279879864
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
sounds like `self._tag_map` stores only the loaded keys. In that case you could let go of `self._loaded_keys` (or make it a map that stores the tags). and rely on dictionary insertion order to retrieve the oldest entry + reinsert as necessary.
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
How about we add a test that existing model references are still valid after we loaded more models than max_model ?
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
you could also use `collections.defaultdict(lambda: uuid.uuid4().hex)` instead of `_get_tag()`
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ model2 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag2).acquire()
+ model3 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag3).acquire()
+ self.assertEqual(2, model2.predict(10))
+ self.assertEqual(3, model3.predict(10))
+
+ def test_model_manager_evicts_models(self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=2)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
+
+ def test_model_manager_evicts_correct_num_of_models_after_being_incremented(
+ self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=1)
+ mm.increment_max_models(1)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
Review Comment:
WDYT about running this portion of test scenario in a subprocess?
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
How about we add a test that existing model references are still valid after we loaded more models than max_model ?
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
How about we add a test that existing model references are still valid after we loaded more models than max_model ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281175468
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ model2 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag2).acquire()
+ model3 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag3).acquire()
+ self.assertEqual(2, model2.predict(10))
+ self.assertEqual(3, model3.predict(10))
+
+ def test_model_manager_evicts_models(self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=2)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
+
+ def test_model_manager_evicts_correct_num_of_models_after_being_incremented(
+ self):
+ mh1 = FakeModelHandler(state=1)
+ mh2 = FakeModelHandler(state=2)
+ mh3 = FakeModelHandler(state=3)
+ mhs = {'key1': mh1, 'key2': mh2, 'key3': mh3}
+ mm = base._ModelManager(mh_map=mhs, max_models=1)
+ mm.increment_max_models(1)
+ tag1 = mm.load('key1')
+ sh1 = multi_process_shared.MultiProcessShared(mh1.load_model, tag=tag1)
+ model1 = sh1.acquire()
+ self.assertEqual(1, model1.predict(10))
+ model1.increment_state(5)
+ self.assertEqual(6, model1.predict(10))
+ sh1.release(model1)
+
+ tag2 = mm.load('key2')
+ tag3 = mm.load('key3')
+ sh2 = multi_process_shared.MultiProcessShared(mh2.load_model, tag=tag2)
+ model2 = sh2.acquire()
+ sh3 = multi_process_shared.MultiProcessShared(mh3.load_model, tag=tag3)
+ model3 = sh3.acquire()
+ model2.increment_state(5)
+ model3.increment_state(5)
+ self.assertEqual(7, model2.predict(10))
+ self.assertEqual(8, model3.predict(10))
+ sh2.release(model2)
+ sh3.release(model3)
+
+ # This should get recreated, so it shouldn't have the state updates
+ model1 = multi_process_shared.MultiProcessShared(
+ mh1.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ # These should not get recreated, so they should have the state updates
+ model2 = multi_process_shared.MultiProcessShared(
+ mh2.load_model, tag=tag2).acquire()
+ self.assertEqual(7, model2.predict(10))
+ model3 = multi_process_shared.MultiProcessShared(
+ mh3.load_model, tag=tag3).acquire()
+ self.assertEqual(8, model3.predict(10))
Review Comment:
It would be a test scenario that verifies the objects are shared if we cross the process boundary. Although perhaps a targeted scenario in https://github.com/apache/beam/blob/master/sdks/python/apache_beam/utils/multi_process_shared_test.py would be more appropriate.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1283392520
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +740,81 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._proxy_map = {}
Review Comment:
Could we add typehints to these dictionaries?
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +740,81 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
Review Comment:
```suggestion
single copy of each model into a MultiProcessShared object and then
```
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
+ mhs = {
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2),
+ 'key3': FakeModelHandler(state=3)
+ }
+ mm = base._ModelManager(mh_map=mhs)
+ tag1 = mm.load('key1')
+ # Use bad_mh's load function to make sure we're actually loading the
+ # version already stored
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=tag1).acquire()
+ self.assertEqual(1, model1.predict(10))
+
+ tag2 = mm.load('key2')
Review Comment:
> Note that this is actually an important property if we have multiple model managers operating at once (e.g. in a pipeline that does multiple sets of sequential inference)
Sounds like two different model managers will not share the models, even if they are the same. Something important to keep in mind for planning memory consumption if users do sequential inferences.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1279934186
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
How about we add a test that existing model references are still valid after we loaded more models than max_model ?
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -964,6 +982,117 @@ def test_child_class_without_env_vars(self):
actual = pcoll | base.RunInference(FakeModelHandlerNoEnvVars())
assert_that(actual, equal_to(expected), label='assert:inferences')
+ def test_model_manager_loads_shared_model(self):
Review Comment:
How about we add a test that existing model references are still valid after we loaded more models than max_model ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm merged pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm merged PR #27603:
URL: https://github.com/apache/beam/pull/27603
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281195717
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
I think it also gives us `popitem(last=False)` (ordered pop). I wasn't aware of `move_to_end`, I actually think having those is helpful enough to make using OrderedDict worth it (see if you agree with changes applied)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] tvalentyn commented on a diff in pull request #27603: Add model manager class to hold many models at once
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on code in PR #27603:
URL: https://github.com/apache/beam/pull/27603#discussion_r1281172399
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -739,8 +739,95 @@ def with_exception_handling(
return self
+class _ModelManager:
+ """
+ A class for efficiently managing copies of multiple models. Will load a
+ single copy of each model into a multi_process_shared object and then
+ return a lookup key for that object. Optionally takes in a max_models
+ parameter, if that is set it will only hold that many models in memory at
+ once before evicting one (using LRU logic).
+ """
+ def __init__(
+ self, mh_map: Dict[str, ModelHandler], max_models: Optional[int] = None):
+ """
+ Args:
+ mh_map: A map from keys to model handlers which can be used to load a
+ model.
+ max_models: The maximum number of models to load at any given time
+ before evicting 1 from memory (using LRU logic). Leave as None to
+ allow unlimited models.
+ """
+ self._max_models = max_models
+ self._mh_map = mh_map
+ self._loaded_keys = []
+ self._proxy_map = {}
+ self._tag_map = {}
+
+ def _get_tag(self, key: str) -> str:
+ """
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to load the model using multi_process_shared.py
+ """
+ if key not in self._tag_map:
+ self._tag_map[key] = uuid.uuid4().hex
+ return self._tag_map[key]
+
+ def load(self, key: str) -> str:
+ """
+ Loads the appropriate model for the given key into memory.
+ Args:
+ key: the key associated with the model we'd like to load.
+ Returns:
+ the tag we can use to access the model using multi_process_shared.py.
+ """
+ tag = self._get_tag(key)
+ if key in self._loaded_keys:
+ if self._max_models is not None:
+ # move key to the back of the list
+ self._loaded_keys.append(
+ self._loaded_keys.pop(self._loaded_keys.index(key)))
+ return tag
+
+ mh = self._mh_map[key]
+ if self._max_models is not None and self._max_models <= len(
+ self._loaded_keys):
+ # If we're about to exceed our LRU size,
+ # remove the front model from the list from memory.
+ key_to_remove = self._loaded_keys[0]
+ tag_to_remove = self._get_tag(key_to_remove)
+ shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
+ shared_handle.release(model_to_remove)
+ del self._tag_map[key]
Review Comment:
the only advantage I am aware of in OrderedDict is https://docs.python.org/3.7/library/collections.html#collections.OrderedDict.move_to_end
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org