quantdata.torch#
Torch base
- class quantdata.torch.BaseEnvironment(*, precision=32, device=None, debug=False)[source]#
Bases:
objectBase environment class to control overall torch model behaviour.
- Parameters:
precision (
int) – Default floating point and integer precision for the model.device (
device|str|None) – Torch 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.
- property fdtype: dtype#
Get default floating point dtype
- property idtype: dtype#
Get default integer dtype
- class quantdata.torch.BaseModel(env)[source]#
Bases:
ModuleBase class for torch models
- class ReturnWhat(*values)[source]#
Bases:
IntEnum- LOSS_WEIGHT = 0#
return float
loss(with gradient) and floatweight(without gradient) for training.
- VALUATION = 1#
return
loss,weightand a dictionary of additional float metrics.
- PATHWISE = 2#
return pathwise metrics
- property env: BaseEnvironment#
Get environment
- property fdtype: dtype#
Get default floating point dtype from environment
- property device: device#
Get device from environment
- property idtype: dtype#
Get default integer dtype from environment
- forward(data, training=False, return_what=ReturnWhat.LOSS_WEIGHT)[source]#
Forward method.
- Parameters:
data (dict) – Input data dictionary containing tensors for the model. The expected keys and tensor shapes depend on the specific model implementation.
training (bool) – Whether the model is in training mode.
return_what (ReturnWhat) – Specifies what the model should return. Options are: -
ReturnWhat.LOSS_WEIGHT: return floatloss(with gradient) and floatweight(without gradient) for training. -ReturnWhat.VALUATION: returnloss,weightand a dictionary of additional float metrics. -ReturnWhat.PATHWISE: return pathwise metrics as dictionary.
- visualizer(sampler)[source]#
Return visualizer for this model. This is a helper function to create a visualizer that can be used to visualize the model’s performance during training and validation.
- 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.torch.BaseVisualizer[source]#
Bases:
objectBase visualizer class to visualize model performance during training and validation.
- class quantdata.torch.BaseSampler[source]#
Bases:
objectBase sampler class to sample data for training and validation.
- class quantdata.torch.BaseSamplerConfig[source]#
Bases:
objectBase class for a sampler configuration
- quantdata.torch.check(tensor, item, shape=None, dtype=None, optional=False, d=None)[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 (
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.torch.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.torch.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.torch.are_broadcastable(shape1, shape2)[source]#
Check if two shapes are broadcastable according to PyTorch’s broadcasting rules.
- quantdata.torch.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.torch.MetricTacker[source]#
Bases:
objectSimple utility class to track metrics during training