gflownet.utils.common

Functions

set_device(device)

Get torch device from device.

set_float_precision(precision)

Get torch float type from precision.

set_int_precision(precision)

Get torch integer type from int precision.

torch2np(x)

Convert a torch tensor to a numpy array.

download_file_if_not_exists(path, url)

Download a file from google drive if path doestn't exist.

resolve_path(path)

Resolve a path by expanding environment variables, user home directory, and making

find_latest_checkpoint(ckpt_dir)

Find the latest checkpoint in the input directory.

read_hydra_config([rundir, config_name])

gflownet_from_config(config[, env])

Create GFlowNet from a Hydra OmegaConf config.

load_gflownet_from_rundir(rundir[, no_wandb, ...])

Load GFlowNet from a run path (directory with a .hydra directory inside).

batch_with_rest(start, stop, step[, tensor])

Yields batches of indices from start to stop with step size. The last batch may be

tfloat(x, device, float_type)

Convert input to a float tensor. If the input is a list of tensors, the tensors

tlong(x, device)

Convert input to a long tensor. If the input is a list of tensors, the tensors

tint(x, device, int_type)

Convert input to an integer tensor. If the input is a list of tensors, the tensors

tbool(x, device)

Convert input to a boolean tensor. If the input is a list of tensors, the tensors

concat_items(list_of_items[, indices])

Concatenates a list of items into a single tensor or array.

extend(orig, new)

Extends the original list or tensor with the new list or tensor.

copy(x)

Makes copy of the input tensor or list.

bootstrap_samples(tensor, num_samples)

Bootstraps tensor along the last dimention

example_documented_function(arg1, arg2)

Summary line: this function is not used anywhere, it's just an example.

select_indices(iterable[, indices])

Select elements form iterable

Module Contents

gflownet.utils.common.set_device(device)[source]

Get torch device from device.

Examples

>>> set_device("cuda")
device(type='cuda')
>>> set_device("cpu")
device(type='cpu')
>>> set_device(torch.device("cuda"))
device(type='cuda')
Parameters:

device (Union[str, torch.device]) – Device.

Returns:

torch.devicetorch device.

gflownet.utils.common.set_float_precision(precision)[source]

Get torch float type from precision.

Examples

>>> set_float_precision(32)
torch.float32
>>> set_float_precision(torch.float32)
torch.float32
Parameters:

precision (Union[int, torch.dtype]) – Precision.

Returns:

torch.dtypetorch float type.

Raises:

ValueError – If precision is not one of [16, 32, 64].

gflownet.utils.common.set_int_precision(precision)[source]

Get torch integer type from int precision.

Examples

>>> set_int_precision(32)
torch.int32
>>> set_int_precision(torch.int32)
torch.int32
Parameters:

precision (Union[int, torch.dtype]) – Integer precision.

Returns:

torch.dtypetorch integer type.

Raises:

ValueError – If precision is not one of [16, 32, 64].

gflownet.utils.common.torch2np(x)[source]

Convert a torch tensor to a numpy array.

Parameters:

x (Union[torch.Tensor, np.ndarray, list]) – Data to be converted.

Returns:

np.ndarray – Converted data.

gflownet.utils.common.download_file_if_not_exists(path, url)[source]

Download a file from google drive if path doestn’t exist. url should be in the format: https://drive.google.com/uc?id=FILE_ID

Parameters:
  • path (str)

  • url (str)

gflownet.utils.common.resolve_path(path)[source]

Resolve a path by expanding environment variables, user home directory, and making it absolute.

Examples

>>> resolve_path("~/scratch/$SLURM_JOB_ID/data")
Path("/home/user/scratch/12345/data")
Parameters:

path (Union[str, Path]) – Path to be resolved.

Returns:

Path – Resolved path.

Return type:

pathlib.Path

gflownet.utils.common.find_latest_checkpoint(ckpt_dir)[source]

Find the latest checkpoint in the input directory.

If the directory contains a checkpoint file with the name “final”, that checkpoint is returned. Otherwise, the latest checkpoint is returned based on the iteration number set in the file names.

Parameters:

ckpt_dir (Union[str, Path]) – Directory in which to search for the checkpoints.

Returns:

Path – Path to the latest checkpoint.

Raises:

ValueError – If no checkpoint files are found in the input directory.

gflownet.utils.common.read_hydra_config(rundir=None, config_name='config')[source]
gflownet.utils.common.gflownet_from_config(config, env=None)[source]

Create GFlowNet from a Hydra OmegaConf config.

Parameters:
  • config (DictConfig) – Config.

  • env (GFlowNetEnv) – Optional environment instance to be used in the initialization.

Returns:

GFN – GFlowNet.

gflownet.utils.common.load_gflownet_from_rundir(rundir, no_wandb=True, print_config=False, device=None, load_last_checkpoint=True, is_resumed=False)[source]

Load GFlowNet from a run path (directory with a .hydra directory inside).

Parameters:
  • rundir (Union[str, Path]) – Path to the run directory. Must contain a .hydra directory.

  • no_wandb (bool, optional) – Whether to disable wandb in the GFN init, by default True.

  • print_config (bool, optional) – Whether to print the loaded config, by default False.

  • device (str, optional) – Device to which the models should be moved. If None (default), take the device from the loaded config.

  • load_last_checkpoint (bool, optional) – Whether to load the final models, by default True.

  • is_resumed (bool, optional) – Whether the GFlowNet is loaded to resume training.

Returns:

Tuple[GFN, DictConfig] – Loaded GFlowNet and the loaded config.

Raises:

ValueError – If no checkpoints are found in the directory.

gflownet.utils.common.batch_with_rest(start, stop, step, tensor=False)[source]

Yields batches of indices from start to stop with step size. The last batch may be smaller than step.

