oar_ocr/core/config/
parallel.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ParallelPolicy {
12 #[serde(default)]
16 pub max_threads: Option<usize>,
17
18 #[serde(default = "ParallelPolicy::default_image_threshold")]
21 pub image_threshold: usize,
22
23 #[serde(default = "ParallelPolicy::default_text_box_threshold")]
26 pub text_box_threshold: usize,
27
28 #[serde(default = "ParallelPolicy::default_batch_threshold")]
31 pub batch_threshold: usize,
32
33 #[serde(default = "ParallelPolicy::default_utility_threshold")]
36 pub utility_threshold: usize,
37
38 #[serde(default = "ParallelPolicy::default_postprocess_pixel_threshold")]
41 pub postprocess_pixel_threshold: usize,
42
43 #[serde(default)]
45 pub onnx_threading: OnnxThreadingConfig,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, Default)]
50pub struct OnnxThreadingConfig {
51 #[serde(default)]
54 pub intra_threads: Option<usize>,
55
56 #[serde(default)]
59 pub inter_threads: Option<usize>,
60
61 #[serde(default)]
64 pub parallel_execution: Option<bool>,
65}
66
67impl OnnxThreadingConfig {
68 pub fn new() -> Self {
70 Self::default()
71 }
72
73 pub fn with_intra_threads(mut self, threads: Option<usize>) -> Self {
75 self.intra_threads = threads;
76 self
77 }
78
79 pub fn with_inter_threads(mut self, threads: Option<usize>) -> Self {
81 self.inter_threads = threads;
82 self
83 }
84
85 pub fn with_parallel_execution(mut self, enabled: Option<bool>) -> Self {
87 self.parallel_execution = enabled;
88 self
89 }
90
91 pub fn to_ort_session_config(&self) -> crate::core::config::OrtSessionConfig {
93 let mut config = crate::core::config::OrtSessionConfig::new();
94
95 if let Some(intra) = self.intra_threads {
96 config = config.with_intra_threads(intra);
97 }
98
99 if let Some(inter) = self.inter_threads {
100 config = config.with_inter_threads(inter);
101 }
102
103 if let Some(parallel) = self.parallel_execution {
104 config = config.with_parallel_execution(parallel);
105 }
106
107 config
108 }
109}
110
111impl ParallelPolicy {
112 pub fn new() -> Self {
114 Self::default()
115 }
116
117 pub fn with_max_threads(mut self, max_threads: Option<usize>) -> Self {
119 self.max_threads = max_threads;
120 self
121 }
122
123 pub fn with_image_threshold(mut self, threshold: usize) -> Self {
125 self.image_threshold = threshold;
126 self
127 }
128
129 pub fn with_text_box_threshold(mut self, threshold: usize) -> Self {
131 self.text_box_threshold = threshold;
132 self
133 }
134
135 pub fn with_batch_threshold(mut self, threshold: usize) -> Self {
137 self.batch_threshold = threshold;
138 self
139 }
140
141 pub fn with_postprocess_pixel_threshold(mut self, threshold: usize) -> Self {
143 self.postprocess_pixel_threshold = threshold;
144 self
145 }
146
147 pub fn with_onnx_threading(mut self, config: OnnxThreadingConfig) -> Self {
149 self.onnx_threading = config;
150 self
151 }
152
153 pub fn with_utility_threshold(mut self, threshold: usize) -> Self {
155 self.utility_threshold = threshold;
156 self
157 }
158
159 fn default_image_threshold() -> usize {
161 1
162 }
163
164 fn default_text_box_threshold() -> usize {
166 1
167 }
168
169 fn default_batch_threshold() -> usize {
171 10
172 }
173
174 fn default_utility_threshold() -> usize {
176 4 }
178
179 fn default_postprocess_pixel_threshold() -> usize {
181 8_000
182 }
183}
184
185impl Default for ParallelPolicy {
186 fn default() -> Self {
187 Self {
188 max_threads: None,
189 image_threshold: Self::default_image_threshold(),
190 text_box_threshold: Self::default_text_box_threshold(),
191 batch_threshold: Self::default_batch_threshold(),
192 utility_threshold: Self::default_utility_threshold(),
193 postprocess_pixel_threshold: Self::default_postprocess_pixel_threshold(),
194 onnx_threading: OnnxThreadingConfig::default(),
195 }
196 }
197}