mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-04-29 03:00:14 -04:00
print_ast
This commit is contained in:
40
test/external_test_gpu_ast.py
Normal file
40
test/external_test_gpu_ast.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
import unittest
|
||||
import numpy as np
|
||||
from tinygrad.ops import LazyOp, ReduceOps, BinaryOps, UnaryOps, MovementOps
|
||||
from tinygrad.shape import ShapeTracker, View, ZeroView
|
||||
from tinygrad.llops.ops_gpu import GPUBuffer, CLASTKernel
|
||||
|
||||
class TestAST(unittest.TestCase):
|
||||
def test_conv_zeroview_ast(self):
|
||||
buf0 = GPUBuffer(shape=ShapeTracker(shape=(1, 1, 3, 4), views=[View((1, 1, 3, 4), (2, 2, 2, 1), -3), ZeroView((1, 1, 1, 2), ((0, 1), (0, 1), (-1, 2), (-1, 3))), View((1, 1, 3, 4), (0, 0, 4, 1), 0)]))
|
||||
buf1 = GPUBuffer(shape=ShapeTracker(shape=(1, 1, 3, 4), views=[View((1, 1, 3, 4), (0, 0, 0, 0), 0)]))
|
||||
op1 = LazyOp(BinaryOps.MUL, (buf0,buf1,), None)
|
||||
ast = LazyOp(UnaryOps.RELU, (op1,), None)
|
||||
k = CLASTKernel(ast)
|
||||
k.codegen()(*k.bufs)
|
||||
|
||||
def test_first_op_conv(self):
|
||||
buf0 = GPUBuffer(shape=ShapeTracker(shape=(1, 64, 128, 8, 4, 1, 1, 3, 3), views=[View((1, 66, 130, 32, 1), (262144, 4096, 32, 1, 1), -4128), ZeroView((1, 64, 128, 32, 1), ((0, 1), (-1, 65), (-1, 129), (0, 32), (0, 1))), View((1, 64, 128, 8, 4, 1, 1, 3, 3), (266240, 4160, 32, 4, 1, 12480, 12480, 4160, 32), 0)]), hostbuf=GPUBuffer(shape=(64, 1024, 4), force_create=True))
|
||||
buf1 = GPUBuffer(shape=ShapeTracker(shape=(1, 64, 128, 8, 4, 1, 1, 3, 3), views=[View((1, 64, 128, 8, 4, 1, 1, 3, 3), (0, 0, 0, 36, 1, 0, 0, 12, 4), 0)]), hostbuf=GPUBuffer(shape=(8, 9, 4), force_create=True))
|
||||
op0 = LazyOp(BinaryOps.MUL, (buf0,buf1,), None)
|
||||
op1 = LazyOp(ReduceOps.SUM, (op0,), (1, 64, 128, 8, 4, 1, 1, 1, 1))
|
||||
buf2 = GPUBuffer(shape=ShapeTracker(shape=(1, 64, 128, 8, 4, 1, 1, 1, 1), views=[View((1, 64, 128, 8, 4, 1, 1, 1, 1), (0, 0, 0, 4, 1, 1, 1, 1, 1), 0)]), hostbuf=GPUBuffer(shape=(32,), force_create=True))
|
||||
op2 = LazyOp(BinaryOps.ADD, (op1,buf2,), None)
|
||||
op3 = LazyOp(UnaryOps.RELU, (op2,), None)
|
||||
buf3 = GPUBuffer(shape=ShapeTracker(shape=(1, 64, 128, 8, 4, 1, 1, 1, 1), views=[View((1, 64, 128, 8, 4, 1, 1, 1, 1), (0, 0, 0, 0, 0, 1, 1, 1, 1), 0)]), hostbuf=GPUBuffer(shape=(1,), backing=np.array([1.], dtype=np.float32)))
|
||||
buf4 = GPUBuffer(shape=ShapeTracker(shape=(1, 64, 128, 8, 4, 1, 1, 1, 1), views=[View((1, 64, 128, 8, 4, 1, 1, 1, 1), (0, 0, 0, 0, 0, 1, 1, 1, 1), 0)]), hostbuf=GPUBuffer(shape=(1,), backing=np.array([1.], dtype=np.float32)))
|
||||
op4 = LazyOp(UnaryOps.EXP, (op2,), None)
|
||||
op5 = LazyOp(BinaryOps.SUB, (buf4,op4,), None)
|
||||
op6 = LazyOp(UnaryOps.RELU, (op5,), None)
|
||||
op7 = LazyOp(BinaryOps.MUL, (buf3,op6,), None)
|
||||
op8 = LazyOp(BinaryOps.SUB, (op3,op7,), None)
|
||||
ast = LazyOp(MovementOps.RESHAPE, (op8,), (64, 1024, 4))
|
||||
k = CLASTKernel(ast)
|
||||
#[x.cl for x in k.bufs]
|
||||
fxn = k.codegen()
|
||||
#print(k.fxn.prg)
|
||||
fxn(*k.bufs)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user