karthik-0306 commented on code in PR #41405:
URL: https://github.com/apache/beam/pull/41405#discussion_r4699388324
##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -2189,6 +2210,122 @@ def
test_model_handler_manager_increments_once_per_process(self):
mm.load('key1')
self.assertEqual(['key3', 'key1'], list(mm._tag_map.keys()))
+ def test_model_handler_manager_retries_failed_load(self):
+ # state=100 makes FakeStatefulModel raise when the model is loaded.
+ mh1 = FakeModelHandler(state=100)
+ mh2 = FakeModelHandler(state=2)
+ mm = base._ModelHandlerManager(mh_map={'key1': mh1, 'key2': mh2})
+ mm.increment_max_models(1)
+ with self.assertRaisesRegex(Exception, 'Oh no'):
+ mm.load('key1')
+ self.assertEqual([], list(mm._tag_map.keys()))
+
+ # The next load of key1 loads the model again and keeps it.
+ mh1._state = 1
+ stats = mm.load('key1')
+ self.assertIsNotNone(stats.load_latency)
+ # Use bad_mh's load function to make sure the model is already stored.
+ bad_mh = FakeModelHandler(state=100)
+ model1 = multi_process_shared.MultiProcessShared(
+ bad_mh.load_model, tag=stats.model_tag).acquire()
+ self.assertEqual(1, model1.predict(10))
+ self.assertIsNone(mm.load('key1').load_latency)
+
+ # Evicting key1 to load key2 works.
+ mm.load('key2')
+ self.assertEqual(['key2'], list(mm._tag_map.keys()))
+
+ def test_model_handler_manager_unknown_key_leaves_no_tag(self):
+ mm = base._ModelHandlerManager(mh_map={'key1': FakeModelHandler(state=1)})
+ with self.assertRaises(KeyError):
+ mm.load('missing')
+ self.assertEqual([], list(mm._tag_map.keys()))
+
+ def test_model_handler_manager_with_max_models_zero_keeps_new_model(self):
+ mm = base._ModelHandlerManager(
+ mh_map={
+ 'key1': FakeModelHandler(state=1),
+ 'key2': FakeModelHandler(state=2)
+ })
+ mm.increment_max_models(0)
+ tag1 = mm.load('key1').model_tag
+ self.assertEqual({'key1': tag1}, dict(mm._tag_map))
+ self.assertEqual([tag1], list(mm._proxy_map.keys()))
+ tag2 = mm.load('key2').model_tag
+ self.assertEqual({'key2': tag2}, dict(mm._tag_map))
+ self.assertEqual([tag2], list(mm._proxy_map.keys()))
+
+ def _start_blocking_load(self, mm, key):
+ """Starts mm.load(key) on a thread and waits until load_model runs."""
+ mh = mm._mh_map[key]
+ results = []
+ thread = threading.Thread(target=lambda: results.append(mm.load(key)))
+ thread.start()
Review Comment:
Done..
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]