tinygrad helpers

Note

You likely want the upstream tinygrad, not tinygrab. Tinygrab contains AI generated docstrings for a tinygrad snapshot. Upstream: https://tinygrad.org

tinygrad.helpers.CACHEDB: str = '/home/jebba/.cache/tinygrad/cache.db'

The database file path is determined by the environment variable ‘CACHEDB’. If it doesn’t exist, then it falls back to a default location within the cache directory.

tinygrad.helpers.CACHELEVEL = 2

The cache level is determined by the environment variable ‘CACHELEVEL’. If it doesn’t exist, then it falls back to a default value of 2.

class tinygrad.helpers.Context(**kwargs)[source]

Bases: ContextDecorator

stack: ClassVar[List[dict[str, int]]] = [{}]
class tinygrad.helpers.ContextVar(key, default_value)[source]

Bases: object

A class for managing context variables.

This class provides a way to manage context variables, including caching instances and retrieving values from the environment. It also includes methods for comparing the value of the instance with another value.

value: int

The value of this context variable.

This is typically retrieved from the environment using the ‘getenv’ function when the instance is created.

class tinygrad.helpers.DType(priority, itemsize, name, np, sz)[source]

Bases: NamedTuple

itemsize: int

Alias for field number 1

name: str

Alias for field number 2

np: type | None

Alias for field number 3

priority: int

Alias for field number 0

scalar()[source]

Returns the scalar version of the current DType object if its size is greater than 1; otherwise, it returns the current DType object. It does this by looking up the name of the data type (without the size appended) in the dtypes dictionary and returning that DType object.

Returns:

The scalar version of the current DType object.

Return type:

DType

sz: int

Alias for field number 4

vec(sz: int)[source]

Creates a new DType object with vectorized attributes from the current DType object. The size of the new DType object is given by the parameter sz. It asserts that the current size is 1 and the provided size is greater than 1, otherwise it raises an error.

Parameters:

sz (int) – The size of the new vectorized data type.

Returns:

A new DType object with vectorized attributes.

Return type:

DType

class tinygrad.helpers.GlobalCounters[source]

Bases: object

Class to store and manage global counters for various operations, memory usage, time etc. The class variables include: 1) global_ops: Class Variable to store the count of global operations. 2) global_mem: Class Variable to store the total memory used globally. 3) time_sum_s: Class Variable to store the cumulative time in seconds. 4) kernel_count: Class Variable to store the number of kernels executed. 5) mem_used: Class Variable to store the amount of memory used, not reset. 6) mem_cached: Class Variable to store the amount of cached memory, not reset.

global_mem: ClassVar[int] = 0

Class Variable to store the total memory used globally. Default value is 0.

global_ops: ClassVar[int] = 0

Class Variable to store the count of global operations. Default value is 0.

kernel_count: ClassVar[int] = 0

Class Variable to store the number of kernels executed. Default value is 0.

mem_cached: ClassVar[int] = 0

Class Variable to store the amount of cached memory, not reset. Default value is 0. NOTE: This variable does not get reset.

mem_used: ClassVar[int] = 0

Class Variable to store the amount of memory used, not reset. Default value is 0. NOTE: This variable does not get reset.

static reset()[source]

This is a static method that resets the global counters.

The global counters include: - GlobalCounters.global_ops: Keeps track of total operations performed - GlobalCounters.global_mem: Keeps track of total memory used - GlobalCounters.time_sum_s: Keeps track of cumulative time in seconds - GlobalCounters.kernel_count: Keeps track of the number of kernels run

This method resets these counters back to their initial states, which are all 0.

time_sum_s: ClassVar[float] = 0.0

Class Variable to store the cumulative time in seconds. Default value is 0.0.

class tinygrad.helpers.ImageDType(priority, itemsize, name, np, shape)[source]

Bases: DType

This is the ImageDType class, a subclass of DType. It is used to create custom data types for images with additional attributes such as shape.

priority

The priority level of the data type.

Type:

int

itemsize

The size of each element in bytes.

Type:

int

name

The name of the data type.

Type:

str

np

The numpy dtype representation.

Type:

numpy.dtype

shape

A tuple representing the shape of the image.

Type:

tuple

class tinygrad.helpers.Profiling(enabled=True, sort='cumtime', frac=0.2)[source]

Bases: ContextDecorator

This class is a context manager for profiling code blocks. It uses the cProfile module to profile the code block and pstats to display statistics about the execution time of functions. The class can be configured with different sorting options (sort) and can limit the output to a certain fraction (frac) of the slowest functions.

