ttnn.clip

ttnn.clip(input_tensor: ttnn.Tensor, *, min: float or ttnn.Tensor = None, max: float or ttnn.Tensor = None, memory_config: ttnn.MemoryConfig = None) ttnn.Tensor

Performs clip function on input_tensor, min, max. Only one of ‘min’ or ‘max’ value can be None.

Parameters:

input_tensor (ttnn.Tensor) – the input tensor.

Keyword Arguments:
  • min (float or ttnn.Tensor) – Minimum value. Defaults to None.

  • max (float or ttnn.Tensor) – Maximum value. Defaults to None.

  • memory_config (ttnn.MemoryConfig, optional) – Memory configuration for the operation. Defaults to None.

Returns:

ttnn.Tensor – the output tensor.

Note

Supported dtypes, layouts, and ranks:

Dtypes

Layouts

Ranks

BFLOAT16

TILE

2, 3, 4

Example

# Create tensors for clipping with tensor bounds
input_tensor = ttnn.from_torch(
    torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16),
    dtype=ttnn.bfloat16,
    layout=ttnn.TILE_LAYOUT,
    device=device,
)
min_tensor = ttnn.from_torch(
    torch.tensor([[0, 2], [0, 4]], dtype=torch.bfloat16),
    dtype=ttnn.bfloat16,
    layout=ttnn.TILE_LAYOUT,
    device=device,
)
max_tensor = ttnn.from_torch(
    torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16),
    dtype=ttnn.bfloat16,
    layout=ttnn.TILE_LAYOUT,
    device=device,
)

# Clip values using tensor bounds
output = ttnn.clip(input_tensor, min_tensor, max_tensor)
logger.info(f"Clip with tensor bounds: {output}")

# Create tensor for clipping with scalar bounds
input_tensor = ttnn.from_torch(
    torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16),
    dtype=ttnn.bfloat16,
    layout=ttnn.TILE_LAYOUT,
    device=device,
)

# Clip values using scalar bounds
output = ttnn.clip(input_tensor, min=2, max=9)
logger.info(f"Clip with scalar bounds: {output}")