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


##########
sdks/python/apache_beam/ml/inference/pytorch_test.py:
##########
@@ -59,6 +63,23 @@ def forward(self, x):
     return out
 
 
+class PytorchLinearRegressionPredictionParams(torch.nn.Module):
+  def __init__(self, input_dim, output_dim):
+    super().__init__()
+    self.linear = torch.nn.Linear(input_dim, output_dim)
+
+  # k1 is the batched input, and prediction_param_array, prediction_param_bool
+  # are non-batchable inputs (typically model-related info) used to configure
+  # the model before its predict call is invoked
+  def forward(self, k1, k2, prediction_param_array, prediction_param_bool):

Review Comment:
   Fixed.



##########
sdks/python/apache_beam/ml/inference/pytorch_test.py:
##########
@@ -117,11 +135,104 @@ def test_inference_runner_multiple_tensor_features(self):
                      ('linear.bias', torch.Tensor([0.5]))]))
     model.eval()
 
+    inference_runner = PytorchInferenceRunner(torch.device('cpu'))
+    predictions = inference_runner.run_inference(examples, model)
+    for actual, expected in zip(predictions, expected_predictions):
+      self.assertEqual(actual, expected)
+
+  def test_inference_runner_kwargs(self):
+    examples = [
+        {
+            'k1': torch.from_numpy(np.array([1], dtype="float32")),
+            'k2': torch.from_numpy(np.array([1.5], dtype="float32"))
+        },
+        {
+            'k1': torch.from_numpy(np.array([5], dtype="float32")),
+            'k2': torch.from_numpy(np.array([5.5], dtype="float32"))
+        },
+        {
+            'k1': torch.from_numpy(np.array([-3], dtype="float32")),
+            'k2': torch.from_numpy(np.array([-3.5], dtype="float32"))
+        },
+        {
+            'k1': torch.from_numpy(np.array([10.0], dtype="float32")),
+            'k2': torch.from_numpy(np.array([10.5], dtype="float32"))
+        },
+    ]
+    expected_predictions = [
+        PredictionResult(ex, pred) for ex,
+        pred in zip(
+            examples,
+            torch.Tensor([(example['k1'] * 2.0 + 0.5) +
+                          (example['k2'] * 2.0 + 0.5)
+                          for example in examples]).reshape(-1, 1))
+    ]
+
+    class PytorchLinearRegressionMultipleArgs(torch.nn.Module):
+      def __init__(self, input_dim, output_dim):
+        super().__init__()
+        self.linear = torch.nn.Linear(input_dim, output_dim)
+
+      def forward(self, k1, k2):
+        out = self.linear(k1) + self.linear(k2)
+        return out
+
+    model = PytorchLinearRegressionMultipleArgs(input_dim=1, output_dim=1)
+    model.load_state_dict(
+        OrderedDict([('linear.weight', torch.Tensor([[2.0]])),
+                     ('linear.bias', torch.Tensor([0.5]))]))
+    model.eval()
+
     inference_runner = PytorchInferenceRunner(torch.device('cpu'))
     predictions = inference_runner.run_inference(examples, model)
     for actual, expected in zip(predictions, expected_predictions):
       self.assertTrue(_compare_prediction_result(actual, expected))
 
+  def test_inference_runner_prediction_params(self):
+    examples = [

Review Comment:
   Refactored.



##########
sdks/python/apache_beam/ml/inference/pytorch_test.py:
##########
@@ -117,11 +135,104 @@ def test_inference_runner_multiple_tensor_features(self):
                      ('linear.bias', torch.Tensor([0.5]))]))
     model.eval()
 
+    inference_runner = PytorchInferenceRunner(torch.device('cpu'))
+    predictions = inference_runner.run_inference(examples, model)
+    for actual, expected in zip(predictions, expected_predictions):
+      self.assertEqual(actual, expected)
+
+  def test_inference_runner_kwargs(self):

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