enabled

If True, the profiler is enabled. Default is True.

Type:

bool

sort

How to sort the statistic output. Default is ‘cumtime’.

Type:

str

frac

The fraction of the slowest functions to print. Default is 0.2.

Type:

float

class tinygrad.helpers.PtrDType(dt: DType)[source]

Bases: DType

This class is a subclass of DType, which represents the pointer data type.

The constructor of this class takes one argument: dt (an instance of DType). It calls the __new__ method of the parent class (DType) with the same parameters passed to it.

The __repr__ method returns a string representation of the PtrDType object, which includes “ptr.” as a prefix to the string representation of its superclass.

class tinygrad.helpers.Timing(prefix='', on_exit=None, enabled=True)[source]

Bases: ContextDecorator

This is a context manager class for timing the execution time of code blocks. It is a subclass of contextlib.ContextDecorator, which makes it easy to use as a context manager or decorator.

prefix

An optional string that can be used to prefix the output message. Defaults to “”.

Type:

str

on_exit

An optional function that will be called with the elapsed time in nanoseconds as its argument when the context manager exits. The return value of this function will be appended to the output message. Defaults to None.

Type:

function

enabled

If set to False, the context manager will not print anything upon exiting the context. Defaults to True.

Type:

bool

tinygrad.helpers.all_int(t: Tuple[Any, ...]) TypeGuard[Tuple[int, ...]][source]

Checks if all elements in a given tuple are integers.

Parameters:

t (Tuple[Any, ...]) – The input tuple to check.

Returns:

True if all elements are integers, False otherwise.

Return type:

TypeGuard[Tuple[int, …]]

tinygrad.helpers.all_same(items: List[T])[source]

Checks if all elements in a list are the same.

Parameters:

items (List[T]) – The input list.

Returns:

True if all elements in the input list are the same, False otherwise.

Return type:

bool

tinygrad.helpers.ansilen(s: str)[source]

Calculates the visible length of a string, ignoring ANSI escape codes.

Parameters:

s (str) – The input string.

Returns:

The visible length of the string after removing ANSI escape codes.

Return type:

int

tinygrad.helpers.ansistrip(s: str)[source]

Removes ANSI escape codes from a string.

Parameters:

s (str) – The input string.

Returns:

The input string without ANSI escape codes.

Return type:

str

tinygrad.helpers.argfix(*x)[source]

Fixes an argument if it’s a tuple or list into a non-ambiguous form.

If the first argument is a tuple or list, it will be returned as a single element tuple. Otherwise, the original arguments are returned.

Parameters:

*x (Any) – The input arguments.

Returns:

If the first argument is a tuple or list, returns a single element tuple containing that argument.

Otherwise, return the original arguments.

Return type:

Union[Tuple, Any]

tinygrad.helpers.argsort(x)[source]

Returns a new sorted list of indices for the input list.

Parameters:

x (List[Any]) – The input list.

Returns:

A list containing the indices of the elements in the input list, in ascending order.

Return type:

List[int]

tinygrad.helpers.colored(st, color: str | None, background=False)[source]

Adds ANSI escape codes to a string to change its color.

Parameters:
  • st (str) – The input string.

  • color (Optional[str]) – The name of the color to use, or None for no color.

  • background (bool) – If True, set the background color instead of the text color.

Returns:

The input string with ANSI escape codes added to change its color.

Return type:

str

tinygrad.helpers.compile_cuda_style(prg, compile_options, prog_t, create_prog, compile_prog, get_code, get_code_size, get_log, get_log_size, check) bytes[source]

Compiles CUDA style code.

This function compiles the given program with the specified compile options and returns the compiled code as a byte object. If the compilation fails, it raises an error with the log information from the compiler.

Parameters:
  • prg (str) – The program to be compiled.

  • compile_options (List[str]) – A list of options for the compiler.

  • prog_t (ctypes type) – The ctypes type of the program.

  • create_prog (Callable) – Function to create a program.

  • compile_prog (Callable) – Function to compile a program.

  • get_code (Callable) – Function to get the compiled code.

  • get_code_size (Callable) – Function to get the size of the compiled code.

  • get_log (Callable) – Function to get the log information from the compiler.

  • get_log_size (Callable) – Function to get the size of the log information.

  • check (Callable) – Function to check for errors.

Returns:

The compiled code as a byte object.

Return type:

bytes

tinygrad.helpers.cpu_time_execution(cb, enable)[source]

Executes a given function and measures the CPU time taken for its execution.

