mani_skill.utils.common module#

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

Functions

mani_skill.utils.common.append_dict_array(x1: dict | Sequence | Tensor | ndarray, x2: dict | Sequence | Tensor | ndarray)[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

mani_skill.utils.common.batch(*args: Tuple[Tensor | ndarray | Sequence])[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

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

Compute angle (radian) between two torch tensors

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

In place recursive merge of merge_dct into dct

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

Flatten a dict by expanding its keys recursively.

mani_skill.utils.common.flatten_state_dict(state_dict: dict, use_torch=False, device: device | str | None = None) Tensor | ndarray | Sequence[source]#

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

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

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

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: int | slice, inplace=True)[source]#

Indexes every array in x1 with slice and returns result.

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

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

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

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

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

Compute angle (radian) between two numpy arrays

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: Tensor, b: Tensor) Tensor[source]#

Get the difference in radians between two quaternions.

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

  • b – second quaternion, shape (N, 4)

Returns:

Difference in radians, shape (N,)

mani_skill.utils.common.to_cpu_tensor(array: Tensor | ndarray | Sequence)[source]#

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

mani_skill.utils.common.to_numpy(array: Tensor | ndarray | Sequence, dtype=None) ndarray[source]#
mani_skill.utils.common.to_tensor(array: Tensor | ndarray | Sequence, device: device | str | None = 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 – The data to map to a tensor

  • 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: dict) dict[source]#

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

mani_skill.utils.common.unbatch(*args: Tuple[Tensor | ndarray | Sequence])[source]#