silence-tensorflow


Namesilence-tensorflow JSON
Version 1.2.3 PyPI version JSON
download
home_pagehttps://github.com/LucaCappelletti94/silence_tensorflow
SummarySimple python package to shut up Tensorflow warnings and logs.
upload_time2024-11-03 21:53:46
maintainerNone
docs_urlNone
authorLuca Cappelletti
requires_pythonNone
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Silence TensorFlow

[![pip](https://badge.fury.io/py/silence-tensorflow.svg)](https://pypi.org/project/silence-tensorflow/)
[![python](https://img.shields.io/pypi/pyversions/silence-tensorflow)](https://pypi.org/project/silence-tensorflow/)
[![license](https://img.shields.io/pypi/l/silence-tensorflow)](https://pypi.org/project/silence-tensorflow/)
[![downloads](https://pepy.tech/badge/silence-tensorflow)](https://pepy.tech/project/silence-tensorflow)
[![Github Actions](https://github.com/LucaCappelletti94/silence_tensorflow/actions/workflows/python.yml/badge.svg)](https://github.com/LucaCappelletti94/silence_tensorflow/actions/)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/e6fe64db1c9042bbaa4c0a20bde585dc)](https://app.codacy.com/gh/LucaCappelletti94/silence_tensorflow/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)

Python package to shut up TensorFlow warnings and logs, letting you focus on the important errors.

## How do I install this package?

As usual, just download it using pip:

```shell
pip install silence_tensorflow
```

## How do I use it?

You only need to import the package before importing TensorFlow:

```python
from silence_tensorflow import silence_tensorflow
silence_tensorflow()
import tensorflow as tf

# your code
```

### Setting the logging level

While by default the logging level is set to error, you can set it to any level you want by passing the level as an argument to the function.

```python
from silence_tensorflow import silence_tensorflow

# Set the logging level to error, meaning only errors will be logged
silence_tensorflow("ERROR")

# Set the logging level to warning, meaning only errors and warnings will be logged
silence_tensorflow("WARNING")

# Set the logging level to info, meaning errors, warnings and info will be logged
silence_tensorflow("INFO")

# Set the logging level to debug, meaning all logs will be shown
silence_tensorflow("DEBUG")
```

## Can it be done within the import?

Sure, you can do everything with a single line by importing the submodule auto.

This will set the logging level to error and the affinity to no verbose.

```python
import silence_tensorflow.auto
import tensorflow as tf

# your code
```

## How can I get pylint to ignore the unused import?

You can use the flag `disable=unused-import` as such:

```python
import silence_tensorflow.auto  # pylint: disable=unused-import
import tensorflow as tf

# your code
```

## How can I get pylint to ignore the unused variable?

If you import `silence_tensorflow` in the context of a function you will get a different warning from pylint: unused variable. You can use the flag `disable=unused-variable` as such:

```python
def func():
    import silence_tensorflow.auto  # pylint: disable=unused-variable
    import tensorflow as tf

    # your code
```

## How does this work under the hood?

This package will set the `KMP_AFFINITY` system variable to `noverbose` and `TF_CPP_MIN_LOG_LEVEL` to level `3` (only errors logged).

If you need a custom value for `KMP_AFFINITY` you should reset it after importing the package, as follows:

```python
import os
from silence_tensorflow import silence_tensorflow
backup = os.environ["KMP_AFFINITY"]
silence_tensorflow()
os.environ["KMP_AFFINITY"] = backup
```

## Known limitations

While I really tried to cover all possible logs that TensorFlow can produce, there are some logs that are not silenced by this package.
Below you find the ones that we are aware of, alongside the reason why they are not silenced and what you can do to silence them.

### NUMA node read from SysFS had negative value

You may have encountered the following log:

```plaintext
successful NUMA node read from SysFS had negative value (-1)
```

This log means that TensorFlow is trying to read the NUMA node from the system file system and it is getting a negative value. This is not an error, but a warning, and this package cannot silence it automatically without administrative privileges. Since executing code you have just found online with administrative privileges is not a good idea, you can silence this log by running as root the following command in your terminal:

```bash
for a in /sys/bus/pci/devices/*; do echo 0 | sudo tee -a $a/numa_node; done
```

This command will set the NUMA node to 0 for all PCI devices, which is the default value and should not cause any issues. It does not fix the underlying issue, but it silences the log until you reboot your system.

### TensorFlow Lite (TFLite)

TFLite logs are not silenced by this package because [they have hardcoded the logging level to `INFO`](https://github.com/tensorflow/tensorflow/blob/3570f6d986066b834a7f54f3c3ec60d0245193bd/tensorflow/lite/minimal_logging_ios.cc#L50) and there is no way to change it from the Python side.

TFLite will cause info logs such as the following to be printed:

```plaintext
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
```

If you are willing to recompile your own version of TensorFlow Lite, you can change the logging level to `ERROR` by changing the line mentioned above or set it in your C++ code as follows, [as described in this issue](https://github.com/tensorflow/tensorflow/issues/58050#issuecomment-1624919480):

```cpp
tflite::LoggerOptions::SetMinimumLogSeverity(tflite::TFLITE_LOG_SILENT);
```

### oneDNN warning

Another common warning that TensorFlow prints is the following:

```plaintext
I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
```

I tried to automatically set the environment variable `TF_ENABLE_ONEDNN_OPTS` to `0` when GPU drivers are detected and in such cases using oneDNN is not necessary. However, this in some instances lead to TensorFlow __occasionally__ deadlocking and I had to revert the change.

## License

This software is distributed under the MIT License.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/LucaCappelletti94/silence_tensorflow",
    "name": "silence-tensorflow",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Luca Cappelletti",
    "author_email": "cappelletti.luca94@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a4/3f/7256c42c70c0fae58ebc7ddae9285957607578b545330f2e7cdf861a0bb5/silence_tensorflow-1.2.3.tar.gz",
    "platform": null,
    "description": "# Silence TensorFlow\n\n[![pip](https://badge.fury.io/py/silence-tensorflow.svg)](https://pypi.org/project/silence-tensorflow/)\n[![python](https://img.shields.io/pypi/pyversions/silence-tensorflow)](https://pypi.org/project/silence-tensorflow/)\n[![license](https://img.shields.io/pypi/l/silence-tensorflow)](https://pypi.org/project/silence-tensorflow/)\n[![downloads](https://pepy.tech/badge/silence-tensorflow)](https://pepy.tech/project/silence-tensorflow)\n[![Github Actions](https://github.com/LucaCappelletti94/silence_tensorflow/actions/workflows/python.yml/badge.svg)](https://github.com/LucaCappelletti94/silence_tensorflow/actions/)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/e6fe64db1c9042bbaa4c0a20bde585dc)](https://app.codacy.com/gh/LucaCappelletti94/silence_tensorflow/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)\n\nPython package to shut up TensorFlow warnings and logs, letting you focus on the important errors.\n\n## How do I install this package?\n\nAs usual, just download it using pip:\n\n```shell\npip install silence_tensorflow\n```\n\n## How do I use it?\n\nYou only need to import the package before importing TensorFlow:\n\n```python\nfrom silence_tensorflow import silence_tensorflow\nsilence_tensorflow()\nimport tensorflow as tf\n\n# your code\n```\n\n### Setting the logging level\n\nWhile by default the logging level is set to error, you can set it to any level you want by passing the level as an argument to the function.\n\n```python\nfrom silence_tensorflow import silence_tensorflow\n\n# Set the logging level to error, meaning only errors will be logged\nsilence_tensorflow(\"ERROR\")\n\n# Set the logging level to warning, meaning only errors and warnings will be logged\nsilence_tensorflow(\"WARNING\")\n\n# Set the logging level to info, meaning errors, warnings and info will be logged\nsilence_tensorflow(\"INFO\")\n\n# Set the logging level to debug, meaning all logs will be shown\nsilence_tensorflow(\"DEBUG\")\n```\n\n## Can it be done within the import?\n\nSure, you can do everything with a single line by importing the submodule auto.\n\nThis will set the logging level to error and the affinity to no verbose.\n\n```python\nimport silence_tensorflow.auto\nimport tensorflow as tf\n\n# your code\n```\n\n## How can I get pylint to ignore the unused import?\n\nYou can use the flag `disable=unused-import` as such:\n\n```python\nimport silence_tensorflow.auto  # pylint: disable=unused-import\nimport tensorflow as tf\n\n# your code\n```\n\n## How can I get pylint to ignore the unused variable?\n\nIf you import `silence_tensorflow` in the context of a function you will get a different warning from pylint: unused variable. You can use the flag `disable=unused-variable` as such:\n\n```python\ndef func():\n    import silence_tensorflow.auto  # pylint: disable=unused-variable\n    import tensorflow as tf\n\n    # your code\n```\n\n## How does this work under the hood?\n\nThis package will set the `KMP_AFFINITY` system variable to `noverbose` and `TF_CPP_MIN_LOG_LEVEL` to level `3` (only errors logged).\n\nIf you need a custom value for `KMP_AFFINITY` you should reset it after importing the package, as follows:\n\n```python\nimport os\nfrom silence_tensorflow import silence_tensorflow\nbackup = os.environ[\"KMP_AFFINITY\"]\nsilence_tensorflow()\nos.environ[\"KMP_AFFINITY\"] = backup\n```\n\n## Known limitations\n\nWhile I really tried to cover all possible logs that TensorFlow can produce, there are some logs that are not silenced by this package.\nBelow you find the ones that we are aware of, alongside the reason why they are not silenced and what you can do to silence them.\n\n### NUMA node read from SysFS had negative value\n\nYou may have encountered the following log:\n\n```plaintext\nsuccessful NUMA node read from SysFS had negative value (-1)\n```\n\nThis log means that TensorFlow is trying to read the NUMA node from the system file system and it is getting a negative value. This is not an error, but a warning, and this package cannot silence it automatically without administrative privileges. Since executing code you have just found online with administrative privileges is not a good idea, you can silence this log by running as root the following command in your terminal:\n\n```bash\nfor a in /sys/bus/pci/devices/*; do echo 0 | sudo tee -a $a/numa_node; done\n```\n\nThis command will set the NUMA node to 0 for all PCI devices, which is the default value and should not cause any issues. It does not fix the underlying issue, but it silences the log until you reboot your system.\n\n### TensorFlow Lite (TFLite)\n\nTFLite logs are not silenced by this package because [they have hardcoded the logging level to `INFO`](https://github.com/tensorflow/tensorflow/blob/3570f6d986066b834a7f54f3c3ec60d0245193bd/tensorflow/lite/minimal_logging_ios.cc#L50) and there is no way to change it from the Python side.\n\nTFLite will cause info logs such as the following to be printed:\n\n```plaintext\nINFO: Created TensorFlow Lite XNNPACK delegate for CPU.\n```\n\nIf you are willing to recompile your own version of TensorFlow Lite, you can change the logging level to `ERROR` by changing the line mentioned above or set it in your C++ code as follows, [as described in this issue](https://github.com/tensorflow/tensorflow/issues/58050#issuecomment-1624919480):\n\n```cpp\ntflite::LoggerOptions::SetMinimumLogSeverity(tflite::TFLITE_LOG_SILENT);\n```\n\n### oneDNN warning\n\nAnother common warning that TensorFlow prints is the following:\n\n```plaintext\nI tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n```\n\nI tried to automatically set the environment variable `TF_ENABLE_ONEDNN_OPTS` to `0` when GPU drivers are detected and in such cases using oneDNN is not necessary. However, this in some instances lead to TensorFlow __occasionally__ deadlocking and I had to revert the change.\n\n## License\n\nThis software is distributed under the MIT License.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple python package to shut up Tensorflow warnings and logs.",
    "version": "1.2.3",
    "project_urls": {
        "Homepage": "https://github.com/LucaCappelletti94/silence_tensorflow"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a43f7256c42c70c0fae58ebc7ddae9285957607578b545330f2e7cdf861a0bb5",
                "md5": "fb15b0e941e4c381d512af69b64bb44f",
                "sha256": "4c55a32951af577e68e47e48675199b49944fd0fe042a026c069148bbef1d293"
            },
            "downloads": -1,
            "filename": "silence_tensorflow-1.2.3.tar.gz",
            "has_sig": false,
            "md5_digest": "fb15b0e941e4c381d512af69b64bb44f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7179,
            "upload_time": "2024-11-03T21:53:46",
            "upload_time_iso_8601": "2024-11-03T21:53:46.599244Z",
            "url": "https://files.pythonhosted.org/packages/a4/3f/7256c42c70c0fae58ebc7ddae9285957607578b545330f2e7cdf861a0bb5/silence_tensorflow-1.2.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-03 21:53:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "LucaCappelletti94",
    "github_project": "silence_tensorflow",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "silence-tensorflow"
}
        
Elapsed time: 0.43790s