Add Tensors
The following simple examples demonstrate how to create two tensors and add them together. TT-NN is a high-level Python API designed for developers to run models like LLaMA, Mistral, Stable Diffusion, and more. For a full list of models see the model matrix.
Lets create the example file, ttnn_add_tensors.py
Import Libraries
[ ]:
import ttnn
from loguru import logger
Open Tenstorrent device
We will create a device to run the program.
[ ]:
# Open the Device
device = ttnn.open_device(device_id=0)
Tensor Creation
Create two TT-NN tensors, and initialize them with values 1 and 2 respectively. The preferred tensor shape is (32, 32) which matches the hardware tile size. Learn more about tensors here.
[ ]:
# Create two TT-NN tensors with TILE_LAYOUT
tt_tensor1 = ttnn.full(
shape=(32, 32),
fill_value=1.0,
dtype=ttnn.float32,
layout=ttnn.TILE_LAYOUT,
device=device,
)
tt_tensor2 = ttnn.full(
shape=(32, 32),
fill_value=2.0,
dtype=ttnn.float32,
layout=ttnn.TILE_LAYOUT,
device=device,
)
logger.info("Input tensors:")
logger.info(tt_tensor1)
logger.info(tt_tensor2)
Addition Operation and Conversion
Now we can perform the addition operation on the two TT-NN tensors and log out the result.
[ ]:
# Perform eltwise addition on the device
tt_result = ttnn.add(tt_tensor1, tt_tensor2)
# Log output tensor
logger.info("Output tensor:")
logger.info(tt_result)
Close the Device
[ ]:
ttnn.close_device(device)
logger.info("Device closed successfully.")
Full Example and Output
Combine all previous examples for a complete example that can be run directly. This example opens a Tenstorrent device, creates two tensors, performs the addition, and logs the output tensor.
Running this script will output the input tensors and the result of their addition, which should be a tensor filled with 3s. As shown below
$ python3 $TT_METAL_HOME/ttnn/tutorials/basic_python/ttnn_add_tensors.py
2025-06-23 09:36:58.211 | INFO | __main__:main:29 - Input tensors:
2025-06-23 09:36:58.211 | INFO | __main__:main:30 - ttnn.Tensor([[ 1.00000, 1.00000, ..., 1.00000, 1.00000],
[ 1.00000, 1.00000, ..., 1.00000, 1.00000],
...,
[ 1.00000, 1.00000, ..., 1.00000, 1.00000],
[ 1.00000, 1.00000, ..., 1.00000, 1.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)
2025-06-23 09:36:58.211 | INFO | __main__:main:31 - ttnn.Tensor([[ 2.00000, 2.00000, ..., 2.00000, 2.00000],
[ 2.00000, 2.00000, ..., 2.00000, 2.00000],
...,
[ 2.00000, 2.00000, ..., 2.00000, 2.00000],
[ 2.00000, 2.00000, ..., 2.00000, 2.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)
2025-06-23 09:37:00.524 | INFO | __main__:main:37 - Output tensor:
2025-06-23 09:37:00.525 | INFO | __main__:main:38 - ttnn.Tensor([[ 3.00000, 3.00000, ..., 3.00000, 3.00000],
[ 3.00000, 3.00000, ..., 3.00000, 3.00000],
...,
[ 3.00000, 3.00000, ..., 3.00000, 3.00000],
[ 3.00000, 3.00000, ..., 3.00000, 3.00000]], shape=Shape([32, 32]), dtype=DataType::FLOAT32, layout=Layout::TILE)