Skip to content

upscale: scalar FSR1 is about 90x slower than ffmpeg's scaler; needs vector kernels #141

Backlog

Description

@tannevaled

The upscale package (FSR 1.0 EASU + RCAS, pure scalar Go) is about 90x slower than ffmpeg 6.1.1's SIMD scaler on one core of a Ryzen 9 7950X (720p to 1080p, ms/frame):

method ms/frame
ffmpeg bilinear 3.3
ffmpeg bicubic 2.0
ffmpeg lanczos 2.8
upscale EASU only 191
upscale EASU + RCAS 239

1080p to 2160p: 935 ms. A slower-than-reference result is an open defect.

Plan: vector EASU/RCAS kernels (8 output pixels per iteration; gather the 12 taps, then vector direction, length and weights), using the Go 1.27 simd package (amd64 AVX2/FMA, arm64 NEON) or go-asmgen, with the scalar code kept as the portable fallback and the float64 oracle (bound 1e-4, TestOracle) as the contract. Expected gain about 7x from the vector math alone; the 12-tap gather and the RCAS neighbourhood will decide the rest. An exact shortcut for pixels whose 12 taps share one colour is also possible.

Measured with fsrbench speed (kept outside the repository), taskset -c 9, load about 2.

Prior art: priorart "upscale SIMD vector kernel" at 2026-11-29 13:48 CET reported prior art; checked: priorart for the subject found only vp8enc simd items and the upscale-fsr1 branch itself; none about the upscale kernel

