carrot/tinygrad_repo/extra/lib_test_ast.py
FrogAi 659adb6457 openpilot v0.9.7 release
date: 2024-03-17T10:14:38
master commit: 7e9a909e0e57ecb31df4c87c5b9a06b1204fd034
2024-05-24 17:43:27 -07:00

26 lines
966 B
Python

import sys
import numpy as np
from typing import Dict, Type
from tinygrad.codegen.ast import ASTKernel
from tinygrad.runtime.ops_cpu import CPUBuffer
from tinygrad.ops import DeviceBuffer, map_buffers
in_test = False
test_cnt = 0
def test_ast(k:ASTKernel, device:Type[DeviceBuffer]=CPUBuffer):
global in_test, test_cnt
if in_test: return
in_test = True
print("testing AST", test_cnt)
test_cnt += 1
# TODO: this should only copy the base buffer and retain the shapetracker (requires CPU shapetracker implementation)
cpubufs : Dict[DeviceBuffer, DeviceBuffer] = {x:device.fromCPU(x.toCPU()) for x in k.bufs}
real_out = cpubufs[k.bufs[0]].toCPU()
assert hasattr(device, 'exec_ast')
test_out = device.exec_ast(map_buffers(cpubufs, k.ast)).toCPU()
if not np.allclose(real_out, test_out, atol=1e-4, rtol=1e-4):
print("MISMATCH")
print(k.print())
sys.tracebacklimit = 0
np.testing.assert_allclose(real_out, test_out)
in_test = False