もくじIndex

ROCmで見る 深層学習

See Deep Learning in ROCm

See Deep Learning in ROCm

ROCm で見るシリーズ

小さな Conv ネットを PyTorch ROCm で 1 回動かし、畳み込みが MIOpen、全結合が rocBLAS へ対応する流れを確認します。

Run a tiny Conv net once on PyTorch ROCm and verify the mapping: convolution to MIOpen, fully-connected math to rocBLAS.

📘 対応する理論ページ: イメージでわかる深層学習 — 畳み込み・フィルタ・solver を比喩で解説

📘 Theory page: Visual Deep Learning — Convolution, filters, and solvers through analogy

このページの読み方: 1) まず入力 shape と出力 shape を見る 2) 次に Conv2d → ReLU → Flatten → Linear の順だけ読む 3) 最後に「Conv は MIOpen、Linear は rocBLAS」とつながれば十分です。

How to read this page: 1) Look at the input and output shapes first 2) Then just read the order Conv2d → ReLU → Flatten → Linear 3) Finally connect that to “Conv uses MIOpen, Linear uses rocBLAS.”

注記: PyTorch ROCm では GPU 指定に "cuda" 文字列を使います。名称互換の都合であり、実体は HIP/ROCm バックエンドです。

Note: PyTorch ROCm uses the string "cuda" for GPU selection. This is only for API-name compatibility; actual execution is on the HIP/ROCm backend.

1. なにをするか

1. What We'll Do

Conv2d を 1 層含む最小ネット(Conv → ReLU → Flatten → Linear)を 1 回だけ forward します。理論ページの「フィルタで特徴抽出」に対応します。

Run a minimal net with one Conv2d layer (Conv → ReLU → Flatten → Linear) once. This maps directly to "feature extraction by filters" in the theory page.

このページでまず見えれば OK: 小さな画像が入り、途中で特徴を取り出し、最後に 2 つのスコアが出ることです。

What is enough to see first: a small image goes in, features are extracted in the middle, and two scores come out at the end.

2. 最小コード

2. Minimal Code

import torch import torch.nn as nn assert torch.version.hip is not None, "This page expects PyTorch ROCm build" DEVICE = torch.device("cuda") # Conv を含む最小ネット: (1,1,4,4) -> Conv -> ReLU -> Linear(16->2) model = nn.Sequential( nn.Conv2d(1, 1, kernel_size=3, padding=1), nn.ReLU(), nn.Flatten(), nn.Linear(16, 2), ).to(DEVICE) x = torch.randn(1, 1, 4, 4).to(DEVICE) y = model(x) print("torch.version.hip:", torch.version.hip) print("入力 shape:", x.shape) print("出力 shape:", y.shape) print("モデル構造:") print(model)

コードと式の対応: このページは Conv を含みますが、まずは「形がどう流れるか」と「最後は行列計算になるか」を見れば十分です。

Code-to-math link: This page includes convolution, but at first it is enough to track “how the shapes flow” and “where the final matrix-style step appears.”

$$x \in \mathbb{R}^{1 \times 1 \times 4 \times 4} \xrightarrow{\mathrm{Conv2d}} h \in \mathbb{R}^{1 \times 1 \times 4 \times 4} \xrightarrow{\mathrm{ReLU}} h' \xrightarrow{\mathrm{Flatten}} z \in \mathbb{R}^{1 \times 16} \xrightarrow{\mathrm{Linear}} y \in \mathbb{R}^{1 \times 2}$$
$$x \in \mathbb{R}^{1 \times 1 \times 4 \times 4} \xrightarrow{\mathrm{Conv2d}} h \in \mathbb{R}^{1 \times 1 \times 4 \times 4} \xrightarrow{\mathrm{ReLU}} h' \xrightarrow{\mathrm{Flatten}} z \in \mathbb{R}^{1 \times 16} \xrightarrow{\mathrm{Linear}} y \in \mathbb{R}^{1 \times 2}$$
$$y = zW + b$$
$$y = zW + b$$

