quantdata.torch#

Torch base

class quantdata.torch.DeviceLock(fall_back=False)[source]#

Bases: object

property device: device#
release()[source]#
allocate_gpu(fall_back=False)[source]#

Obtain a GPU device lock

class quantdata.torch.BaseEnvironment(*, precision=32, device=None, debug=False)[source]#

Bases: object

Base 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. If None, 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

static get_number_of_cuda_devices()[source]#

Get the number of available CUDA devices.

class quantdata.torch.BaseModel(env)[source]#

Bases: Module

Base class for torch models

class ReturnWhat(*values)[source]#

Bases: IntEnum

LOSS_WEIGHT = 0#

return float loss (with gradient) and float weight (without gradient) for training.

VALUATION = 1#

return loss, weight and a dictionary of additional float metrics.

PATHWISE = 2#

return pathwise metrics

property env: BaseEnvironment#

Get environment

property debug: bool#

Whether debug mode is enabled

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 float loss (with gradient) and float weight (without gradient) for training. - ReturnWhat.VALUATION: return loss, weight and 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 debug is enabled via BaseEnvironment. This is a helper function to perform checks during model execution. If debug is not enabled, this function is a NOOP.

Parameters:
  • condition (callable) – A function that returns a boolean tensor. This function will be called only if debug is True.

  • message (str | callable) – A string or a function that returns a string. This will be called only if debug is 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 debug is 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 debug is not enabled, this function is a NOOP.

compute_grad_norm()[source]#

Compute the average L2 norm of the gradients across all parameters in the model. This is a helper function to perform checks during model execution. If there are no gradients, this function will return None.

class quantdata.torch.BaseVisualizer[source]#

Bases: object

Base visualizer class to visualize model performance during training and validation.

update(model, epoch)[source]#

Update visualizer with new model and epoch number

class quantdata.torch.BaseSampler[source]#

Bases: object

Base sampler class to sample data for training and validation.

sample(epoch)[source]#

Sample data for a given epoch number. The expected output is a dictionary containing tensors for the model. The specific keys and tensor shapes depend on the model implementation.

class quantdata.torch.BaseSamplerConfig[source]#

Bases: object

Base class for a sampler configuration

instantiate_from_cache(*, verbose=<cdxcore.verbose.Context object>)[source]#

Creates the Sampler corresponding to this config from cache only. This function will fail if not all data are availanle.

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 debug is enabled via BaseEnvironment. This is a helper function to perform checks during model execution. If debug is not enabled, this function is a NOOP.

Parameters:
  • condition (callable) – A function that returns a boolean tensor. This function will be called only if debug is True.

  • message (str | callable) – A string or a function that returns a string. This will be called only if debug is 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: object

Simple utility class to track metrics during training

record(epoch, metrics)[source]#

Update the metric tracker with new metrics and their corresponding weights

record_metric(epoch, name, mean, std=None)[source]#

Add a single metric

items()[source]#

Get all metrics as an iterable of (name, epochs, values) tuples

get(name, default=None)[source]#

Get metric by name with default

state_dict()[source]#

Get a serializable copy of the tracked metrics as numpy arrays

static load_state_dict(state)[source]#

Load tracked metrics from a serializable state

Modules#