Skip to content

metal: add ADD1 support#21274

Closed
xuir268 wants to merge 1 commit into
ggml-org:masterfrom
xuir268:metal-add1-support
Closed

metal: add ADD1 support#21274
xuir268 wants to merge 1 commit into
ggml-org:masterfrom
xuir268:metal-add1-support

Conversation

@xuir268

@xuir268 xuir268 commented Apr 1, 2026

Copy link
Copy Markdown

Overview

Implement GGML_OP_ADD1 for the Metal backend using the existing
kernel_unary_impl infrastructure.
#14909

Additional information

#14909

Requirements

The requirement comes directly from issue #14909:

"Feature parity between backends is always desirable"

The goal is simple — every GGML operation should work on every backend. ADD1 worked on CPU and CUDA but not on Metal. That means any model graph containing an ADD1 op on an Apple Silicon Mac would force that operation to fall back from GPU to CPU, causing a memory copy round-trip and killing performance.

test-backend-ops -o ADD1:

 ./build/bin/test-backend-ops -o ADD1
ggml_metal_device_init: tensor API disabled for pre-M5 and pre-A19 devices
ggml_metal_library_init: using embedded metal library
ggml_metal_library_init: loaded in 0.026 sec
ggml_metal_rsets_init: creating a residency set collection (keep_alive = 180 s)
ggml_metal_device_init: GPU name:   MTL0
ggml_metal_device_init: GPU family: MTLGPUFamilyApple9  (1009)
ggml_metal_device_init: GPU family: MTLGPUFamilyCommon3 (3003)
ggml_metal_device_init: GPU family: MTLGPUFamilyMetal4  (5002)
ggml_metal_device_init: simdgroup reduction   = true
ggml_metal_device_init: simdgroup matrix mul. = true
ggml_metal_device_init: has unified memory    = true
ggml_metal_device_init: has bfloat            = true
ggml_metal_device_init: has tensor            = false
ggml_metal_device_init: use residency sets    = true
ggml_metal_device_init: use shared buffers    = true
ggml_metal_device_init: recommendedMaxWorkingSetSize  = 19069.67 MB
Testing 3 devices

ggml_metal_init: allocating
ggml_metal_init: found device: Apple M3
ggml_metal_init: picking default device: Apple M3
ggml_metal_init: use fusion         = true
ggml_metal_init: use concurrency    = true
ggml_metal_init: use graph optimize = true
Backend 1/3: MTL0
  Device description: Apple M3
  Device memory: 18186 MB (18185 MB free)

ggml_metal_library_compile_pipeline: compiling pipeline: base = 'kernel_unary_f32_f32', name = 'kernel_unary_f32_f32_op=19_cnt=1'
ggml_metal_library_compile_pipeline: loaded kernel_unary_f32_f32_op=19_cnt=1              0x105bb6c90 | th_max = 1024 | th_width =   32
  ADD1(type=f32,ne=[10,5,4,3]): OK
ggml_metal_library_compile_pipeline: compiling pipeline: base = 'kernel_unary_f32_f32_4', name = 'kernel_unary_f32_f32_4_op=19_cnt=0'
ggml_metal_library_compile_pipeline: loaded kernel_unary_f32_f32_4_op=19_cnt=0            0x105bb4990 | th_max = 1024 | th_width =   32
  ADD1(type=f32,ne=[1024,1024,1,1]): OK
  2/2 tests passed
  Backend MTL0: OK
ggml_metal_free: deallocating
Backend 2/3: BLAS
  Device description: Accelerate
  Device memory: 0 MB (0 MB free)

  ADD1(type=f32,ne=[10,5,4,3]): not supported [BLAS] 
  ADD1(type=f32,ne=[1024,1024,1,1]): not supported [BLAS] 
  0/0 tests passed
  Backend BLAS: OK
Backend 3/3: CPU
  Skipping CPU backend
3/3 backends passed
OK
./build/bin/test-backend-ops perf -o ADD1
ggml_metal_device_init: tensor API disabled for pre-M5 and pre-A19 devices
ggml_metal_library_init: using embedded metal library
ggml_metal_library_init: loaded in 0.027 sec
ggml_metal_rsets_init: creating a residency set collection (keep_alive = 180 s)
ggml_metal_device_init: GPU name:   MTL0
ggml_metal_device_init: GPU family: MTLGPUFamilyApple9  (1009)
ggml_metal_device_init: GPU family: MTLGPUFamilyCommon3 (3003)
ggml_metal_device_init: GPU family: MTLGPUFamilyMetal4  (5002)
ggml_metal_device_init: simdgroup reduction   = true
ggml_metal_device_init: simdgroup matrix mul. = true
ggml_metal_device_init: has unified memory    = true
ggml_metal_device_init: has bfloat            = true
ggml_metal_device_init: has tensor            = false
ggml_metal_device_init: use residency sets    = true
ggml_metal_device_init: use shared buffers    = true
ggml_metal_device_init: recommendedMaxWorkingSetSize  = 19069.67 MB
Testing 3 devices

ggml_metal_init: allocating
ggml_metal_init: found device: Apple M3
ggml_metal_init: picking default device: Apple M3
ggml_metal_init: use fusion         = true
ggml_metal_init: use concurrency    = true
ggml_metal_init: use graph optimize = true
Backend 1/3: MTL0
  Device description: Apple M3
  Device memory: 18186 MB (18185 MB free)

  Backend MTL0: OK
ggml_metal_free: deallocating
Backend 2/3: BLAS
  Device description: Accelerate
  Device memory: 0 MB (0 MB free)

  Backend BLAS: OK
Backend 3/3: CPU
  Skipping CPU backend
3/3 backends passed
OK
  • I have read and agree with the contributing guidelines
  • AI usage disclosure:
    Yes , PR commit message and description only

@xuir268 xuir268 requested a review from a team as a code owner April 1, 2026 17:17
@xuir268 xuir268 changed the title ggml-metal: add ADD1 support metal: add ADD1 support Apr 1, 2026
@ggml-gh-bot

This comment was marked as resolved.

@xuir268

xuir268 commented Apr 1, 2026

Copy link
Copy Markdown
Author

I have used AI content for commit message and description only @ggml-gh-bot

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Apr 1, 2026
@xuir268

xuir268 commented Apr 3, 2026

Copy link
Copy Markdown
Author

@ggerganov this MR is now ready for review

Comment on lines +785 to +788
if (op->op == GGML_OP_ADD1) {
args.val = *(float *) op->src[1]->data;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expressing this as unary op is not correct. Specifically this part is not guaranteed to work.

@ggerganov

Copy link
Copy Markdown
Member

Closing this in favor of #21267

@ggerganov ggerganov closed this Apr 3, 2026
@xuir268

xuir268 commented Apr 3, 2026

Copy link
Copy Markdown
Author

okay , will go ahead for another feature

@xuir268 xuir268 deleted the metal-add1-support branch April 3, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants