From f6f3f353293a1a3a328dfc8ec47725c8c324a8df Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 13 Jul 2025 13:50:08 +0000 Subject: [PATCH] Fix(tensorboard): Check for executable to prevent crash I replaced the direct import of `tensorflow` with a check for the `tensorboard` executable. This resolves the `illegal hardware instruction` crash that occurred on systems with CPUs lacking AVX2 support. The new implementation ensures that the TensorBoard feature remains available if the `tensorboard` command is in the system's PATH, without causing a fatal error when the `tensorflow` library is incompatible with the underlying hardware. --- kohya_gui/class_tensorboard.py | 11 +++++++---- tests/test_tensorboard_visibility.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/test_tensorboard_visibility.py diff --git a/kohya_gui/class_tensorboard.py b/kohya_gui/class_tensorboard.py index 001c894..9c11379 100644 --- a/kohya_gui/class_tensorboard.py +++ b/kohya_gui/class_tensorboard.py @@ -3,12 +3,15 @@ import gradio as gr import subprocess import time import webbrowser +import shutil try: - os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" - import tensorflow # Attempt to import tensorflow to check if it is installed - - visibility = True + # os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" + # import tensorflow # Attempt to import tensorflow to check if it is installed + if shutil.which("tensorboard"): + visibility = True + else: + visibility = False except ImportError: visibility = False diff --git a/tests/test_tensorboard_visibility.py b/tests/test_tensorboard_visibility.py new file mode 100644 index 0000000..3b16e66 --- /dev/null +++ b/tests/test_tensorboard_visibility.py @@ -0,0 +1,20 @@ +import unittest +from unittest.mock import patch +import importlib + +class TestTensorboardVisibility(unittest.TestCase): + + @patch('shutil.which', return_value='/usr/bin/tensorboard') + def test_tensorboard_visibility_when_tensorboard_is_present(self, mock_which): + import kohya_gui.class_tensorboard + importlib.reload(kohya_gui.class_tensorboard) + self.assertTrue(kohya_gui.class_tensorboard.visibility) + + @patch('shutil.which', return_value=None) + def test_tensorboard_visibility_when_tensorboard_is_absent(self, mock_which): + import kohya_gui.class_tensorboard + importlib.reload(kohya_gui.class_tensorboard) + self.assertFalse(kohya_gui.class_tensorboard.visibility) + +if __name__ == '__main__': + unittest.main()