Skip to content

upscale: Scaler with Workers, zero allocations per frame, faster vector kernels - #149

Merged
tannevaled merged 6 commits into
mainfrom
upscale-fsr-multicore-gpu
Nov 29, 2026
Merged

tannevaled merged 6 commits into
mainfrom
upscale-fsr-multicore-gpu

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What

upscale (FSR 1.0: EASU then RCAS) gets a reusable, multicore, allocation-free entry point and faster vector kernels. Refs #141.

  • upscale.NewScaler(opt) returns a Scaler; (*Scaler).Upscale(dst *image.RGBA, src image.Image) writes into a caller-owned image and allocates nothing per frame once warm (TestScalerAllocatesNothing). Upscale() is now a wrapper around it.
  • Options.Workers splits the output into bands of rows, one goroutine each (0 or less = GOMAXPROCS, capped by MaxWorkers = 64 and by the number of output rows). The bytes are those of one worker for every count (TestWorkersSameBytes): RCAS neighbours of a band are recomputed from the frame, not from the band.
  • Kernels (GOEXPERIMENT=simd): tap slices resolved once per output row, the rotation factored out of the twelve tap offsets (four column terms and four row terms per vector), a vector luma, and an overlapped last vector in RCAS instead of a scalar tail.
  • upscale/fsrref: the float64 oracle is now a public package, so that other implementations (a GPU one follows in its own PR) are held to the same bound.
  • tools/fsrbench speed --threads N --cpus LIST: ffmpeg with -threads 0 -filter_threads N, swscale threads=N, elapsed time instead of user CPU time.

Measured

Ryzen 9 7950X, Go 1.27.4, GOEXPERIMENT=simd (AVX-512), 1280x720 YCbCr 4:2:0 to 1920x1080 RGBA, sharpness 0.2, 1-minute load 1.2 to 4.2, ms per frame, 0 allocs/op. One thread is pinned to core 9; several threads run on CPUs 0-12,14-28,30,31 (core 13 and its sibling left to another session).

Threads upscale at v0.24.0 upscale, this PR ffmpeg 6.1.1 lanczos
1 37 26.2 2.77
2 13.2
4 6.9
8 4.25 to 4.64 0.20
16 3.47 to 3.65 0.20

The comparison favours ffmpeg: its figure has the source and the RGBA conversion subtracted, ours includes the YCbCr conversion and the packing. The ffmpeg multi-thread figure is a difference of two elapsed times of about 0.1 s over 300 frames, best of five, so it is coarse.

Error against the float64 oracle: 5.7e-06 at worst over 662520 samples, for one and for three workers, against the stated bound of 2e-4 (TestOracle; 2.7e-05 before the integer positions below). The contract is that bound, not bit equality with another float32 code.

Review

Two adversarial reviewers (Opus, one on concurrency and bounds, one on numerics and the published figures) read head 533ad35. Same bytes for every worker count, no race, no goroutine leak, the rotation algebra and the RCAS loop bounds held. Three findings, fixed or stated in 72c6376:

  • Sample positions (older than this PR, behaviour change). (i+0.5)*in/out - 0.5 was float32 here and float64 in the oracle. For sizes that are both odd once reduced (16 to 48, 640 to 1920) it is an integer for every other sample, and the two floors chose footprints one sample apart: a saturated diagonal edge at 3x differed from the oracle by 0.064. No size of the oracle corpus was of that kind. fsrref.Phase computes it in integers, for both sides (TestPhase, TestPhaseTies). Output pixels at those positions differ from v0.24.0.
  • dst was trusted. A dst whose Pix is shorter than its rectangle, or whose Stride is under a row, panicked in a worker goroutine or made two bands write the same bytes. Refused with the new ErrDst (TestScalerRefusesShortDst).
  • The bound is not universal (stated, not fixed). On noise of at most 3 levels of 255, about 0.9 % of samples lie outside ErrorBound, by up to 4.7e-3, under the local colour range. TestOracleLowContrast measures it and asserts those two limits; the package comment and the README say so. The cause is not established.

Not fixed: a stream whose frames alternate between opaque and non-opaque RGBA reallocates on each switch (the README states the condition).

Not done, and why

  • Single-core FSR is 9.4 times slower than lanczos, not within 2 times. The README states it and gives the multicore figures.
  • No 16-bit integer or half-float path: by reasoning (11 significant bits in a half-float against a bound of 2e-4 after twelve weighted taps and a division), not by measurement.
  • Scaling is 7.4 times at 16 workers; where the rest goes has not been profiled.
  • No timing on the Mac: its 1-minute load stayed between 22 and 91.

🤖 Generated with Claude Code

tannevaled and others added 6 commits November 29, 2026 17:42
A frame is now shared between Options.Workers goroutines (default
GOMAXPROCS, at most MaxWorkers = 64) as bands of output rows. RCAS reads
the EASU rows above and below clamped to the frame, not to the band, and an
EASU row depends on the source and its own index only, so the output is the
same byte for byte for any worker count (TestWorkersSameBytes). The YCbCr /
RGBA / NRGBA / Gray conversion is shared the same way, by source rows.

Scaler keeps the planes, the phase tables and each worker's rows between
frames of one geometry: a frame allocates nothing after the first
(TestScalerAllocatesNothing, one and four workers). Upscale is a wrapper.

The float64 oracle moves from the test file to the package upscale/fsrref,
so that paths outside this package (a GPU one) can be held to ErrorBound
against the same reference. TestOracle now runs on one and three workers.