ここでは Conv2d を無理に 1 つの大きな行列式へ押し込まず、最後の Linear(16, 2) が $zW+b$ という行列計算になる、と結びつけるのが入りやすいです。

Here we do not force Conv2d into one giant matrix formula. It is easier to connect the final Linear(16, 2) to the matrix-style expression $zW+b$.

コードを全部いっぺんに追わなくても大丈夫です。最初は次の 4 つだけ見れば十分です。

You do not need to trace the whole code at once. At first, these four pieces are enough.

nn.Conv2d(...): 画像の中から特徴を探す部品です。

nn.Conv2d(...): A part that searches for features in the image.

nn.ReLU(): マイナスを 0 にする、よく使う簡単な関数です。

nn.ReLU(): A common simple function that turns negative values into 0.

nn.Flatten(): 表の形のデータを 1 列に並べ直します。

nn.Flatten(): This rearranges table-shaped data into one long row.

nn.Linear(16, 2): 最後に 2 つの答え候補のスコアを出します。

nn.Linear(16, 2): This produces the final scores for two answer candidates.

3. 実行するとどうなるか

3. What Happens When You Run It

torch.version.hip: 6.x.x 入力 shape: torch.Size([1, 1, 4, 4]) 出力 shape: torch.Size([1, 2]) モデル構造: Sequential( (0): Conv2d(1, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (1): ReLU() (2): Flatten(start_dim=1, end_dim=-1) (3): Linear(in_features=16, out_features=2, bias=True) )

画像テンソルが入り、2 クラス分のスコアが出ています。間で起きていることは:

An image tensor goes in and two class scores come out. In between:

① Conv2d: フィルタ走査で特徴抽出(MIOpen が担当)
② ReLU: マイナス値を 0 にする要素演算(GPU kernel)
③ Linear: 最終判定を行列積で計算(rocBLAS GEMM)

① Conv2d: Feature extraction by filter scan (handled by MIOpen)
② ReLU: Element-wise clamp of negative values (GPU kernel)
③ Linear: Final classification via matrix multiply (rocBLAS GEMM)

🏭 理論ページとの対応: 理論ページの「フィルタで特徴抽出 → 不要値を抑える → 最終判定」が、ここでは Conv2d → ReLU → Linear として観測できます。

🏭 Theory page connection: "Extract features by filters → suppress irrelevant signals → final judgment" appears here as Conv2d → ReLU → Linear.

4. 裏では何が起きているか

4. Under the Hood

この例では Conv2d は MIOpenLinear は rocBLAS が主担当です。PyTorch ROCm は演算種別に応じてバックエンドを切り替えます。

In this example, Conv2d is mainly handled by MIOpen, while Linear is mainly handled by rocBLAS. PyTorch ROCm switches backend by operation type.

やさしく言うと、PyTorch は「この部分は畳み込み」「この部分は行列計算」と見分けて、下の ROCm ライブラリへ仕事を振り分けています。

In simpler words, PyTorch tells whether a part is “convolution” or “matrix math,” then hands that work to the matching ROCm library.

MIOpen の solver は、「この畳み込みをどういうやり方で計算するかを選ぶ係」くらいに読めば十分です。GPU や shape によって、選ばれるやり方が変わることがあります。

You can read the MIOpen solver as “the chooser for how to compute this convolution.” The chosen method can change with the GPU and the shape.

5. ROCmで観測するポイント

5. What to Observe on ROCm

# ROCm ビルド確認 python -c "import torch; print('torch.version.hip =', torch.version.hip)" # Conv/Linear 実行時の HIP 呼び出し観測 rocprof --hip-trace python deep_learning_example.py

このページの主眼は、「1 つのネットの中でも計算の種類ごとに下の ROCm ライブラリが変わる」と読むことです。細かい trace は、必要になってからで大丈夫です。

The key point here is that even inside one network, the underlying ROCm library changes with the kind of computation. Fine-grained traces can wait until you need them.