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::{
204 single_ref_p6_ctx, uni_comp_ref_p1_ctx,204 single_ref_p6_ctx, uni_comp_ref_p1_ctx,
205 };205 };
206 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};206 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};
208 208
209 const PARTITION_NONE: usize = 0;209 const PARTITION_NONE: usize = 0;
210 const PARTITION_HORZ: usize = 1;210 const PARTITION_HORZ: usize = 1;
@@ -2057,6 +2057,61 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc {
2057 /// entropy parsing no longer touches the frame's own planes (except the two2057 /// entropy parsing no longer touches the frame's own planes (except the two
2058 /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run2058 /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run
2059 /// the replay on a superblock-row wavefront worker.2059 /// 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+
2060 pub(crate) enum ReconOp {2115 pub(crate) enum ReconOp {
2061 Intra {2116 Intra {
2062 plane: usize,2117 plane: usize,
@@ -2070,7 +2125,7 @@ pub(crate) enum ReconOp {
2070 mode: usize,2125 mode: usize,
2071 angle_delta: i32,2126 angle_delta: i32,
2072 reach: Reach,2127 reach: Reach,
2073- residual: Vec<i32>,2128+ residual: Residual,
2074 cfl: Option<(i32, CflSrc)>,2129 cfl: Option<(i32, CflSrc)>,
2075 filter_intra: Option<usize>,2130 filter_intra: Option<usize>,
2076 smooth_neighbor: bool,2131 smooth_neighbor: bool,
@@ -2087,7 +2142,7 @@ pub(crate) enum ReconOp {
2087 w: usize,2142 w: usize,
2088 h: usize,2143 h: usize,
2089 prediction: Vec<u16>,2144 prediction: Vec<u16>,
2090- residual: Vec<i32>,2145+ residual: Residual,
2091 },2146 },
2092 }2147 }
2093 2148
@@ -2423,7 +2478,7 @@ fn push_intra(
2423 let pred = take_palette_pred(fctx);2478 let pred = take_palette_pred(fctx);
2424 push_op(fctx, ReconOp::Intra {2479 push_op(fctx, ReconOp::Intra {
2425 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,2480 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,
2427 });2482 });
2428 }2483 }
2429 2484
@@ -2452,7 +2507,7 @@ fn push_intra_owned(
2452 let pred = take_palette_pred(fctx);2507 let pred = take_palette_pred(fctx);
2453 push_op(fctx, ReconOp::Intra {2508 push_op(fctx, ReconOp::Intra {
2454 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,2509 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,
2456 });2511 });
2457 }2512 }
2458 2513
@@ -2472,7 +2527,8 @@ fn push_mc_rect_owned(
2472 return;2527 return;
2473 }2528 }
2474 push_op(fctx, ReconOp::Mc {2529 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),
2476 });2532 });
2477 }2533 }
2478 2534
@@ -2501,7 +2557,7 @@ fn push_intra_rect(
2501 let pred = take_palette_pred(fctx);2557 let pred = take_palette_pred(fctx);
2502 push_op(fctx, ReconOp::Intra {2558 push_op(fctx, ReconOp::Intra {
2503 plane, x, y, bw, bh, rect: true, mode, angle_delta, reach,2559 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,
2505 });2561 });
2506 }2562 }
2507 2563
@@ -2534,7 +2590,66 @@ fn push_mc_rect(
2534 }2590 }
2535 push_op(fctx, ReconOp::Mc {2591 push_op(fctx, ReconOp::Mc {
2536 plane, x, y, stride, w, h,2592 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()),
2538 });2653 });
2539 }2654 }
2540 2655
@@ -2577,11 +2692,11 @@ fn exec_op(
2577 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,2692 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,
2578 filter_intra, smooth_neighbor, pred,2693 filter_intra, smooth_neighbor, pred,
2579 } => exec_intra(2694 } => exec_intra(
2580- y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual, cfl,2695+ y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual.resolve(),
2581- filter_intra, smooth_neighbor, pred, fctx,2696+ cfl, filter_intra, smooth_neighbor, pred, fctx,
2582 ),2697 ),
2583 ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => {2698 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);
2585 }2700 }
2586 }2701 }
2587 }2702 }
@@ -12672,21 +12787,28 @@ fn read_plane(
12672 full12787 full
12673 };12788 };
12674 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);12789 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+ };
12676 if crate::envflags::env_flag!("EC_AV1_TRACE") {12794 if crate::envflags::env_flag!("EC_AV1_TRACE") {
12795+ let residual = tx.run(&levels);
12677 eprintln!(12796 eprintln!(
12678 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",12797 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
12679 );12798 );
12680 }12799 }
12681- push_intra_owned(12800+ push_intra_tx(
12682 plane_idx,12801 plane_idx,
12683 x,12802 x,
12684 y,12803 y,
12685 side,12804 side,
12805+ side,
12806+ false,
12686 predict_mode,12807 predict_mode,
12687 angle_delta,12808 angle_delta,
12688 reach,12809 reach,
12689- residual,12810+ tx,
12811+ &levels,
12690 cfl,12812 cfl,
12691 filter_intra,12813 filter_intra,
12692 smooth_neighbor, fctx,12814 smooth_neighbor, fctx,
@@ -16877,26 +16999,17 @@ fn read_inter_plane_rect(
16877 full16999 full
16878 };17000 };
16879 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);17001 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- );
16890 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {17002 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {
16891 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");17003 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");
16892 }17004 }
16893 // `reconstruct_mc_rect` reads both buffers at the square prediction17005 // `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.17006+ // stride, so the dense `w x h` residual is re-laid at that stride
16895- let mut strided = vec![0i32; stride * stride];17007+ // (`TxParams::stride`) after the inverse transform.
16896- for row in 0..h {17008+ let tx = TxParams {
16897- strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]);17009+ w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type,
16898- }17010+ stride,
16899- push_mc_rect_owned(plane_idx, x, y, stride, w, h, prediction, strided, fctx);17011+ };
17012+ push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx);
16900 Ok((grid, tx_type))17013 Ok((grid, tx_type))
16901 }17014 }
16902 17015
@@ -21421,8 +21534,11 @@ fn read_inter_plane(
21421 full21534 full
21422 };21535 };
21423 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);21536 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);21537+ let tx = TxParams {
21425- push_mc_rect_owned(plane_idx, x, y, side, side, side, prediction, residual, fctx);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);
21426 Ok((grid, tx_type))21542 Ok((grid, tx_type))
21427 }21543 }
21428 21544