Skip to content

About

Job queue that runs AMD HIP and rocBLAS matrix-multiplication kernels on a GPU worker and checks every result against NumPy. FastAPI, RabbitMQ, PostgreSQL, React.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

gemm-queue

A small job system for running GPU matrix-multiplication kernels. You submit M, N and K in a web page; the API queues one task per kernel on RabbitMQ; a GPU worker runs five AMD HIP and rocBLAS kernels, checks every result against NumPy, and the timings come back to the page.

The kernels are the workload. The project is mostly about the system around them: queueing, retries, dead-letter handling, and making sure results are never silently lost.

block-beta
  columns 8
  space:2 DB[("Postgres")] space SW["Sweeper<br/>every 60 s"]:2 space:2
  space:3 RC["Results consumers<br/>1 to N"] space:4
  B["Browser"] space API["API<br/>FastAPI"] space
  block:MQ:2
    columns 2
    MQL["RabbitMQ"]:2
    R[["gemm.results"]] space
    space T[["gemm.tasks"]]
    TD[["gemm.tasks.dlq"]] RD[["gemm.results.dlq"]]
  end
  space W["GPU workers<br/>1 to N"]
  space:8
  block:MON:8
    columns 3
    MONL["Monitoring"]:3
    PGW["Pushgateway"] P["Prometheus"] G["Grafana"]
    space PE["postgres-exporter"] space
  end
  B -- "POST /jobs<br/>GET /jobs/:id" --> API
  API -- "jobs and runs" --> DB
  API -- "5 tasks per job" --> T
  T --> W
  W -- "result, then ack" --> R
  R --> RC
  RC -- "save run" --> DB
  SW -- "stuck runs" --> DB
  SW -- "re-publish" --> T
  PGW --> P
  PE --> P
  P --> G
  style MQL fill:none,stroke:none
  style MONL fill:none,stroke:none
Loading

One job through the system:

sequenceDiagram
    autonumber
    participant B as Browser
    participant A as API (FastAPI)
    participant DB as Postgres
    participant Q as RabbitMQ
    participant W as GPU worker
    participant C as Results consumer
    B->>A: POST /jobs (M, N, K)
    A->>DB: save job + 5 runs
    A->>Q: 5 tasks on gemm.tasks
    A-->>B: 202, job id
    loop for each kernel
        Q->>W: task
        W->>W: run kernel, check against NumPy
        W->>Q: result on gemm.results
        W->>Q: ack task
        Q->>C: result
        C->>DB: save run
    end
    B->>A: poll GET /jobs/:id
    A-->>B: runs with timings
Loading

Design

  • At-least-once delivery. The worker acks a task only after its result is published, and results are written by task ID, so a redelivered task or a duplicate result changes nothing.
  • Dead-letter queues on both sides. A task that keeps failing goes to gemm.tasks.dlq after 4 attempts; a malformed result goes straight to gemm.results.dlq. Database errors are retried with a pause instead of burning through the delivery limit.
  • Results consumer as its own service. It used to run inside the API process. Load tests showed that process was the bottleneck: saving results halved how many jobs it could accept, and gemm.results grew while gemm.tasks stayed empty. Now it runs separately and scales with RESULTS=N. The numbers are in docs/load-tests.md.
  • No lock per job. A job's status isn't stored; it's derived from its runs when the job is read. Each result is saved with one UPDATE of its own run row, so the 5 results of a job are saved in parallel, by one consumer or several, without waiting on each other. (An earlier version stored the status and had to lock the job row: without the lock, 5 of 5 test runs left jobs stuck.) An integration test runs two consumers on shuffled results.
  • Sweeper batch job. Every 60 s it looks for runs with no result after 10 minutes, re-publishes their task, and marks them failed after 3 attempts, so every job ends as done or failed. Its updates check in the same statement that the run is still queued (compare-and-set), so a result that arrives at the same moment always wins.
  • Metrics. The API serves /metrics for Prometheus to pull; the workers, results consumers and sweeper push theirs to a Pushgateway; RabbitMQ and Postgres are scraped through their exporters. Each sweeper pass is also logged in a sweeper_runs table.
  • Pluggable runner. HipRunner calls the compiled HIP binary; FakeRunner returns the NumPy product, so the whole pipeline runs and is tested without a GPU.

Load tests

On one 32-thread machine with fake workers (64³ matrices), so this measures the system around the kernels: the whole system handles ~3,200 tasks/s (~640 jobs/s) with no queue growing.

