@@ -20,10 +20,12 @@ use crate::{
2020impl From < TensorElementType > for DType {
2121 fn from ( dtype : TensorElementType ) -> Self {
2222 match dtype {
23+ TensorElementType :: Int4 => Self :: Int4 ,
2324 TensorElementType :: Int8 => Self :: Int8 ,
2425 TensorElementType :: Int16 => Self :: Int16 ,
2526 TensorElementType :: Int32 => Self :: Int32 ,
2627 TensorElementType :: Int64 => Self :: Int64 ,
28+ TensorElementType :: Uint4 => Self :: Uint4 ,
2729 TensorElementType :: Uint8 => Self :: Uint8 ,
2830 TensorElementType :: Uint16 => Self :: Uint16 ,
2931 TensorElementType :: Uint32 => Self :: Uint32 ,
@@ -32,14 +34,19 @@ impl From<TensorElementType> for DType {
3234 TensorElementType :: Float32 => Self :: Fp32 ,
3335 TensorElementType :: Float64 => Self :: Fp64 ,
3436 TensorElementType :: Bfloat16 => Self :: Bf16 ,
35- TensorElementType :: String => Self :: String ,
36- TensorElementType :: Bool => Self :: Bool ,
37+ TensorElementType :: Float8E4M3FN => Self :: Fp8e4m3fn ,
38+ TensorElementType :: Float8E4M3FNUZ => Self :: Fp8e4m3fnuz ,
39+ TensorElementType :: Float8E5M2 => Self :: Fp8e5m2 ,
40+ TensorElementType :: Float8E5M2FNUZ => Self :: Fp8e5m2fnuz ,
41+ TensorElementType :: Complex64 => Self :: Complex64 ,
42+ TensorElementType :: Complex128 => Self :: Complex128 ,
43+ _ => todo ! ( ) ,
3744 }
3845 }
3946}
4047
4148/// A struct for tensor attrs composed of the names, the dtypes, and the dimensions.
42- #[ derive( Builder , Debug , Clone ) ]
49+ #[ derive( Builder , Debug , Clone , Default ) ]
4350pub struct OrtTensorAttr {
4451 pub names : Vec < String > ,
4552 pub dtypes : Vec < TensorElementType > ,
@@ -133,7 +140,9 @@ impl Engine {
133140 let param = tensor_proto. dims. iter( ) . product:: <i64 >( ) as usize ;
134141 params += param;
135142 let param = Ops :: make_divisible( param, byte_alignment) ;
136- let n = Self :: nbytes_from_onnx_dtype_id( tensor_proto. data_type as usize ) ;
143+ let n = Self :: get_ort_dtype_from_proto_dtype_id( tensor_proto. data_type)
144+ . map( |x| x. byte_size( 1 ) )
145+ . unwrap_or_default( ) ;
137146 let wbmem = param * n;
138147 wbmems += wbmem;
139148 }
@@ -145,7 +154,10 @@ impl Engine {
145154 let param = tensor. dims. iter( ) . product:: <i64 >( ) as usize ;
146155 params += param;
147156 let param = Ops :: make_divisible( param, byte_alignment) ;
148- let n = Self :: nbytes_from_onnx_dtype_id( tensor. data_type as usize ) ;
157+ let n = Self :: get_ort_dtype_from_proto_dtype_id( tensor. data_type)
158+ . map( |x| x. byte_size( 1 ) )
159+ . unwrap_or_default( ) ;
160+
149161 let wbmem = param * n;
150162 wbmems += wbmem;
151163 }
@@ -211,7 +223,7 @@ impl Engine {
211223
212224 // update
213225 pb. set_message ( format ! (
214- "{}({}) on {:? }" ,
226+ "{}({}) on {}" ,
215227 self . spec,
216228 match self . params {
217229 Some ( bytes) if bytes != 0 => {
@@ -231,7 +243,7 @@ impl Engine {
231243
232244 pub fn run ( & mut self , xs : Xs ) -> Result < Xs > {
233245 let mut ys = xs. derive ( ) ;
234- if let Some ( onnx) = & self . onnx {
246+ if let Some ( onnx) = & mut self . onnx {
235247 // alignment
236248 let xs_ = elapsed ! ( & format!( "[{}] ort_preprocessing" , self . spec) , self . ts, {
237249 let mut xs_ = Vec :: new( ) ;
@@ -267,38 +279,22 @@ impl Engine {
267279
268280 fn preprocess ( x : & X , dtype : & TensorElementType ) -> Result < DynValue > {
269281 let x = match dtype {
270- TensorElementType :: Float32 => Value :: from_array ( x. view ( ) ) ?. into_dyn ( ) ,
271- TensorElementType :: Float16 => {
272- Value :: from_array ( x. mapv ( f16:: from_f32) . view ( ) ) ?. into_dyn ( )
273- }
274- TensorElementType :: Float64 => Value :: from_array ( x. view ( ) ) ?. into_dyn ( ) ,
275- TensorElementType :: Bfloat16 => {
276- Value :: from_array ( x. mapv ( bf16:: from_f32) . view ( ) ) ?. into_dyn ( )
277- }
278- TensorElementType :: Int8 => Value :: from_array ( x. mapv ( |x_| x_ as i8 ) . view ( ) ) ?. into_dyn ( ) ,
279- TensorElementType :: Int16 => {
280- Value :: from_array ( x. mapv ( |x_| x_ as i16 ) . view ( ) ) ?. into_dyn ( )
281- }
282- TensorElementType :: Int32 => {
283- Value :: from_array ( x. mapv ( |x_| x_ as i32 ) . view ( ) ) ?. into_dyn ( )
284- }
285- TensorElementType :: Int64 => {
286- Value :: from_array ( x. mapv ( |x_| x_ as i64 ) . view ( ) ) ?. into_dyn ( )
282+ TensorElementType :: Float32 | TensorElementType :: Float64 => {
283+ Value :: from_array ( x. 0 . clone ( ) ) ?. into_dyn ( )
287284 }
288- TensorElementType :: Uint8 => Value :: from_array ( x. mapv ( |x_| x_ as u8 ) . view ( ) ) ?. into_dyn ( ) ,
289- TensorElementType :: Uint16 => {
290- Value :: from_array ( x. mapv ( |x_| x_ as u16 ) . view ( ) ) ?. into_dyn ( )
291- }
292- TensorElementType :: Uint32 => {
293- Value :: from_array ( x. mapv ( |x_| x_ as u32 ) . view ( ) ) ?. into_dyn ( )
294- }
295- TensorElementType :: Uint64 => {
296- Value :: from_array ( x. mapv ( |x_| x_ as u64 ) . view ( ) ) ?. into_dyn ( )
297- }
298- TensorElementType :: Bool => Value :: from_array ( x. mapv ( |x_| x_ != 0. ) . view ( ) ) ?. into_dyn ( ) ,
285+ TensorElementType :: Float16 => Value :: from_array ( x. mapv ( f16 :: from_f32 ) ) ?. into_dyn ( ) ,
286+ TensorElementType :: Bfloat16 => Value :: from_array ( x . mapv ( bf16 :: from_f32 ) ) ? . into_dyn ( ) ,
287+ TensorElementType :: Int8 => Value :: from_array ( x. mapv ( |x_| x_ as i8 ) ) ?. into_dyn ( ) ,
288+ TensorElementType :: Int16 => Value :: from_array ( x . mapv ( |x_| x_ as i16 ) ) ? . into_dyn ( ) ,
289+ TensorElementType :: Int32 => Value :: from_array ( x . mapv ( |x_| x_ as i32 ) ) ? . into_dyn ( ) ,
290+ TensorElementType :: Int64 => Value :: from_array ( x. mapv ( |x_| x_ as i64 ) ) ?. into_dyn ( ) ,
291+ TensorElementType :: Uint8 => Value :: from_array ( x . mapv ( |x_| x_ as u8 ) ) ? . into_dyn ( ) ,
292+ TensorElementType :: Uint16 => Value :: from_array ( x . mapv ( |x_| x_ as u16 ) ) ? . into_dyn ( ) ,
293+ TensorElementType :: Uint32 => Value :: from_array ( x. mapv ( |x_| x_ as u32 ) ) ?. into_dyn ( ) ,
294+ TensorElementType :: Uint64 => Value :: from_array ( x . mapv ( |x_| x_ as u64 ) ) ? . into_dyn ( ) ,
295+ TensorElementType :: Bool => Value :: from_array ( x. mapv ( |x_| x_ != 0. ) ) ?. into_dyn ( ) ,
299296 _ => unimplemented ! ( ) ,
300297 } ;
301-
302298 Ok ( x)
303299 }
304300
@@ -307,7 +303,7 @@ impl Engine {
307303 where
308304 T : Clone + ' static + ort:: tensor:: PrimitiveTensorElementType ,
309305 {
310- match x. try_extract_tensor :: < T > ( ) {
306+ match x. try_extract_array :: < T > ( ) {
311307 Err ( err) => {
312308 debug ! ( "Failed to extract from ort outputs: {:?}. A default value has been generated." , err) ;
313309 Array :: zeros ( 0 ) . into_dyn ( )
@@ -344,7 +340,7 @@ impl Engine {
344340 \n Consider enabling them by passing, e.g., `--features #FEATURE`";
345341
346342 match self . device {
347- Device :: TensorRT ( id) => {
343+ Device :: TensorRt ( id) => {
348344 #[ cfg( not( feature = "trt" ) ) ]
349345 {
350346 anyhow:: bail!( feature_help
@@ -431,16 +427,28 @@ impl Engine {
431427 }
432428 }
433429 }
434- Device :: CoreML ( id) => {
435- #[ cfg( not( feature = "mps " ) ) ]
430+ Device :: CoreMl ( id) => {
431+ #[ cfg( not( feature = "coreml " ) ) ]
436432 {
437433 anyhow:: bail!( feature_help
438434 . replace( "#EP" , "CoreML" )
439- . replace( "#FEATURE" , "mps " ) ) ;
435+ . replace( "#FEATURE" , "coreml " ) ) ;
440436 }
441- #[ cfg( feature = "mps " ) ]
437+ #[ cfg( feature = "coreml " ) ]
442438 {
443- let ep = ort:: execution_providers:: CoreMLExecutionProvider :: default ( ) ;
439+ let ep = ort:: execution_providers:: CoreMLExecutionProvider :: default ( )
440+ . with_model_cache_dir (
441+ crate :: Dir :: Cache
442+ . crate_dir_default_with_subs ( & [ "coreml-cache" ] ) ?
443+ . display ( ) ,
444+ )
445+ . with_compute_units ( ort:: execution_providers:: coreml:: CoreMLComputeUnits :: All )
446+ . with_static_input_shapes ( false )
447+ . with_subgraphs ( true )
448+ . with_model_format ( ort:: execution_providers:: coreml:: CoreMLModelFormat :: MLProgram )
449+ . with_specialization_strategy (
450+ ort:: execution_providers:: coreml:: CoreMLSpecializationStrategy :: FastPrediction ,
451+ ) ;
444452 match ep. is_available ( ) {
445453 Ok ( true ) => {
446454 ep. register ( & mut builder) . map_err ( |err| {
@@ -452,13 +460,14 @@ impl Engine {
452460 }
453461 }
454462 _ => {
455- let ep = ort:: execution_providers:: CPUExecutionProvider :: default ( ) ;
463+ let ep = ort:: execution_providers:: CPUExecutionProvider :: default ( )
464+ . with_arena_allocator ( true ) ;
456465 match ep. is_available ( ) {
457466 Ok ( true ) => {
458467 ep. register ( & mut builder)
459468 . map_err ( |err| anyhow:: anyhow!( "Failed to register Cpu: {}" , err) ) ?;
460469 }
461- _ => anyhow :: bail! ( compile_help . replace ( "#EP" , "Cpu" ) ) ,
470+ _ => unreachable ! ( "CPU EP is not available. This case should ideally not be reached under normal circumstances." ) ,
462471 }
463472 }
464473 }
@@ -532,38 +541,8 @@ impl Engine {
532541 Ok ( ys)
533542 }
534543
535- #[ allow( dead_code) ]
536- fn nbytes_from_onnx_dtype_id ( x : usize ) -> usize {
537- match x {
538- 7 | 11 | 13 => 8 , // i64, f64, u64
539- 1 | 6 | 12 => 4 , // f32, i32, u32
540- 10 | 16 | 5 | 4 => 2 , // f16, bf16, i16, u16
541- 2 | 3 | 9 => 1 , // u8, i8, bool
542- 8 => 4 , // string(1~4)
543- _ => 1 , // TODO: others
544- }
545- }
546-
547- #[ allow( dead_code) ]
548- fn nbytes_from_onnx_dtype ( x : & TensorElementType ) -> usize {
549- match x {
550- TensorElementType :: Float64 | TensorElementType :: Uint64 | TensorElementType :: Int64 => 8 , // i64, f64, u64
551- TensorElementType :: Float32
552- | TensorElementType :: Uint32
553- | TensorElementType :: Int32
554- | TensorElementType :: String => 4 , // f32, i32, u32, string(1~4)
555- TensorElementType :: Float16
556- | TensorElementType :: Bfloat16
557- | TensorElementType :: Int16
558- | TensorElementType :: Uint16 => 2 , // f16, bf16, i16, u16
559- TensorElementType :: Uint8 | TensorElementType :: Int8 | TensorElementType :: Bool => 1 , // u8, i8, bool
560- }
561- }
562-
563- #[ allow( dead_code) ]
564- fn ort_dtype_from_onnx_dtype_id ( value : i32 ) -> Option < TensorElementType > {
544+ fn get_ort_dtype_from_proto_dtype_id ( value : i32 ) -> Option < TensorElementType > {
565545 match value {
566- 0 => None ,
567546 1 => Some ( TensorElementType :: Float32 ) ,
568547 2 => Some ( TensorElementType :: Uint8 ) ,
569548 3 => Some ( TensorElementType :: Int8 ) ,
@@ -577,10 +556,16 @@ impl Engine {
577556 11 => Some ( TensorElementType :: Float64 ) ,
578557 12 => Some ( TensorElementType :: Uint32 ) ,
579558 13 => Some ( TensorElementType :: Uint64 ) ,
580- 14 => None , // COMPLEX64
581- 15 => None , // COMPLEX128
559+ 14 => Some ( TensorElementType :: Complex64 ) ,
560+ 15 => Some ( TensorElementType :: Complex128 ) ,
582561 16 => Some ( TensorElementType :: Bfloat16 ) ,
583- _ => None ,
562+ 17 => Some ( TensorElementType :: Float8E4M3FN ) ,
563+ 18 => Some ( TensorElementType :: Float8E4M3FNUZ ) ,
564+ 19 => Some ( TensorElementType :: Float8E5M2 ) ,
565+ 20 => Some ( TensorElementType :: Float8E5M2FNUZ ) ,
566+ 21 => Some ( TensorElementType :: Uint4 ) ,
567+ 22 => Some ( TensorElementType :: Int4 ) ,
568+ _ => None , // 23: Float4e2m1, 0: Undefined
584569 }
585570 }
586571
@@ -609,7 +594,7 @@ impl Engine {
609594 _ => continue ,
610595 } ;
611596 let tensor_type = tensor. elem_type ;
612- let tensor_type = match Self :: ort_dtype_from_onnx_dtype_id ( tensor_type) {
597+ let tensor_type = match Self :: get_ort_dtype_from_proto_dtype_id ( tensor_type) {
613598 Some ( dtype) => dtype,
614599 None => continue ,
615600 } ;
@@ -642,24 +627,6 @@ impl Engine {
642627 } )
643628 }
644629
645- // pub fn to_ort(&self) -> TensorElementType {
646- // match self {
647- // Self::Int8 => TensorElementType::Int8,
648- // Self::Int16 => TensorElementType::Int16,
649- // Self::Int32 => TensorElementType::Int32,
650- // Self::Int64 => TensorElementType::Int64,
651- // Self::Uint8 => TensorElementType::Uint8,
652- // Self::Uint16 => TensorElementType::Uint16,
653- // Self::Uint32 => TensorElementType::Uint32,
654- // Self::Uint64 => TensorElementType::Uint64,
655- // Self::Fp16 => TensorElementType::Float16,
656- // Self::Fp32 => TensorElementType::Float32,
657- // Self::Fp64 => TensorElementType::Float64,
658- // Self::Bf16 => TensorElementType::Bfloat16,
659- // _ => todo!(),
660- // }
661- // }
662-
663630 pub fn load_onnx < P : AsRef < std:: path:: Path > > ( p : P ) -> Result < onnx:: ModelProto > {
664631 let f = std:: fs:: read ( p. as_ref ( ) ) ?;
665632 onnx:: ModelProto :: decode ( f. as_slice ( ) ) . map_err ( |err| {
0 commit comments