wip(av1): sb128b lane snapshot at the turn cap (orchestrator)
Browse files at this commitd2974e01951c714746db061f206fdf3231ae53efParent: 19e7ef83 changed files +121 -1
- crates/ec-av1/src/decode.rs +3 -0
- crates/ec-av1/src/msac.rs +100 -1
- crates/ec-av1/src/mvstack.rs +18 -0
crates/ec-av1/src/decode.rsmodified+3 -0
| @@ -28177,6 +28177,9 @@ fn decode_inter_block( | ||
| 28177 | 28177 | }; |
| 28178 | 28178 | (mv, false) |
| 28179 | 28179 | }; |
| 28180 | + crate::msac::symtrace::note(&format!( | |
| 28181 | + " MODE mi=({mi_row},{mi_col}) dbg={dbg_mode} mv={mv:?}" | |
| 28182 | + )); | |
| 28180 | 28183 | if crate::envflags::env_flag!("EC_TRACE_MODE") { |
| 28181 | 28184 | for (i, e) in stack.entries.iter().enumerate() { |
| 28182 | 28185 | eprintln!( |
crates/ec-av1/src/msac.rsmodified+100 -1
| @@ -144,6 +144,9 @@ impl SymbolEncoder { | ||
| 144 | 144 | /// shorter than a two-symbol alphabet's three entries. |
| 145 | 145 | pub fn symbol_fixed(&mut self, symbol: usize, cdf: &[u16]) { |
| 146 | 146 | assert!(cdf.len() >= 3, "a CDF covers at least two symbols"); |
| 147 | + if !self.pricing && symtrace::dir().is_some() { | |
| 148 | + symtrace::push(symbol, cdf.len() - 1, cdf[0]); | |
| 149 | + } | |
| 147 | 150 | let nsyms = cdf.len() - 1; |
| 148 | 151 | assert!(symbol < nsyms, "symbol {symbol} is outside the alphabet"); |
| 149 | 152 | |
| @@ -201,6 +204,7 @@ impl SymbolEncoder { | ||
| 201 | 204 | /// payload end on a byte boundary with no terminator. |
| 202 | 205 | pub fn finish(mut self) -> Vec<u8> { |
| 203 | 206 | assert!(!self.pricing, "a pricer holds no bytes to flush"); |
| 207 | + symtrace::flush('w'); | |
| 204 | 208 | // Round the interval's low end up to a value with 14 low zero bits, |
| 205 | 209 | // then set bit 14: the result is inside the interval (the interval is |
| 206 | 210 | // at least 2^15 wide before normalisation) and its tail bits are all |
| @@ -303,6 +307,12 @@ pub struct SymbolDecoder<'a> { | ||
| 303 | 307 | max_bits: i32, |
| 304 | 308 | } |
| 305 | 309 | |
| 310 | +impl Drop for SymbolDecoder<'_> { | |
| 311 | + fn drop(&mut self) { | |
| 312 | + symtrace::flush('r'); | |
| 313 | + } | |
| 314 | +} | |
| 315 | + | |
| 306 | 316 | impl<'a> SymbolDecoder<'a> { |
| 307 | 317 | /// `init_symbol` (spec 8.2.2). |
| 308 | 318 | #[must_use] |
| @@ -397,6 +407,16 @@ impl<'a> SymbolDecoder<'a> { | ||
| 397 | 407 | /// [`SymbolEncoder::symbol_fixed`]. |
| 398 | 408 | #[inline] |
| 399 | 409 | pub fn symbol_fixed(&mut self, cdf: &[u16]) -> usize { |
| 410 | + if symtrace::dir().is_some() { | |
| 411 | + let s = self.symbol_fixed_raw(cdf); | |
| 412 | + symtrace::push(s, cdf.len() - 1, cdf[0]); | |
| 413 | + return s; | |
| 414 | + } | |
| 415 | + self.symbol_fixed_raw(cdf) | |
| 416 | + } | |
| 417 | + | |
| 418 | + #[inline] | |
| 419 | + fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize { | |
| 400 | 420 | let nsyms = cdf.len() - 1; |
| 401 | 421 | // Two-symbol alphabets are most of the stream (every flag, every bit of |
| 402 | 422 | // a literal), and their walk has exactly one boundary: computing it |
| @@ -493,7 +513,11 @@ impl<'a> SymbolDecoder<'a> { | ||
| 493 | 513 | // `EQUIPROBABLE`'s single boundary: `f >> EC_PROB_SHIFT` is 256, so |
| 494 | 514 | // the range multiply is a shift. |
| 495 | 515 | let cur = ((self.range >> 8) << 7) + EC_MIN_PROB; |
| 496 | - v = (v << 1) | self.bool_cur(cur) as u32; | |
| 516 | + let bit = self.bool_cur(cur); | |
| 517 | + if symtrace::dir().is_some() { | |
| 518 | + symtrace::push(bit, 2, EQUIPROBABLE[0]); | |
| 519 | + } | |
| 520 | + v = (v << 1) | bit as u32; | |
| 497 | 521 | } |
| 498 | 522 | v |
| 499 | 523 | } |
| @@ -790,3 +814,78 @@ pub(crate) mod tests { | ||
| 790 | 814 | } |
| 791 | 815 | } |
| 792 | 816 | } |
| 817 | + | |
| 818 | +/// Symbol-by-symbol writer/reader trace (`EC_AV1_SYMTRACE=<dir>`), the | |
| 819 | +/// instrument a desync between OUR writer and OUR reader is bisected with: | |
| 820 | +/// every non-pricing [`SymbolEncoder`] and every [`SymbolDecoder`] dumps one | |
| 821 | +/// line per symbol (`index value nsyms cdf0`) into `<dir>/w-<n>.txt` / | |
| 822 | +/// `<dir>/r-<n>.txt`, and the first line where the two differ names the | |
| 823 | +/// divergent symbol. `EC_AV1_SYMTRACE_AT=<index>` additionally prints a | |
| 824 | +/// backtrace at that index on both sides, which names the syntax element. | |
| 825 | +/// Off (the variable unset) this is one relaxed load per symbol. | |
| 826 | +pub(crate) mod symtrace { | |
| 827 | + use std::cell::RefCell; | |
| 828 | + use std::sync::OnceLock; | |
| 829 | + | |
| 830 | + static DIR: OnceLock<Option<String>> = OnceLock::new(); | |
| 831 | + static AT: OnceLock<Option<(usize, usize)>> = OnceLock::new(); | |
| 832 | + static SEQ: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 833 | + | |
| 834 | + thread_local! { | |
| 835 | + static BUF: RefCell<(String, usize)> = const { RefCell::new((String::new(), 0)) }; | |
| 836 | + } | |
| 837 | + | |
| 838 | + pub(crate) fn dir() -> Option<&'static str> { | |
| 839 | + DIR.get_or_init(|| crate::envflags::var("EC_AV1_SYMTRACE").ok().filter(|v| !v.is_empty())) | |
| 840 | + .as_deref() | |
| 841 | + } | |
| 842 | + | |
| 843 | + /// `EC_AV1_SYMTRACE_AT=<i>` or `=<from>:<to>` (inclusive). | |
| 844 | + fn at() -> Option<(usize, usize)> { | |
| 845 | + *AT.get_or_init(|| { | |
| 846 | + let v = crate::envflags::var("EC_AV1_SYMTRACE_AT").ok()?; | |
| 847 | + let (a, b) = v.split_once(':').unwrap_or((v.as_str(), v.as_str())); | |
| 848 | + Some((a.parse().ok()?, b.parse().ok()?)) | |
| 849 | + }) | |
| 850 | + } | |
| 851 | + | |
| 852 | + /// A caller's own annotation, interleaved with this coder's symbol lines | |
| 853 | + /// (`# ...`) so the writer/reader diff shows the state around a desync. | |
| 854 | + pub(crate) fn note(line: &str) { | |
| 855 | + if dir().is_none() { | |
| 856 | + return; | |
| 857 | + } | |
| 858 | + BUF.with(|b| { | |
| 859 | + let (buf, _) = &mut *b.borrow_mut(); | |
| 860 | + buf.push_str("# "); | |
| 861 | + buf.push_str(line); | |
| 862 | + buf.push('\n'); | |
| 863 | + }); | |
| 864 | + } | |
| 865 | + | |
| 866 | + pub(crate) fn push(value: usize, nsyms: usize, cdf0: u16) { | |
| 867 | + BUF.with(|b| { | |
| 868 | + let (buf, n) = &mut *b.borrow_mut(); | |
| 869 | + if at().is_some_and(|(a, b)| (a..=b).contains(n)) { | |
| 870 | + eprintln!("SYMTRACE at {n}: {}", std::backtrace::Backtrace::force_capture()); | |
| 871 | + } | |
| 872 | + buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n")); | |
| 873 | + *n += 1; | |
| 874 | + }); | |
| 875 | + } | |
| 876 | + | |
| 877 | + /// Flushes this coder's lines as `<dir>/<side>-<n>.txt` and starts the | |
| 878 | + /// next coder's trace. | |
| 879 | + pub(crate) fn flush(side: char) { | |
| 880 | + let Some(dir) = dir() else { return }; | |
| 881 | + BUF.with(|b| { | |
| 882 | + let (buf, n) = &mut *b.borrow_mut(); | |
| 883 | + if !buf.is_empty() { | |
| 884 | + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 885 | + let _ = std::fs::write(format!("{dir}/{side}-{seq:05}.txt"), buf.as_bytes()); | |
| 886 | + } | |
| 887 | + buf.clear(); | |
| 888 | + *n = 0; | |
| 889 | + }); | |
| 890 | + } | |
| 891 | +} |
crates/ec-av1/src/mvstack.rsmodified+18 -0
| @@ -1742,6 +1742,24 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 1742 | 1742 | .checked_sub(1) |
| 1743 | 1743 | .is_some_and(|c| grid.skip_at(mi_row, c)); |
| 1744 | 1744 | |
| 1745 | + // lane-sb128b: the writer/reader stack diff instrument (`EC_AV1_MVDBG=1`) | |
| 1746 | + // -- one line per built stack, in build order, so the two sides' logs | |
| 1747 | + // diff to the first block whose candidate list differs. | |
| 1748 | + if crate::msac::symtrace::dir().is_some() { | |
| 1749 | + crate::msac::symtrace::note(&format!( | |
| 1750 | + "MVDBG mi=({mi_row},{mi_col}) b=({bw4}x{bh4}) ref={ref_frame} n={} mv={:?}", | |
| 1751 | + candidates.len(), | |
| 1752 | + candidates.iter().map(|e| (e.mv, e.weight)).collect::<Vec<_>>(), | |
| 1753 | + )); | |
| 1754 | + let row = mi_row.checked_sub(1).map(|r| { | |
| 1755 | + (mi_col..mi_col + bw4).map(|c| grid.get(r, c).map(|m| (m.ref_frame, m.mv))).collect::<Vec<_>>() | |
| 1756 | + }); | |
| 1757 | + let col = mi_col.checked_sub(1).map(|c| { | |
| 1758 | + (mi_row..mi_row + bh4).map(|r| grid.get(r, c).map(|m| (m.ref_frame, m.mv))).collect::<Vec<_>>() | |
| 1759 | + }); | |
| 1760 | + crate::msac::symtrace::note(&format!(" above={row:?}")); | |
| 1761 | + crate::msac::symtrace::note(&format!(" left={col:?}")); | |
| 1762 | + } | |
| 1745 | 1763 | MvStack { |
| 1746 | 1764 | entries: candidates, |
| 1747 | 1765 | nearest_mv, |