forge
Explore
Log in

Don't you dare go Hollow

Form the levels off the forward transform's own fixed point

Browse files at this commit

1be1614ab09010fb7c4bf58c21dbcae4563c46aaAhmet Kaan GümüşParent: eb8e099
  • perf
    • form the levels off the forward transform's own fixed point
Show full message
perf(av1): form the levels off the forward transform's own fixed point

`forward_and_quantize` transformed into a `Vec<f64>` of coefficients and
then read it back to quantize. With the network already in fixed point
the column pass can hand each column straight to the level formation
while it is still in `t`, so the coefficient buffer and the pass over it
are gone -- and a 64-point transform's uncoded columns (everything
outside the top-left 32x32 the syntax carries) are not transformed at
all.

209.0 G -> 178.9 G instructions:u on the sequence bench (-14.4%).

Two eight-wide column passes were measured and REVERTED: the `[i32; 8]`
Lane over eight columns is +3.4%, an in-place whole-buffer column pass
+2.3%. Both lose the per-column zero skip and the register-resident level
formation, which this encoder's mostly-small transforms live on. Noted at
the call site.
crates/ec-av1/src/transform.rsmodified+71 -15
@@ -1777,6 +1777,25 @@ fn forward_gain(n: usize) -> f64 {
1777 /// fixed-point gain. It is deterministic, and its accuracy is measured1777 /// fixed-point gain. It is deterministic, and its accuracy is measured
1778 /// against the decoder's own inverse rather than asserted.1778 /// against the decoder's own inverse rather than asserted.
1779 pub fn forward_transform_2d(residual: &[i32], side: usize) -> Vec<f64> {1779 pub fn forward_transform_2d(residual: &[i32], side: usize) -> Vec<f64> {
1780+ let rows = forward_rows(residual, side);
1781+ let scale = forward_scale(side);
1782+ let mut out = vec![0.0f64; side * side];
1783+ let mut t = [0i32; 64];
1784+ for v in 0..side {
1785+ if !forward_column(&rows, side, v, &mut t) {
1786+ continue;
1787+ }
1788+ for u in 0..side {
1789+ out[u * side + v] = f64::from(t[u]) * scale;
1790+ }
1791+ }
1792+ out
1793+}
1794+
1795+/// The row pass, in fixed point: each of `side` residual rows through
1796+/// [`forward_dct_fixed`], at [`FORWARD_PRECISION_BITS`] extra precision and
1797+/// the network's own gain (both of which [`forward_scale`] divides back out).
1798+fn forward_rows(residual: &[i32], side: usize) -> Vec<i32> {
1780 assert_eq!(1799 assert_eq!(
1781 residual.len(),1800 residual.len(),
1782 side * side,1801 side * side,
@@ -1798,27 +1817,38 @@ pub fn forward_transform_2d(residual: &[i32], side: usize) -> Vec<f64> {
1798 forward_dct_fixed(&mut t, log2, FORWARD_RANGE);1817 forward_dct_fixed(&mut t, log2, FORWARD_RANGE);
1799 rows[i * side..][..side].copy_from_slice(&t[..side]);1818 rows[i * side..][..side].copy_from_slice(&t[..side]);
1800 }1819 }
1801- // Both passes carry the network's gain, and the decoder owes the levels1820+ rows
1802- // `INVERSE_GAIN_RECIPROCAL` times the ORTHONORMAL coefficient, so the one1821+}
1803- // multiply that leaves fixed point divides it back out.1822+
1804- let g = forward_gain(side);1823+/// One column of [`forward_rows`]'s output through the network, into `t`;
1805- let scale = INVERSE_GAIN_RECIPROCAL / (g * g * f64::from(1u32 << FORWARD_PRECISION_BITS));1824+/// `false` when the column is all zero and `t` was left untouched.
1806- let mut out = vec![0.0f64; side * side];1825+///
1807- for v in 0..side {1826+/// One column at a time, and the caller consumes `t` before asking for the
1827+/// next: eight columns through the same `[i32; 8]` [`Lane`] the decoder's
1828+/// column pass uses was MEASURED SLOWER (+3.4% instructions on the sequence
1829+/// bench, and +2.3% for an in-place whole-buffer column pass). This encoder's
1830+/// transforms are mostly small -- a group of eight loses the per-column zero
1831+/// skip that a 4x4 or 8x8 residual keeps hitting, and the levels stop being
1832+/// formed while the column is still in registers.
1833+fn forward_column(rows: &[i32], side: usize, v: usize, t: &mut [i32; 64]) -> bool {
1808 let mut any = 0i32;1834 let mut any = 0i32;
1809 for i in 0..side {1835 for i in 0..side {
1810 t[i] = rows[i * side + v];1836 t[i] = rows[i * side + v];
1811 any |= t[i];1837 any |= t[i];
1812 }1838 }
1813 if any == 0 {1839 if any == 0 {
1814- continue;1840+ return false;
1815- }
1816- forward_dct_fixed(&mut t, log2, FORWARD_RANGE);
1817- for u in 0..side {
1818- out[u * side + v] = f64::from(t[u]) * scale;
1819 }1841 }
1842+ forward_dct_fixed(t, side.trailing_zeros(), FORWARD_RANGE);
1843+ true
1820 }1844 }
1821- out1845+
1846+/// What one fixed-point coefficient is worth on the `8 * orthonormal` scale
1847+/// the levels are formed on: both passes carry the network's gain, and the
1848+/// row pass carried the headroom shift too.
1849+fn forward_scale(side: usize) -> f64 {
1850+ let g = forward_gain(side);
1851+ INVERSE_GAIN_RECIPROCAL / (g * g * f64::from(1u32 << FORWARD_PRECISION_BITS))
1822 }1852 }
1823 1853
1824 /// The old direct `O(n^2)` matmul against [`build_dct_basis`], kept only as1854 /// The old direct `O(n^2)` matmul against [`build_dct_basis`], kept only as
@@ -1904,8 +1934,34 @@ pub fn forward_and_quantize(
1904 q_idx: i32,1934 q_idx: i32,
1905 deadzone: f64,1935 deadzone: f64,
1906 ) -> Vec<i32> {1936 ) -> Vec<i32> {
1907- let coeffs = forward_transform_2d(residual, side);1937+ // Fused: the levels are formed straight off the fixed-point coefficients,
1908- quantize(&coeffs, side, bit_depth, q_idx, deadzone)1938+ // so neither the `Vec<f64>` of coefficients nor the pass that reads it
1939+ // exists.
1940+ let rows = forward_rows(residual, side);
1941+ let scale = forward_scale(side);
1942+ let dc = scale / f64::from(crate::quant::dc_q(bit_depth, q_idx));
1943+ let ac = scale / f64::from(crate::quant::ac_q(bit_depth, q_idx));
1944+ // A 64-point transform carries its top-left 32x32 alone, so the other
1945+ // half's columns are not even transformed.
1946+ let coded = side.min(32);
1947+ let mut levels = vec![0i32; side * side];
1948+ let mut t = [0i32; 64];
1949+ for v in 0..coded {
1950+ if !forward_column(&rows, side, v, &mut t) {
1951+ continue;
1952+ }
1953+ for u in 0..coded {
1954+ let scaled = f64::from(t[u]) * if u == 0 && v == 0 { dc } else { ac };
1955+ let magnitude = scaled.abs() + deadzone;
1956+ let level = if magnitude < 1.0 {
1957+ 0
1958+ } else {
1959+ magnitude.floor().min(f64::from(i32::MAX)) as i32
1960+ };
1961+ levels[u * side + v] = if scaled < 0.0 { -level } else { level };
1962+ }
1963+ }
1964+ levels
1909 }1965 }
1910 1966
1911 #[cfg(test)]1967 #[cfg(test)]