xref: /aosp_15_r20/external/tensorflow/tensorflow/python/compiler/tensorrt/test/test_utils.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Utilities to test TF-TensorRT integration."""
16
17import os
18
19from contextlib import contextmanager
20
21
22@contextmanager
23def experimental_feature_scope(feature_name):
24  """Creates a context manager to enable the given experimental feature.
25
26  This helper function creates a context manager setting up an experimental
27  feature temporarily.
28
29  Example:
30
31  ```python
32  with self._experimental_feature_scope("feature_1"):
33    do_smthg()
34  ```
35
36  Args:
37    feature_name: Name of the feature being tested for activation.
38  """
39
40  env_varname = "TF_TRT_EXPERIMENTAL_FEATURES"
41  env_value_bckp = os.environ.get(env_varname, default="")
42
43  exp_features = env_value_bckp.split(",")
44  os.environ[env_varname] = ",".join(list(set(exp_features + [feature_name])))
45
46  try:
47    yield
48  finally:
49    os.environ[env_varname] = env_value_bckp
50