[BUG]: softmax_inline[logsoftmax=True] returns -inf on GPU where the CPU path is finite

オープン
#7,741 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発

調査の方向性

Start in max/kernels/src/nn/softmax.mojo: the logsoftmax dispatch at L1009-L1011 still calls the legacy softmax_kernel, which stores exp(x - max) rounded to float16 (L670-L676) and logs it later (L689-L698). Compare with the CPU branch (L450-L457), which computes (x - max) - log(sum) in the accumulation type. Keep the shifted logits until the final write — in-place calls are allowed — then extend test_gpu_logsoftmax, whose rand inputs never produce wide logit gaps, with a float16 case over [0, -10, -18, -25]. Done means GPU and CPU agree on that row in float16 and float32.

索引モデルが issue の本文から書いたものです。

説明

bug_feature_triaged

Bug description

Quick one on the GPU log-softmax: with float16 on an A100, softmax_inline[logsoftmax=True] turns the row [0, -10, -18, -25] into [0, -10, -inf, -inf], while the CPU branch of the same function gives [0, -10, -18, -25]. I ran into it while comparing CPU and GPU numerics for the float model in MojoProof; AI helped me trace it and I checked the trace.

Actual behavior

-inf on GPU for the -18 and -25 entries, while the CPU results stay finite.

Expected behavior

The finite (x - max) - log(sum), as on CPU.

Steps to reproduce

from layout import Coord, TileTensor, row_major
from max.gpu.host import DeviceContext
from nn.softmax import softmax_inline
from std.utils import IndexList

comptime dt = DType.float16
comptime n = 4


def main() raises:
    var row: List[Float16] = [0, -10, -18, -25]
    var shape = Coord(IndexList[2](1, n))

    # CPU: softmax_inline with no context.
    var y_cpu = List[Float16](length=n, fill=0)
    var x_cpu = TileTensor(row, row_major(shape))

    def load_cpu[w: Int](c: Coord) {var x_cpu} -> SIMD[dt, w]:
        return x_cpu.load[width=w](c)

    softmax_inline[dt, 1, logsoftmax=True](
        load_cpu, shape, TileTensor(y_cpu, row_major(shape)), 1
    )

    with DeviceContext() as ctx:
        var x = ctx.enqueue_create_buffer[dt](n)
        var y = ctx.enqueue_create_buffer[dt](n)
        with x.map_to_host() as h:
            for i in range(n):
                h[i] = row[i]
        var x_gpu = TileTensor(x.unsafe_ptr(), row_major(shape))

        def load_gpu[w: Int](c: Coord) {var x_gpu} -> SIMD[dt, w]:
            return x_gpu.load[width=w](c)

        softmax_inline[dt, 1, logsoftmax=True](
            load_gpu, shape, TileTensor(y, row_major(shape)), 1, ctx
        )
        ctx.synchronize()
        with y.map_to_host() as h:
            for i in range(n):
                print("x =", row[i], " CPU:", y_cpu[i], " GPU:", h[i])

mojo run repro.mojo on an A100:

x = 0.0  CPU: 0.0  GPU: 0.0
x = -10.0  CPU: -10.0  GPU: -10.0
x = -18.0  CPU: -18.0  GPU: -inf
x = -25.0  CPU: -25.0  GPU: -inf
AI-generated analysis

It looks like logsoftmax still goes to the legacy softmax_kernel (softmax.mojo L1009-L1011), which stores exp(x - max) already rounded to the output dtype (L670-L676) and later takes the log of that (L689-L698); in float16, exp(-18) rounds to 0. The CPU branch subtracts log(sum) instead (L450-L457). float32 is hit too, just further out: [0, -50, -100, -200] gives -inf at -100 and -200. test_gpu_logsoftmax uses rand inputs between 0 and 1, so it never sees big logit gaps.

Computing (input - row_max) - log(exp_sum) in the accumulation type and casting once should fix it, as long as the original (or shifted) logits are kept until the final write, since in-place calls are allowed. From Python, ops.logsoftmax builds shifted - log(sum(exp(shifted))) itself in the accumulation type, so it doesn't go through this kernel. Happy to send a PR with a float16 test :)

System information

Mojo 1.3.0.dev2026112105 (f23ed0af)
MAX 26.8.0.dev2026112105
installed with pip from the nightly index (https://whl.modular.com/nightly/simple/)
Amazon Linux 2023, x86_64, Intel Xeon Platinum 8275CL
NVIDIA A100-SXM4-80GB, driver 580.167.08, CUDA 13.0

主要言語
Mojo
スター
30.2k
フォーク
3.2k
平均マージ
5日 18時間
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modular/modular のほかの issue

modular/modular の issue をすべて見る

似ている issue

Machine Learning の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。