forked from nv-morpheus/Morpheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_abp.py
executable file
·293 lines (229 loc) · 12 KB
/
test_abp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
# SPDX-FileCopyrightText: Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed 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.
import os
from unittest import mock
import numpy as np
import pytest
from _utils import TEST_DIRS
from _utils import calc_error_val
from _utils import compare_class_to_scores
from _utils import mk_async_infer
from morpheus.config import Config
from morpheus.config import ConfigFIL
from morpheus.config import PipelineModes
from morpheus.messages import MessageMeta
from morpheus.messages import MultiInferenceMessage
from morpheus.messages import MultiMessage
from morpheus.messages import MultiResponseMessage
from morpheus.pipeline import LinearPipeline
from morpheus.stages.general.monitor_stage import MonitorStage
from morpheus.stages.inference.triton_inference_stage import TritonInferenceStage
from morpheus.stages.input.file_source_stage import FileSourceStage
from morpheus.stages.output.write_to_file_stage import WriteToFileStage
from morpheus.stages.postprocess.add_classifications_stage import AddClassificationsStage
from morpheus.stages.postprocess.add_scores_stage import AddScoresStage
from morpheus.stages.postprocess.serialize_stage import SerializeStage
from morpheus.stages.postprocess.validation_stage import ValidationStage
from morpheus.stages.preprocess.deserialize_stage import DeserializeStage
from morpheus.stages.preprocess.preprocess_fil_stage import PreprocessFILStage
from morpheus.utils.file_utils import load_labels_file
# End-to-end test intended to imitate the ABP validation test
FEATURE_LENGTH = 18
MODEL_MAX_BATCH_SIZE = 1024
@pytest.mark.slow
@pytest.mark.use_python
@mock.patch('tritonclient.grpc.InferenceServerClient')
def test_abp_no_cpp(mock_triton_client, config: Config, tmp_path):
mock_metadata = {
"inputs": [{
'name': 'input__0', 'datatype': 'FP32', "shape": [-1, FEATURE_LENGTH]
}],
"outputs": [{
'name': 'output__0', 'datatype': 'FP32', 'shape': ['-1', '1']
}]
}
mock_model_config = {"config": {"max_batch_size": MODEL_MAX_BATCH_SIZE}}
mock_triton_client.return_value = mock_triton_client
mock_triton_client.is_server_live.return_value = True
mock_triton_client.is_server_ready.return_value = True
mock_triton_client.is_model_ready.return_value = True
mock_triton_client.get_model_metadata.return_value = mock_metadata
mock_triton_client.get_model_config.return_value = mock_model_config
data = np.loadtxt(os.path.join(TEST_DIRS.tests_data_dir, 'triton_abp_inf_results.csv'), delimiter=',')
inf_results = np.split(data, range(MODEL_MAX_BATCH_SIZE, len(data), MODEL_MAX_BATCH_SIZE))
async_infer = mk_async_infer(inf_results)
mock_triton_client.async_infer.side_effect = async_infer
config.mode = PipelineModes.FIL
config.class_labels = ["mining"]
config.model_max_batch_size = MODEL_MAX_BATCH_SIZE
config.pipeline_batch_size = 1024
config.feature_length = FEATURE_LENGTH
config.edge_buffer_size = 128
config.num_threads = 1
config.fil = ConfigFIL()
config.fil.feature_columns = load_labels_file(os.path.join(TEST_DIRS.data_dir, 'columns_fil.txt'))
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'abp-validation-data.jsonlines')
out_file = os.path.join(tmp_path, 'results.csv')
results_file_name = os.path.join(tmp_path, 'results.json')
pipe = LinearPipeline(config)
pipe.set_source(FileSourceStage(config, filename=val_file_name, iterative=False))
pipe.add_stage(DeserializeStage(config))
pipe.add_stage(PreprocessFILStage(config))
pipe.add_stage(
TritonInferenceStage(config, model_name='abp-nvsmi-xgb', server_url='test:0000', force_convert_inputs=True))
pipe.add_stage(MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf"))
pipe.add_stage(AddClassificationsStage(config))
pipe.add_stage(AddScoresStage(config, prefix="score_"))
pipe.add_stage(
ValidationStage(config, val_file_name=val_file_name, results_file_name=results_file_name, rel_tol=0.05))
pipe.add_stage(SerializeStage(config))
pipe.add_stage(WriteToFileStage(config, filename=out_file, overwrite=False))
pipe.run()
compare_class_to_scores(out_file, config.class_labels, '', 'score_', threshold=0.5)
results = calc_error_val(results_file_name)
assert results.diff_rows == 0
@pytest.mark.slow
@pytest.mark.use_cpp
@pytest.mark.usefixtures("launch_mock_triton")
def test_abp_cpp(config, tmp_path):
config.mode = PipelineModes.FIL
config.class_labels = ["mining"]
config.model_max_batch_size = MODEL_MAX_BATCH_SIZE
config.pipeline_batch_size = 1024
config.feature_length = FEATURE_LENGTH
config.edge_buffer_size = 128
config.num_threads = 1
config.fil = ConfigFIL()
config.fil.feature_columns = load_labels_file(os.path.join(TEST_DIRS.data_dir, 'columns_fil.txt'))
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'abp-validation-data.jsonlines')
out_file = os.path.join(tmp_path, 'results.csv')
results_file_name = os.path.join(tmp_path, 'results.json')
pipe = LinearPipeline(config)
pipe.set_source(FileSourceStage(config, filename=val_file_name, iterative=False))
pipe.add_stage(DeserializeStage(config))
pipe.add_stage(PreprocessFILStage(config))
# We are feeding TritonInferenceStage the port to the grpc server because that is what the validation tests do
# but the code under-the-hood replaces this with the port number of the http server
pipe.add_stage(
TritonInferenceStage(config, model_name='abp-nvsmi-xgb', server_url='localhost:8001',
force_convert_inputs=True))
pipe.add_stage(MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf"))
pipe.add_stage(AddClassificationsStage(config))
pipe.add_stage(AddScoresStage(config, prefix="score_"))
pipe.add_stage(
ValidationStage(config, val_file_name=val_file_name, results_file_name=results_file_name, rel_tol=0.05))
pipe.add_stage(SerializeStage(config))
pipe.add_stage(WriteToFileStage(config, filename=out_file, overwrite=False))
pipe.run()
compare_class_to_scores(out_file, config.class_labels, '', 'score_', threshold=0.5)
results = calc_error_val(results_file_name)
assert results.diff_rows == 0
@pytest.mark.slow
@pytest.mark.use_python
@mock.patch('tritonclient.grpc.InferenceServerClient')
def test_abp_multi_segment_no_cpp(mock_triton_client, config: Config, tmp_path):
mock_metadata = {
"inputs": [{
'name': 'input__0', 'datatype': 'FP32', "shape": [-1, FEATURE_LENGTH]
}],
"outputs": [{
'name': 'output__0', 'datatype': 'FP32', 'shape': ['-1', '1']
}]
}
mock_model_config = {"config": {"max_batch_size": MODEL_MAX_BATCH_SIZE}}
mock_triton_client.return_value = mock_triton_client
mock_triton_client.is_server_live.return_value = True
mock_triton_client.is_server_ready.return_value = True
mock_triton_client.is_model_ready.return_value = True
mock_triton_client.get_model_metadata.return_value = mock_metadata
mock_triton_client.get_model_config.return_value = mock_model_config
data = np.loadtxt(os.path.join(TEST_DIRS.tests_data_dir, 'triton_abp_inf_results.csv'), delimiter=',')
inf_results = np.split(data, range(MODEL_MAX_BATCH_SIZE, len(data), MODEL_MAX_BATCH_SIZE))
async_infer = mk_async_infer(inf_results)
mock_triton_client.async_infer.side_effect = async_infer
config.mode = PipelineModes.FIL
config.class_labels = ["mining"]
config.model_max_batch_size = MODEL_MAX_BATCH_SIZE
config.pipeline_batch_size = 1024
config.feature_length = FEATURE_LENGTH
config.edge_buffer_size = 128
config.num_threads = 1
config.fil = ConfigFIL()
config.fil.feature_columns = load_labels_file(os.path.join(TEST_DIRS.data_dir, 'columns_fil.txt'))
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'abp-validation-data.jsonlines')
out_file = os.path.join(tmp_path, 'results.csv')
results_file_name = os.path.join(tmp_path, 'results.json')
pipe = LinearPipeline(config)
pipe.set_source(FileSourceStage(config, filename=val_file_name, iterative=False))
pipe.add_stage(DeserializeStage(config))
pipe.add_segment_boundary(MultiMessage) # Boundary 1
pipe.add_stage(PreprocessFILStage(config))
pipe.add_segment_boundary(MultiInferenceMessage) # Boundary 2
pipe.add_stage(
TritonInferenceStage(config, model_name='abp-nvsmi-xgb', server_url='test:0000', force_convert_inputs=True))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 3
pipe.add_stage(MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf"))
pipe.add_stage(AddClassificationsStage(config))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 4
pipe.add_stage(
ValidationStage(config, val_file_name=val_file_name, results_file_name=results_file_name, rel_tol=0.05))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 5
pipe.add_stage(SerializeStage(config))
pipe.add_segment_boundary(MessageMeta) # Boundary 6
pipe.add_stage(WriteToFileStage(config, filename=out_file, overwrite=False))
pipe.run()
results = calc_error_val(results_file_name)
assert results.diff_rows == 0
@pytest.mark.slow
@pytest.mark.use_cpp
@pytest.mark.usefixtures("launch_mock_triton")
def test_abp_multi_segment_cpp(config, tmp_path):
config.mode = PipelineModes.FIL
config.class_labels = ["mining"]
config.model_max_batch_size = MODEL_MAX_BATCH_SIZE
config.pipeline_batch_size = 1024
config.feature_length = FEATURE_LENGTH
config.edge_buffer_size = 128
config.num_threads = 1
config.fil = ConfigFIL()
config.fil.feature_columns = load_labels_file(os.path.join(TEST_DIRS.data_dir, 'columns_fil.txt'))
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'abp-validation-data.jsonlines')
out_file = os.path.join(tmp_path, 'results.csv')
results_file_name = os.path.join(tmp_path, 'results.json')
pipe = LinearPipeline(config)
pipe.set_source(FileSourceStage(config, filename=val_file_name, iterative=False))
pipe.add_stage(DeserializeStage(config))
pipe.add_segment_boundary(MultiMessage) # Boundary 1
pipe.add_stage(PreprocessFILStage(config))
pipe.add_segment_boundary(MultiInferenceMessage) # Boundary 2
# We are feeding TritonInferenceStage the port to the grpc server because that is what the validation tests do
# but the code under-the-hood replaces this with the port number of the http server
pipe.add_stage(
TritonInferenceStage(config, model_name='abp-nvsmi-xgb', server_url='localhost:8001',
force_convert_inputs=True))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 3
pipe.add_stage(MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf"))
pipe.add_stage(AddClassificationsStage(config))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 4
pipe.add_stage(
ValidationStage(config, val_file_name=val_file_name, results_file_name=results_file_name, rel_tol=0.05))
pipe.add_segment_boundary(MultiResponseMessage) # Boundary 5
pipe.add_stage(SerializeStage(config))
pipe.add_segment_boundary(MessageMeta) # Boundary 6
pipe.add_stage(WriteToFileStage(config, filename=out_file, overwrite=False))
pipe.run()
results = calc_error_val(results_file_name)
assert results.diff_rows == 0