mani_skill.utils.common#

Common utilities often reused for internal code and task building for users.

Functions#

_batch(array)

_to_numpy(array)

_unbatch(array)

append_dict_array(x1, x2)

Append x2 in front of x1 and returns the result. Tries to do this in place if possible.

batch(*args)

Adds one dimension in front of everything. If given a dictionary, every leaf in the dictionary

compute_angle_between(x1, x2)

Compute angle (radian) between two torch tensors

dict_merge(dct, merge_dct)

In place recursive merge of merge_dct into dct

flatten_dict_keys(d[, prefix])

Flatten a dict by expanding its keys recursively.

flatten_state_dict(state_dict[, use_torch, device])

Flatten a dictionary containing states recursively. Expects all data to be either torch or numpy

index_dict_array(x1, idx[, inplace])

Indexes every array in x1 with slice and returns result.

merge_dicts(ds[, asarray])

Merge multiple dicts with the same keys to a single one.

normalize_vector(x[, eps])

normalizes a given torch tensor x and if the norm is less than eps, set the norm to 0

np_compute_angle_between(x1, x2)

Compute angle (radian) between two numpy arrays

np_normalize_vector(x[, eps])

normalizes a given numpy array x and if the norm is less than eps, set the norm to 0

quat_diff_rad(a, b)

Get the difference in radians between two quaternions.

to_cpu_tensor(array)

Maps any given sequence to a torch tensor on the CPU.

to_numpy(array[, dtype])

to_tensor(array[, device])

Maps any given sequence to a torch tensor on the CPU/GPU. If physx gpu is not enabled then we use CPU, otherwise GPU, unless specified

torch_clone_dict(data)

Recursively clones all torch tensors in a dictionary.

unbatch(*args)

Module Contents#

mani_skill.utils.common._batch(array)[source]#
Parameters:

array (Union[mani_skill.utils.structs.types.Array, Sequence]) –

mani_skill.utils.common._to_numpy(array)[source]#
Parameters:

array (Union[mani_skill.utils.structs.types.Array, Sequence]) –

Return type:

numpy.ndarray

mani_skill.utils.common._unbatch(array)[source]#
Parameters:

array (Union[mani_skill.utils.structs.types.Array, Sequence]) –

mani_skill.utils.common.append_dict_array(x1, x2)[source]#

Append x2 in front of x1 and returns the result. Tries to do this in place if possible. Assumes both x1, x2 have the same dictionary structure if they are dictionaries. They may also both be lists/sequences in which case this is just appending like normal

Parameters:
  • x1 (Union[dict, Sequence, mani_skill.utils.structs.types.Array]) –

  • x2 (Union[dict, Sequence, mani_skill.utils.structs.types.Array]) –

mani_skill.utils.common.batch(*args)[source]#

Adds one dimension in front of everything. If given a dictionary, every leaf in the dictionary has a new dimension. If given a tuple, returns the same tuple with each element batched

Parameters:

args (Tuple[Union[mani_skill.utils.structs.types.Array, Sequence]]) –

mani_skill.utils.common.compute_angle_between(x1, x2)[source]#

Compute angle (radian) between two torch tensors

Parameters:
  • x1 (torch.Tensor) –

  • x2 (torch.Tensor) –

mani_skill.utils.common.dict_merge(dct, merge_dct)[source]#

In place recursive merge of merge_dct into dct

Parameters:
  • dct (dict) –

  • merge_dct (dict) –

mani_skill.utils.common.flatten_dict_keys(d, prefix='')[source]#

Flatten a dict by expanding its keys recursively.

Parameters:

d (dict) –

mani_skill.utils.common.flatten_state_dict(state_dict, use_torch=False, device=None)[source]#

Flatten a dictionary containing states recursively. Expects all data to be either torch or numpy

Parameters:
  • state_dict (dict) – a dictionary containing scalars or 1-dim vectors.

  • use_torch (bool) – Whether to convert the data to torch tensors.

  • device (Optional[mani_skill.utils.structs.types.Device]) –

Raises:

AssertionError – If a value of @state_dict is an ndarray with ndim > 2.

Returns:

flattened states.

Return type:

np.ndarray | torch.Tensor

Notes

The input is recommended to be ordered (e.g. dict). However, since python 3.7, dictionary order is guaranteed to be insertion order.

mani_skill.utils.common.index_dict_array(x1, idx, inplace=True)[source]#

Indexes every array in x1 with slice and returns result.

Parameters:

idx (Union[int, slice]) –

mani_skill.utils.common.merge_dicts(ds, asarray=False)[source]#

Merge multiple dicts with the same keys to a single one.

Parameters:

ds (Sequence[dict]) –

mani_skill.utils.common.normalize_vector(x, eps=1e-06)[source]#

normalizes a given torch tensor x and if the norm is less than eps, set the norm to 0

Parameters:

x (torch.Tensor) –

mani_skill.utils.common.np_compute_angle_between(x1, x2)[source]#

Compute angle (radian) between two numpy arrays

Parameters:
  • x1 (numpy.ndarray) –

  • x2 (numpy.ndarray) –

mani_skill.utils.common.np_normalize_vector(x, eps=1e-06)[source]#

normalizes a given numpy array x and if the norm is less than eps, set the norm to 0

mani_skill.utils.common.quat_diff_rad(a, b)[source]#

Get the difference in radians between two quaternions.

Parameters:
  • a (torch.Tensor) – first quaternion, shape (N, 4)

  • b (torch.Tensor) – second quaternion, shape (N, 4)

Returns:

Difference in radians, shape (N,)

Return type:

torch.Tensor

mani_skill.utils.common.to_cpu_tensor(array)[source]#

Maps any given sequence to a torch tensor on the CPU.

Parameters:

array (mani_skill.utils.structs.types.Array) –

mani_skill.utils.common.to_numpy(array, dtype=None)[source]#
Parameters:

array (Union[mani_skill.utils.structs.types.Array, Sequence]) –

Return type:

numpy.ndarray

mani_skill.utils.common.to_tensor(array, device=None)[source]#

Maps any given sequence to a torch tensor on the CPU/GPU. If physx gpu is not enabled then we use CPU, otherwise GPU, unless specified by the device argument

Parameters:
  • array (mani_skill.utils.structs.types.Array) – The data to map to a tensor

  • device (Optional[mani_skill.utils.structs.types.Device]) – The device to put the tensor on. By default this is None and to_tensor will put the device on the GPU if physx is enabled and CPU otherwise

mani_skill.utils.common.torch_clone_dict(data)[source]#

Recursively clones all torch tensors in a dictionary. If the input was a torch tensor, it will return a clone of the tensor.

Parameters:

data (dict) –

Return type:

dict

mani_skill.utils.common.unbatch(*args)[source]#
Parameters:

args (Tuple[Union[mani_skill.utils.structs.types.Array, Sequence]]) –