Skip to content

Commit 0717a23

Browse files
authored
feat(yoloe): support TensorRT/CUDA/CoreML; export ONNX models with dynamic axes (#150)
1 parent 57f5b97 commit 0717a23

6 files changed

Lines changed: 53 additions & 170 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "usls"
33
edition = "2021"
4-
version = "0.1.3"
4+
version = "0.1.4"
55
rust-version = "1.85"
66
description = "A Rust library integrated with ONNXRuntime, providing a collection of ML models."
77
repository = "https://github.com/jamjamjon/usls"

examples/yoloe-text-prompt/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ struct Args {
2121
option,
2222
default = "vec![
2323
String::from(\"person\"),
24-
String::from(\"bus\"),
2524
String::from(\"dog\"),
25+
String::from(\"bus\"),
2626
String::from(\"cat\"),
2727
String::from(\"sign\"),
2828
String::from(\"tree\"),
2929
]"
3030
)]
3131
labels: Vec<String>,
32+
33+
/// batch size
34+
#[argh(option, default = "1")]
35+
batch_size: usize,
3236
}
3337

3438
fn main() -> Result<()> {
@@ -39,10 +43,11 @@ fn main() -> Result<()> {
3943
let args: Args = argh::from_env();
4044

4145
// config
42-
let config = Config::yoloe_v8l_seg_tp_80()
46+
let config = Config::yoloe_v8m_seg_tp()
47+
.with_batch_size_all_min_opt_max(1, args.batch_size, 8)
4348
.with_model_dtype(args.dtype.as_str().parse()?)
44-
.with_model_device(args.device.as_str().parse()?)
45-
.with_batch_size_all(1)
49+
.with_textual_dtype("fp16".parse()?) // Use FP32 when TensorRT is enabled
50+
.with_device_all(args.device.as_str().parse()?)
4651
.commit()?;
4752
let mut model = YOLO::new(config)?;
4853

src/core/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,33 @@ impl Config {
198198
self
199199
}
200200

201+
pub fn with_batch_size_all_min_opt_max(mut self, min: usize, opt: usize, max: usize) -> Self {
202+
self.visual = self.visual.with_ixx(0, 0, (min, opt, max).into());
203+
self.textual = self.textual.with_ixx(0, 0, (min, opt, max).into());
204+
self.model = self.model.with_ixx(0, 0, (min, opt, max).into());
205+
self.encoder = self.encoder.with_ixx(0, 0, (min, opt, max).into());
206+
self.decoder = self.decoder.with_ixx(0, 0, (min, opt, max).into());
207+
self.visual_encoder = self.visual_encoder.with_ixx(0, 0, (min, opt, max).into());
208+
self.textual_encoder = self.textual_encoder.with_ixx(0, 0, (min, opt, max).into());
209+
self.visual_decoder = self.visual_decoder.with_ixx(0, 0, (min, opt, max).into());
210+
self.textual_decoder = self.textual_decoder.with_ixx(0, 0, (min, opt, max).into());
211+
self.textual_decoder_merged =
212+
self.textual_decoder_merged
213+
.with_ixx(0, 0, (min, opt, max).into());
214+
self.size_encoder = self.size_encoder.with_ixx(0, 0, (min, opt, max).into());
215+
self.size_decoder = self.size_decoder.with_ixx(0, 0, (min, opt, max).into());
216+
self.coord_encoder = self.coord_encoder.with_ixx(0, 0, (min, opt, max).into());
217+
self.coord_decoder = self.coord_decoder.with_ixx(0, 0, (min, opt, max).into());
218+
self.visual_projection = self
219+
.visual_projection
220+
.with_ixx(0, 0, (min, opt, max).into());
221+
self.textual_projection = self
222+
.textual_projection
223+
.with_ixx(0, 0, (min, opt, max).into());
224+
225+
self
226+
}
227+
201228
pub fn with_batch_size_all(mut self, batch_size: usize) -> Self {
202229
self.visual = self.visual.with_ixx(0, 0, batch_size.into());
203230
self.textual = self.textual.with_ixx(0, 0, batch_size.into());

src/core/ort_config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ impl ORTConfig {
126126
self
127127
}
128128

129-
// Hardware configuration methods
130129
pub fn with_cpu_arena_allocator(mut self, x: bool) -> Self {
131130
self.hardware.cpu.arena_allocator = x;
132131
self

src/core/ort_engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ impl OrtEngine {
694694
.with_static_input_shapes(self.hardware.coreml.static_input_shapes)
695695
.with_subgraphs(self.hardware.coreml.subgraph_running)
696696
.with_compute_units(ort::execution_providers::coreml::CoreMLComputeUnits::All)
697-
.with_model_format(ort::execution_providers::coreml::CoreMLModelFormat::MLProgram)
697+
// .with_model_format(ort::execution_providers::coreml::CoreMLModelFormat::MLProgram)
698698
.with_specialization_strategy(
699699
ort::execution_providers::coreml::CoreMLSpecializationStrategy::FastPrediction,
700700
);

src/models/yolo/config.rs

Lines changed: 15 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -180,205 +180,57 @@ impl Config {
180180

181181
fn yoloe_seg_tp() -> Self {
182182
Self::yolo()
183-
.with_num_dry_run_all(0) // TODO: The text encoder must align with the number of classes; running a dry run will cause an error.
184183
.with_batch_size_all(1)
184+
.with_nc(80)
185+
.with_model_ixx(1, 1, (1, 80, 300).into()) // max_text_classes
185186
.with_task(Task::InstanceSegmentation)
186187
.with_textual_file("mobileclip/blt-textual.onnx")
187188
.with_model_max_length(77)
189+
.with_textual_ixx(0, 1, 77.into())
188190
.with_tokenizer_file("clip/tokenizer.json")
189191
.with_tokenizer_config_file("clip/tokenizer_config.json")
190192
.with_special_tokens_map_file("clip/special_tokens_map.json")
191193
}
192194

193-
pub fn yoloe_v8s_seg_tp_80() -> Self {
194-
Self::yoloe_seg_tp()
195-
.with_version(8.into())
196-
.with_scale(Scale::S)
197-
.with_nc(80)
198-
.with_model_file("yoloe-v8s-seg-tp-80.onnx")
199-
}
200-
201-
pub fn yoloe_v8m_seg_tp_80() -> Self {
202-
Self::yoloe_seg_tp()
203-
.with_version(8.into())
204-
.with_scale(Scale::M)
205-
.with_nc(80)
206-
.with_model_file("yoloe-v8m-seg-tp-80.onnx")
207-
}
208-
209-
pub fn yoloe_v8l_seg_tp_80() -> Self {
210-
Self::yoloe_seg_tp()
211-
.with_version(8.into())
212-
.with_scale(Scale::L)
213-
.with_nc(80)
214-
.with_model_file("yoloe-v8l-seg-tp-80.onnx")
215-
}
216-
217-
pub fn yoloe_v8s_seg_tp_300() -> Self {
218-
Self::yoloe_seg_tp()
219-
.with_version(8.into())
220-
.with_scale(Scale::S)
221-
.with_nc(300)
222-
.with_model_file("yoloe-v8s-seg-tp-300.onnx")
223-
}
224-
225-
pub fn yoloe_v8m_seg_tp_300() -> Self {
226-
Self::yoloe_seg_tp()
227-
.with_version(8.into())
228-
.with_scale(Scale::M)
229-
.with_nc(300)
230-
.with_model_file("yoloe-v8m-seg-tp-300.onnx")
231-
}
232-
233-
pub fn yoloe_v8l_seg_tp_300() -> Self {
234-
Self::yoloe_seg_tp()
235-
.with_version(8.into())
236-
.with_scale(Scale::L)
237-
.with_nc(300)
238-
.with_model_file("yoloe-v8l-seg-tp-300.onnx")
239-
}
240-
241-
pub fn yoloe_v8s_seg_tp_500() -> Self {
242-
Self::yoloe_seg_tp()
243-
.with_version(8.into())
244-
.with_scale(Scale::S)
245-
.with_nc(500)
246-
.with_model_file("yoloe-v8s-seg-tp-500.onnx")
247-
}
248-
249-
pub fn yoloe_v8m_seg_tp_500() -> Self {
250-
Self::yoloe_seg_tp()
251-
.with_version(8.into())
252-
.with_scale(Scale::M)
253-
.with_nc(500)
254-
.with_model_file("yoloe-v8m-seg-tp-500.onnx")
255-
}
256-
257-
pub fn yoloe_v8l_seg_tp_500() -> Self {
258-
Self::yoloe_seg_tp()
259-
.with_version(8.into())
260-
.with_scale(Scale::L)
261-
.with_nc(500)
262-
.with_model_file("yoloe-v8l-seg-tp-500.onnx")
263-
}
264-
265-
pub fn yoloe_v8s_seg_tp_2000() -> Self {
195+
pub fn yoloe_v8s_seg_tp() -> Self {
266196
Self::yoloe_seg_tp()
267197
.with_version(8.into())
268198
.with_scale(Scale::S)
269-
.with_nc(2000)
270-
.with_model_file("yoloe-v8s-seg-tp-2000.onnx")
199+
.with_model_file("yoloe-v8s-seg-tp.onnx")
271200
}
272201

273-
pub fn yoloe_v8m_seg_tp_2000() -> Self {
202+
pub fn yoloe_v8m_seg_tp() -> Self {
274203
Self::yoloe_seg_tp()
275204
.with_version(8.into())
276205
.with_scale(Scale::M)
277-
.with_nc(2000)
278-
.with_model_file("yoloe-v8m-seg-tp-2000.onnx")
206+
.with_model_file("yoloe-v8m-seg-tp.onnx")
279207
}
280208

281-
pub fn yoloe_v8l_seg_tp_2000() -> Self {
209+
pub fn yoloe_v8l_seg_tp() -> Self {
282210
Self::yoloe_seg_tp()
283211
.with_version(8.into())
284212
.with_scale(Scale::L)
285-
.with_nc(2000)
286-
.with_model_file("yoloe-v8l-seg-tp-2000.onnx")
287-
}
288-
289-
pub fn yoloe_11s_seg_tp_80() -> Self {
290-
Self::yoloe_seg_tp()
291-
.with_version(11.into())
292-
.with_scale(Scale::S)
293-
.with_nc(80)
294-
.with_model_file("yoloe-11s-seg-tp-80.onnx")
295-
}
296-
297-
pub fn yoloe_11m_seg_tp_80() -> Self {
298-
Self::yoloe_seg_tp()
299-
.with_version(11.into())
300-
.with_scale(Scale::M)
301-
.with_nc(80)
302-
.with_model_file("yoloe-11m-seg-tp-80.onnx")
303-
}
304-
305-
pub fn yoloe_11l_seg_tp_80() -> Self {
306-
Self::yoloe_seg_tp()
307-
.with_version(11.into())
308-
.with_scale(Scale::L)
309-
.with_nc(80)
310-
.with_model_file("yoloe-11l-seg-tp-80.onnx")
311-
}
312-
313-
pub fn yoloe_11s_seg_tp_300() -> Self {
314-
Self::yoloe_seg_tp()
315-
.with_version(11.into())
316-
.with_scale(Scale::S)
317-
.with_nc(300)
318-
.with_model_file("yoloe-11s-seg-tp-300.onnx")
319-
}
320-
321-
pub fn yoloe_11m_seg_tp_300() -> Self {
322-
Self::yoloe_seg_tp()
323-
.with_version(11.into())
324-
.with_scale(Scale::M)
325-
.with_nc(300)
326-
.with_model_file("yoloe-11m-seg-tp-300.onnx")
327-
}
328-
329-
pub fn yoloe_11l_seg_tp_300() -> Self {
330-
Self::yoloe_seg_tp()
331-
.with_version(11.into())
332-
.with_scale(Scale::L)
333-
.with_nc(300)
334-
.with_model_file("yoloe-11l-seg-tp-300.onnx")
335-
}
336-
337-
pub fn yoloe_11s_seg_tp_500() -> Self {
338-
Self::yoloe_seg_tp()
339-
.with_version(11.into())
340-
.with_scale(Scale::S)
341-
.with_nc(500)
342-
.with_model_file("yoloe-11s-seg-tp-500.onnx")
343-
}
344-
345-
pub fn yoloe_11m_seg_tp_500() -> Self {
346-
Self::yoloe_seg_tp()
347-
.with_version(11.into())
348-
.with_scale(Scale::M)
349-
.with_nc(500)
350-
.with_model_file("yoloe-11m-seg-tp-500.onnx")
351-
}
352-
353-
pub fn yoloe_11l_seg_tp_500() -> Self {
354-
Self::yoloe_seg_tp()
355-
.with_version(11.into())
356-
.with_scale(Scale::L)
357-
.with_nc(500)
358-
.with_model_file("yoloe-11l-seg-tp-500.onnx")
213+
.with_model_file("yoloe-v8l-seg-tp.onnx")
359214
}
360215

361-
pub fn yoloe_11s_seg_tp_2000() -> Self {
216+
pub fn yoloe_11s_seg_tp() -> Self {
362217
Self::yoloe_seg_tp()
363218
.with_version(11.into())
364219
.with_scale(Scale::S)
365-
.with_nc(2000)
366-
.with_model_file("yoloe-11s-seg-tp-2000.onnx")
220+
.with_model_file("yoloe-11s-seg-tp.onnx")
367221
}
368222

369-
pub fn yoloe_11m_seg_tp_2000() -> Self {
223+
pub fn yoloe_11m_seg_tp() -> Self {
370224
Self::yoloe_seg_tp()
371225
.with_version(11.into())
372226
.with_scale(Scale::M)
373-
.with_nc(2000)
374-
.with_model_file("yoloe-11m-seg-tp-2000.onnx")
227+
.with_model_file("yoloe-11m-seg-tp.onnx")
375228
}
376229

377-
pub fn yoloe_11l_seg_tp_2000() -> Self {
230+
pub fn yoloe_11l_seg_tp() -> Self {
378231
Self::yoloe_seg_tp()
379232
.with_version(11.into())
380233
.with_scale(Scale::L)
381-
.with_nc(2000)
382-
.with_model_file("yoloe-11l-seg-tp-2000.onnx")
234+
.with_model_file("yoloe-11l-seg-tp.onnx")
383235
}
384236
}

0 commit comments

Comments
 (0)