|
| 1 | +# |
| 2 | +# Copyright (c) 2023, NVIDIA CORPORATION. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import shutil |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import pandas as pd |
| 21 | +import pytest |
| 22 | +import tritonclient.utils |
| 23 | + |
| 24 | +from merlin.schema import ColumnSchema, Schema |
| 25 | +from merlin.systems.dag.ensemble import Ensemble |
| 26 | +from merlin.systems.dag.ops.pytorch import PredictPyTorch |
| 27 | +from merlin.systems.triton.utils import run_ensemble_on_tritonserver |
| 28 | + |
| 29 | +torch = pytest.importorskip("torch") |
| 30 | + |
| 31 | +TRITON_SERVER_PATH = shutil.which("tritonserver") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.skipif(not TRITON_SERVER_PATH, reason="triton server not found") |
| 35 | +def test_model_in_ensemble(tmpdir): |
| 36 | + class MyModel(torch.nn.Module): |
| 37 | + def forward(self, x): |
| 38 | + v = torch.stack(list(x.values())).sum(axis=0) |
| 39 | + return v |
| 40 | + |
| 41 | + model = MyModel() |
| 42 | + |
| 43 | + traced_model = torch.jit.trace(model, {"a": torch.tensor(1), "b": torch.tensor(2)}, strict=True) |
| 44 | + |
| 45 | + model_input_schema = Schema( |
| 46 | + [ColumnSchema("a", dtype="int64"), ColumnSchema("b", dtype="int64")] |
| 47 | + ) |
| 48 | + model_output_schema = Schema([ColumnSchema("output", dtype="int64")]) |
| 49 | + |
| 50 | + model_node = model_input_schema.column_names >> PredictPyTorch( |
| 51 | + traced_model, model_input_schema, model_output_schema |
| 52 | + ) |
| 53 | + |
| 54 | + ensemble = Ensemble(model_node, model_input_schema) |
| 55 | + |
| 56 | + ensemble_config, _ = ensemble.export(str(tmpdir)) |
| 57 | + |
| 58 | + df = pd.DataFrame({"a": [1], "b": [2]}) |
| 59 | + |
| 60 | + response = run_ensemble_on_tritonserver( |
| 61 | + str(tmpdir), model_input_schema, df, ["output"], ensemble_config.name |
| 62 | + ) |
| 63 | + np.testing.assert_array_equal(response["output"], np.array([3])) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.skipif(not TRITON_SERVER_PATH, reason="triton server not found") |
| 67 | +def test_model_error(tmpdir): |
| 68 | + class MyModel(torch.nn.Module): |
| 69 | + def forward(self, x): |
| 70 | + v = torch.stack(list(x.values())).sum() |
| 71 | + return v |
| 72 | + |
| 73 | + model = MyModel() |
| 74 | + |
| 75 | + traced_model = torch.jit.trace(model, {"a": torch.tensor(1), "b": torch.tensor(2)}, strict=True) |
| 76 | + |
| 77 | + model_input_schema = Schema([ColumnSchema("a", dtype="int64")]) |
| 78 | + model_output_schema = Schema([ColumnSchema("output", dtype="int64")]) |
| 79 | + |
| 80 | + model_node = model_input_schema.column_names >> PredictPyTorch( |
| 81 | + traced_model, model_input_schema, model_output_schema |
| 82 | + ) |
| 83 | + |
| 84 | + ensemble = Ensemble(model_node, model_input_schema) |
| 85 | + |
| 86 | + ensemble_config, _ = ensemble.export(str(tmpdir)) |
| 87 | + |
| 88 | + # run inference with missing input (that was present when model was compiled) |
| 89 | + # we're expecting a KeyError at runtime. |
| 90 | + df = pd.DataFrame({"a": [1]}) |
| 91 | + |
| 92 | + with pytest.raises(tritonclient.utils.InferenceServerException) as exc_info: |
| 93 | + run_ensemble_on_tritonserver( |
| 94 | + str(tmpdir), model_input_schema, df, ["output"], ensemble_config.name |
| 95 | + ) |
| 96 | + assert "The following operation failed in the TorchScript interpreter" in str(exc_info.value) |
| 97 | + assert "RuntimeError: KeyError: b" in str(exc_info.value) |
0 commit comments