quantdata.jax#
Jax Base#
Base import for jax based computations.
- class quantdata.jax.BaseEnvironment(*, precision=32, device=None, debug=False)[source]#
Bases:
objectBase environment class to control overall jax model behaviour.
- Parameters:
precision (
int) – Default floating point and integer precision for the model.device (
Device|str|None) – Jax device to use for the model. IfNone, it will use “cuda” if available, otherwise “cpu”.debug (
bool) – Whether to enable debug mode. If True, the model will perform additional checks and print debug information. This is heavy; avoid in a production setting.
- class quantdata.jax.BaseModel(env)[source]#
Bases:
ModuleBase class for jax models
- property env: BaseEnvironment#
Get environment
- property device: Device#
Get device from environment
- verify(condition, message, exception=<class 'RuntimeError'>)[source]#
Verify condition and print message if
debugis enabled viaBaseEnvironment. This is a helper function to perform checks during model execution. Ifdebugis not enabled, this function is a NOOP.- Parameters:
condition (callable) – A function that returns a boolean tensor. This function will be called only if
debugis True.message (str | callable) – A string or a function that returns a string. This will be called only if
debugis True and the condition is not satisfied.exception (Exception) – The type of exception to raise if the condition is not satisfied.
- debug_detect_grad_issues()[source]#
Detect common gradient issues such as missing gradients or non-finite values. This is a helper function to perform checks during model execution. If any issues are detected, an AssertionError will be raised with a message describing the problems.
If
debugis not enabled, this function is a NOOP.
- debug_finite_parameters()[source]#
Verify that all parameters in the model are finite. This is a helper function to perform checks during model execution. If any non-finite parameters are detected, an AssertionError will be raised with a message describing the problems
If
debugis not enabled, this function is a NOOP.
- class quantdata.jax.BaseVisualizer[source]#
Bases:
objectBase visualizer class to visualize model performance during training and validation.
- class quantdata.jax.BaseSampler[source]#
Bases:
objectBase sampler class to sample data for training and validation.
- quantdata.jax.check(tensor, item, shape=None, dtype=None, optional=False)[source]#
Check that a tensor satisfies the specified shape and dtype requirements. This is a helper function to perform checks during model execution.
- Parameters:
tensor (torch.Tensor) – The tensor to check.
item (str) – The name of the item being checked, used for error messages.
shape (tuple | int | None) – The expected shape of the tensor. If int, it will be converted to a tuple with one element. If None, no shape check will be performed.
dtype (type | None) – The expected dtype of the tensor. If None, no dtype check will be performed.
optional (bool) – Whether the tensor is optional. If True, the function will return None if the tensor is None. If False, the function will raise an error if the tensor is None.
- quantdata.jax.get(d, item, shape=None, dtype=None, optional=False)[source]#
Helper function to get an item from a dictionary and perform checks on its shape and dtype. This is useful for verifying the structure of the input data during model execution.
- Parameters:
d (dict) – The dictionary to get the item from.
item (str) – The key of the item to get from the dictionary.
shape (tuple | int | None) – The expected shape of the item. If int, it will be converted to a tuple with one element. If None, no shape check will be performed.
dtype (type | None) – The expected dtype of the item. If None, no dtype check will be performed.
optional (bool) – Whether the item is optional. If True, the function will return None if the item is not found in the dictionary.
- quantdata.jax.pop(d, item, shape=None, dtype=None, optional=False)[source]#
Helper function to pop an item from a dictionary and perform checks on its shape and dtype. This is useful for verifying the structure of the input data during model execution.
- Parameters:
d (dict) – The dictionary to pop the item from.
item (str) – The key of the item to get from the dictionary.
shape (tuple | int | None) – The expected shape of the item. If int, it will be converted to a tuple with one element. If None, no shape check will be performed.
dtype (type | None) – The expected dtype of the item. If None, no dtype check will be performed.
optional (bool) – Whether the item is optional. If True, the function will return None if the item is not found in the dictionary.
- quantdata.jax.are_broadcastable(shape1, shape2)[source]#
Check if two shapes are broadcastable according to PyTorch’s broadcasting rules.
- quantdata.jax.lazy_verify(debug, condition, message, exception=<class 'RuntimeError'>)[source]#
Verify condition and print message if
debugis enabled viaBaseEnvironment. This is a helper function to perform checks during model execution. Ifdebugis not enabled, this function is a NOOP.- Parameters:
condition (callable) – A function that returns a boolean tensor. This function will be called only if
debugis True.message (str | callable) – A string or a function that returns a string. This will be called only if
debugis True and the condition is not satisfied.exception (Exception) – The type of exception to raise if the condition is not satisfied.
- class quantdata.jax.MetricTacker[source]#
Bases:
objectSimple utility class to track metrics during training