serve.torch_serve
PyTorchServer
Bases: GradioUserInference
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
__init__(server_config)
The init function is called when the class is instantiated. It sets up the instance of the class, and defines all its attributes. The init function can accept arguments, which are passed at instantiation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required | |
server_config |
PyTorchServerConfig
|
PyTorchServerConfig: Pass the configuration parameters to the class |
required |
Returns:
Type | Description |
---|---|
The app, which is a fastapi object |
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
end()
The end function is used to stop the server. It will wait for the process to end before returning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
Type | Description |
---|---|
A boolean value |
Source code in src/python/easydel/serve/torch_serve.py
503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
fire()
The fire function starts the uvicorn server in a separate process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
Type | Description |
---|---|
A process that runs the uvicorn server |
Source code in src/python/easydel/serve/torch_serve.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
format_chat(history, prompt, system)
Here you will get the system, prompt and history from user, and you can apply your prompting style
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
format_instruct(system, instruction)
Here you will get the system and instruction from user, and you can apply your prompting style
Source code in src/python/easydel/serve/torch_serve.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
forward_chat_fast_api(data)
The forward_chat_fast_api function is a ReST API endpoint that takes in a ChatRequest object and returns the response from the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the object itself |
required | |
data |
ChatRequest
|
ChatRequest: Pass the data from the serve_engine to the function |
required |
Returns:
Type | Description |
---|---|
A dictionary with a single key, response |
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
forward_instruct_fast_api(data)
The forward_instruct_fast_api function is a ReST API endpoint that takes in an InstructRequest object and returns a response. The InstructRequest object contains the following fields: - system (str): A string representing the name of the system to be instructed. This should match one of the systems defined in your server_config file, or else it will default to "default". If you want to instruct multiple systems at once, use forward_instruct_fast instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the object itself |
required | |
data |
InstructRequest
|
InstructRequest: Pass in the data that is used to generate the response |
required |
Returns:
Type | Description |
---|---|
A dictionary with a single key, response |
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
get_gpu_memory(num_gpus_req=None)
staticmethod
The get_gpu_memory function returns the amount of available GPU memory in GB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_gpus_req |
Specify the number of gpus to be used |
None
|
Returns:
Type | Description |
---|---|
The amount of free memory on each gpu |
Source code in src/python/easydel/serve/torch_serve.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
get_model_load_kwargs()
The get_model_load_kwargs function is used to set the torch_dtype, device_map and max_memory parameters for loading a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Bind the method to an object |
required |
Returns:
Type | Description |
---|---|
A dictionary with the following keys: |
Source code in src/python/easydel/serve/torch_serve.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
load(pretrained_model_name_or_path, tokenizer_repo=None, auto_config=True, **kwargs)
The load function is used to load a model from the HuggingFace Model Hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required | |
pretrained_model_name_or_path |
str
|
str: Specify the name of the model to be loaded |
required |
tokenizer_repo |
str
|
str: Specify the repo id of the tokenizer |
None
|
auto_config |
bool
|
bool: Determine whether the model should be loaded with a server_config file or not |
True
|
kwargs |
Pass a variable number of keyword arguments to the function |
{}
|
Returns:
Type | Description |
---|---|
A tuple of model and tokenizer |
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
sample(string, max_new_tokens=None, max_sequence_length=None, temperature=0.6, top_k=50, top_p=0.9, repetition_penalty=1.2, stream=True, sample=True)
The sample function is the main function of this class. It takes a string as input and returns a generator that yields strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required | |
string |
str
|
str: Pass the string to be generated |
required |
max_new_tokens |
Optional[int]
|
Optional[int]: Limit the number of new tokens that can be generated |
None
|
max_sequence_length |
Optional[int]
|
Optional[int]: Set the maximum length of the generated text |
None
|
temperature |
Optional[float]
|
Optional[float]: Control the randomness of the text generation |
0.6
|
top_k |
Optional[int]
|
Optional[int]: Filter out the top k tokens with the highest probability |
50
|
top_p |
Optional[float]
|
Optional[int]: Control the probability of sampling from the top n tokens |
0.9
|
repetition_penalty |
Optional[float]
|
optional[float]: repetition penalty for generation |
1.2
|
stream |
bool
|
bool: Determine whether to stream the output or not |
True
|
sample |
bool
|
optional[bool]: Indicate whether to sample from the distribution or take the argmax |
True
|
Returns:
Type | Description |
---|---|
A generator |
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
status()
The status function returns a dictionary with the following keys: server_config: A dictionary of configuration parameters. devices: The number of GPUs available to the server. device_sharding: Whether device sharding is enabled. If True, then each request will be served by a different GPU (if multiple GPUs are available). If False, then all requests will be served by the same GPU (or CPU if no GPUs are available). This parameter can also be set in your client"s initialization function via torch-serve"s DeviceShardingStrategy class. See https://pytorch-lightning.readthedoc
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
Type | Description |
---|---|
A dictionary with the following keys: |
Source code in src/python/easydel/serve/torch_serve.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
PyTorchServerConfig
dataclass
It sets up the instance of the class, and defines all its attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host |
str
|
Specify the ip address of the server |
'0.0.0.0'
|
port |
int
|
Specify the port number that will be used by the server |
2059
|
batch_size |
int
|
Determine the number of samples to be generated in a single batch |
1
|
max_sequence_length |
int
|
Set the maximum length of a sentence |
4096
|
max_new_tokens |
int
|
Limit the number of new tokens that can be generated in a single batch |
4096
|
temperature |
float
|
Control the randomness of the generated text |
0.8
|
pad_token_id |
Optional[int]
|
Optional[int]: The id of the Padding Token |
None
|
bos_token_id |
Optional[int]
|
Optional[int]: The id of the Start of sentence Token |
None
|
eos_token_id |
Optional[int]
|
Optional[int]: The id of the End of sentence Token |
None
|
top_p |
float
|
Control the probability of sampling from the top candidates |
0.95
|
top_k |
int
|
Limit the number of tokens that are considered for each token |
50
|
logging |
bool
|
Control whether the server will print out |
True
|
dtype |
str
|
Specify the data type of the tensors |
'fp16'
|
max_number_of_gpus |
Optional[int]
|
Limit the number of gpus used by the server |
None
|
max_gpu_perc_to_use |
float
|
Specify the maximum percentage of gpu memory that can be used by the server |
0.95
|
max_compile_tokens |
int
|
int: Limit the number of tokens that can be streamed to a single client |
1
|
Source code in src/python/easydel/serve/torch_serve.py
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 |
|
__repr__()
The repr function is used to generate a string representation of an object. This function should return a string that can be parsed by the Python interpreter to recreate the object. The repr function is called when you use print() on an object, or when you type its name in the REPL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the instance of the class |
required |
Returns:
Type | Description |
---|---|
A string representation of the object |
Source code in src/python/easydel/serve/torch_serve.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
__str__()
The str function is called when you use the print function or when str() is used. It should return a string representation of the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the instance of the class |
required |
Returns:
Type | Description |
---|---|
The object's string representation |
Source code in src/python/easydel/serve/torch_serve.py
96 97 98 99 100 101 102 103 104 105 |
|