Skip to content

[MPS] Migrate comparison ops (eq/ne/lt/le/gt/ge) from MPSGraph to Metal - #183019

Closed
malfet wants to merge 11 commits into
gh/malfet/895/basefrom
gh/malfet/895/head
Closed

malfet wants to merge 11 commits into
gh/malfet/895/basefrom
gh/malfet/895/head

Conversation

@malfet

@malfet malfet commented May 9, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Used the metal-kernel skill to port the boolean comparison binary ops (eq, ne, lt, le, gt, ge) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes kBool as natural_output_dtype (the new contract introduced in #183055) so non-bool out= is correctly routed through the strided castout path. Stubs also pass ilp_threshold = 1u << 18, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

torch.eq(a, b, out=c) on M-series, default dispatcher (us/call, lower is better):

numel torch 2.11 (MPSGraph) this PR speedup
float32, 65K 16.6 12.7 1.3x
float32, 262K 111.5 18.6 6.0x
float32, 1M 43.1 42.3 1.0x
float32, 16M 601.7 632.6 0.95x
int32, 65K 16.3 3.7 4.4x
int32, 262K 16.4 5.2 3.2x
int32, 1M 23.3 24.4 1.0x
int32, 16M 613.1 606.2 1.0x

Authored with assistance from Claude.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented May 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/183019

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 5 Pending

As of commit 044c9b2 with merge base e775750 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels May 9, 2026
@pytorch-bot

pytorch-bot Bot commented May 9, 2026

Copy link
Copy Markdown

Attention! native_functions.yaml was changed

If you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info.

@malfet
malfet requested a review from Skylion007 May 9, 2026 01:13
@malfet malfet added the topic: improvements topic category label May 9, 2026
malfet added 2 commits May 8, 2026 22:07
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
malfet and others added 7 commits May 10, 2026 14:24
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@malfet
malfet requested a review from mruberry as a code owner May 11, 2026 14:51
malfet added a commit that referenced this pull request May 11, 2026
Used the metal-kernel skill to port the boolean comparison binary ops
to native Metal kernels via the existing TensorIterator stub-dispatch
path, dropping the MPSGraph-based implementations.

Adds complex `eq`/`ne`. The shared `DEFINE_BINARY_COMPARISON_FUNCTOR`
macro can't be reused for complex because `float2 == float2` returns
`bool2` (componentwise) in Metal and doesn't implicitly convert to
`bool`; `eq_functor`/`ne_functor` are defined inline instead and gain
explicit `float2`/`half2` overloads that reduce via `all(a == b)` /
`any(a != b)`. A new `REGISTER_COMPLEX_EQ_OP` macro instantiates the
complex variants (with castout siblings) for `eq` and `ne` only --
`lt`/`le`/`gt`/`ge` remain real-only.

Authored with assistance from Claude.

ghstack-source-id: d969cb8
Pull-Request: #183019
@malfet

malfet commented May 11, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "Lint + MPS are green"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request May 11, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in #183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: #183055
Approved by: https://github.com/Skylion007
malfet added a commit to pytorch/test-infra that referenced this pull request May 11, 2026
## Summary

- Unclassified failures (workflow didn't run on the merge base) were
rendered as success: title showed "No Failures", a `:green_heart: Looks
good so far!` line was emitted, and the `UNCLASSIFIED FAILURE`
`<details>` block was collapsed. Reviewers could easily miss real,
potentially PR-introduced breakage.
- Now: `:x:` icon fires when `hasUnknownFailures`, "No Failures" and the
`:green_heart:` reassurance are suppressed, and the unclassified section
is rendered with `<details open>` so affected jobs are immediately
visible.
- Adds a test covering the unclassified-only case (failure icon present;
"No Failures"/`:green_heart:` absent; `<details open>` for the
UNCLASSIFIED block).

## Test plan

- [x] `yarn jest drci.test.ts` passes (including the new test).
- [x] `yarn format --check` passes.
- [ ] Spot-check a PR like pytorch/pytorch#183019 in staging to confirm
the title and section rendering.
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request May 13, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in pytorch#183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: pytorch#183055
Approved by: https://github.com/Skylion007
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request May 13, 2026
…al (pytorch#183019)

Used the metal-kernel skill to port the boolean comparison binary ops (`eq`, `ne`, `lt`, `le`, `gt`, `ge`) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes `kBool` as `natural_output_dtype` (the new contract introduced in pytorch#183055) so non-bool `out=` is correctly routed through the strided castout path. Stubs also pass `ilp_threshold = 1u << 18`, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

`torch.eq(a, b, out=c)` on M-series, default dispatcher (us/call, lower is better):

| numel | torch 2.11 (MPSGraph) | this PR | speedup |
|---|---|---|---|
| float32, 65K | 16.6 | 12.7 | 1.3x |
| float32, 262K | 111.5 | 18.6 | 6.0x |
| float32, 1M | 43.1 | 42.3 | 1.0x |
| float32, 16M | 601.7 | 632.6 | 0.95x |
| int32, 65K | 16.3 | 3.7 | 4.4x |
| int32, 262K | 16.4 | 5.2 | 3.2x |
| int32, 1M | 23.3 | 24.4 | 1.0x |
| int32, 16M | 613.1 | 606.2 | 1.0x |

Authored with assistance from Claude.
Pull Request resolved: pytorch#183019
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#183055
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in pytorch#183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: pytorch#183055
Approved by: https://github.com/Skylion007
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
…al (pytorch#183019)

Used the metal-kernel skill to port the boolean comparison binary ops (`eq`, `ne`, `lt`, `le`, `gt`, `ge`) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes `kBool` as `natural_output_dtype` (the new contract introduced in pytorch#183055) so non-bool `out=` is correctly routed through the strided castout path. Stubs also pass `ilp_threshold = 1u << 18`, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

`torch.eq(a, b, out=c)` on M-series, default dispatcher (us/call, lower is better):

| numel | torch 2.11 (MPSGraph) | this PR | speedup |
|---|---|---|---|
| float32, 65K | 16.6 | 12.7 | 1.3x |
| float32, 262K | 111.5 | 18.6 | 6.0x |
| float32, 1M | 43.1 | 42.3 | 1.0x |
| float32, 16M | 601.7 | 632.6 | 0.95x |
| int32, 65K | 16.3 | 3.7 | 4.4x |
| int32, 262K | 16.4 | 5.2 | 3.2x |
| int32, 1M | 23.3 | 24.4 | 1.0x |
| int32, 16M | 613.1 | 606.2 | 1.0x |

Authored with assistance from Claude.
Pull Request resolved: pytorch#183019
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#183055
@github-actions
github-actions Bot deleted the gh/malfet/895/head branch June 12, 2026 02:32
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in pytorch#183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: pytorch#183055
Approved by: https://github.com/Skylion007
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…al (pytorch#183019)

Used the metal-kernel skill to port the boolean comparison binary ops (`eq`, `ne`, `lt`, `le`, `gt`, `ge`) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes `kBool` as `natural_output_dtype` (the new contract introduced in pytorch#183055) so non-bool `out=` is correctly routed through the strided castout path. Stubs also pass `ilp_threshold = 1u << 18`, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

`torch.eq(a, b, out=c)` on M-series, default dispatcher (us/call, lower is better):

| numel | torch 2.11 (MPSGraph) | this PR | speedup |
|---|---|---|---|
| float32, 65K | 16.6 | 12.7 | 1.3x |
| float32, 262K | 111.5 | 18.6 | 6.0x |
| float32, 1M | 43.1 | 42.3 | 1.0x |
| float32, 16M | 601.7 | 632.6 | 0.95x |
| int32, 65K | 16.3 | 3.7 | 4.4x |
| int32, 262K | 16.4 | 5.2 | 3.2x |
| int32, 1M | 23.3 | 24.4 | 1.0x |
| int32, 16M | 613.1 | 606.2 | 1.0x |

Authored with assistance from Claude.
Pull Request resolved: pytorch#183019
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#183055
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in pytorch#183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: pytorch#183055
Approved by: https://github.com/Skylion007
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…al (pytorch#183019)

Used the metal-kernel skill to port the boolean comparison binary ops (`eq`, `ne`, `lt`, `le`, `gt`, `ge`) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes `kBool` as `natural_output_dtype` (the new contract introduced in pytorch#183055) so non-bool `out=` is correctly routed through the strided castout path. Stubs also pass `ilp_threshold = 1u << 18`, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

`torch.eq(a, b, out=c)` on M-series, default dispatcher (us/call, lower is better):

| numel | torch 2.11 (MPSGraph) | this PR | speedup |
|---|---|---|---|
| float32, 65K | 16.6 | 12.7 | 1.3x |
| float32, 262K | 111.5 | 18.6 | 6.0x |
| float32, 1M | 43.1 | 42.3 | 1.0x |
| float32, 16M | 601.7 | 632.6 | 0.95x |
| int32, 65K | 16.3 | 3.7 | 4.4x |
| int32, 262K | 16.4 | 5.2 | 3.2x |
| int32, 1M | 23.3 | 24.4 | 1.0x |
| int32, 16M | 613.1 | 606.2 | 1.0x |

Authored with assistance from Claude.
Pull Request resolved: pytorch#183019
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#183055
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Several MPS binary TensorIterator dispatcher fixes, plus tests.

**Scalar promotion shortcut** restricted to lossless int-scalar→float-common only. Was building wrong kernel names (`mul_dense_scalar_float2_float` for `complex_convolution`) and rounding float scalars on the host before launch (float16 CrossEntropyLoss drift). The `cast_suffix_type` selection now uses `iter.common_dtype()` (with a `kBool` branch for comparison ops) so cast-kernel name lookups match the post-promotion template instantiation. Fixes `cosine_similarity` `mps_float16` (`half.clamp_min_(float)`).

**`natural_output_dtype` contract on `exec_binary_kernel`** + new `binary_strided_castout` kernel template (with a `store_at_offs` helper). Default natural = `iter.dtype(0)`, so arithmetic ops dispatch through the existing `<DTYPEO>_<DTYPEI>` cast-kernel matrix as before. Ops whose kernel produces a fixed natural that may diverge from `iter.dtype(0)` (comparisons → `kBool`) declare it via the new parameter; the dispatcher then routes through `_strided_castout_<DTYPEO>_<DTYPEI>`, which computes at compile-time precision and runtime-casts on store. Castout is explicit opt-in via a separate `REGISTER_BINARY_CASTOUT_OP(NAME, DTYPEI, DTYPEO)` macro -- arithmetic ops don't pay binary size for unused kernels. Missing combos fail loudly via `getPipelineStateForFunc` rather than silent corruption. The contract is used by the comparison-ops migration in pytorch#183019 to fix `cdist` `mps_float32` (`linalg_vector_norm(p=0)` → `ne_outf(float, 0, float_out)`) and inductor `test_complex_from_real_imag_mps`.

**`C10_METAL_ALL_TYPES_FUNCTOR` extended** to `(EnumName, EnumValue, MetalType)`. `val_at_offs` and `store_at_offs` are generated from the same X-macro instead of hand-written per-type switches.

**Caller-tunable ILP threshold** via a new `ilp_threshold` parameter. Float-output default kept at 256K; non-float defaults off. `binary_dense_ilp` is `unsigned ILP`-templated and the unroll width appears in host names (`add_dense_ilp4_float_float`) so future `ilp8`/`ilp16` can coexist.

Tests: new `TestBinaryIteratorConformance` (two synthetic functors -- `simple_add` and `simple_ge` -- compiled via `MetalShaderLibrary` and dispatched through the same path real ops use; `simple_ge` opts into castout). Plus `test_binary_kernels` regressions for scalar precision and in-place narrowing.

Authored with assistance from Claude.
Pull Request resolved: pytorch#183055
Approved by: https://github.com/Skylion007
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…al (pytorch#183019)

Used the metal-kernel skill to port the boolean comparison binary ops (`eq`, `ne`, `lt`, `le`, `gt`, `ge`) to native Metal kernels via the existing TensorIterator stub-dispatch path, dropping the MPSGraph-based implementations.

Each stub passes `kBool` as `natural_output_dtype` (the new contract introduced in pytorch#183055) so non-bool `out=` is correctly routed through the strided castout path. Stubs also pass `ilp_threshold = 1u << 18`, matching the floating-point default — measurably faster for small/medium tensors and avoids the 2b-param dispatch cliff in MPSGraph.

`torch.eq(a, b, out=c)` on M-series, default dispatcher (us/call, lower is better):

| numel | torch 2.11 (MPSGraph) | this PR | speedup |
|---|---|---|---|
| float32, 65K | 16.6 | 12.7 | 1.3x |
| float32, 262K | 111.5 | 18.6 | 6.0x |
| float32, 1M | 43.1 | 42.3 | 1.0x |
| float32, 16M | 601.7 | 632.6 | 0.95x |
| int32, 65K | 16.3 | 3.7 | 4.4x |
| int32, 262K | 16.4 | 5.2 | 3.2x |
| int32, 1M | 23.3 | 24.4 | 1.0x |
| int32, 16M | 613.1 | 606.2 | 1.0x |

Authored with assistance from Claude.
Pull Request resolved: pytorch#183019
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#183055
pytorchmergebot pushed a commit that referenced this pull request Sep 28, 2026
Comparison ops have no uint16/uint32/uint64 kernels since migration to metal kernels in #183019. Since CPU and CUDA both support them, it's reasonable for MPS to support them as well. This was first reported in kornia/kornia#4522
Pull Request resolved: #198860
Approved by: https://github.com/malfet
pytorchmergebot pushed a commit that referenced this pull request Sep 29, 2026
Comparison ops have no uint16/uint32/uint64 kernels since migration to metal kernels in #183019. Since CPU and CUDA both support them, it's reasonable for MPS to support them as well. This was first reported in kornia/kornia#4522
Pull Request resolved: #198860
Approved by: https://github.com/malfet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mps Run MPS tests (subset of trunk) Merged release notes: mps Release notes category topic: improvements topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants