Skip to content

Commit 0f12211

Browse files
authored
Enhanced python_backend autocomplete (triton-inference-server#317)
* Added to python_backend autocomplete: optional input and model_transaction_policy
1 parent cba7ed3 commit 0f12211

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ class TritonPythonModel:
249249
inputs = [{
250250
'name': 'INPUT0',
251251
'data_type': 'TYPE_FP32',
252-
'dims': [4]
252+
'dims': [4],
253+
# this parameter will set `INPUT0 as an optional input`
254+
'optional': True
253255
}, {
254256
'name': 'INPUT1',
255257
'data_type': 'TYPE_FP32',
@@ -394,6 +396,23 @@ function to gain read-only access to the `pb_utils.ModelConfig` object.
394396
The `pb_utils.ModelConfig` object being returned from here will be used as the
395397
final configuration for the model.
396398

399+
In addition to minimal properties, you can also set [model_transaction_policy](
400+
https://github.com/triton-inference-server/server/blob/main/docs/user_guide/model_configuration.md#model-transaction-policy)
401+
through `auto_complete_config` using `set_model_transaction_policy`.
402+
For example,
403+
```python
404+
import triton_python_backend_utils as pb_utils
405+
406+
407+
class TritonPythonModel:
408+
@staticmethod
409+
def auto_complete_config(auto_complete_model_config):
410+
...
411+
transaction_policy = {"decoupled": True}
412+
auto_complete_model_config.set_model_transaction_policy(transaction_policy)
413+
...
414+
```
415+
397416
Note: The Python interpreter used to invoke this function will be destroyed
398417
upon returning from this function and as a result none of the objects
399418
created here will be available in the `initialize`, `execute`, or `finalize`

src/resources/triton_python_backend_utils.py

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,12 @@ def add_input(self, input):
381381
Raises
382382
------
383383
ValueError
384-
If input contains property other than 'name', 'data_type'
385-
and 'dims' or any of the properties are not set, or if an
386-
input with the same name already exists in the configuration
387-
but has different data_type or dims property
384+
If input contains property other than 'name', 'data_type',
385+
'dims', 'optional' or any of the non-optional properties
386+
are not set, or if an input with the same name already exists
387+
in the configuration but has different data_type or dims property
388388
"""
389-
valid_properties = ["name", "data_type", "dims"]
389+
valid_properties = ["name", "data_type", "dims", "optional"]
390390
for current_property in input:
391391
if current_property not in valid_properties:
392392
raise ValueError(
@@ -447,9 +447,26 @@ def add_input(self, input):
447447
+ " but the model configuration specifies dims "
448448
+ str(current_input["dims"])
449449
)
450+
elif (
451+
"optional" in current_input
452+
and "optional" in input
453+
and current_input["optional"] != input["optional"]
454+
):
455+
raise ValueError(
456+
"model '"
457+
+ self._model_config["name"]
458+
+ "', tensor '"
459+
+ input["name"]
460+
+ "': the model expects optional "
461+
+ str(input["optional"])
462+
+ " but the model configuration specifies optional "
463+
+ str(current_input["optional"])
464+
)
450465
else:
451466
current_input["data_type"] = input["data_type"]
452467
current_input["dims"] = input["dims"]
468+
if "optional" in input:
469+
current_input["optional"] = input["optional"]
453470
return
454471

455472
self._model_config["input"].append(input)
@@ -538,6 +555,53 @@ def add_output(self, output):
538555

539556
self._model_config["output"].append(output)
540557

558+
def set_model_transaction_policy(self, transaction_policy_dict):
559+
"""
560+
Set model transaction policy for the model.
561+
Parameters
562+
----------
563+
transaction_policy_dict : dict
564+
The dict, containing all properties to be set as a part
565+
of `model_transaction_policy` field.
566+
Raises
567+
------
568+
ValueError
569+
If transaction_policy_dict contains property other
570+
than 'decoupled', or if `model_transaction_policy` already exists
571+
in the configuration, but has different `decoupled` property.
572+
"""
573+
valid_properties = ["decoupled"]
574+
for current_property in transaction_policy_dict.keys():
575+
if current_property not in valid_properties:
576+
raise ValueError(
577+
"model transaction property in auto-complete-config "
578+
+ "function for model '"
579+
+ self._model_config["name"]
580+
+ "' contains property other than 'decoupled'."
581+
)
582+
583+
if "model_transaction_policy" not in self._model_config:
584+
self._model_config["model_transaction_policy"] = {}
585+
586+
if "decoupled" in transaction_policy_dict.keys():
587+
if (
588+
"decoupled" in self._model_config["model_transaction_policy"]
589+
and self._model_config["model_transaction_policy"]["decoupled"]
590+
!= transaction_policy_dict["decoupled"]
591+
):
592+
raise ValueError(
593+
"trying to change decoupled property in auto-complete-config "
594+
+ "for model '"
595+
+ self._model_config["name"]
596+
+ "', which is already set to '"
597+
+ str(self._model_config["model_transaction_policy"]["decoupled"])
598+
+ "'."
599+
)
600+
601+
self._model_config["model_transaction_policy"][
602+
"decoupled"
603+
] = transaction_policy_dict["decoupled"]
604+
541605

542606
TRITONSERVER_REQUEST_FLAG_SEQUENCE_START = 1
543607
TRITONSERVER_REQUEST_FLAG_SEQUENCE_END = 2

0 commit comments

Comments
 (0)