This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/arch/common/rad_resample.h
T
Deadcode 9eaf10da6e radiance 1.11.0
New models:
- GLM-5.3-Flash reads pictures: its vision tower and processor (--image-tokens sets the budget),
  with decode kernels for a few rows and the DSA indexer split over the ranks
- Sage-A1.5 (fp8 and MXFP4) with its vision tower and a DFlash2 drafter
- Unlimited-OCR (bf16)
- Gemma 4 31B with z-lab's DFlash and RedHatAI's DFlash2 drafters; at four cards its all-reduces
  are fused with the junctions, overlapped with the compute and sent compressed
- Ornith-1.5-35B-A3B w4a8 compose files

Speed:
- Block drafters (DFlash, DFlash2) accept their drafts on the card, and the next step is issued
  ahead
- The four-rank all-reduce is faster for every --tp 4 model
- qwen35_w8a8: the norms and SiLU write the int8 linears' operands directly
- --attn-prefill-fp8 off|qk|pv|both: fp8 QK and PV legs for the h256 prefill attention (about +20%
  prefill on a 221K-token prompt for the 27B MXFP4 at TP2); lossy, off by default

Features:
- Control vectors: --control-vector off|on|disabled (disabled by default), a per-request switch,
  and the published Qwen3.8-Flash-Next refusal-direction vector (its license is in NOTICE)
- Krea 2: the `rebalance` request field, as ComfyUI's ConditioningKrea2Rebalance
- Cards with a small PCI BAR run, with kernel arguments kept in host memory
- Dashboard: live acceptance figures; statsdump rotates its file at a size cap
- KV caches every rank holds whole (GLM-5.3's latent and index caches) take one host-tier slot

Fixes:
- Long conversations taking turns no longer fall back to an early snapshot: a host-tier restore
  makes room as memory pressure does and waits a step for the previous turn's release (two
  conversations of 142K and 147K tokens sent back to back: 31.3 s per turn before, 2.8 s now)
- Kernels before Linux 6.16 (amdgpu below 3.64) no longer hang at startup: virtual memory
  management is turned off there, with a warning
- Gemma 4 dense scales its token embeddings as Hugging Face does (bf16), which changes its output
- The four-rank all-reduce fences prefill-sized messages, for a rare divergence after a fresh start
- GLM-5.3: every refusal names the setting and the reason; its template keeps string content
- Several pictures in one request no longer refuse processors that take one image at a time

Changes: Plugin ABI 26: out-of-tree plugins must be rebuilt. The environment switches the PRs
added are fixed rules or flags now (R4D_ATTN_FP8 is --attn-prefill-fp8).
2026-10-23 11:57:52 +02:00

168 lines
8.1 KiB
C++