Parameters:
  • start (int) – Start index

  • stop (int) – End index (exclusive)

  • step (int) – Step size

  • tensor (bool, optional) – Whether to return a torch tensor of indices instead of a numpy array, by default False.

Yields:

Union[np.ndarray, torch.Tensor] – Batch of indices

gflownet.utils.common.tfloat(x, device, float_type)[source]

Convert input to a float tensor. If the input is a list of tensors, the tensors are stacked along the first dimension.

The resulting tensor is moved to the specified device.

Parameters:
  • x (Union[List[torch.Tensor], torch.Tensor, List[Union[int, float]], Union[int,)

  • float]] – Input to be converted to a float tensor.

  • device (torch.device) – Device to which the tensor should be moved.

  • float_type (torch.dtype) – Float type to which the tensor should be converted.

Returns:

Union[torch.Tensor, List[torch.Tensor]] – Float tensor.

gflownet.utils.common.tlong(x, device)[source]

Convert input to a long tensor. If the input is a list of tensors, the tensors are stacked along the first dimension.

The resulting tensor is moved to the specified device.

Parameters:
  • x (Union[List[torch.Tensor], torch.Tensor, List[Union[int, float]], Union[int,)

  • float]] – Input to be converted to a long tensor.

  • device (torch.device) – Device to which the tensor should be moved.

Returns:

Union[torch.Tensor, List[torch.Tensor]] – Long tensor.

gflownet.utils.common.tint(x, device, int_type)[source]

Convert input to an integer tensor. If the input is a list of tensors, the tensors are stacked along the first dimension.

The resulting tensor is moved to the specified device.

Parameters:
  • x (Union[List[torch.Tensor], torch.Tensor, List[Union[int, float]], Union[int,)

  • float]] – Input to be converted to an integer tensor.

  • device (torch.device) – Device to which the tensor should be moved.

  • int_type (torch.dtype) – Integer type to which the tensor should be converted.

Returns:

Union[torch.Tensor, List[torch.Tensor]] – Integer tensor.

gflownet.utils.common.tbool(x, device)[source]

Convert input to a boolean tensor. If the input is a list of tensors, the tensors are stacked along the first dimension.

The resulting tensor is moved to the specified device.

Parameters:
  • x (Union[List[torch.Tensor], torch.Tensor, List[Union[int, float]], Union[int,)

  • float]] – Input to be converted to a boolean tensor.

  • device (torch.device) – Device to which the tensor should be moved.

Returns:

Union[torch.Tensor, List[torch.Tensor]] – Boolean tensor.

gflownet.utils.common.concat_items(list_of_items, indices=None)[source]

Concatenates a list of items into a single tensor or array.

Parameters:
  • list_of_items – List of items to be concatenated, i.e. list of arrays or list of tensors.

  • indices (Union[List[np.ndarray], List[torch.Tensor]], optional) – Indices to select in the resulting concatenated tensor or array, by default None.

Returns:

Union[np.ndarray, torch.Tensor] – Concatenated tensor or array, with optional selection of indices.

Raises:

NotImplementedError – If the input type is not supported, i.e., not a list of arrays or a list of tensors.

gflownet.utils.common.extend(orig, new)[source]

Extends the original list or tensor with the new list or tensor.

Returns:

Union[List, TensorType[”…”]] – Extended list or tensor.

Raises:

NotImplementedError – If the input type is not supported, i.e., not a list or a tensor.

Parameters:
  • orig (Union[List, torchtyping.TensorType[...]])

  • new (Union[List, torchtyping.TensorType[...]])

Return type:

Union[List, torchtyping.TensorType[…]]

gflownet.utils.common.copy(x)[source]

Makes copy of the input tensor or list.

A tensor is cloned and detached from the computational graph.

Parameters:

x (Union[List, TensorType["..."]]) – Input tensor or list to be copied.

Returns:

Union[List, TensorType[”…”]] – Copy of the input tensor or list.

gflownet.utils.common.bootstrap_samples(tensor, num_samples)[source]

Bootstraps tensor along the last dimention returns tensor of the shape [initial_shape, num_samples]

gflownet.utils.common.example_documented_function(arg1, arg2)[source]

Summary line: this function is not used anywhere, it’s just an example.

Extended description of function from the docstrings tutorial Full Example.

Refer to

Prepenend with ~ to refer to the name of the object only instead of the full path -> set_device() will display as set_device instead of the full path.

Great maths:

\[\int_0^1 x^2 dx = \frac{1}{3}\]

Important

A docstring with math MUST be a raw Python string (a string prepended with an r: r"raw") to avoid backslashes being treated as escape characters.

Alternatively, you can use double backslashes.

Warning

Display a warning. See Learn by example. (<– this is a cross reference, learn about it here)

Examples

>>> function(1, 'a')
True
>>> function(1, 2)
True
>>> function(1, 1)
Traceback (most recent call last):
    ...

Notes

This block uses $ ... $ for inline maths -> \(e^{\frac{x}{2}}\).

Or $$ ... $$ for block math instead of the .. math: directive above.

\[\int_0^1 x^2 dx = \frac{1}{3}\]

Parameters:
  • arg1 (int) – Description of arg1

  • arg2 (str) – Description of arg2

Returns:

bool – Description of return value

gflownet.utils.common.select_indices(iterable, indices=None)[source]

Select elements form iterable

Parameters:
  • iterable (list, tuple, tensor or np.ndarray) – An iterable to select elements from. It can have multiple dimensions and selection is always preformed over the first dimension

  • indices (list, tuple, tensor or np.ndarray) – 1-dimentional sequence of indecies for selecting elements, optional. If None, the iterable will be returned as is. Default None

Returns:

list, tuple, tensor or np.ndarray – A sequence of selected elements. The type of the returned sequence is the same as the type of the input iterable