π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Remove redundant OCR input allocation and batch decoding wrappers
laurenspriem715850a86537
diff --git a/rust/crates/ml/src/ocr/context.rs b/rust/crates/ml/src/ocr/context.rsindex 957fbc95c5..d45c8dfef1 100644--- a/rust/crates/ml/src/ocr/context.rs+++ b/rust/crates/ml/src/ocr/context.rs@@ -193,16 +193,17 @@ pub(super) fn branch_widths() -> &'static [usize] {}pub(super) fn recognizer_paths(mut selected: Vec<Input>, width: usize) -> Vec<Input> {- let mut original = selected.remove(0);- let mut image = Input::zeros("x", &[1, 3, 48, 7168]);- if width == 7168 {- std::mem::swap(&mut image.values, &mut original.values);+ let original = selected.remove(0);+ let image = if width == 7168 {+ original} else {+ let mut image = Input::zeros("x", &[1, 3, 48, 7168]);for row in 0..3 * 48 {image.values[row * 7168..row * 7168 + width].copy_from_slice(&original.values[row * width..(row + 1) * width]);}- }+ image+ };static UNUSED: std::sync::OnceLock<Vec<Vec<Input>>> = std::sync::OnceLock::new();let widths = branch_widths();let unused = UNUSED.get_or_init(|| {@@ -353,10 +354,10 @@ mod tests {let (inputs, offsets) = packed(&[&line], 7168, 21);assert_eq!(offsets, [0]);assert_eq!(input(&inputs, "mask_1_896").values[895], 1.0);- assert_eq!(- input(&recognizer_paths(inputs, 7168), "path_0").values[0],- 0.0- );+ let pixels = input(&inputs, "x").values.as_ptr();+ let inputs = recognizer_paths(inputs, 7168);+ assert_eq!(input(&inputs, "path_0").values[0], 0.0);+ assert_eq!(input(&inputs, "x").values.as_ptr(), pixels);}}}diff --git a/rust/crates/ml/src/ocr/recognize.rs b/rust/crates/ml/src/ocr/recognize.rsindex f3aa49918b..f738d77abb 100644--- a/rust/crates/ml/src/ocr/recognize.rs+++ b/rust/crates/ml/src/ocr/recognize.rs@@ -143,17 +143,11 @@ impl TextRecognizer {let line = &lines[index];let start = offset / 8 * 2;let count = line.width.div_ceil(4) / 2;- let layout = BatchLayout {- target_width: line.width as i32,- content_widths: vec![line.content_width],- };- let mut decoded = decode_compact_output(- &[1, count as i64, 2],+ results[crop_indices[index]] = decode_line(&values[start..start + count * 2],- &layout,+ line.width as f32 / line.content_width as f32,dictionary,)?;- results[crop_indices[index]] = decoded.remove(0);lines[index].values = Vec::new();}}@@ -161,50 +155,21 @@ impl TextRecognizer {}}-fn decode_compact_output(- shape: &[i64],- values: &[f32],- layout: &BatchLayout,- dictionary: &[String],-) -> MlResult<Vec<Recognition>> {- let count = layout.content_widths.len();- let steps = match *shape {- [n, steps, 2]- if n == count as i64 && steps > 0 && values.len() == count * steps as usize * 2 =>- {- steps as usize- }- _ => {- return Err(MlError::CorruptModel(format!(- "text recognizer produced output shape {shape:?} with {} values, expected [{count}, T, 2]",- values.len()- )));- }- };- layout- .content_widths- .iter()- .zip(values.chunks_exact(steps * 2))- .map(|(&content_width, sequence)| {- let best = compact_steps(sequence, dictionary.len())?;- Ok(decode_steps(- &best,- dictionary,- layout.padding_scale(content_width),- ))- })- .collect()-}--fn compact_steps(values: &[f32], vocabulary: usize) -> MlResult<Vec<StepBest>> {- values+fn decode_line(values: &[f32], padding_scale: f32, dictionary: &[String]) -> MlResult<Recognition> {+ if values.is_empty() || !values.len().is_multiple_of(2) {+ return Err(MlError::CorruptModel(format!(+ "text recognizer produced {} values for a line, expected index/probability pairs",+ values.len()+ )));+ }+ let best = values.as_chunks::<2>().0.iter().map(|&[index, probability]| {if !index.is_finite()|| index < 0.0- || index >= vocabulary as f32+ || index >= dictionary.len() as f32|| index.fract() != 0.0|| !probability.is_finite()|| probability < 0.0@@ -218,7 +183,8 @@ fn compact_steps(values: &[f32], vocabulary: usize) -> MlResult<Vec<StepBest>> {probability,})})- .collect()+ .collect::<MlResult<Vec<_>>>()?;+ Ok(decode_steps(&best, dictionary, padding_scale))}struct PlannedBatch {@@ -319,10 +285,6 @@ impl BatchLayout {})}- fn padding_scale(&self, content_width: i32) -> f32 {- self.target_width as f32 / content_width as f32- }-fn tensor(&self, batch: &[&ImageU8]) -> MlResult<Vec<f32>> {let columns = batch.len().checked_mul(self.target_width as usize);if !(REC_BASE_WIDTH..=REC_MAX_WIDTH).contains(&self.target_width)@@ -576,7 +538,7 @@ mod tests {logits,output.vocabulary,dictionary,- layout.padding_scale(content_width),+ layout.target_width as f32 / content_width as f32,)}).collect())@@ -669,18 +631,25 @@ mod tests {content_widths: vec![160, 320],};let expected = decode_output(&[2, 6, 4], &full, &layout, &dictionary()).unwrap();- let actual = decode_compact_output(&[2, 6, 2], &compact, &layout, &dictionary()).unwrap();+ let actual: Vec<_> = compact+ .chunks_exact(12)+ .zip(&layout.content_widths)+ .map(|(values, &content_width)| {+ decode_line(+ values,+ layout.target_width as f32 / content_width as f32,+ &dictionary(),+ )+ .unwrap()+ })+ .collect();assert_eq!(actual, expected);assert_eq!(actual[0].text, "aabc");assert_eq!(actual[1], Recognition::default());}#[test]- fn compact_output_rejects_invalid_indices_probabilities_and_shapes() {- let layout = BatchLayout {- target_width: 320,- content_widths: vec![320],- };+ fn compact_output_rejects_invalid_indices_probabilities_and_lengths() {for values in [[-1.0, 0.9],[4.0, 0.9],@@ -691,10 +660,10 @@ mod tests {[1.0, f32::INFINITY],[1.0, -0.1],] {- assert!(decode_compact_output(&[1, 1, 2], &values, &layout, &dictionary()).is_err());+ assert!(decode_line(&values, 1.0, &dictionary()).is_err());}- for shape in [[2, 1, 2], [1, 0, 2], [1, 2, 2], [1, 1, 4]] {- assert!(decode_compact_output(&shape, &[1.0, 0.9], &layout, &dictionary()).is_err());+ for values in [&[][..], &[1.0], &[1.0, 0.9, 2.0]] {+ assert!(decode_line(values, 1.0, &dictionary()).is_err());}}@@ -752,7 +721,6 @@ mod tests {let layout = BatchLayout::new(&crops.iter().collect::<Vec<_>>()).unwrap();assert_eq!(layout.target_width, 400);assert_eq!(layout.content_widths, [100, 400, 112]);- assert_close(layout.padding_scale(100), 4.0);let narrow = [solid(10, 48, [0; 3])];let layout = BatchLayout::new(&narrow.iter().collect::<Vec<_>>()).unwrap();@@ -1107,7 +1075,6 @@ mod tests {assert_eq!(layout.target_width, REC_MAX_WIDTH);assert_eq!(layout.content_widths, [REC_MAX_WIDTH]);assert_eq!(content_height(&crop), resized_height);- assert_eq!(layout.padding_scale(REC_MAX_WIDTH), 1.0);let tensor = layout.tensor(&[&crop]).unwrap();assert_eq!(tensor.len(), 3 * 48 * 7168);let top = (48 - resized_height) as usize / 2;