diff --git a/test/null/test_device.py b/test/null/test_device.py index 31ccf1b26b..d5f6fffa19 100644 --- a/test/null/test_device.py +++ b/test/null/test_device.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import unittest, os, subprocess +from unittest.mock import patch from tinygrad import Tensor from tinygrad.device import Device, Compiler, enumerate_devices_str from tinygrad.helpers import diskcache_get, diskcache_put, getenv, Context, WIN, CI, OSX @@ -76,6 +77,18 @@ class TestDevice(unittest.TestCase): self.assertIsInstance(Device["CPU"].compiler, CPULLVMCompiler) assert inst is Device["CPU"].compiler # cached + @unittest.skipIf(Device.DEFAULT != "CPU", "only run on CPU") + def test_compiler_autodetect_fallback(self): + from tinygrad.runtime.support.compiler_cpu import CPULLVMCompiler + + try: CPULLVMCompiler() + except Exception as e: self.skipTest(f"skipping: LLVM not available: {e}") + + dev = Device["CPU"] + dev.cached_pair.clear() + with patch("tinygrad.renderer.cstyle.ClangJITRenderer.__init__", side_effect=RuntimeError("broken")): + self.assertIsInstance(dev.renderer.compiler, CPULLVMCompiler) + class MockCompiler(Compiler): def __init__(self, key): super().__init__(key) def compile(self, src) -> bytes: return src.encode() diff --git a/tinygrad/device.py b/tinygrad/device.py index eeba279627..6f95b01a4c 100644 --- a/tinygrad/device.py +++ b/tinygrad/device.py @@ -304,7 +304,7 @@ class Compiled: # remove disabled compilers for en, rc in self.comp_sets.values(): - if en is not None and en.value == 0 and rc in comps: comps.remove(rc) + if en is not None and en.value == 0 and en.key in os.environ and rc in comps: comps.remove(rc) return select_first_inited(list(forced_comps) if len(forced_comps)>0 else comps, f"No compiler for {self.device} is available", self.cached_pair)