File size: 12,809 Bytes
e0c2d04 |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# Copyright 2022, Lefebvre Dalloz Services
#
# 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 inspect
import tempfile
from pathlib import Path
import pytest
from transformers import AutoConfig, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer
from transformer_deploy.t5_utils import t5_model
from transformer_deploy.triton.configuration import EngineType
from transformer_deploy.triton.configuration_decoder import ConfigurationDec
from transformer_deploy.triton.configuration_encoder import ConfigurationEnc
from transformer_deploy.triton.configuration_question_answering import ConfigurationQuestionAnswering
from transformer_deploy.triton.configuration_t5 import ConfigurationT5Decoder, ConfigurationT5Encoder
from transformer_deploy.triton.configuration_token_classifier import ConfigurationTokenClassifier
from transformer_deploy.utils import generative_model, python_tokenizer, question_answering, token_classifier
@pytest.fixture
def working_directory() -> tempfile.TemporaryDirectory:
return tempfile.TemporaryDirectory()
@pytest.fixture
def conf_encoder(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationEnc(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX # should be provided later...
return conf
@pytest.fixture
def conf_decoder(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationDec(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX # should be provided later...
return conf
@pytest.fixture
def conf_token_classifier(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationTokenClassifier(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX
return conf
@pytest.fixture
def conf_question_answering(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationQuestionAnswering(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX
return conf
@pytest.fixture
def conf_encoder_t5(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationT5Encoder(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX # should be provided later...
return conf
@pytest.fixture
def conf_decoder_t5(working_directory: tempfile.TemporaryDirectory):
conf = ConfigurationT5Decoder(
model_name_base="test",
dim_output=[-1, 2],
nb_instance=1,
tensor_input_names=["input_ids", "attention_mask"],
working_directory=working_directory.name,
device="cuda",
)
conf.engine_type = EngineType.ONNX # should be provided later...
return conf
def test_model_conf(conf_encoder, conf_decoder, conf_token_classifier):
expected = """
name: "test_onnx_model"
max_batch_size: 0
platform: "onnxruntime_onnx"
default_model_filename: "model.bin"
input [
{
name: "input_ids"
data_type: TYPE_INT32
dims: [-1, -1]
},
{
name: "attention_mask"
data_type: TYPE_INT32
dims: [-1, -1]
}
]
output {
name: "output"
data_type: TYPE_FP32
dims: [-1, 2]
}
instance_group [
{
count: 1
kind: KIND_GPU
}
]
""" # noqa: W293
assert expected.strip() == conf_encoder.get_model_conf()
assert expected.strip() == conf_decoder.get_model_conf()
assert expected.strip() == conf_token_classifier.get_model_conf()
def test_tokenizer_conf(conf_encoder):
expected = """
name: "test_onnx_tokenize"
max_batch_size: 0
backend: "python"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output [
{
name: "input_ids"
data_type: TYPE_INT32
dims: [-1, -1]
},
{
name: "attention_mask"
data_type: TYPE_INT32
dims: [-1, -1]
}
]
instance_group [
{
count: 1
kind: KIND_GPU
}
]
""" # noqa: W293
assert expected.strip() == conf_encoder.get_tokenize_conf()
def test_inference_conf(conf_encoder):
expected = """
name: "test_onnx_inference"
max_batch_size: 0
platform: "ensemble"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output {
name: "output"
data_type: TYPE_FP32
dims: [-1, 2]
}
ensemble_scheduling {
step [
{
model_name: "test_onnx_tokenize"
model_version: -1
input_map {
key: "TEXT"
value: "TEXT"
}
output_map [
{
key: "input_ids"
value: "input_ids"
},
{
key: "attention_mask"
value: "attention_mask"
}
]
},
{
model_name: "test_onnx_model"
model_version: -1
input_map [
{
key: "input_ids"
value: "input_ids"
},
{
key: "attention_mask"
value: "attention_mask"
}
]
output_map {
key: "output"
value: "output"
}
}
]
}
""" # noqa: W293
assert expected.strip() == conf_encoder.get_inference_conf()
def test_generate_conf(conf_decoder):
expected = """
name: "test_onnx_generate"
max_batch_size: 0
backend: "python"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output [
{
name: "output"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
instance_group [
{
count: 1
kind: KIND_GPU
}
]
parameters: {
key: "FORCE_CPU_ONLY_INPUT_TENSORS"
value: {
string_value:"no"
}
}
""" # noqa: W293
print(conf_decoder.get_generation_conf())
assert expected.strip() == conf_decoder.get_generation_conf()
def test_token_classifier_inference_conf(conf_token_classifier):
expected = """
name: "test_onnx_inference"
max_batch_size: 0
backend: "python"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output [
{
name: "output"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
instance_group [
{
count: 1
kind: KIND_GPU
}
]
parameters: {
key: "FORCE_CPU_ONLY_INPUT_TENSORS"
value: {
string_value:"no"
}
}
"""
assert expected.strip() == conf_token_classifier.get_inference_conf()
def test_question_answering_inference_conf(conf_question_answering):
expected = """
name: "test_onnx_inference"
max_batch_size: 0
backend: "python"
input [
{
name: "QUESTION"
data_type: TYPE_STRING
dims: [ -1 ]
},
{
name: "CONTEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output [
{
name: "output"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
instance_group [
{
count: 1
kind: KIND_GPU
}
]
parameters: {
key: "FORCE_CPU_ONLY_INPUT_TENSORS"
value: {
string_value:"no"
}
}
"""
assert expected.strip() == conf_question_answering.get_inference_conf()
def test_t5_encoder_inference_conf(conf_encoder_t5):
expected = """
name: "test_onnx_inference"
max_batch_size: 0
platform: "ensemble"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output {
name: "output"
data_type: TYPE_FP32
dims: [-1, 2]
}
ensemble_scheduling {
step [
{
model_name: "test_onnx_tokenize"
model_version: -1
input_map {
key: "TEXT"
value: "TEXT"
}
output_map [
{
key: "input_ids"
value: "input_ids"
},
{
key: "attention_mask"
value: "attention_mask"
}
]
},
{
model_name: "test_onnx_model"
model_version: -1
input_map [
{
key: "input_ids"
value: "input_ids"
},
{
key: "attention_mask"
value: "attention_mask"
}
]
output_map {
key: "output"
value: "output"
}
}
]
}
"""
assert expected.strip() == conf_encoder_t5.get_inference_conf()
def test_t5_decoder_generate_conf(conf_decoder_t5):
expected = """
name: "t5_model_generate"
max_batch_size: 0
backend: "python"
input [
{
name: "TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
]
output {
name: "OUTPUT_TEXT"
data_type: TYPE_STRING
dims: [ -1 ]
}
instance_group [
{
count: 1
kind: KIND_GPU
}
]
parameters: {
key: "FORCE_CPU_ONLY_INPUT_TENSORS"
value: {
string_value:"no"
}
}
"""
assert expected.strip() == conf_decoder_t5.get_generation_conf()
def test_create_folders(
conf_encoder,
conf_decoder,
conf_token_classifier,
conf_question_answering,
conf_encoder_t5,
conf_decoder_t5,
working_directory: tempfile.TemporaryDirectory,
):
fake_model_path = Path(working_directory.name).joinpath("fake_model.bin")
fake_model_path.write_bytes(b"abc")
for conf, paths, python_code in [
(
conf_encoder,
[
conf_encoder.model_folder_name,
conf_encoder.python_folder_name,
conf_encoder.inference_folder_name,
],
python_tokenizer,
),
(
conf_decoder,
[
conf_decoder.model_folder_name,
conf_decoder.python_folder_name,
conf_decoder.inference_folder_name,
],
generative_model,
),
(
conf_token_classifier,
[
conf_token_classifier.model_folder_name,
conf_token_classifier.python_folder_name,
conf_token_classifier.inference_folder_name,
],
token_classifier,
),
(
conf_question_answering,
[
conf_question_answering.model_folder_name,
conf_question_answering.python_folder_name,
conf_question_answering.inference_folder_name,
],
question_answering,
),
(
conf_encoder_t5,
[
conf_encoder_t5.model_folder_name,
conf_encoder_t5.python_folder_name,
conf_encoder_t5.inference_folder_name,
],
t5_model,
),
(
conf_decoder_t5,
[
conf_decoder_t5.model_folder_name,
conf_decoder_t5.python_folder_name,
],
t5_model,
),
]:
model_name = (
"t5-small"
if type(conf) in [ConfigurationT5Decoder, ConfigurationT5Encoder]
else "philschmid/MiniLM-L6-H384-uncased-sst2"
)
tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(model_name)
config: PretrainedConfig = AutoConfig.from_pretrained(model_name)
conf.create_configs(tokenizer=tokenizer, config=config, model_path=fake_model_path, engine_type=EngineType.ONNX)
for folder_name in paths:
path = Path(conf.working_dir).joinpath(folder_name)
assert path.joinpath("config.pbtxt").exists()
assert path.joinpath("config.pbtxt").read_text() != ""
assert path.joinpath("1").exists()
model_path = Path(conf.working_dir).joinpath(conf.python_folder_name).joinpath("1").joinpath("model.py")
assert model_path.exists()
if type(conf) not in [ConfigurationT5Decoder, ConfigurationT5Encoder]:
assert model_path.read_text() == inspect.getsource(python_code)
|