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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
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,
)
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"
}
__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
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__del__
¤
__del__()
Source code in tinygrad/runtime/ops_cpu.py
172 173 174 | |
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
304 | |
compile
¤
Source code in tinygrad/device.py
305 | |
compile_cached
¤
Source code in tinygrad/device.py
306 307 308 309 310 311 | |
compile_server
¤
Source code in tinygrad/device.py
316 317 318 319 | |
disassemble
¤
disassemble(lib: bytes)
Source code in tinygrad/device.py
312 | |
server
¤
Source code in tinygrad/device.py
313 314 315 | |