Activity

  1. tannevaled commented on Nov 29, 2026

    @tannevaled
    ContributorAuthor

    Status after #147 (720p to 1080p, one core of a Ryzen 9 7950X, AVX-512, ffmpeg 6.1.1 on the same machine):

    ms/frame
    FSR EASU+RCAS, scalar 242
    v0.22.0, simd ~82
    v0.23.0, simd 55
    #147, simd ~37 (EASU alone ~28)
    ffmpeg bilinear / bicubic / lanczos 3.3 / 2.0 / 2.8

    So FSR is still about 13x behind ffmpeg lanczos (more than 5x): the gap is not closed.

    Remaining profile of the full pipeline (pprof, 60 frames, share of CPU):

    • EASU block with the 12-tap loads (bandRows.tap and the vector arithmetic): about 41% cumulative
    • rcasRowVec: 25%
    • fromImage plus finish (YCbCr to planar float, float to uint8): 24%
    • permute gather and lerp: about 10%
    • allocation clearing: 3%

    Why it is a different amount of work, not just slower code. I read libswscale/utils.c of the 6.1.1 tree: for SWS_LANCZOS the default size factor is 6, and when upscaling filterSize = 1 + sizeFactor, so 7 taps per axis, and the filter is separable (a horizontal pass, then a vertical pass; lumPixBuf holds the horizontal results). I did not open the x86 assembly, so the 16-bit fixed-point intermediate and the lane count below are my understanding of swscale, not something I measured here.

    Work per output pixel, counting multiply-adds per sample:

    • swscale lanczos: 7 horizontal taps (each source row filtered once, and 720/1080 = 0.67 source rows per output row, so about 4.7) plus 7 vertical taps = about 12 MACs per luma sample, times 1.5 samples per pixel for 4:2:0 = about 17 MACs per pixel, in 16-bit fixed point (twice the lanes of fp32).
    • FSR: 12 taps x 3 colour channels = 36 MACs, plus for each of the 12 taps a weight computed per pixel (distance, two squared lobes, clamp: about 15 flops, so about 180), plus the edge analysis (about 40 flops per source pixel, shared) and RCAS (5 taps x 3 channels plus the limiter, about 40). That is about 300 flops per pixel in fp32, 16 lanes.

    That is roughly 17x more arithmetic per pixel, and about 35x once the 2x lane density is counted; we measure 13x, so the implementation is already beyond what the arithmetic would suggest, and what is left is mostly that work. Per pixel, FSR's weights depend on the local edge direction, so they cannot be tabulated per phase the way swscale's separable filter coefficients are.

    What would close the rest, in order of expected gain:

    1. Apply the 12-tap EASU to the luma plane only and upscale chroma with a cheaper filter: about 3x fewer colour MACs. This changes the output, so it would be an option with a stated error bound, not the default.
    2. Fuse EASU and RCAS per row band to keep the intermediate in L1 (RCAS is 25% and reads the whole EASU frame back from memory).
    3. 16-bit fixed point for the colour accumulation, as swscale does: 2x lanes, with an error bound to assert against the fp64 oracle.
    4. Several cores: the band structure is row-parallel; this is outside the single-core comparison.

    Even all of 1 to 3 would leave FSR about 5x behind a separable lanczos, because the weight computation per pixel is inherent to FSR 1.0. So I propose to keep #141 open for the options in 1 to 3 only if a use case needs the extra speed; the port itself is now at about 37 ms (27 fps single core, real time on a few cores).

  2. tannevaled commented on Nov 29, 2026

    @tannevaled
    ContributorAuthor

    State at v0.25.0

    Released as v0.25.0 at fc8301e (the Scaler pull request, then the coverage-gate fix; CI on main green at that commit). The GPU module (upscale/gpu) is on the branch upscale-gpu and is not released yet.

    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 2.9. Single-core figures on core 9; multicore on cores 0-12,14-28,30,31. ms per frame.

    Path FSR (avkit) ffmpeg 6.1.1 lanczos
    v0.24.0, 1 core 37 2.77
    merged code, 1 worker 28.7 to 29.3 2.77
    2 workers 14.5 to 14.9
    4 workers 7.55 to 7.78
    8 workers 5.37 to 5.48 0.20 (8 threads)
    16 workers 3.58 to 3.73 0.20 (16 threads)
    GPU, Vulkan compute (RADV NAVI32), upload and readback included 5.77 to 5.87 (dispatch alone 1.99 to 2.02)

    Single-core FSR is 10.4 times slower than lanczos, so the target of this issue (within 2 times) is not met; the issue stays open.

    Error against the float64 oracle (ErrorBound = 2e-4)

    Path Worst error Samples outside the bound
    CPU, any worker count (bytes identical across counts) 5.7e-6 0
    CPU, sizes whose phases tie (TestPhaseTies) 2.4e-7 0
    GPU, Metal 5.7e-6 0
    GPU, Vulkan 5.7e-6 0
    CPU, noise of at most 3 levels of 255 (TestOracleLowContrast) 4.7e-3 0.9 %

    What is not established

    1. Low-contrast input leaves the bound (0.9 % of samples, up to 4.7e-3). Cause not diagnosed. The bound is stated for the oracle corpus only.
    2. The integer sample position costs 10 % on one core (26.2 ms before it, 28.7 to 29.3 after, same session, back to back). It changes the per-axis setup, not the per-pixel code; the mechanism is not found.
    3. Moving the two AVX-512 closures of the permute gather to their own file cost 21 % (28.8 to 34.9 ms) with their bodies unchanged. Not shipped. Items 2 and 3 suggest the kernel's speed depends on code layout or inlining decisions that nothing here controls.
    4. -benchmem with several workers reports 0 allocs/op and 1 to 522 B/op: an allocation every few frames, origin not traced. One worker is allocation-free.
    5. Frames that alternate between opaque and translucent reallocate (the cache is keyed on opacity).
    6. CI executes the AVX-512 permute gather only when the hosted runner has AVX-512; otherwise the job prints a warning with the number of unmeasured statements.

    Not done

    • An fp16 or int16 packed path, EASU fused with RCAS through a tile buffer, and 4 outputs per analysis were not built; the remaining 10 times gap on one core is where they would have to pay.
    • Mac timings (CPU and Metal): deferred, the machine's load was far above 8. Metal was checked for correctness only.
    • No comparison with FSR in a player or the AMD sample: neither was available on the machines used.
    • ffmpeg at 8 and 16 threads reads 0.20 ms from a coarse measurement (few frames); treat it as an order of magnitude.

    🤖 Generated with Claude Code

  3. tannevaled commented on Nov 29, 2026

    @tannevaled
    ContributorAuthor

    Low-contrast error: cause found, contract stated in v0.25.1

    Item 1 of "What is not established" in the v0.25.0 comment is resolved. Two corrections to that comment: the GPU module is released (upscale/gpu/v0.1.0, #151, f1890a8), not "on a branch, not released yet".

    Cause. Not an arithmetic defect: the conditioning of EASU's edge analysis. Per bilinear corner, lenX = sat(|d-b| * rcp(max(|d-c|, |c-b|))) on three neighbouring lumas. Where different colours share a luma (about 13 luma values on 3-level noise) the numerator and the denominator are both zero in exact arithmetic: 0/0, and ffx_fsr1.h defines no answer. Each precision answers with rounding noise: the oracle sees the float32 colours in float64 (a tie is 7.45e-9 from zero), the float32 path rounds the same luma to exactly 0 or 6e-8. Worst case found: edge strength 0 against 0.94, which reshapes the kernel; EASU's final clamp bounds the effect by the range of the four nearest colours. Located by comparing the intermediates (direction identical to 6 digits, ln 1.05 against 1.88), then confirmed by classification: 42 228 of 1.17 M samples have such a neighbourhood and hold all 10 394 outliers; the other 1 130 097 are within 3.4e-5.

    Affected range. All versions of the package: the same probe on v0.21.0, v0.22.0, v0.23.0, v0.24.0, v0.25.0, scalar and GOEXPERIMENT=simd, gives worst 4.67e-3 and about 11 200 samples outside on each. The GPU on Metal (Apple M4 Max), at upscale/gpu/v0.1.0: well-conditioned 1 130 097 samples, worst 3.4e-5, 0 outside; degenerate 42 228, worst 4.67e-3, 10 393 outside. Vulkan was not probed (no Vulkan device on that machine); its shader divides the same way. Worker count does not enter.

    Fix. #152, released as v0.25.1: no code change; the contract is restated and asserted. ErrorBound holds on every sample whose edge analysis is well conditioned, and any sample is within ErrorBound plus the range of its four nearest source colours (TestOracleLowContrast). Removing the ambiguity instead would put a flush in every vector kernel and shader and deviate from the reference where it is undefined. A mutation of the arithmetic (ln *= 0.49) passed the old test and fails the new one.

    Docs: go-avkit/docs#74 (accuracy section and the GPU section).

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions