forge
Explore
Log in

Don't you dare go Hollow

The inverse transform travels with the recorded write, not its result

Browse files at this commit

c027c27c3d4a4a890828e2aa41a5883bc8a06f39Ahmet Kaan GümüşParent: 34a3d01
  • perf
    • the inverse transform travels with the recorded write, not its result
Show full message
perf(av1): the inverse transform travels with the recorded write, not its result

A recorded reconstruction carried the already-transformed residual, so the
parsing thread ran the dequantization and the inverse transform for every
transform unit before a recon worker ever saw it. The three fused
coefficient readers now record TxParams + the coded grid instead
(ReconOp::{Intra,Mc}.residual is a Residual: Done or Coeffs), and
TxParams::run is the single copy of the produce-the-residual code both
paths call -- so at EC_AV1_RECON_THREADS=1 it still runs at the push site,
unchanged, and above 1 it runs on the worker.

Byte-exact at recon 1 and 4 on all four probe inputs (seg1080 c2d4ef66,
seg4k 3ac25ee5, seg4k_lr d197dbe0, palette 2bfb6839).
crates/ec-av1/src/decode.rsmodified+148 -32
@@ -204,7 +204,7 @@ use crate::mvstack::{
204204 single_ref_p6_ctx, uni_comp_ref_p1_ctx,
205205 };
206206 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};
207-use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh};
207+use crate::transform::{TxType, dequant_and_inverse_typed_wh};
208208
209209 const PARTITION_NONE: usize = 0;
210210 const PARTITION_HORZ: usize = 1;
@@ -2057,6 +2057,61 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc {
20572057 /// entropy parsing no longer touches the frame's own planes (except the two
20582058 /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run
20592059 /// the replay on a superblock-row wavefront worker.
2060+/// The dequantization + inverse transform of one transform unit, kept as its
2061+/// INPUTS so that a recorded write carries the work instead of its result
2062+/// (lane-wave3): at `EC_AV1_RECON_THREADS > 1` the transform then runs on the
2063+/// recon worker, not on the parsing thread. `stride == 0` means the dense
2064+/// `w x h` residual; otherwise it is re-laid into the `stride x stride` grid
2065+/// `reconstruct_mc_rect` reads both of its buffers at.
2066+#[derive(Clone, Copy)]
2067+pub(crate) struct TxParams {
2068+ pub(crate) w: usize,
2069+ pub(crate) h: usize,
2070+ pub(crate) bit_depth: u8,
2071+ pub(crate) q_idx: i32,
2072+ pub(crate) dc_delta: i32,
2073+ pub(crate) ac_delta: i32,
2074+ pub(crate) tx_type: TxType,
2075+ pub(crate) stride: usize,
2076+}
2077+
2078+impl TxParams {
2079+ /// The one copy of the produce-the-residual code: the inline path calls it
2080+ /// on the parse thread, the wavefront worker calls it on its own, so both
2081+ /// are the same function on the same inputs and byte-identical.
2082+ fn run(self, grid: &[i32]) -> Vec<i32> {
2083+ let residual = dequant_and_inverse_typed_wh(
2084+ grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta,
2085+ self.tx_type,
2086+ );
2087+ if self.stride == 0 {
2088+ return residual;
2089+ }
2090+ let mut strided = vec![0i32; self.stride * self.stride];
2091+ for row in 0..self.h {
2092+ strided[row * self.stride..][..self.w]
2093+ .copy_from_slice(&residual[row * self.w..][..self.w]);
2094+ }
2095+ strided
2096+ }
2097+}
2098+
2099+/// A recorded write's residual: already transformed, or the coefficients and
2100+/// [`TxParams`] the worker transforms itself.
2101+pub(crate) enum Residual {
2102+ Done(Vec<i32>),
2103+ Coeffs(TxParams, Vec<i32>),
2104+}
2105+
2106+impl Residual {
2107+ fn resolve(self) -> Vec<i32> {
2108+ match self {
2109+ Self::Done(v) => v,
2110+ Self::Coeffs(p, grid) => p.run(&grid),
2111+ }
2112+ }
2113+}
2114+
20602115 pub(crate) enum ReconOp {
20612116 Intra {
20622117 plane: usize,
@@ -2070,7 +2125,7 @@ pub(crate) enum ReconOp {
20702125 mode: usize,
20712126 angle_delta: i32,
20722127 reach: Reach,
2073- residual: Vec<i32>,
2128+ residual: Residual,
20742129 cfl: Option<(i32, CflSrc)>,
20752130 filter_intra: Option<usize>,
20762131 smooth_neighbor: bool,
@@ -2087,7 +2142,7 @@ pub(crate) enum ReconOp {
20872142 w: usize,
20882143 h: usize,
20892144 prediction: Vec<u16>,
2090- residual: Vec<i32>,
2145+ residual: Residual,
20912146 },
20922147 }
20932148
@@ -2423,7 +2478,7 @@ fn push_intra(
24232478 let pred = take_palette_pred(fctx);
24242479 push_op(fctx, ReconOp::Intra {
24252480 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,
2426- residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred,
2481+ residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
24272482 });
24282483 }
24292484
@@ -2452,7 +2507,7 @@ fn push_intra_owned(
24522507 let pred = take_palette_pred(fctx);
24532508 push_op(fctx, ReconOp::Intra {
24542509 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,
2455- residual, cfl, filter_intra, smooth_neighbor, pred,
2510+ residual: Residual::Done(residual), cfl, filter_intra, smooth_neighbor, pred,
24562511 });
24572512 }
24582513
@@ -2472,7 +2527,8 @@ fn push_mc_rect_owned(
24722527 return;
24732528 }
24742529 push_op(fctx, ReconOp::Mc {
2475- plane, x, y, stride, w, h, prediction: prediction.to_vec(), residual,
2530+ plane, x, y, stride, w, h, prediction: prediction.to_vec(),
2531+ residual: Residual::Done(residual),
24762532 });
24772533 }
24782534
@@ -2501,7 +2557,7 @@ fn push_intra_rect(
25012557 let pred = take_palette_pred(fctx);
25022558 push_op(fctx, ReconOp::Intra {
25032559 plane, x, y, bw, bh, rect: true, mode, angle_delta, reach,
2504- residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred,
2560+ residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
25052561 });
25062562 }
25072563
@@ -2534,7 +2590,66 @@ fn push_mc_rect(
25342590 }
25352591 push_op(fctx, ReconOp::Mc {
25362592 plane, x, y, stride, w, h,
2537- prediction: prediction.to_vec(), residual: residual.to_vec(),
2593+ prediction: prediction.to_vec(), residual: Residual::Done(residual.to_vec()),
2594+ });
2595+}
2596+
2597+/// [`push_intra_rect`] carrying the transform's INPUTS: at one reconstruction
2598+/// thread the transform runs here, exactly as before; on the wavefront it
2599+/// travels with the op and runs on the worker (lane-wave3).
2600+#[allow(clippy::too_many_arguments)]
2601+fn push_intra_tx(
2602+ plane: usize,
2603+ x: usize,
2604+ y: usize,
2605+ bw: usize,
2606+ bh: usize,
2607+ rect: bool,
2608+ mode: usize,
2609+ angle_delta: i32,
2610+ reach: Reach,
2611+ tx: TxParams,
2612+ grid: &[i32],
2613+ cfl: Option<(i32, CflSrc)>,
2614+ filter_intra: Option<usize>,
2615+ smooth_neighbor: bool,
2616+ fctx: &crate::decode::FrameCtx,
2617+) {
2618+ if fctx.recon_planes.with(std::cell::Cell::get).is_some() {
2619+ inline_intra(
2620+ plane, x, y, bw, bh, rect, mode, angle_delta, reach, &tx.run(grid), cfl, filter_intra,
2621+ smooth_neighbor, fctx,
2622+ );
2623+ return;
2624+ }
2625+ let pred = take_palette_pred(fctx);
2626+ push_op(fctx, ReconOp::Intra {
2627+ plane, x, y, bw, bh, rect, mode, angle_delta, reach,
2628+ residual: Residual::Coeffs(tx, grid.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
2629+ });
2630+}
2631+
2632+/// [`push_mc_rect`] carrying the transform's inputs -- see [`push_intra_tx`].
2633+#[allow(clippy::too_many_arguments)]
2634+fn push_mc_rect_tx(
2635+ plane: usize,
2636+ x: usize,
2637+ y: usize,
2638+ stride: usize,
2639+ w: usize,
2640+ h: usize,
2641+ prediction: &[u16],
2642+ tx: TxParams,
2643+ grid: &[i32],
2644+ fctx: &crate::decode::FrameCtx,
2645+) {
2646+ if fctx.recon_planes.with(std::cell::Cell::get).is_some() {
2647+ inline_mc(plane, x, y, stride, w, h, prediction, &tx.run(grid), fctx);
2648+ return;
2649+ }
2650+ push_op(fctx, ReconOp::Mc {
2651+ plane, x, y, stride, w, h, prediction: prediction.to_vec(),
2652+ residual: Residual::Coeffs(tx, grid.to_vec()),
25382653 });
25392654 }
25402655
@@ -2577,11 +2692,11 @@ fn exec_op(
25772692 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,
25782693 filter_intra, smooth_neighbor, pred,
25792694 } => exec_intra(
2580- y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual, cfl,
2581- filter_intra, smooth_neighbor, pred, fctx,
2695+ y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual.resolve(),
2696+ cfl, filter_intra, smooth_neighbor, pred, fctx,
25822697 ),
25832698 ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => {
2584- exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual, fctx);
2699+ exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual.resolve(), fctx);
25852700 }
25862701 }
25872702 }
@@ -12672,21 +12787,28 @@ fn read_plane(
1267212787 full
1267312788 };
1267412789 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
12675- let residual = dequant_and_inverse_typed(&levels, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type);
12790+ let tx = TxParams {
12791+ w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
12792+ ac_delta, tx_type, stride: 0,
12793+ };
1267612794 if crate::envflags::env_flag!("EC_AV1_TRACE") {
12795+ let residual = tx.run(&levels);
1267712796 eprintln!(
1267812797 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
1267912798 );
1268012799 }
12681- push_intra_owned(
12800+ push_intra_tx(
1268212801 plane_idx,
1268312802 x,
1268412803 y,
1268512804 side,
12805+ side,
12806+ false,
1268612807 predict_mode,
1268712808 angle_delta,
1268812809 reach,
12689- residual,
12810+ tx,
12811+ &levels,
1269012812 cfl,
1269112813 filter_intra,
1269212814 smooth_neighbor, fctx,
@@ -16877,26 +16999,17 @@ fn read_inter_plane_rect(
1687716999 full
1687817000 };
1687917001 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
16880- let residual = dequant_and_inverse_typed_wh(
16881- &grid,
16882- w,
16883- h,
16884- bit_depth(fctx),
16885- block_q_idx(fctx),
16886- dc_delta,
16887- ac_delta,
16888- tx_type,
16889- );
1689017002 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {
1689117003 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");
1689217004 }
1689317005 // `reconstruct_mc_rect` reads both buffers at the square prediction
16894- // stride, so the dense `w x h` residual is re-laid at that stride first.
16895- let mut strided = vec![0i32; stride * stride];
16896- for row in 0..h {
16897- strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]);
16898- }
16899- push_mc_rect_owned(plane_idx, x, y, stride, w, h, prediction, strided, fctx);
17006+ // stride, so the dense `w x h` residual is re-laid at that stride
17007+ // (`TxParams::stride`) after the inverse transform.
17008+ let tx = TxParams {
17009+ w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type,
17010+ stride,
17011+ };
17012+ push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx);
1690017013 Ok((grid, tx_type))
1690117014 }
1690217015
@@ -21421,8 +21534,11 @@ fn read_inter_plane(
2142121534 full
2142221535 };
2142321536 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
21424- let residual = dequant_and_inverse_typed(&grid, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type);
21425- push_mc_rect_owned(plane_idx, x, y, side, side, side, prediction, residual, fctx);
21537+ let tx = TxParams {
21538+ w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
21539+ ac_delta, tx_type, stride: 0,
21540+ };
21541+ push_mc_rect_tx(plane_idx, x, y, side, side, side, prediction, tx, &grid, fctx);
2142621542 Ok((grid, tx_type))
2142721543 }
2142821544