Parameters:
  • cb (Callable[[], None]) – The callback function to be executed. It takes no arguments and returns nothing.

  • enable (bool) – If True, measure the CPU time of the function execution. Otherwise, just execute the function without measuring.

Returns:

The measured CPU time of the function execution if ‘enable’ is True, None otherwise.

Return type:

float | None

tinygrad.helpers.db_connection()[source]

Establishes a database connection.

This function checks if there is an active database connection. If not, it creates one and sets up necessary configurations.

Returns:

Database connection object.

tinygrad.helpers.dedup(x: Iterable[T])[source]

Remove duplicates from an iterable while preserving the order.

Parameters:

x (Iterable[T]) – The input iterable.

Returns:

A list with the same elements as the input, but without any duplicates. The order of the elements is preserved.

Return type:

List[T]

tinygrad.helpers.diskcache(func)[source]

Retrieve a value from the disk cache.

Parameters:
  • table (str) – The name of the table to retrieve data from.

  • key (Union[Dict, str, int]) – The key used to identify the stored value. If provided as an integer or string,

  • key. (it will be converted into a dictionary with 'key' as the) –

Returns:

The retrieved value from cache, if found. Otherwise, None.

Return type:

Any

Note

This function assumes that a valid connection to the database is established in the ‘db_connection()’ method and that the ‘CACHELEVEL’ variable is defined elsewhere in the code.

tinygrad.helpers.diskcache_get(table: str, key: Dict | str | int) Any[source]

Retrieve a value from the disk cache. Parameters: table (str): The name of the table to retrieve data from. key (Union[Dict, str, int]): The key used to identify the stored value. If provided as an integer or string, it will be converted into a dictionary with ‘key’ as the key. Returns: Any: The retrieved value from cache, if found. Otherwise, None.

Note

This function assumes that a valid connection to the database is established in the ‘db_connection()’ method and that the ‘CACHELEVEL’ variable is defined elsewhere in the code.

tinygrad.helpers.diskcache_put(table: str, key: Dict | str | int, val: Any)[source]

This function is used to put a value into the disk cache. It first checks if the cache level is set to 0, in which case it returns the value without caching. If the cache level is not 0, it proceeds with caching the value. It establishes a database connection and creates a cursor object for executing SQL commands. Next, it checks if the given table exists in the database. If it does not exist, it creates the table using the keys of the dictionary passed as the ‘key’ argument. The function then executes an SQL command to replace or insert the key-value pair into the table. Finally, it commits the changes to the database, closes the cursor and returns the value.

Parameters:
  • table (str) – The name of the table where the value will be stored.

  • key (Union[Dict, str, int]) – The key used for indexing the value in the cache. It can be a dictionary, string or integer.

  • val (Any) – The value to be stored in the cache. Can be of any type.

Returns:

Returns the value that was passed as an argument.

class tinygrad.helpers.dtypes[source]

Bases: object

Class representing data types for various operations.

This class contains static methods to check if a given data type is an integer, float, unsigned, etc., and also provides methods to get the data types from numpy and get all available data types in dictionary format.

The class also defines several Final variables representing different data types like bool, int8, int16, etc.

bfloat16: Final[DType] = (9, 2, '__bf16', None, 1)
bool: Final[DType] = (0, 1, 'bool', <class 'numpy.bool_'>, 1)
double = (11, 8, 'double', <class 'numpy.float64'>, 1)
static fields() Dict[str, DType][source]

Get a dictionary containing all available data types.

Returns:

A dictionary where the keys are the names of the data types and the values are their corresponding data type objects.

Return type:

Dict[str, DType]

float = (10, 4, 'float', <class 'numpy.float32'>, 1)
float16: Final[DType] = (9, 2, 'half', <class 'numpy.float16'>, 1)
float32: Final[DType] = (10, 4, 'float', <class 'numpy.float32'>, 1)
float64: Final[DType] = (11, 8, 'double', <class 'numpy.float64'>, 1)
static from_np(x) DType[source]

Get the data type object corresponding to a numpy data type object.

Parameters:

x – The numpy data type object.

Returns:

The data type object corresponding to the given numpy data type object.

Return type:

DType

half = (9, 2, 'half', <class 'numpy.float16'>, 1)
static imagef(shp)[source]

Creates an ImageDType instance with float32 data type and given shape.

Parameters:

shp (Tuple[int, int]) – Shape of the image.

Returns:

An instance of ImageDType with specified properties.

Return type:

ImageDType

static imageh(shp)[source]

