Skip to content

Guard shared Metal helper source across custom kernels - #65

Open
audreyt wants to merge 1 commit into
apple:mainfrom
audreyt:fix/helper-src-guards
Open

Guard shared Metal helper source across custom kernels#65
audreyt wants to merge 1 commit into
apple:mainfrom
audreyt:fix/helper-src-guards

Conversation

@audreyt

@audreyt audreyt commented Aug 24, 2026

Copy link
Copy Markdown

Summary

  • wrap each non-empty rendered helper_src block in a deterministic content-hash include guard
  • compute the guard after dtype-template substitution so distinct typed variants remain independent
  • cover identical shared helpers, typed helper variants, and kernels without helpers

Problem

Core AI can compile several generated custom-kernel sources into one Metal library. Two TorchMetalKernel instances that reuse an unguarded static helper then emit the same function twice and fail at runtime with a Metal redefinition error. Callers currently have to coordinate matching #ifndef guards manually.

A minimal two-kernel runtime repro on upstream main (98eecc0) aborts with:

error: redefinition of 'shared_increment'

With this change, the same asset loads and executes successfully (x + 2).

The hash covers the fully rendered helper text. Identical helpers therefore share one guard, while helpers specialized to half, float, or other template dtypes receive distinct guards. Kernels without helpers are unchanged.

Verification

  • runtime two-kernel helper-collision smoke: PASS
  • uv run pytest tests/dsl/test_kernel_collisions.py -q: 10 passed
  • custom Metal suite: 54 passed, 1 xfailed
  • full suite: 6308 passed, 126 skipped, 18 xfailed, 1 xpassed
  • uv run ruff check ...: PASS
  • uv run ruff format --check ...: PASS

Core AI can compile multiple custom kernels and scalar specializations into one Metal library. Re-emitting identical helper_src functions then fails with a symbol redefinition unless every caller manually adds matching preprocessor guards.

Wrap each rendered helper block in a deterministic content-hash guard. Hash after dtype substitution so distinct templated variants remain independent while identical rendered helpers deduplicate. Add coverage for shared helpers, dtype variants, and kernels without helpers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant