0Pricing
Mojo Academy · 课时

优化内积

将点积向量化并展开循环

优化内积 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Dot Product Core

The inner product multiplies two sequences element by element and adds the results. Speeding it up speeds your whole matmul.

var acc: Float32 = 0.0
for k in range(K):
    acc += a[k] * b[k]

One Lane at a Time Is Slow

A plain loop handles a single pair per step. Modern CPUs can do many at once, so scalar code leaves lanes idle.

Enter SIMD

A SIMD value packs several numbers and applies one operation to all of them together, doing real work in parallel per instruction.

var v = SIMD[DType.float32, 4](1, 2, 3, 4)

Load a Chunk at Once

Instead of one float, load a whole SIMD-width slice of each array in a single move, ready for vector math.

var va = a.load[width=4](k)
var vb = b.load[width=4](k)

Multiply Whole Vectors

Multiplying two SIMD values yields a vector of products in one step. The element-wise work happens across all lanes together.

var prod = va * vb  # 4 products at once

Accumulate Into a Vector

Keep a SIMD accumulator and add each product vector into it. You delay the final sum until the loop is done.

var acc = SIMD[DType.float32, 4](0)
acc += va * vb

Step by the Width

The loop now advances by the SIMD width, not by one. Four lanes wide means a quarter as many iterations.

for k in range(0, K, 4):
    acc += a.load[width=4](k) * b.load[width=4](k)

Reduce at the End

After the loop, collapse the lane accumulator into one number with a horizontal reduce, giving the final dot product.

var total = acc.reduce_add()

Unrolling the Loop

Unrolling processes several widths per iteration, cutting loop overhead and exposing more independent work to the CPU.

Handle the Remainder

If K is not a multiple of the width, a few elements are left over. A small tail loop finishes them one at a time.

for k in range(K - K % 4, K):
    total += a[k] * b[k]

Let Mojo Help

Mojo offers the vectorize helper to apply a width-parameterized body across a range, handling stepping and the tail for you.

Quick Check

After a SIMD-width dot product loop, why do you call reduce_add at the end?

Recap

Vectorize the inner product by loading SIMD chunks, multiplying lanes together, accumulating, then reduce; unroll and clean up the tail. ⚡

常见问题解答

「优化内积」课时是免费的吗?

是的 — 「优化内积」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。

「优化内积」这节课中我会学到什么?

将点积向量化并展开循环 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「优化内积」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Mojo Academy 课中编写并运行代码吗?

能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 在 Mojo 中建模张量
  2. 逐步构建矩阵乘法
  3. 优化内积
  4. 验证数值正确性
← 返回 Mojo Academy