Creates an ImageDType instance with float16 data type and given shape.

Parameters:

shp (Tuple[int, int]) – Shape of the image.

Returns:

An instance of ImageDType with specified properties.

Return type:

ImageDType

int = (5, 4, 'int', <class 'numpy.int32'>, 1)
int16: Final[DType] = (3, 2, 'short', <class 'numpy.int16'>, 1)
int32: Final[DType] = (5, 4, 'int', <class 'numpy.int32'>, 1)
int64: Final[DType] = (7, 8, 'long', <class 'numpy.int64'>, 1)
int8: Final[DType] = (1, 1, 'char', <class 'numpy.int8'>, 1)
static is_float(x: DType) bool[source]

Check if a given data type is a floating point number.

This method checks whether the provided data type is one of the float types like float16, float32, etc.

Parameters:

x (DType) – The data type to check.

Returns:

True if the given data type is a floating point number, False otherwise.

Return type:

bool

static is_int(x: DType) bool[source]

Check if a given data type is an integer.

This method checks whether the provided data type is one of the integer types like int8, int16, etc.

Parameters:

x (DType) – The data type to check.

Returns:

True if the given data type is an integer, False otherwise.

Return type:

bool

static is_unsigned(x: DType) bool[source]

Check if a given data type is an unsigned integer.

This method checks whether the provided data type is one of the unsigned integer types like uint8, uint16, etc.

Parameters:

x (DType) – The data type to check.

Returns:

True if the given data type is an unsigned integer, False otherwise.

Return type:

bool

uint16: Final[DType] = (4, 2, 'unsigned short', <class 'numpy.uint16'>, 1)
uint32: Final[DType] = (6, 4, 'unsigned int', <class 'numpy.uint32'>, 1)
uint64: Final[DType] = (8, 8, 'unsigned long', <class 'numpy.uint64'>, 1)
uint8: Final[DType] = (2, 1, 'unsigned char', <class 'numpy.uint8'>, 1)
tinygrad.helpers.encode_args_cuda_style(bufs, vals, device_ptr_t, marks) Tuple[Array, Structure][source]

This function encodes arguments using CUDA style. It initializes a C structure with given buffer and value inputs.

Parameters:
  • bufs (List[ctypes]) – A list of buffers for encoding.

  • vals (List[int]) – A list of values for encoding.

  • device_ptr_t (ctypes) – Device pointer type.

  • marks (List[ctypes]) – A list of marks for encoding.

Returns:

A tuple containing a C-style void pointer array and a C structure initialized with the given buffers and values.

Return type:

Tuple[ctypes.Array, ctypes.Structure]

tinygrad.helpers.fetch(url: str, name: Path | str | None = None, allow_caching=True) Path[source]

Fetch a file from a given URL and store it locally. Optionally provide a name for the local file.

If caching is enabled (which is the default behavior unless the “DISABLE_HTTP_CACHE” environment variable is set), this function will first check if the file has already been downloaded before attempting to download it again.

The fetched file is stored in a directory called “tinygrad/downloads” within the cache directory, unless an alternative name or path is provided.

If the size of the fetched file does not match the expected content length from the HTTP response, a RuntimeError will be raised.

Parameters:
  • url (str) – The URL of the file to download.

  • name (Optional[Union[pathlib.Path, str]]) – An optional name or path for the local file. If not provided, a hash of the URL is used as the filename.

  • allow_caching (bool) – Whether to cache downloaded files locally and check for existing files before downloading (defaults to True unless “DISABLE_HTTP_CACHE” env var is set).

Returns:

The path of the fetched file.

Return type:

pathlib.Path

tinygrad.helpers.flat_mv(mv: memoryview)[source]

Flattens a memory view.

Parameters:

mv – The memory view to be flattened. It is expected to be of type memoryview.

Returns:

A flattened version of the inputted memory view. The return type will be memoryview with element type “B” (unsigned char) and shape equal to the number of bytes in the original memory view. If the inputted memory view is empty, the function simply returns the memory view as it is already flat.

tinygrad.helpers.flatten(l: Iterable[Iterable[T]])[source]

Flattens a list of lists.

Parameters:

l (Iterable[Iterable[T]]) – An iterable containing iterables.

Returns:

A list with the elements of the nested iterables.

Return type:

List[T]

tinygrad.helpers.from_mv(mv, to_type=<class 'ctypes.c_char'>)[source]

