-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathtest_phishing.py
executable file
·154 lines (135 loc) · 6.89 KB
/
test_phishing.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
#!/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 mk_async_infer
from morpheus.config import Config
from morpheus.config import PipelineModes
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.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_nlp_stage import PreprocessNLPStage
from morpheus.utils.file_utils import load_labels_file
# End-to-end test intended to imitate the Phishing validation test
FEATURE_LENGTH = 128
MODEL_MAX_BATCH_SIZE = 32
@pytest.mark.slow
@pytest.mark.use_python
@mock.patch('tritonclient.grpc.InferenceServerClient')
def test_email_no_cpp(mock_triton_client: mock.MagicMock, config: Config, tmp_path: str, morpheus_log_level: int):
mock_metadata = {
"inputs": [{
"name": "input_ids", "datatype": "INT64", "shape": [-1, FEATURE_LENGTH]
}, {
"name": "attention_mask", "datatype": "INT64", "shape": [-1, FEATURE_LENGTH]
}],
"outputs": [{
"name": "output", "datatype": "FP32", "shape": [-1, 2]
}]
}
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_phishing_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.NLP
config.class_labels = load_labels_file(os.path.join(TEST_DIRS.data_dir, "labels_phishing.txt"))
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
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'phishing-email-validation-data.jsonlines')
vocab_file_name = os.path.join(TEST_DIRS.data_dir, 'bert-base-uncased-hash.txt')
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(
PreprocessNLPStage(config,
vocab_hash_file=vocab_file_name,
truncation=True,
do_lower_case=True,
add_special_tokens=False))
pipe.add_stage(
TritonInferenceStage(config, model_name='phishing-bert-onnx', server_url='test:0000',
force_convert_inputs=True))
pipe.add_stage(
MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf", log_level=morpheus_log_level))
pipe.add_stage(AddClassificationsStage(config, labels=["is_phishing"], threshold=0.7))
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()
results = calc_error_val(results_file_name)
assert results.diff_rows == 153
@pytest.mark.slow
@pytest.mark.use_cpp
@pytest.mark.usefixtures("launch_mock_triton")
def test_email_cpp(config: Config, tmp_path: str, morpheus_log_level: int):
config.mode = PipelineModes.NLP
config.class_labels = load_labels_file(os.path.join(TEST_DIRS.data_dir, "labels_phishing.txt"))
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
val_file_name = os.path.join(TEST_DIRS.validation_data_dir, 'phishing-email-validation-data.jsonlines')
vocab_file_name = os.path.join(TEST_DIRS.data_dir, 'bert-base-uncased-hash.txt')
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(
PreprocessNLPStage(config,
vocab_hash_file=vocab_file_name,
truncation=True,
do_lower_case=True,
add_special_tokens=False))
pipe.add_stage(
TritonInferenceStage(config,
model_name='phishing-bert-onnx',
server_url='localhost:8001',
force_convert_inputs=True))
pipe.add_stage(
MonitorStage(config, description="Inference Rate", smoothing=0.001, unit="inf", log_level=morpheus_log_level))
pipe.add_stage(AddClassificationsStage(config, labels=["is_phishing"], threshold=0.7))
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()
results = calc_error_val(results_file_name)
assert results.diff_rows == 682