Creation Operations#

Functions for creating Genesis 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 Tensors#

Standard PyTorch tensors become Genesis 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 tensor
pos = robot.get_pos()
combined = pos + external  # Result is Genesis 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#