Skip to content

KernelInterface: describe grid limits that bound work-items, not work-groups #882

Description

@maleadt

KI.max_num_groups(backend) is the maximum number of work-groups per dimension, and has to hold for any work-group size. That matches CUDA, which limits the number of blocks per dimension ((2³¹−1, 65535, 65535)), but not back ends that limit the number of work-items per grid dimension:

  • Metal: groups * threads ≤ 2³²−1 per dimension. It reports typemax(UInt32) ÷ max_threadgroup_dims, i.e. ~4.2M groups on an M1, where 256-wide groups could have ~16.7M.
  • AMDGPU: min(max_groups, max_grid_size ÷ max_work_group_dims).
  • OpenCL: typemax(Int) ÷ max_work_group_dims.

(Raised by @christiangnrd in JuliaGPU/Metal.jl#994 (comment).)

KA only uses this in select_launch (src/launch.jl), to choose between NDLaunch and LinearLaunch, so the underestimate never rejects a launch; the back end's check at launch stays authoritative. It does push very large 2-D/3-D grids to the linearized launch, which costs index divisions, and KI users get a misleading limit.

Proposal (additive, can go in a KI 0.4.x):

  • add KI.max_grid_size(backend)::NTuple{3, Int}, the maximum number of work-items per dimension, defaulting to typemax(Int)s;
  • back ends report their actual limits in both queries (Metal: max_num_groups = typemax(UInt32), max_grid_size = typemax(UInt32));
  • select_launch checks blocks[d] ≤ max_num_groups[d] && blocks[d] * items[d] ≤ max_grid_size[d].

Caveat: with a tuned work-group size, select_launch picks the launch type before the size is known, and assumes one work-item per group. That stays conservative unless tuning moves earlier.

Activity

  1. added
    runtime
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtime

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions