|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +# pytype: skip-file |
| 19 | + |
| 20 | +import os |
| 21 | +import shutil |
| 22 | +import tempfile |
| 23 | +import unittest |
| 24 | +from collections import OrderedDict |
| 25 | + |
| 26 | +import numpy as np |
| 27 | +import pytest |
| 28 | + |
| 29 | +import apache_beam as beam |
| 30 | +from apache_beam.testing.test_pipeline import TestPipeline |
| 31 | +from apache_beam.testing.util import assert_that |
| 32 | +from apache_beam.testing.util import equal_to |
| 33 | + |
| 34 | +# Protect against environments where pytorch library is not available. |
| 35 | +# pylint: disable=wrong-import-order, wrong-import-position, ungrouped-imports |
| 36 | +try: |
| 37 | + import torch |
| 38 | + from apache_beam.ml.inference.api import PredictionResult |
| 39 | + from apache_beam.ml.inference.base import RunInference |
| 40 | + from apache_beam.ml.inference.pytorch import PytorchInferenceRunner |
| 41 | + from apache_beam.ml.inference.pytorch import PytorchModelLoader |
| 42 | +except ImportError: |
| 43 | + raise unittest.SkipTest('PyTorch dependencies are not installed') |
| 44 | + |
| 45 | + |
| 46 | +def _compare_prediction_result(a, b): |
| 47 | + return ( |
| 48 | + torch.equal(a.inference, b.inference) and |
| 49 | + torch.equal(a.example, b.example)) |
| 50 | + |
| 51 | + |
| 52 | +class PytorchLinearRegression(torch.nn.Module): |
| 53 | + def __init__(self, input_dim, output_dim): |
| 54 | + super().__init__() |
| 55 | + self.linear = torch.nn.Linear(input_dim, output_dim) |
| 56 | + |
| 57 | + def forward(self, x): |
| 58 | + out = self.linear(x) |
| 59 | + return out |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.uses_pytorch |
| 63 | +class PytorchRunInferenceTest(unittest.TestCase): |
| 64 | + def setUp(self): |
| 65 | + self.tmpdir = tempfile.mkdtemp() |
| 66 | + |
| 67 | + def tearDown(self): |
| 68 | + shutil.rmtree(self.tmpdir) |
| 69 | + |
| 70 | + def test_inference_runner_single_tensor_feature(self): |
| 71 | + examples = [ |
| 72 | + torch.from_numpy(np.array([1], dtype="float32")), |
| 73 | + torch.from_numpy(np.array([5], dtype="float32")), |
| 74 | + torch.from_numpy(np.array([-3], dtype="float32")), |
| 75 | + torch.from_numpy(np.array([10.0], dtype="float32")), |
| 76 | + ] |
| 77 | + expected_predictions = [ |
| 78 | + PredictionResult(ex, pred) for ex, |
| 79 | + pred in zip( |
| 80 | + examples, |
| 81 | + torch.Tensor([example * 2.0 + 0.5 |
| 82 | + for example in examples]).reshape(-1, 1)) |
| 83 | + ] |
| 84 | + |
| 85 | + model = PytorchLinearRegression(input_dim=1, output_dim=1) |
| 86 | + model.load_state_dict( |
| 87 | + OrderedDict([('linear.weight', torch.Tensor([[2.0]])), |
| 88 | + ('linear.bias', torch.Tensor([0.5]))])) |
| 89 | + model.eval() |
| 90 | + |
| 91 | + inference_runner = PytorchInferenceRunner(torch.device('cpu')) |
| 92 | + predictions = inference_runner.run_inference(examples, model) |
| 93 | + for actual, expected in zip(predictions, expected_predictions): |
| 94 | + self.assertTrue(_compare_prediction_result(actual, expected)) |
| 95 | + |
| 96 | + def test_inference_runner_multiple_tensor_features(self): |
| 97 | + examples = torch.from_numpy( |
| 98 | + np.array([1, 5, 3, 10, -14, 0, 0.5, 0.5], |
| 99 | + dtype="float32")).reshape(-1, 2) |
| 100 | + examples = [ |
| 101 | + torch.from_numpy(np.array([1, 5], dtype="float32")), |
| 102 | + torch.from_numpy(np.array([3, 10], dtype="float32")), |
| 103 | + torch.from_numpy(np.array([-14, 0], dtype="float32")), |
| 104 | + torch.from_numpy(np.array([0.5, 0.5], dtype="float32")), |
| 105 | + ] |
| 106 | + expected_predictions = [ |
| 107 | + PredictionResult(ex, pred) for ex, |
| 108 | + pred in zip( |
| 109 | + examples, |
| 110 | + torch.Tensor([f1 * 2.0 + f2 * 3 + 0.5 |
| 111 | + for f1, f2 in examples]).reshape(-1, 1)) |
| 112 | + ] |
| 113 | + |
| 114 | + model = PytorchLinearRegression(input_dim=2, output_dim=1) |
| 115 | + model.load_state_dict( |
| 116 | + OrderedDict([('linear.weight', torch.Tensor([[2.0, 3]])), |
| 117 | + ('linear.bias', torch.Tensor([0.5]))])) |
| 118 | + model.eval() |
| 119 | + |
| 120 | + inference_runner = PytorchInferenceRunner(torch.device('cpu')) |
| 121 | + predictions = inference_runner.run_inference(examples, model) |
| 122 | + for actual, expected in zip(predictions, expected_predictions): |
| 123 | + self.assertTrue(_compare_prediction_result(actual, expected)) |
| 124 | + |
| 125 | + def test_num_bytes(self): |
| 126 | + inference_runner = PytorchInferenceRunner(torch.device('cpu')) |
| 127 | + examples = torch.from_numpy( |
| 128 | + np.array([1, 5, 3, 10, -14, 0, 0.5, 0.5], |
| 129 | + dtype="float32")).reshape(-1, 2) |
| 130 | + self.assertEqual((examples[0].element_size()) * 8, |
| 131 | + inference_runner.get_num_bytes(examples)) |
| 132 | + |
| 133 | + def test_namespace(self): |
| 134 | + inference_runner = PytorchInferenceRunner(torch.device('cpu')) |
| 135 | + self.assertEqual( |
| 136 | + 'RunInferencePytorch', inference_runner.get_metrics_namespace()) |
| 137 | + |
| 138 | + def test_pipeline_local_model(self): |
| 139 | + with TestPipeline() as pipeline: |
| 140 | + examples = torch.from_numpy( |
| 141 | + np.array([1, 5, 3, 10, -14, 0, 0.5, 0.5], |
| 142 | + dtype="float32")).reshape(-1, 2) |
| 143 | + expected_predictions = [ |
| 144 | + PredictionResult(ex, pred) for ex, |
| 145 | + pred in zip( |
| 146 | + examples, |
| 147 | + torch.Tensor([f1 * 2.0 + f2 * 3 + 0.5 |
| 148 | + for f1, f2 in examples]).reshape(-1, 1)) |
| 149 | + ] |
| 150 | + |
| 151 | + state_dict = OrderedDict([('linear.weight', torch.Tensor([[2.0, 3]])), |
| 152 | + ('linear.bias', torch.Tensor([0.5]))]) |
| 153 | + path = os.path.join(self.tmpdir, 'my_state_dict_path') |
| 154 | + torch.save(state_dict, path) |
| 155 | + |
| 156 | + model_loader = PytorchModelLoader( |
| 157 | + state_dict_path=path, |
| 158 | + model_class=PytorchLinearRegression(input_dim=2, output_dim=1)) |
| 159 | + |
| 160 | + pcoll = pipeline | 'start' >> beam.Create(examples) |
| 161 | + predictions = pcoll | RunInference(model_loader) |
| 162 | + assert_that( |
| 163 | + predictions, |
| 164 | + equal_to(expected_predictions, equals_fn=_compare_prediction_result)) |
| 165 | + |
| 166 | + def test_pipeline_gcs_model(self): |
| 167 | + with TestPipeline() as pipeline: |
| 168 | + examples = torch.from_numpy( |
| 169 | + np.array([1, 5, 3, 10], dtype="float32").reshape(-1, 1)) |
| 170 | + expected_predictions = [ |
| 171 | + PredictionResult(ex, pred) for ex, |
| 172 | + pred in zip( |
| 173 | + examples, |
| 174 | + torch.Tensor([example * 2.0 + 0.5 |
| 175 | + for example in examples]).reshape(-1, 1)) |
| 176 | + ] |
| 177 | + |
| 178 | + gs_pth = 'gs://apache-beam-ml/pytorch_lin_reg_model_2x+0.5_state_dict.pth' |
| 179 | + model_loader = PytorchModelLoader( |
| 180 | + state_dict_path=gs_pth, |
| 181 | + model_class=PytorchLinearRegression(input_dim=1, output_dim=1)) |
| 182 | + |
| 183 | + pcoll = pipeline | 'start' >> beam.Create(examples) |
| 184 | + predictions = pcoll | RunInference(model_loader) |
| 185 | + assert_that( |
| 186 | + predictions, |
| 187 | + equal_to(expected_predictions, equals_fn=_compare_prediction_result)) |
| 188 | + |
| 189 | + def test_invalid_input_type(self): |
| 190 | + with self.assertRaisesRegex(TypeError, "expected Tensor as element"): |
| 191 | + with TestPipeline() as pipeline: |
| 192 | + examples = np.array([1, 5, 3, 10], dtype="float32").reshape(-1, 1) |
| 193 | + |
| 194 | + state_dict = OrderedDict([('linear.weight', torch.Tensor([[2.0]])), |
| 195 | + ('linear.bias', torch.Tensor([0.5]))]) |
| 196 | + path = os.path.join(self.tmpdir, 'my_state_dict_path') |
| 197 | + torch.save(state_dict, path) |
| 198 | + |
| 199 | + model_loader = PytorchModelLoader( |
| 200 | + state_dict_path=path, |
| 201 | + model_class=PytorchLinearRegression(input_dim=1, output_dim=1)) |
| 202 | + |
| 203 | + pcoll = pipeline | 'start' >> beam.Create(examples) |
| 204 | + # pylint: disable=expression-not-assigned |
| 205 | + pcoll | RunInference(model_loader) |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == '__main__': |
| 209 | + unittest.main() |
0 commit comments