0Pricing
Mojo Academy · Lesson

Modeling a Tensor in Mojo

Shape, strides, and element layout.

Modeling a Tensor in Mojo is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Tensor?

A tensor is just an n-dimensional grid of numbers. A vector is 1D, a matrix is 2D, and an image batch is often 4D.

Flat Memory Underneath

However many dimensions you imagine, a tensor really lives as one flat buffer of values in memory, laid out end to end.

var data = List[Float32](0, 1, 2, 3, 4, 5)

Shape Describes the Grid

The shape is a tuple of sizes, one per dimension. A shape of (2, 3) means two rows of three columns.

var shape = (2, 3)  # 2 rows, 3 cols

Indices Pick an Element

You name an element with one index per dimension. In a (2, 3) tensor, (1, 2) is the last column of the second row.

Strides Map to Memory

A stride tells you how many flat slots to jump to move one step along a dimension. Strides turn indices into a single offset.

var strides = (3, 1)  # row jump 3, col jump 1

The Offset Formula

Flatten any index by a dot product: offset equals the sum of each index times its stride. That offset reaches the right slot.

offset = i * strides[0] + j * strides[1]

Row-Major Layout

Mojo numeric code usually stores rows contiguously, called row-major. The last dimension has stride 1, so columns sit side by side.

Why Strides Are Powerful

With clever strides you can view, slice, or transpose a tensor without copying data. Only the strides change, not the buffer.

Element Type Matters

Every tensor has a fixed dtype, like Float32 or Int64. A uniform type lets Mojo pack values tightly and compute fast.

alias dtype = DType.float32

A Minimal Tensor Struct

You can model a tensor as a struct that bundles a data pointer with its shape and strides, keeping layout info in one place.

struct Tensor:
    var data: UnsafePointer[Float32]
    var shape: (Int, Int)
    var strides: (Int, Int)

Contiguous vs Strided

A contiguous tensor has no gaps, so a linear scan is cheap. Strided views may skip around, which can slow memory access.

Quick Check

You have a (2, 3) row-major tensor. How do you reach element (1, 2)?

Recap

A tensor is a flat buffer plus shape and strides; the offset formula maps indices to memory, and row-major keeps columns contiguous. 🧮

Frequently asked questions

Is the “Modeling a Tensor in Mojo” lesson free?

Yes — the full text of “Modeling a Tensor in Mojo” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Modeling a Tensor in Mojo”?

Shape, strides, and element layout. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modeling a Tensor in Mojo” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Modeling a Tensor in Mojo
  2. Building a Matmul Step by Step
  3. Optimizing the Inner Product
  4. Verifying Numeric Correctness
← Back to Mojo Academy