See Linear Algebra in ROCm
ROCm で見るシリーズ
理論ページで学んだ「行列のかけ算」が、ROCm の上では PyTorch のたった数行でどう動くかを見ます。
See how the "matrix multiplication" from the theory page runs on ROCm in just a few lines of PyTorch.
📘 対応する理論ページ: イメージでわかる線形代数 — ベクトル・行列・行列積の意味を図で理解する
📘 Theory page: Visual Linear Algebra — Understand vectors, matrices, and matrix multiply through diagrams
このページの読み方: 1) まず出力の C を見る 2) 次に A と B の shape を見る 3) 最後に torch.matmul が ROCm 側で rocBLAS へつながる、と読めば十分です。
How to read this page: 1) Look at the output C first 2) Then check the shapes of A and B 3) Finally read torch.matmul as something that connects to rocBLAS on ROCm.
注記: PyTorch の ROCm ビルドでは、GPU デバイス名に "cuda" 文字列を使います。これは名前の互換性のためで、実行は HIP/ROCm 側です。
Note: In PyTorch ROCm builds, GPU device selection still uses the string "cuda". This is for API-name compatibility; execution still goes through HIP/ROCm.
2つの行列をかけ算して、結果がどう出るかを見ます。
Multiply two matrices and see what comes out.
理論ページでは「行列 A × 行列 B = 行列 C」を図で見ました。ここでは同じ計算を、PyTorch で GPU に投げて実際に動かします。
The theory page showed "Matrix A × Matrix B = Matrix C" as diagrams. Here we run the same calculation on GPU through PyTorch.
このページでまず見えれば OK: A は (2, 3)、B は (3, 2)、結果の C は (2, 2) です。
What is enough to see first: A has shape (2, 3), B has shape (3, 2), and the result C has shape (2, 2).
GPU で行列積を実行する最小の例です。
The smallest example that runs a matrix multiplication on GPU.
コードと式の対応: この例の中心行 C_gpu = torch.matmul(A_gpu, B_gpu) は、そのまま $C = AB$ です。
Code-to-math link: The central line C_gpu = torch.matmul(A_gpu, B_gpu) is directly $C = AB$.
shape で読むと、$A$ は $(2, 3)$、$B$ は $(3, 2)$ なので、内側の $3$ と $3$ がつながって、結果 $C$ は $(2, 2)$ になります。
In shape terms, $A$ is $(2, 3)$ and $B$ is $(3, 2)$, so the inner $3$ and $3$ match, and the result $C$ becomes $(2, 2)$.
コードを全部いっぺんに読む必要はありません。最初は次の 4 行だけ見れば十分です。
You do not need to read the whole code at once. At first, these four lines are enough.
DEVICE = torch.device("cuda"): GPU を使う準備です。
DEVICE = torch.device("cuda"): This prepares GPU use.
A = ... と B = ...: かけたい 2 つの表を作っています。
A = ... and B = ...: These create the two tables we want to multiply.
C_gpu = torch.matmul(A_gpu, B_gpu): ここが本体で、「表どうしをかける」です。
C_gpu = torch.matmul(A_gpu, B_gpu): This is the core line: “multiply the two tables.”
C_gpu.cpu(): GPU 上の結果を、表示しやすい場所へ戻しています。
C_gpu.cpu(): This brings the GPU result back to a place where it can be printed easily.
C の 1行1列目 = 58 は、A の 1行目 [1, 2, 3] と B の 1列目 [7, 9, 11] の内積です: 1×7 + 2×9 + 3×11 = 7 + 18 + 33 = 58。理論ページで見た「行と列をペアにして足す」が、そのまま GPU で実行されています。
C[0,0] = 58 is the dot product of A's row 0 [1, 2, 3] and B's column 0 [7, 9, 11]: 1×7 + 2×9 + 3×11 = 58. The "pair rows and columns and add up" from the theory page is running on GPU exactly as described.
torch.matmul は内部で rocBLAS の GEMM(General Matrix Multiply)を呼び出しています。「行列積」という数学の操作を GPU 向けに速くしたものが GEMM です。AI の計算でも、こうした行列積はとてもよく出てきます。
torch.matmul internally calls rocBLAS GEMM (General Matrix Multiply). GEMM is matrix multiplication implemented in a GPU-friendly fast form. In AI work, this kind of matrix multiply appears very often.
やさしく言うと、PyTorch が「この表をかけて」と受け取り、その仕事を ROCm 側の得意な計算ライブラリへ渡しています。
In simpler words, PyTorch receives “please multiply these tables,” then hands that job to a ROCm-side library that is good at this calculation.
この流れをまとめると:
The flow looks like:
Python コード → PyTorch → HIP → rocBLAS GEMM → GPU で並列計算
Python code → PyTorch → HIP → rocBLAS GEMM → Parallel computation on GPU
たった1行の torch.matmul が、GPU 上で何千もの積和を並列に実行しています。行列が大きくなるほど GPU の並列性が活きます。
A single line torch.matmul runs thousands of multiply-add operations in parallel on the GPU. The larger the matrices, the more GPU parallelism pays off.
このページの主眼は「torch.matmul が ROCm 側では rocBLAS の行列積へつながる」と読むことです。まず torch.version.hip で ROCm ビルドを確認し、必要なら rocprof で実行の様子を観測します。
The key point here is to read torch.matmul as something that connects to rocBLAS matrix multiplication on ROCm. First confirm the ROCm build with torch.version.hip, then observe the run with rocprof if needed.