|
4 | 4 | import traceback
|
5 | 5 |
|
6 | 6 | try:
|
7 |
| - TF_ADDONS_PY_OPS = bool(int(os.environ["TF_ADDONS_PY_OPS"])) |
| 7 | + _TF_ADDONS_PY_OPS = bool(int(os.environ["TF_ADDONS_PY_OPS"])) |
8 | 8 | except KeyError:
|
9 | 9 | if platform.system() == "Linux":
|
10 |
| - TF_ADDONS_PY_OPS = False |
| 10 | + _TF_ADDONS_PY_OPS = False |
11 | 11 | else:
|
12 |
| - TF_ADDONS_PY_OPS = True |
| 12 | + _TF_ADDONS_PY_OPS = True |
13 | 13 |
|
14 |
| - |
15 |
| -FALLBACK_WARNING_TEMPLATE = """{} |
| 14 | +_FALLBACK_WARNING_TEMPLATE = """{} |
16 | 15 |
|
17 | 16 | The {} C++/CUDA custom op could not be loaded.
|
18 | 17 | For this reason, Addons will fallback to an implementation written
|
|
26 | 25 | If you want this warning to disappear, either make sure the TensorFlow installed
|
27 | 26 | is compatible with this version of Addons, or tell TensorFlow Addons to
|
28 | 27 | prefer using Python implementations and not custom C++/CUDA ones. You can do that
|
29 |
| -by changing the TF_ADDONS_PY_OPS flag |
30 |
| -either with the environment variable: |
| 28 | +by setting the enviornment variable `TF_ADDONS_PY_OPS=1`: |
31 | 29 | ```bash
|
32 | 30 | TF_ADDONS_PY_OPS=1 python my_script.py
|
33 | 31 | ```
|
34 |
| -or in your code, after your imports: |
| 32 | +or run `tfa.options.disable_custom_kernel()` in your code, after your imports: |
35 | 33 | ```python
|
36 | 34 | import tensorflow_addons as tfa
|
37 | 35 | import ...
|
38 | 36 | import ...
|
39 | 37 |
|
40 |
| -tfa.options.TF_ADDONS_PY_OPS = True |
| 38 | +tfa.options.disable_custom_kernel() |
41 | 39 | ```
|
42 | 40 | """
|
43 | 41 |
|
44 | 42 |
|
45 | 43 | def warn_fallback(op_name):
|
46 |
| - warning_msg = FALLBACK_WARNING_TEMPLATE.format(traceback.format_exc(), op_name) |
| 44 | + warning_msg = _FALLBACK_WARNING_TEMPLATE.format(traceback.format_exc(), op_name) |
47 | 45 | warnings.warn(warning_msg, RuntimeWarning)
|
48 |
| - global TF_ADDONS_PY_OPS |
49 |
| - TF_ADDONS_PY_OPS = True |
| 46 | + disable_custom_kernel() |
| 47 | + |
| 48 | + |
| 49 | +def enable_custom_kernel(): |
| 50 | + """Prefer custom C++/CUDA kernel to pure python operations. |
| 51 | +
|
| 52 | + Enable using custom C++/CUDA kernel instead of pure python operations. |
| 53 | + It has the same effect as setting environment variable `TF_ADDONS_PY_OPS=0`. |
| 54 | + """ |
| 55 | + global _TF_ADDONS_PY_OPS |
| 56 | + _TF_ADDONS_PY_OPS = False |
| 57 | + |
| 58 | + |
| 59 | +def disable_custom_kernel(): |
| 60 | + """Prefer pure python operations to custom C++/CUDA kernel. |
| 61 | +
|
| 62 | + Disable using custom C++/CUDA kernel instead of pure python operations. |
| 63 | + It has the same effect as setting environment variable `TF_ADDONS_PY_OPS=1`. |
| 64 | + """ |
| 65 | + global _TF_ADDONS_PY_OPS |
| 66 | + _TF_ADDONS_PY_OPS = True |
| 67 | + |
| 68 | + |
| 69 | +def is_custom_kernel_disabled(): |
| 70 | + """Return whether custom C++/CUDA kernel is disabled.""" |
| 71 | + return _TF_ADDONS_PY_OPS |
0 commit comments