Refs #141.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ant alpha guard

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Without a size check the later cases allocate what they ask for (85 GB);
found as two HUNG mutants.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…--threads

Single core, 720p YCbCr 4:2:0 to 1080p RGBA, sharpness 0.2, Ryzen 9 7950X
core 9, GOEXPERIMENT=simd, Scaler with one worker, 0 allocs/op:

  before  33.6 ms/frame
  after   26.2 ms/frame

- EASU: the slice of each of the twelve taps is resolved once per output
  row instead of once per vector; the rotated, stretched offset of a tap is
  linear in (offx, offy) and the taps share four values of each, so its
  halves are made once per column and once per row of the footprint and a
  tap adds two of them.
- RCAS: the last vector of a row overlaps the previous one instead of
  falling back to the scalar tail.
- Luma (the analysis plane) is computed by a vector kernel whose bits are
  those of the scalar one (TestLumaVec).

Error against the float64 oracle is unchanged: 2.73e-05 at worst over
662520 samples, bound 2e-4 (TestOracle, one and three workers).

tools/fsrbench speed gains --threads N and --cpus LIST: above one thread
ffmpeg runs with -threads 0 -filter_threads N and swscale threads=N, and
the elapsed time is read instead of the user CPU time, which is the sum
over the threads.

Refs #141

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ore table

Refs #141

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s rectangle

Two findings of the adversarial review of this pull request, both older
than it.

- The position of an output sample in the source, (i+0.5)*in/out - 0.5,
  was computed in float32 here and in float64 in the oracle. When the two
  sizes, reduced, are both odd (16 to 48, 640 to 1920) it is an integer for
  every other sample, and the two floors chose footprints one sample apart:
  a saturated diagonal edge at 3x differed from the oracle by 0.064, against
  a bound of 2e-4, and no size of the oracle corpus was of that kind.
  fsrref.Phase now computes it in integers and both sides use it
  (TestPhase against exact rationals, TestPhaseTies). The worst error of
  TestOracle goes from 2.7e-5 to 5.7e-6. Output at such positions changes.
- Scaler.Upscale trusted dst.Rect. A dst whose Pix is shorter than its
  rectangle, or whose Stride is under a row, made a worker goroutine panic
  (not recoverable) or two bands write the same bytes. It is refused with
  ErrDst before anything is allocated (TestScalerRefusesShortDst).

Also stated, not fixed: on noise of at most 3 levels of 255, about 0.9 % of
the samples lie outside ErrorBound, by up to 4.7e-3, under the local colour
range. TestOracleLowContrast measures it and asserts those two limits; the
package comment and the README no longer present the bound as universal.
The cause is not established.

README: ratio 9.4, the metric of each column, the CPU list of the runs.

Refs #141

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 0a15a86 into main Nov 29, 2026
20 checks passed
@tannevaled
tannevaled deleted the upscale-fsr-multicore-gpu branch November 29, 2026 17:55
tannevaled added a commit that referenced this pull request Nov 29, 2026
The table merged with #149 was measured one commit before its head. At
0a15a86 on the same core (Ryzen 9 7950X, core 9, interleaved runs) the Scaler
takes 28.7 to 29.3 ms per frame, not 26.2: the only non-test difference is the
integer position of an output pixel (72c6376), and why it costs 10 % with the
per-pixel code unchanged is not established. Multicore figures re-measured:
14.5, 7.5, 5.4 and 3.6 ms at 2, 4, 8 and 16 workers; with several workers
-benchmem reports 0 allocs/op and 1 to 522 B/op.

covgate also says, on a CPU that has the flag, that the file is held like the
rest, so the job log states which of the two the runner was.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tannevaled added a commit that referenced this pull request Nov 29, 2026
… re-measured at the merged code (#150)

* ci: the vector coverage gate no longer depends on the runner's CPU

main went red on the merge commit of #149 (run 46292291989, job "test,
GOEXPERIMENT=simd (ubuntu-latest)": upscale coverage 94.4% < 100%) although
the same tree had 21 green checks on the pull request.

upscale/easu_perm_amd64.go, added by #147, runs only with AVX-512, and a
hosted amd64 runner has it or not from one job to the next. Reproduced on a
Ryzen 9 7950X at 0a15a86: 100.0% natively, 94.5% under
GODEBUG=cpu.avx512f=off, the only functions below 100% being usePerm and
permGather. The gate was a draw since #147.

tools/covgate (cobra, a module of its own, 100% covered, parser fuzzed)
requires every block of the profile and excuses the blocks of one named file
only when /proc/cpuinfo lacks the named flag, printing a warning that counts
the statements that were not measured. An unreadable cpuinfo, an empty
profile and a malformed line are refusals, not passes.

Not solved here: on a runner without AVX-512 the permute gather is not
executed at all, so a commit can be released with that kernel untested by CI.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* README: the upscale speed table as measured at the merged code

The table merged with #149 was measured one commit before its head. At
0a15a86 on the same core (Ryzen 9 7950X, core 9, interleaved runs) the Scaler
takes 28.7 to 29.3 ms per frame, not 26.2: the only non-test difference is the
integer position of an output pixel (72c6376), and why it costs 10 % with the
per-pixel code unchanged is not established. Multicore figures re-measured:
14.5, 7.5, 5.4 and 3.6 ms at 2, 4, 8 and 16 workers; with several workers
-benchmem reports 0 allocs/op and 1 to 522 B/op.

covgate also says, on a CPU that has the flag, that the file is held like the
rest, so the job log states which of the two the runner was.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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