/* rad_resample.h -- fp32 bicubic resampling reproducing torch's
* F.interpolate(x, size=(n, m), mode="bicubic", antialias=True) on the CPU float32 kernel,
* operation for operation, for the Unlimited-OCR DeepEncoder's position tables.
*
* The reference resamples two fixed tables (deepencoder.py):
* get_abs_pos_sam() permutes SAM's pos_embed [1,64,64,768] to (0,3,1,2), casts fp32 and
* interpolates to 40x40; get_abs_pos() takes CLIP position_embedding rows 1: as a 16x16 grid,
* interpolates to 10x10, and cats row 0 (cls) in front. Here the table is row-major [h, w, C]
* with C innermost and each of the C channels is interpolated over the (h, w) grid
* independently; the cls split/catenation is the caller's job.
*
* The float32 antialias path (upsample_bicubic2d_aa_kernel_impl ->
* upsample_separable_Nd_kernel_impl -> _compute_index_ranges_weights with antialias=true,
* aten/src/ATen/native/cpu/UpSampleKernel.cpp, torch v2.14.0) is mirrored operation for
* operation, scalar_t = float:
* per axis scale = (float)(in / out); support = scale >= 1 ? (interp_size/2) * scale =
* 2 * scale : 2; invscale = scale >= 1 ? 1 / scale : 1;
* the output-pixel centre center = scale * (i + 0.5f);
* the taps run over xmin = max((int64)(center - support + 0.5), 0) ..
* min((int64)(center + support + 0.5), in) - 1 -- truncating casts, so a negative
* start truncates toward zero before the clamp to 0; border taps are simply absent,
* the row normalization below absorbs the lost mass;
* each tap weight is aa_filter((j + xmin - center + 0.5) * invscale), the Keys cubic
* with a = -0.5 (torch's antialias filter is the Pillow-compatible -0.5 kernel; the
* -0.75 OpenCV-compatible one belongs to the non-antialias path);
* the axis weights are normalized by their float running sum total_w, exactly as
* _compute_indices_min_size_weights_aa divides wt_ptr[j] /= total_w;
* the resize is separable: the width pass over every input row first, then the height
* pass (the order upsample_separable_Nd_kernel_impl walks the dims), each tap
* accumulating acc = (float)(acc + w * v) in float.
* Golden-checked by tests/resample_golden.py (geometries 64->40, 24->20, 8->5, 16->10,
* 16x20->10x13, and both checkpoint tables).
*/
#pragma once
#ifndef RAD_RESAMPLE_H
#define RAD_RESAMPLE_H
#include <cmath>
#include <cstdint>
#include <cstring>
#include <vector>
namespace rad {
namespace arch {
namespace resample_detail {
/* aa_filter, the Keys cubic with a = -0.5 that torch's antialias path uses (the -0.75
* OpenCV-compatible kernel belongs to the non-antialias path), in float. */
inline float cubic_aa(float x) {
const float ax = std::fabs(x);
if (ax < 1.0f) return ((1.5f * ax - 2.5f) * ax) * ax + 1.0f;
if (ax < 2.0f) return (((-0.5f * ax + 2.5f) * ax - 4.0f) * ax) + 2.0f;
return 0.0f;
}
struct Axis {
int n = 0; /* tap slots per output pixel (max_interp_size) */
std::vector<int32_t> size; /* [out] taps this output pixel reads */
std::vector<int32_t> idx; /* [out*n] input index per tap (xmin + j) */
std::vector<float> w; /* [out*n] weights, normalized by the tap sum */
};
/* One axis of _compute_indices_min_size_weights_aa, float32 expression for expression. */
inline Axis make_axis(int in, int out) {
Axis a;
const float scale = (float)((double)in / (double)out);
const float support = scale >= 1.0f ? (float)(2.0 * (double)scale) : 2.0f;
const float invscale = scale >= 1.0f ? (float)(1.0 / (double)scale) : 1.0f;
a.n = (int)std::ceil((double)support) * 2 + 1; /* max_interp_size */
a.size.resize((size_t)out, 0);
a.idx.assign((size_t)out * (size_t)a.n, 0);
a.w.assign((size_t)out * (size_t)a.n, 0.0f);
for (int i = 0; i < out; ++i) {
const float c = ((float)i + 0.5f) * scale;
int xmin = (int)(c - support + 0.5f);
if (xmin < 0) xmin = 0;
int xsize = (int)(c + support + 0.5f);
if (xsize > in) xsize = in;
xsize -= xmin;
if (xsize < 0) xsize = 0;
/* ATen's own `xsize = min(xsize, n)`, kept for parity. It cannot bind here: with
* xmin > 0 the window spans 2*support + 1 taps, at most a.n = 2*ceil(support) + 1
* (the bound ATen sized the slots from), and with xmin == 0 it spans fewer. So the
* clamp is a no-op safety net, never a silent tap truncation. */
if (xsize > a.n) xsize = a.n;
int* idx = &a.idx[(size_t)i * (size_t)a.n];
float* w = &a.w[(size_t)i * (size_t)a.n];
float total = 0.0f;
for (int j = 0; j < xsize; ++j) {
w[j] = cubic_aa(((float)(xmin + j) - c + 0.5f) * invscale);
idx[j] = xmin + j;
total = (float)(total + w[j]);
}
a.size[i] = xsize;
if (total != 0.0f)
for (int j = 0; j < xsize; ++j) w[j] = (float)(w[j] / total);
}
return a;
}
/* One 1D resize pass. `rows` scans travel `src_row_stride` floats apart, taps step
* `tap_stride` floats along the resized axis, and outputs land at
* dst + r * dst_row_stride + i * out_stride + channel; tap order and float accumulation
* follow ATen's separable 1D resize. */
inline void resize_axis(const float* src, int64_t src_row_stride, int64_t tap_stride,
float* dst, int64_t dst_row_stride, int64_t out_stride, int rows,
const Axis& ax, int out_n, int channels) {
for (int r = 0; r < rows; ++r) {
const float* srow = src + (size_t)r * (size_t)src_row_stride;
float* drow = dst + (size_t)r * (size_t)dst_row_stride;
for (int i = 0; i < out_n; ++i) {
const int n = ax.size[i];
const int32_t* idx = &ax.idx[(size_t)i * (size_t)ax.n];
const float* w = &ax.w[(size_t)i * (size_t)ax.n];
for (int c = 0; c < channels; ++c) {
if (n == 0) { drow[(size_t)i * (size_t)out_stride + (size_t)c] = 0.0f; continue; }
float acc = (float)(srow[(size_t)idx[0] * (size_t)tap_stride + (size_t)c] * w[0]);
for (int j = 1; j < n; ++j)
acc = (float)(acc +
srow[(size_t)idx[j] * (size_t)tap_stride + (size_t)c] * w[j]);
drow[(size_t)i * (size_t)out_stride + (size_t)c] = acc;
}
}
}
}
} // namespace resample_detail
/* Resample an [in_h, in_w, channels] fp32 table (channels innermost) to
* [out_h, out_w, channels] with torch's bicubic + antialias interpolation: the width pass
* over every input row, then the height pass. Runs once at load. */
inline void resample_bicubic_aa(const float* src, int in_h, int in_w, int out_h, int out_w,
int channels, float* dst) {
if (!src || !dst || in_h <= 0 || in_w <= 0 || out_h <= 0 || out_w <= 0 || channels <= 0)
return;
if (in_h == out_h && in_w == out_w) {
std::memcpy(dst, src, (size_t)in_h * (size_t)in_w * (size_t)channels * sizeof(float));
return;
}
const resample_detail::Axis ay = resample_detail::make_axis(in_h, out_h);
const resample_detail::Axis ax = resample_detail::make_axis(in_w, out_w);
const float* work = src;
int work_w = in_w;
std::vector<float> wide;
if (in_w != out_w) { /* width pass first, the order upsample_separable_Nd walks the dims */
wide.assign((size_t)in_h * (size_t)out_w * (size_t)channels, 0.0f);
resample_detail::resize_axis(src, (int64_t)in_w * channels, channels, wide.data(),
(int64_t)out_w * channels, channels, in_h, ax, out_w,
channels);
work = wide.data();
work_w = out_w;
}
if (in_h == out_h) {
if (work != dst)
std::memcpy(dst, work, (size_t)out_h * (size_t)out_w * (size_t)channels * sizeof(float));
return;
}
/* height pass over the widened rows: one scan per output-width column */
resample_detail::resize_axis(work, channels, (int64_t)work_w * channels, dst, channels,
(int64_t)out_w * channels, work_w, ay, out_h, channels);
}
} // namespace arch
} // namespace rad
#endif // RAD_RESAMPLE_H