Skip to content

Add CUDA backend support - #15

Open
greenjava wants to merge 8 commits into
PABannier:mainfrom
greenjava:cuda
Open

Add CUDA backend support#15
greenjava wants to merge 8 commits into
PABannier:mainfrom
greenjava:cuda

Conversation

@greenjava

Copy link
Copy Markdown

Description

The Linux CPU version is currently functional but extremely slow. I took the liberty of adding a CUDA backend to significantly improve performance. As with the Metal backend, this also required a few changes to ggml.

The implementation seems to work well in my testing, so I thought I'd submit these changes in case they are useful or of interest to the project.

Summary of changes

  • Added CUDA backend support (-DSAM3_CUDA=ON)
  • Added install targets and CPack packaging (static library, headers, examples, tests, and SAM3 CMake package configuration)
  • Updated ci.yml and release.yml to use cmake --install and the CPack package target
  • Added an Ubuntu 24.04 CUDA CI job
  • Updated the ggml submodule to the greenjava/ggml fork (commit 499c8a76, branch sam3-ops), which includes:
    • CUDA WIN_PART / WIN_UNPART kernels
    • Metal WIN_PART / WIN_UNPART kernels (ported from sam3-metal-ops)
    • CUDA fast paths for conv2d_transpose and head-size-32 attention
  • Fixed prompt-change tracking
  • Fixed the encode-img-size argument
  • Improved the video example

How to test

  1. git submodule update --init --recursive

  2. CPU / Metal:

    cmake -B build
    cmake --build build
  3. CUDA:

    cmake -B build -DSAM3_CUDA=ON
    cmake --build build
  4. Build the package:

    cmake --build build --target package

Note about ggml

  • The ggml submodule currently points to my greenjava/ggml fork (branch sam3-ops, commit 499c8a76), which contains the required changes.
  • I haven't opened a PR against the ggml repository yet, so the fork is used temporarily until the Metal/CUDA changes can be upstreamed.
  • I also tried to merge the changes from the sam3-metal-ops branch to bring WIN_PART support to the Metal backend. This involved resolving a large number of conflicts. Since I'm not very familiar with the Metal backend and I don't have access to a Mac, I wasn't able to test those changes. My apologies if I inadvertently broke anything on that platform.

- enable cuda inference via cmake flag
- add cuda backend initialization logic
- implement flash attention with manual fallback
- replace pool_1d with manual mean pooling
- add cuda inference test suite
- update build and readme documentation
- add --encode-img-size cli argument to override default grid dimensions
- update help text to include the new flag
- crop global attention RoPE frequencies to effective grid size
- derive feature map sizes from actual encoder token count
- support variable spatial grids in position embeddings and FPN
- add timing logs for pcs stages (text, geo, fusion, detr, seghead)
- store applied prompt in state to detect edits
- auto-reset tracker when text input changes
- extract reset logic into reset_all function
- update Reset button to use new function
- save propagated mask logits for pending masklets
- fix masklet id churn by storing mask data early
- configure cmake install rules for headers, libs, and binaries
- generate package config and version files for downstream usage
- add install targets for all example and test executables
- enable cpack to build tgz archives
- add cuda build matrix for linux ci and release
- replace manual packaging with cpack
- standardize artifact naming and upload paths
- upgrade checkout, upload, download artifact, and release actions
- replace system("mkdir") calls with portable macro
- move ensure_dir logic to dedicated test_fs.h header
@greenjava
greenjava marked this pull request as ready for review August 6, 2026 10:20
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