openpilot/tinygrad_repo/test/test_kernel_cache.py
Vehicle Researcher c5d5c5d1f3 openpilot v0.10.1 release
date: 2025-10-24T00:30:59
master commit: 405631baf9685e171a0dd19547cb763f1b163d18
2025-10-24 00:31:03 -07:00

30 lines
863 B
Python

#!/usr/bin/env python
import unittest
from tinygrad.tensor import Tensor
from tinygrad import Device
class TestKernelCache(unittest.TestCase):
def test_kernel_cache_in_action(self):
if Device.DEFAULT not in ["CPU"]:
self.skipTest("No custom kernel cache is implemented")
unique_const = 0.6765677269
a = Tensor.rand(4,4).realize()
b = Tensor.rand(4,4).realize()
x = a + b + unique_const
x.realize()
a1 = Tensor.rand(4,4).realize()
b1 = Tensor.rand(4,4).realize()
orig_compile_func = Device['CPU'].compiler.compile_cached
Device['CPU'].compiler.compile_cached = None # making it not callable
try:
x1 = a1 + b1 + unique_const
x1.realize() # Same kernel should be from cache.
finally:
Device['CPU'].compiler.compile_cached = orig_compile_func
if __name__ == "__main__":
unittest.main()