This function takes a memoryview object ‘mv’ and optionally a ctypes data type ‘to_type’. It returns the result of casting the address of ‘to_type’ which is created from buffer ‘mv’ to a pointer of ‘to_type’. If no ‘to_type’ is provided, it defaults to ctypes.c_char.

Parameters:
  • mv (memoryview) – A memoryview object that needs to be converted.

  • to_type (ctypes data type, optional) – The ctypes data type to which the memoryview will be casted. Defaults to ctypes.c_char.

tinygrad.helpers.fromimport(mod, frm)[source]

Imports a specific attribute or module.

This function imports the specified attribute or module dynamically. It is particularly useful when you need to import a module or attribute that may not be available in all environments or versions.

Parameters:
  • mod (str) – The name of the module to import.

  • frm (str) – The specific attribute or submodule to import from the module.

Returns:

The imported attribute or module.

tinygrad.helpers.get_bytes(arg, get_sz, get_str, check) bytes[source]

Get the byte representation of a given argument using the provided functions.

Parameters:
  • arg – The input argument.

  • get_sz – A function that takes the input argument and a ctypes.c_size_t reference to fill with the size information.

  • get_str – A function that takes the input argument and a string buffer to fill with the actual data.

  • check – A function that checks whether an operation is successful or not. It takes the result of the previous two functions as argument.

Returns:

The byte representation of the given argument.

tinygrad.helpers.get_child(obj, key)[source]

Recursively retrieve child element from object using dot notation.

Parameters:
  • obj (Any) – Parent object

  • key (str) – Dot-separated string to access child elements

Returns:

Child element of the object specified by the key

Return type:

Any

tinygrad.helpers.getenv(key: str, default=0)[source]

Retrieve environment variable value by key or return a default value if not found.

Parameters:
  • key (str) – Environment variable name

  • default (Union[int, float, str]) – Default value to return if the environment variable is not found

Returns:

Value of the environment variable or the default value

Return type:

Union[int, float, str]

tinygrad.helpers.init_c_struct_t(fields: Tuple[Tuple[str, _SimpleCData], ...])[source]

Create a new class that inherits from ctypes.Structure and set its _pack_ and _fields_ attributes.

Parameters:

fields – A tuple of tuples containing the field name as string and field type which should inherit from ctypes._SimpleCData.

Returns:

The created class.

tinygrad.helpers.init_c_var(ctypes_var, creat_cb)[source]

Initialize a ctypes variable using the provided creation callback.

Parameters:
  • ctypes_var – The ctypes variable to initialize.

  • creat_cb – The creation callback function that takes the ctypes variable as argument and returns an initialized version of it.

Returns:

The initialized ctypes variable.

tinygrad.helpers.make_pair(x: int | Tuple[int, ...], cnt=2) Tuple[int, ...][source]

Converts a single integer or tuple of integers into a tuple of integers.

If the input is a single integer, it will be repeated to form a tuple of the specified length. If the input is a tuple, it will be returned unchanged.

Parameters:
  • x (Union[int, Tuple[int, ...]]) – The input value.

  • cnt (int) – The number of times to repeat the integer if the input is an integer.

Returns:

A tuple of integers.

Return type:

Tuple[int, …]

tinygrad.helpers.merge_dicts(ds: Iterable[Dict[T, U]]) Dict[T, U][source]

Merge dictionaries in a list.

This function takes an iterable of dictionaries as input and merges them into one dictionary. It checks for duplicate keys across the dictionaries and raises an assertion error if any are found.

Parameters:

ds – An iterable containing dictionaries with keys of type T and values of type U.

Returns:

A merged dictionary with keys of type T and values of type U.

tinygrad.helpers.partition(lst: List[T], fxn: Callable[[T], bool])[source]

Partition a list into two lists based on a predicate function.

This function takes a list and a predicate function as input and partitions the list into two lists based on the output of the predicate function applied to each element in the list. The first list contains elements for which the predicate is True, while the second list contains elements for which it is False.

Parameters:
  • lst – A list of type T.

  • fxn – A function that takes an element of type T and returns a boolean value.

Returns:

A tuple containing two lists: one with elements that satisfy the predicate, and another with elements that do not.

tinygrad.helpers.pretty_ptx(s)[source]

This function takes a string as input and modifies it based on various regular expressions. The purpose of these modifications is to highlight different parts of the string in different colors for easier readability.

Identifiers are matched and replaced with color(<expr>). Types are matched and replaced with color(<type>). Instructions are matched and replaced with color(<instruction>). Numbers are matched and replaced with color(<number>). Space, derivatives are also highlighted.

Parameters:

