yeandy commented on code in PR #17470:
URL: https://github.com/apache/beam/pull/17470#discussion_r878106500


##########
sdks/python/apache_beam/ml/inference/pytorch.py:
##########
@@ -39,25 +42,47 @@ class PytorchInferenceRunner(InferenceRunner):
   def __init__(self, device: torch.device):
     self._device = device
 
-  def run_inference(self, batch: List[torch.Tensor],
-                    model: torch.nn.Module) -> Iterable[PredictionResult]:
+  def run_inference(
+      self,
+      batch: List[Union[torch.Tensor, Dict[str, torch.Tensor]]],
+      model: torch.nn.Module,
+      prediction_params: Optional[Dict[str, Any]] = None,
+  ) -> Iterable[PredictionResult]:
     """
     Runs inferences on a batch of Tensors and returns an Iterable of
     Tensor Predictions.
 
     This method stacks the list of Tensors in a vectorized format to optimize
     the inference call.
     """
+    if prediction_params is None:
+      prediction_params = {}
 
-    torch_batch = torch.stack(batch)
-    if torch_batch.device != self._device:
-      torch_batch = torch_batch.to(self._device)
-    predictions = model(torch_batch)
+    if isinstance(batch[0], dict):
+      result_dict = defaultdict(list)
+      for el in batch:
+        for k, v in el.items():
+          result_dict[k].append(v)
+      for k in result_dict:
+        batched_values = torch.stack(result_dict[k])
+        if batched_values.device != self._device:
+          batched_values = batched_values.to(self._device)
+        result_dict[k] = batched_values
+      predictions = model(**result_dict, **prediction_params)
+    else:

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]

Reply via email to