Skip to content

Dtypes

simplegrad.core.dtypes.set_dtype(dtype: str)

Set global default dtype (string key).

Source code in simplegrad/core/dtypes.py
def set_dtype(dtype: str):
    """Set global default dtype (string key)."""
    global _DTYPE
    if dtype not in DTYPES:
        raise ValueError(f"Unsupported dtype '{dtype}'. Must be one of {list(DTYPES)}.")
    _DTYPE = dtype

simplegrad.core.dtypes.get_global_dtype()

Return current dtype string key.

Source code in simplegrad/core/dtypes.py
def get_global_dtype():
    """Return current dtype string key."""
    return _DTYPE

simplegrad.core.dtypes.get_global_dtype_class()

Return current NumPy dtype class.

Source code in simplegrad/core/dtypes.py
def get_global_dtype_class():
    """Return current NumPy dtype class."""
    return DTYPES[_DTYPE]

simplegrad.core.dtypes.get_dtype_class(dtype: str)

Return NumPy dtype class for given dtype string key.

Source code in simplegrad/core/dtypes.py
def get_dtype_class(dtype: str):
    """Return NumPy dtype class for given dtype string key."""
    if dtype not in DTYPES:
        raise ValueError(f"Unsupported dtype '{dtype}'. Must be one of {list(DTYPES)}.")
    return DTYPES[dtype]

simplegrad.core.dtypes.as_array(values, dtype=None, **kwargs)

Convert values into numpy array with current dtype.

Source code in simplegrad/core/dtypes.py
def as_array(values, dtype=None, **kwargs):
    """Convert values into numpy array with current dtype."""
    return np.array(
        values, dtype=dtype if dtype is not None else get_global_dtype_class(), **kwargs
    )

simplegrad.core.dtypes.convert_to_dtype(array: np.ndarray, dtype: str | None = None)

Convert a numpy array to the global default dtype.

Source code in simplegrad/core/dtypes.py
def convert_to_dtype(array: np.ndarray, dtype: str | None = None):
    """Convert a numpy array to the global default dtype."""
    return array.astype(get_global_dtype_class() if dtype is None else DTYPES[dtype])