tf.experimental.tensorrt.Converter
Stay organized with collections
Save and categorize content based on your preferences.
An offline converter for TF-TRT transformation for TF 2.0 SavedModels.
tf.experimental.tensorrt.Converter(
input_saved_model_dir=None, input_saved_model_tags=None,
input_saved_model_signature_key=None,
conversion_params=DEFAULT_TRT_CONVERSION_PARAMS
)
Currently this is not available on Windows platform.
Note that in V2, is_dynamic_op=False is not supported, meaning TRT engines
will be built only when the corresponding TRTEngineOp is executed. But we
still provide a way to avoid the cost of building TRT engines during inference
(see more below).
There are several ways to run the conversion:
- FP32/FP16 precision
params = DEFAULT_TRT_CONVERSION_PARAMS._replace(
precision_mode='FP16')
converter = tf.experimental.tensorrt.Converter(
input_saved_model_dir="my_dir", conversion_params=params)
converter.convert()
converter.save(output_saved_model_dir)
In this case, no TRT engines will be built or saved in the converted
SavedModel. But if input data is available during conversion, we can still
build and save the TRT engines to reduce the cost during inference (see
option 2 below).
- FP32/FP16 precision with pre-built engines
params = DEFAULT_TRT_CONVERSION_PARAMS._replace(
precision_mode='FP16',
# Set this to a large enough number so it can cache all the engines.
maximum_cached_engines=16)
converter = tf.experimental.tensorrt.Converter(
input_saved_model_dir="my_dir", conversion_params=params)
converter.convert()
# Define a generator function that yields input data, and use it to execute
# the graph to build TRT engines.
# With TensorRT 5.1, different engines will be built (and saved later) for
# different input shapes to the TRTEngineOp.
def my_input_fn():
for _ in range(num_runs):
inp1, inp2 = ...
yield inp1, inp2
converter.build(input_fn=my_input_fn) # Generate corresponding TRT engines
converter.save(output_saved_model_dir) # Generated engines will be saved.
In this way, one engine will be built/saved for each unique input shapes of
the TRTEngineOp. This is good for applications that cannot afford building
engines during inference but have access to input data that is similar to
the one used in production (for example, that has the same input shapes).
Also, the generated TRT engines is platform dependent, so we need to run
build()
in an environment that is similar to production (e.g. with
same type of GPU).
- INT8 precision and calibration with pre-built engines
params = DEFAULT_TRT_CONVERSION_PARAMS._replace(
precision_mode='INT8',
# Currently only one INT8 engine is supported in this mode.
maximum_cached_engines=1,
use_calibration=True)
converter = tf.experimental.tensorrt.Converter(
input_saved_model_dir="my_dir", conversion_params=params)
# Define a generator function that yields input data, and run INT8
# calibration with the data. All input data should have the same shape.
# At the end of convert(), the calibration stats (e.g. range information)
# will be saved and can be used to generate more TRT engines with different
# shapes. Also, one TRT engine will be generated (with the same shape as
# the calibration data) for save later.
def my_calibration_input_fn():
for _ in range(num_runs):
inp1, inp2 = ...
yield inp1, inp2
converter.convert(calibration_input_fn=my_calibration_input_fn)
# (Optional) Generate more TRT engines offline (same as the previous
# option), to avoid the cost of generating them during inference.
def my_input_fn():
for _ in range(num_runs):
inp1, inp2 = ...
yield inp1, inp2
converter.build(input_fn=my_input_fn)
# Save the TRT engine and the engines.
converter.save(output_saved_model_dir)
Args |
input_saved_model_dir
|
the directory to load the SavedModel which contains
the input graph to transforms. Used only when input_graph_def is None.
|
input_saved_model_tags
|
list of tags to load the SavedModel.
|
input_saved_model_signature_key
|
the key of the signature to optimize the
graph for.
|
conversion_params
|
a TrtConversionParams instance.
|
Raises |
ValueError
|
if the combination of the parameters is invalid.
|
Methods
build
View source
build(
input_fn
)
Run inference with converted graph in order to build TensorRT engines.
Args |
input_fn
|
a generator function that yields input data as a list or tuple,
which will be used to execute the converted signature to generate TRT
engines.
Example:
def input_fn():
yield input1, input2, input3
|
convert
View source
convert(
calibration_input_fn=None
)
Convert the input SavedModel in 2.0 format.
Args |
calibration_input_fn
|
a generator function that yields input data as a
list or tuple, which will be used to execute the converted signature for
calibration. All the returned input data should have the same shape.
Example:
def input_fn():
yield input1, input2, input3
|
Raises |
ValueError
|
if the input combination is invalid.
|
Returns |
The TF-TRT converted Function.
|
save
View source
save(
output_saved_model_dir
)
Save the converted SavedModel.
Args |
output_saved_model_dir
|
directory to saved the converted SavedModel.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.experimental.tensorrt.Converter\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/compiler/tensorrt/trt_convert.py#L784-L1091) |\n\nAn offline converter for TF-TRT transformation for TF 2.0 SavedModels. \n\n tf.experimental.tensorrt.Converter(\n input_saved_model_dir=None, input_saved_model_tags=None,\n input_saved_model_signature_key=None,\n conversion_params=DEFAULT_TRT_CONVERSION_PARAMS\n )\n\nCurrently this is not available on Windows platform.\n\nNote that in V2, is_dynamic_op=False is not supported, meaning TRT engines\nwill be built only when the corresponding TRTEngineOp is executed. But we\nstill provide a way to avoid the cost of building TRT engines during inference\n(see more below).\n\nThere are several ways to run the conversion:\n\n1. FP32/FP16 precision\n\n\u003e params = DEFAULT_TRT_CONVERSION_PARAMS._replace(\n\u003e precision_mode='FP16')\n\u003e converter = tf.experimental.tensorrt.Converter(\n\u003e input_saved_model_dir=\"my_dir\", conversion_params=params)\n\u003e converter.convert()\n\u003e converter.save(output_saved_model_dir)\n\nIn this case, no TRT engines will be built or saved in the converted\nSavedModel. But if input data is available during conversion, we can still\nbuild and save the TRT engines to reduce the cost during inference (see\noption 2 below).\n\n1. FP32/FP16 precision with pre-built engines\n\n\u003e params = DEFAULT_TRT_CONVERSION_PARAMS._replace(\n\u003e precision_mode='FP16',\n\u003e # Set this to a large enough number so it can cache all the engines.\n\u003e maximum_cached_engines=16)\n\u003e converter = tf.experimental.tensorrt.Converter(\n\u003e input_saved_model_dir=\"my_dir\", conversion_params=params)\n\u003e converter.convert()\n\u003e\n\u003e # Define a generator function that yields input data, and use it to execute\n\u003e # the graph to build TRT engines.\n\u003e # With TensorRT 5.1, different engines will be built (and saved later) for\n\u003e # different input shapes to the TRTEngineOp.\n\u003e def my_input_fn():\n\u003e for _ in range(num_runs):\n\u003e inp1, inp2 = ...\n\u003e yield inp1, inp2\n\u003e\n\u003e converter.build(input_fn=my_input_fn) # Generate corresponding TRT engines\n\u003e converter.save(output_saved_model_dir) # Generated engines will be saved.\n\nIn this way, one engine will be built/saved for each unique input shapes of\nthe TRTEngineOp. This is good for applications that cannot afford building\nengines during inference but have access to input data that is similar to\nthe one used in production (for example, that has the same input shapes).\nAlso, the generated TRT engines is platform dependent, so we need to run\n`build()` in an environment that is similar to production (e.g. with\nsame type of GPU).\n\n1. INT8 precision and calibration with pre-built engines\n\n\u003e params = DEFAULT_TRT_CONVERSION_PARAMS._replace(\n\u003e precision_mode='INT8',\n\u003e # Currently only one INT8 engine is supported in this mode.\n\u003e maximum_cached_engines=1,\n\u003e use_calibration=True)\n\u003e converter = tf.experimental.tensorrt.Converter(\n\u003e input_saved_model_dir=\"my_dir\", conversion_params=params)\n\u003e\n\u003e # Define a generator function that yields input data, and run INT8\n\u003e # calibration with the data. All input data should have the same shape.\n\u003e # At the end of convert(), the calibration stats (e.g. range information)\n\u003e # will be saved and can be used to generate more TRT engines with different\n\u003e # shapes. Also, one TRT engine will be generated (with the same shape as\n\u003e # the calibration data) for save later.\n\u003e def my_calibration_input_fn():\n\u003e for _ in range(num_runs):\n\u003e inp1, inp2 = ...\n\u003e yield inp1, inp2\n\u003e\n\u003e converter.convert(calibration_input_fn=my_calibration_input_fn)\n\u003e\n\u003e # (Optional) Generate more TRT engines offline (same as the previous\n\u003e # option), to avoid the cost of generating them during inference.\n\u003e def my_input_fn():\n\u003e for _ in range(num_runs):\n\u003e inp1, inp2 = ...\n\u003e yield inp1, inp2\n\u003e converter.build(input_fn=my_input_fn)\n\u003e\n\u003e # Save the TRT engine and the engines.\n\u003e converter.save(output_saved_model_dir)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| `input_saved_model_dir` | the directory to load the SavedModel which contains the input graph to transforms. Used only when input_graph_def is None. |\n| `input_saved_model_tags` | list of tags to load the SavedModel. |\n| `input_saved_model_signature_key` | the key of the signature to optimize the graph for. |\n| `conversion_params` | a TrtConversionParams instance. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------|\n| `ValueError` | if the combination of the parameters is invalid. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `build`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/compiler/tensorrt/trt_convert.py#L1026-L1040) \n\n build(\n input_fn\n )\n\nRun inference with converted graph in order to build TensorRT engines.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input_fn` | a generator function that yields input data as a list or tuple, which will be used to execute the converted signature to generate TRT engines. Example: \u003cbr /\u003e def input_fn(): yield input1, input2, input3 \u003cbr /\u003e |\n\n\u003cbr /\u003e\n\n### `convert`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/compiler/tensorrt/trt_convert.py#L949-L1024) \n\n convert(\n calibration_input_fn=None\n )\n\nConvert the input SavedModel in 2.0 format.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `calibration_input_fn` | a generator function that yields input data as a list or tuple, which will be used to execute the converted signature for calibration. All the returned input data should have the same shape. Example: \u003cbr /\u003e def input_fn(): yield input1, input2, input3 \u003cbr /\u003e |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|--------------------------------------|\n| `ValueError` | if the input combination is invalid. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| The TF-TRT converted Function. ||\n\n\u003cbr /\u003e\n\n### `save`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/compiler/tensorrt/trt_convert.py#L1042-L1091) \n\n save(\n output_saved_model_dir\n )\n\nSave the converted SavedModel.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------------------------|----------------------------------------------|\n| `output_saved_model_dir` | directory to saved the converted SavedModel. |\n\n\u003cbr /\u003e"]]