Skip to content

Make host Tiled broadcast shape-correct#261

Closed
AntonOresten wants to merge 1 commit into
mainfrom
ao/broadcast-shape-fix
Closed

Make host Tiled broadcast shape-correct#261
AntonOresten wants to merge 1 commit into
mainfrom
ao/broadcast-shape-fix

Conversation

@AntonOresten

Copy link
Copy Markdown
Collaborator

Host-level Tiled broadcasts loaded every array leaf with the destination's block index and tile shape, and never validated axes — so shape-mismatched inputs (both Base-legal (1,N) vs (M,N) expansion and outright wrong sizes) silently produced garbage.

Changes

  • Validation: _tiled_broadcast! now calls Broadcast.check_broadcast_axes, so Base-illegal shapes throw DimensionMismatch instead of computing garbage.
  • Size-1 / trailing-dim expansion works: leaves whose shape differs from the destination are wrapped in BroadcastLeaf{P} (P = static mask of expanded dims), load a 1-wide slice at index 1 in those dims, and are expanded by the existing device-side tile broadcast (broadcast_shape/broadcast_to). Exact-shape leaves keep the previous fast path unchanged.
  • Scalar-only RHS: Tiled(C) .= 0 used to MethodError on convert(Tile{T}, ::Number); the kernel result is now routed through _bc_result, which splats scalars via broadcast_to(Tile(T(x)), tile_size) (and also expands sub-shaped result tiles when every leaf is size-1 in a dim).
  • Empty destinations: no-op instead of launching a grid with a 0 dimension.
  • ct.@. return value: in-place assignment (ct.@. C = A + B) now evaluates to the original destination array C rather than the Tiled wrapper.

Tests

9 new testsets in test/host/broadcast.jl: mismatch throws (1D/2D, nested), row/column size-1 expansion, expansion-only RHS, rank expansion (vector + matrix), allocating form with expansion, scalar fill, empty no-op, and macro return identity. Full host broadcast suite passes on GPU: 32/32.

🤖 Generated with Claude Code

@AntonOresten AntonOresten deleted the ao/broadcast-shape-fix branch July 4, 2026 15:39
@AntonOresten AntonOresten restored the ao/broadcast-shape-fix branch July 4, 2026 15:41
@AntonOresten AntonOresten added the duplicate This issue or pull request already exists label Jul 4, 2026
@AntonOresten

Copy link
Copy Markdown
Collaborator Author

Pardon the clutter...

Host-level Tiled broadcasts previously loaded every array leaf with the
destination's block index and tile shape, silently producing garbage for
any shape-mismatched input, and never validated axes at all. Fix:

- Validate via Broadcast.check_broadcast_axes: Base-illegal shapes now
  throw DimensionMismatch instead of computing garbage.
- Base-legal size-1 (and missing trailing) dims now work: such leaves are
  wrapped in BroadcastLeaf{P}, load a 1-wide slice at index 1 in flagged
  dims, and are expanded by the device-side tile broadcast.
- Scalar-only RHS (Tiled(C) .= 0) now fills the destination instead of
  MethodError-ing on convert(Tile{T}, ::Number).
- Empty destinations are a no-op instead of launching a 0-block grid.
- ct.@. in-place assignment returns the original destination array.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant