Creation operations#

Functions for creating Genesis World tensors that are compatible with differentiable simulation.

Overview#

When working with differentiable simulation, use these functions to create tensors that properly integrate with the gradient system.

Creating tensors#

From Python values#

import genesis as gs
import torch

gs.init()

# Create tensor on correct device
tensor = torch.tensor([1.0, 2.0, 3.0], device=gs.device, dtype=gs.tc_float)

# With gradient tracking
tensor = torch.tensor(
    [1.0, 2.0, 3.0],
    device=gs.device,
    dtype=gs.tc_float,
    requires_grad=True,
)

Zeros/ones#

# Create zero tensor
zeros = torch.zeros(10, device=gs.device, dtype=gs.tc_float)

# Create ones tensor
ones = torch.ones(10, device=gs.device, dtype=gs.tc_float)

# With gradient tracking
zeros_grad = torch.zeros(10, device=gs.device, dtype=gs.tc_float, requires_grad=True)

Random tensors#

# Random uniform [0, 1)
rand = torch.rand(10, device=gs.device, dtype=gs.tc_float)

# Random normal
randn = torch.randn(10, device=gs.device, dtype=gs.tc_float)

Converting to Genesis World tensors#

Standard PyTorch tensors become Genesis World tensors when combined with scene state:

# Standard PyTorch tensor
external = torch.tensor([1.0, 2.0, 3.0], device=gs.device, requires_grad=True)

# Combine with scene state -> Genesis World tensor
pos = robot.get_pos()
combined = pos + external  # Result is Genesis World tensor

API reference#

genesis.grad.creation_ops.torch_op_wrapper(torch_op)[source]#
genesis.grad.creation_ops.from_torch(torch_tensor, dtype=None, requires_grad=False, detach=True, scene=None)[source]#

By default, detach is True, meaning that this function returns a new leaf tensor which is not connected to torch_tensor’s computation gragh.

See also#