﻿# Triton tensor strides: equal shapes can require different pointer arithmetic

Preserve runtime element strides for Triton kernels handling transposed or sliced tensor views instead of assuming shape implies contiguous storage.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-triton-strides-noncontiguous-view-contract/
Language: en

## Distinguish logical size from storage layout

Two matrices can have equal shapes while one is a transposed or sliced view with a different memory layout. The [PyTorch stride reference](https://docs.pytorch.org/docs/stable/generated/torch.Tensor.stride.html) defines strides as the storage step for a tensor dimension. A custom Triton kernel that infers contiguous addressing from shape alone can read valid addresses containing the wrong logical elements, so the absence of a memory fault is not enough to establish correctness.

## Read the explicit pointer formula

The [Triton matrix-multiplication tutorial](https://triton-lang.org/main/getting-started/tutorials/03-matrix-multiplication.html) demonstrates multidimensional pointer arithmetic using matrix coordinates and strides. Preserve that distinction when adapting a kernel to a tensor view. Strides are element-based in the tensor interface; multiplying them again as though they were byte offsets can create another addressing error. Treat each operand's layout as an independent contract rather than copying the first input's strides into every pointer calculation.

## Keep launch coordinates separate from tensor coordinates

The [program identifier API](https://triton-lang.org/main/python-api/generated/triton.language.program_id.html) identifies a launched program along a grid axis. That identifier is not automatically a row or column of the logical tensor until the kernel maps it to a tile. Record the mapping from program, local offsets and tensor strides to actual addresses. A changed grid ordering can preserve the set of programs while altering which logical tile each program should own.

## Decide how to support unfamiliar views

An implementation can support a documented set of strided views or require a contiguous input through an explicit adapter. Either choice needs a visible contract. Silently copying a tensor changes memory traffic and timing; silently assuming contiguity changes mathematical results. Also inspect output layout and ownership. The input's valid stride formula does not by itself establish that several programs write disjoint output regions under a new view configuration.

## Validate logical values on transformed inputs

Compare against a reference using a transpose and a nontrivial slice, with distinguishable element values rather than a uniform matrix. Check both values and intended output layout before measuring throughput. The guides to [tensor-axis meanings](/journal/tensor-axes-batch-sequence-hidden/) and [address arithmetic](/journal/guide-perf-integer-address/) provide background. Triton's particular requirement is preserving the complete coordinate-to-storage mapping; equal tensor shapes and valid allocated addresses do not make two view layouts interchangeable.
