Runtime Overview¤
Overview¤
A typical runtime consists of the following parts:
Compiled¤
The Compiled class is responsible for initializing and managing a device.
Compiled
¤
Compiled(
device: str,
allocator: Allocator,
renderers: list[type[Renderer]],
runtime: type[Program[Self]] | None,
graph=None,
arch=None,
)
Methods:
-
synchronize–Synchronize all pending operations on the device.
synchronize
¤
synchronize()
Synchronize all pending operations on the device.
This method ensures that all previously queued operations on the device have been completed before proceeding.
Allocator¤
The Allocator class is responsible for managing memory on the device. There is also a version called the LRUAllocator, which caches allocated buffers to optimize performance.
Allocator
¤
LRUAllocator
¤
LRUAllocator(dev: DeviceType, **kwargs)
Bases: Allocator, Generic[DeviceType]
The LRU Allocator is responsible for caching buffers. It ensures that buffers are not freed until it is absolutely necessary, optimizing performance.
Methods:
-
alloc– -
free– -
free_cache–
Attributes:
Program¤
The Program class is created for each loaded program. It is responsible for executing the program on the device. As an example, here is a CPUProgram implementation which loads program and runs it.
CPUProgram
¤
CPUProgram(dev: CPUDevice, obj: TinyELF)
Bases: Program['CPUDevice']
Methods:
Attributes:
Source code in tinygrad/runtime/ops_cpu.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
addr
instance-attribute
¤
addr = ctypes.windll.kernel32.VirtualAlloc(
ctypes.c_void_p(0),
ctypes.c_size_t(len(obj.lib)),
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE,
)
fxn
instance-attribute
¤
fxn = (
ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self.addr)
if self.lvp
else ctypes.CFUNCTYPE(None)(self.addr)
)
mem
instance-attribute
¤
mem = mmap.mmap(
-1,
len(obj.lib),
mmap.MAP_ANON
| mmap.MAP_PRIVATE
| (MAP_JIT if OSX else 0),
mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC,
)
__call__
¤
__call__(
*bufs: HCQBuffer,
global_size: tuple[int, int, int] = (1, 1, 1),
local_size: tuple[int, int, int] = (1, 1, 1),
vals: tuple[int | None, ...] = (),
wait: bool = False,
timeout: int | None = None
) -> float | None
Source code in tinygrad/runtime/ops_cpu.py
59 60 61 62 63 64 65 66 67 68 69 70 71 | |
__del__
¤
__del__()
Source code in tinygrad/runtime/ops_cpu.py
73 74 75 | |
_load
¤
_load(lib, base=0)
Source code in tinygrad/runtime/ops_cpu.py
22 | |
Compiler¤
The Compiler class compiles the output from the Renderer and produces it in a device-specific format.
Compiler
¤
Compiler(cachekey: str | None = None)
Methods:
-
compile– -
compile_cached– -
compile_server– -
disassemble– -
server–
Attributes:
-
cachekey–
Source code in tinygrad/device.py
296 | |
compile
¤
Source code in tinygrad/device.py
297 | |
compile_cached
¤
Source code in tinygrad/device.py
298 299 300 301 302 303 | |
compile_server
¤
Source code in tinygrad/device.py
308 309 310 311 | |
disassemble
¤
disassemble(lib: bytes)
Source code in tinygrad/device.py
304 | |
server
¤
Source code in tinygrad/device.py
305 306 307 | |