s (str) – The input string to be processed.

Returns:

The modified string with certain parts highlighted with different colors.

Return type:

str

tinygrad.helpers.prod(x: Iterable[T]) T | int[source]

Calculate the product of all elements in an iterable.

This function takes an iterable (e.g., list, tuple, set) as input and returns the product of all its elements. If the iterable is empty or contains only one element, the function will return 1 by default, regardless of the type of x.

Parameters:

x (Iterable[T]) – An iterable of elements to calculate the product for.

Returns:

The product of all elements in the iterable or 1 if the iterable is empty.

Return type:

Union[T, int]

tinygrad.helpers.round_up(num, amt: int)[source]

Rounds a number up to the nearest multiple of a given amount.

Parameters:
  • num (int) – The number to be rounded up.

  • amt (int) – The amount to round up to.

Returns:

The smallest integer greater than or equal to the input number that is a multiple of the given amount.

Return type:

int

tinygrad.helpers.strip_parens(fst: str)[source]

Removes parentheses from a string if they match correctly.

This function checks whether the first character is an open parenthesis and the last character is a closing parenthesis, and that there are no unmatched parentheses in between. If these conditions are met, it removes the parentheses; otherwise, it leaves the string as is.

Parameters:

fst (str) – The input string.

Returns:

The string with parentheses removed if they match correctly, or the original string otherwise.

Return type:

str

tinygrad.helpers.temp(x: str) str[source]

Get path to a file in the temporary directory.

Parameters:

x (str) – File name or relative file path

Returns:

Full path to the file in the temporary directory

Return type:

str

tinygrad.helpers.time_execution_cuda_style(cb, ev_t, evcreate, evrecord, evsync, evdestroy, evtime, enable=False) float | None[source]

This function measures the execution time of a given callback function cb in a CUDA-style manner.

Parameters:
  • cb (Callable[[], None]) – The callback function to be measured. It should be a callable object (function).

  • ev_t (Type[ctypes._CData]) – Event type. This is typically a ctypes data structure or class.

  • evcreate (Callable[[ctypes.pointer(ev_t), int], None]) – Function to create an event. It should take two arguments: the event object and flags.

  • evrecord (Callable[[ctypes.pointer(ev_t), Optional[Stream]], None]) – Function to record an event. It should take two arguments: the event object and stream.

  • evsync (Callable[[Optional[Stream]], None]) – Function to synchronize a stream. It should take one argument: the stream object.

  • evdestroy (Callable[[ctypes.pointer(ev_t)], None]) – Function to destroy an event. It should take one argument: the event object.

  • evtime (Callable[[ctypes.pointer(ctypes.c_float), ctypes.pointer(ev_t), ctypes.pointer(ev_t)], None]) – Function to calculate the elapsed time between two events. It should take three arguments: a pointer to the result, the start event, and the end event.

  • enable (bool) – If True, measure the execution time; if False, skip measuring and just run the callback function. Default is False.

Returns:

The elapsed time in milliseconds if timing was enabled, otherwise None.

Return type:

Optional[float]

tinygrad.helpers.to_char_p_p(options: ~typing.List[bytes], to_type=<class 'ctypes.c_char'>)[source]

This function takes a list of bytes ‘options’ and optionally a ctypes data type ‘to_type’. It returns the result of creating an array of pointers each pointing to a string buffer created from ‘options’, which are then casted to pointer of ‘to_type’. If no ‘to_type’ is provided, it defaults to ctypes.c_char.

Parameters:
  • options (List[bytes]) – A list of bytes that need to be converted.

  • to_type (ctypes data type, optional) – The ctypes data type to which the byte strings will be casted. Defaults to ctypes.c_char.

tinygrad.helpers.to_function_name(s: str)[source]

Convert string to a valid function name by replacing invalid characters with their ASCII code.

Parameters:

s (str) – Input string

Returns:

String converted into a valid function name

Return type:

str

tinygrad.helpers.unwrap(x: T | None) T[source]

Unwrap an optional value.

This function takes an optional value as input and unwraps it, returning the underlying value. It raises an assertion error if the input is None.

Parameters:

x – An optional value of type T.

Returns:

The underlying value of type T.

tinygrad.helpers.unwrap2(x: Tuple[T, Any]) T[source]

Unwrap a tuple with an optional error message.

This function takes a tuple containing a value and an optional error message as input and unwraps the value. It raises an assertion error if there is an error message in the tuple.

Parameters:

x – A tuple containing a value of type T and an optional error message.

Returns:

The underlying value of type T.