rszper commented on code in PR #22250: URL: https://github.com/apache/beam/pull/22250#discussion_r921344872
########## sdks/python/apache_beam/examples/snippets/transforms/elementwise/runinference_test.py: ########## @@ -0,0 +1,93 @@ +# coding=utf-8 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# pytype: skip-file + +import unittest +from io import StringIO + +import mock + +from apache_beam.examples.snippets.util import assert_matches_stdout +from apache_beam.testing.test_pipeline import TestPipeline + +from . import runinference + +def check_torch_keyed_model_handler(actual): + expected = '''[START torch_keyed_model_handler] +('first_question', PredictionResult(example=tensor([105.]), inference=tensor([523.6982], grad_fn=<UnbindBackward0>))) +('second_question', PredictionResult(example=tensor([108.]), inference=tensor([538.5867], grad_fn=<UnbindBackward0>))) +('third_question', PredictionResult(example=tensor([1000.]), inference=tensor([4965.4019], grad_fn=<UnbindBackward0>))) +('fourth_question', PredictionResult(example=tensor([1013.]), inference=tensor([5029.9180], grad_fn=<UnbindBackward0>))) +[END torch_keyed_model_handler]'''.splitlines()[1:-1] + assert_matches_stdout(actual, expected) + + +def check_sklearn_keyed_model_handler(actual): + expected = '''[START sklearn_keyed_model_handler] +('first_question', PredictionResult(example=[105.0], inference=array([525.]))) +('second_question', PredictionResult(example=[108.0], inference=array([540.]))) +('third_question', PredictionResult(example=[1000.0], inference=array([5000.]))) +('fourth_question', PredictionResult(example=[1013.0], inference=array([5065.]))) +[END sklearn_keyed_model_handler] '''.splitlines()[1:-1] + assert_matches_stdout(actual, expected) + + +def check_torch_unkeyed_model_handler(actual): + expected = '''[START torch_unkeyed_model_handler] +PredictionResult(example=tensor([10.]), inference=tensor([52.2325], grad_fn=<UnbindBackward0>)) +PredictionResult(example=tensor([40.]), inference=tensor([201.1165], grad_fn=<UnbindBackward0>)) +PredictionResult(example=tensor([60.]), inference=tensor([300.3724], grad_fn=<UnbindBackward0>)) +PredictionResult(example=tensor([90.]), inference=tensor([449.2563], grad_fn=<UnbindBackward0>)) +[END torch_unkeyed_model_handler] '''.splitlines()[1:-1] + assert_matches_stdout(actual, expected) + + +def check_sklearn_unkeyed_model_handler(actual): + expected = '''[START sklearn_unkeyed_model_handler] +PredictionResult(example=array([20.], dtype=float32), inference=array([100.], dtype=float32)) +PredictionResult(example=array([40.], dtype=float32), inference=array([200.], dtype=float32)) +PredictionResult(example=array([60.], dtype=float32), inference=array([300.], dtype=float32)) +PredictionResult(example=array([90.], dtype=float32), inference=array([450.], dtype=float32)) +[END sklearn_unkeyed_model_handler] '''.splitlines()[1:-1] + assert_matches_stdout(actual, expected) + [email protected]('apache_beam.Pipeline', TestPipeline) [email protected]( + 'apache_beam.examples.snippets.transforms.elementwise.runinference.print', str) +class RunInferenceTest(unittest.TestCase): + def test_torch_unkeyed_model_handler(self): + runinference.torch_unkeyed_model_handler(check_torch_unkeyed_model_handler) + + def test_torch_keyed_model_handler(self): + runinference.torch_keyed_model_handler(check_torch_keyed_model_handler) + + def test_sklearn_unkeyed_model_handler(self): + runinference.sklearn_unkeyed_model_handler(check_sklearn_unkeyed_model_handler) + + def test_sklearn_keyed_model_handler(self): + runinference.sklearn_keyed_model_handler(check_sklearn_keyed_model_handler) + + def test_images(self): + runinference.images(check_images) + + def test_digits(self): + runinference.digits(check_digits) + Review Comment: Thanks. I missed that when I removed everything else. -- 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]
