damccorm commented on code in PR #37945:
URL: https://github.com/apache/beam/pull/37945#discussion_r3010006958


##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -2319,6 +2322,264 @@ def test_batching_kwargs_none_values_omitted(self):
     self.assertEqual(kwargs['min_batch_size'], 5)
 
 
+class PaddingReportingStringModelHandler(base.ModelHandler[str, str,
+                                                           FakeModel]):
+  """Reports each element with the max length of the batch it ran in."""
+  def load_model(self):
+    return FakeModel()
+
+  def run_inference(self, batch, model, inference_args=None):
+    max_len = max(len(s) for s in batch)
+    return [f'{s}:{max_len}' for s in batch]
+
+
+class RunInferenceLengthAwareBatchingTest(unittest.TestCase):
+  """End-to-end tests for PR2 length-aware batching in RunInference."""
+  def test_run_inference_with_length_aware_batch_elements(self):
+    handler = PaddingReportingStringModelHandler(
+        min_batch_size=2,
+        max_batch_size=2,
+        max_batch_duration_secs=60,
+        batch_length_fn=len,
+        batch_bucket_boundaries=[5])
+
+    examples = ['a', 'cccccc', 'bb', 'ddddddd']
+    with TestPipeline('FnApiRunner') as p:
+      results = (
+          p
+          | beam.Create(examples, reshuffle=False)
+          | base.RunInference(handler))
+      assert_that(results, equal_to(['a:2', 'bb:2', 'cccccc:7', 'ddddddd:7']))
+
+
+class HandlerBucketingKwargsForwardingTest(unittest.TestCase):
+  """Verify each concrete ModelHandler forwards batch_length_fn and
+  batch_bucket_boundaries through to batch_elements_kwargs()."""
+  _BUCKETING_KWARGS = {
+      'batch_length_fn': len,
+      'batch_bucket_boundaries': [32],
+  }
+
+  def _assert_bucketing_kwargs_forwarded(self, handler):
+    kwargs = handler.batch_elements_kwargs()
+    self.assertIs(kwargs['length_fn'], len)
+    self.assertEqual(kwargs['bucket_boundaries'], [32])
+
+  def _load_handler_class(self, case):
+    try:
+      module = importlib.import_module(case['module_name'])
+    except ImportError:
+      raise unittest.SkipTest(case['skip_message'])

Review Comment:
   Rather than taking in a skip_message, we can just grab the module name; that 
way we don't mask the cause if our skip message is wrong.
   
   ```
   except ImportError as e:
        raise unittest.SkipTest(f"Could not import {e.name}")
   ```



##########
sdks/python/apache_beam/ml/inference/base_test.py:
##########
@@ -2319,6 +2322,264 @@ def test_batching_kwargs_none_values_omitted(self):
     self.assertEqual(kwargs['min_batch_size'], 5)
 
 
+class PaddingReportingStringModelHandler(base.ModelHandler[str, str,
+                                                           FakeModel]):
+  """Reports each element with the max length of the batch it ran in."""
+  def load_model(self):
+    return FakeModel()
+
+  def run_inference(self, batch, model, inference_args=None):
+    max_len = max(len(s) for s in batch)
+    return [f'{s}:{max_len}' for s in batch]
+
+
+class RunInferenceLengthAwareBatchingTest(unittest.TestCase):
+  """End-to-end tests for PR2 length-aware batching in RunInference."""
+  def test_run_inference_with_length_aware_batch_elements(self):
+    handler = PaddingReportingStringModelHandler(
+        min_batch_size=2,
+        max_batch_size=2,
+        max_batch_duration_secs=60,
+        batch_length_fn=len,
+        batch_bucket_boundaries=[5])
+
+    examples = ['a', 'cccccc', 'bb', 'ddddddd']
+    with TestPipeline('FnApiRunner') as p:
+      results = (
+          p
+          | beam.Create(examples, reshuffle=False)
+          | base.RunInference(handler))
+      assert_that(results, equal_to(['a:2', 'bb:2', 'cccccc:7', 'ddddddd:7']))
+
+
+class HandlerBucketingKwargsForwardingTest(unittest.TestCase):

Review Comment:
   Actually, I would probably just remove these tests actually; as it stands, 
the only thing they protect us against is removing one of these arguments, 
which almost certainly would need to be intentional or obvious from the diff.



-- 
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]

Reply via email to