The bottleneck moved from the API (one Python process) to the workers, then to the results consumers, and is now the machine's CPU. Every part does more than 4,700 tasks/s on its own, but together they need ~7 ms of CPU per task, which is about all 32 threads can supply at that rate. Putting every part at its maximum adds only 3–6%.

Numbers, method and history: docs/load-tests.md.

Failure tests

make test-chaos breaks the running stack under load, one fault per test, then checks that every job the API accepted (202) finished, every run completed and nothing ended up in a dead-letter queue. The sweeper's timeout is shortened to 20 s for the run.

Fault Jobs accepted Requests that failed Runs re-published by the sweeper What recovered it
Worker killed mid-task 716 0 0 RabbitMQ redelivers its unacked task
Results consumer killed mid-save 724 0 0 redelivery; saving a result twice changes nothing
RabbitMQ restarted 1,088 0 900 the sweeper
Postgres restarted 1,084 8 0 the consumers' retry; the client saw the 8 failures
Queued tasks lost (queue purged) 844 0 540 the sweeper

When RabbitMQ restarts, the API still answers 202: the job is saved, its tasks fail to publish, and the sweeper publishes them later. Nothing is lost, but with the real 10 minute timeout those jobs wait 10 minutes. Retrying the publish in the API, or an outbox, would shorten that.

GPU results

One 8192 × 8192 × 8192 job on a Radeon 8060S (gfx1151, ROCm 7.2.1), averaged over 5 timed runs after a warm-up:

Kernel Time TFLOPS Max relative error
naive 3670 ms 0.30 4.3e-6
lds (shared-memory tiles) 1009 ms 1.09 4.3e-6
half2 (packed fp16) 438 ms 2.51 4.9e-4
rocBLAS sgemm 506 ms 2.17 4.4e-6
rocBLAS fp16 37 ms 29.6 4.9e-4

half2 stores A and B in fp16, so it beats rocBLAS sgemm at the cost of precision. It needs even K and N; other sizes fail on purpose and the error is shown on the page.

Running it

Needs Podman with Compose, uv, Node and, for the GPU worker, an AMD GPU with ROCm.

cp .env.example .env     # set the passwords and API_KEY
make up                  # builds both images, starts Postgres, RabbitMQ, API, results consumer, worker, sweeper and monitoring
make load-test           # optional: submit 20 jobs to watch

Then open http://localhost:8000 for the app and http://localhost:3000 for the Grafana dashboard. For a GPU other than gfx1151, run make up GPU_ARCH=gfx1100 (or whatever rocminfo reports).

The number of workers, their runner and prefetch, the number of results consumers and of API processes can be set when starting:

make up WORKERS=4 RUNNER=fake RESULTS=2   # 4 fake workers (NumPy instead of the GPU), 2 results consumers
make api API_PROCESSES=2                  # 2 uvicorn processes in the API container (default 1)
make worker WORKERS=16 RUNNER=fake PREFETCH=4   # each fake worker runs 4 tasks at once (keep 1 for the GPU)
make worker WORKERS=1                     # scale only the workers, runner from WORKER_RUNNER in .env
make results RESULTS=1                    # scale only the results consumers

Without a GPU, run the worker on the host with the fake runner:

make db mq build-frontend migrate
make run-api                         # in one terminal
make run-results                     # in another
WORKER_RUNNER=fake make run-worker   # in a third

Tests

make test               # 75 unit tests, no services needed
make test-integration   # 12 tests against real Postgres and RabbitMQ (make db mq first)
make test-chaos         # 5 failure tests against the running stack (make up RUNNER=fake WORKERS=4 RESULTS=2)
make lint               # ruff, oxlint and pre-commit checks

Layout

gemm_queue/common/    shared settings, enums, messages, RabbitMQ helpers
gemm_queue/api/       FastAPI routes, database, migrations
gemm_queue/results/   results consumer (its own service)
gemm_queue/worker/    task handler, runners, HIP kernel source
gemm_queue/batch/     sweeper (batch job)
gemm_queue/frontend/  React + Vite page
deploy/               docker-compose.yml, the two Dockerfiles, Prometheus and Grafana config
scripts/              load generator and benchmark
docs/                 load test results
tests/                unit/, integration/ and chaos/ (failure tests)

About

Job queue that runs AMD HIP and rocBLAS matrix-multiplication kernels on a GPU worker and checks every result against NumPy. FastAPI, RabbitMQ, PostgreSQL, React.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages