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: HCQProgram['CPUDevice']
Methods:
-
__del__–
Attributes:
-
addr– -
fxn– -
mem– -
rt_lib– -
runtimevars–
Source code in tinygrad/runtime/ops_cpu.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
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,
)
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,
)
rt_lib
class-attribute
instance-attribute
¤
rt_lib = ctypes.CDLL(
ctypes.util.find_library(
"System" if OSX else "kernel32"
)
if (OSX or WIN)
else "libgcc_s.so.1"
)
runtimevars
instance-attribute
¤
runtimevars = {
name: slot
for name, slot, *_ in (obj.signature)
if name == "core_id"
}
__del__
¤
__del__()
Source code in tinygrad/runtime/ops_cpu.py
134 135 136 | |
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– -
disassemble–
Attributes:
-
cachekey–
Source code in tinygrad/device.py
276 | |
compile
¤
Source code in tinygrad/device.py
277 | |
compile_cached
¤
Source code in tinygrad/device.py
278 279 280 281 282 283 | |