@@ -8,6 +8,7 @@ use std::rc::Rc;
88use dom_struct:: dom_struct;
99use js:: jsapi:: JSObject ;
1010use js:: typedarray:: { ArrayBufferU8 , Float32 , Int8 , Int16 , Int32 , Uint8 , Uint16 , Uint32 } ;
11+ use rustnn:: graph:: DataType ;
1112use webnn_traits:: { ContextId , GraphId , WebNNMsg } ;
1213
1314use crate :: dom:: bindings:: buffer_source:: create_buffer_source;
@@ -94,6 +95,15 @@ pub(crate) struct MLContext {
9495 lost : Rc < Promise > ,
9596}
9697
98+ fn operand_data_type_name ( data_type : DataType ) -> Option < & ' static str > {
99+ match data_type {
100+ DataType :: Float32 => Some ( "float32" ) ,
101+ DataType :: Int32 => Some ( "int32" ) ,
102+ DataType :: Uint8 => Some ( "uint8" ) ,
103+ _ => None ,
104+ }
105+ }
106+
97107impl MLContext {
98108 /// <https://webmachinelearning.github.io/webnn/#api-ml-createcontext>
99109 pub ( crate ) fn new_inherited (
@@ -1024,7 +1034,11 @@ impl MLContextMethods<crate::DomTypeHolder> for MLContext {
10241034 // - ban very large tensors to avoid exhausting the GPU process
10251035 // (the "large inputs" tests use ~137 MB per tensor)
10261036
1027- let data_types = Some ( vec ! [ MLOperandDataType :: Float32 , MLOperandDataType :: Int32 ] ) ;
1037+ let data_types = Some ( vec ! [
1038+ MLOperandDataType :: Float32 ,
1039+ MLOperandDataType :: Int32 ,
1040+ MLOperandDataType :: Uint8 ,
1041+ ] ) ;
10281042 // limit the size to something comfortably smaller than the large-input
10291043 // tests in wpt (/6000×6000 float32 ≈ 144 000 000 bytes).
10301044 // Pick a value comfortably below the ~144 MB used by the
@@ -1314,11 +1328,8 @@ impl MLContextMethods<crate::DomTypeHolder> for MLContext {
13141328 // Compare descriptor: operand descriptor -> tensor descriptor
13151329 if let Some ( op) = gi. operands . get ( op_id as usize ) {
13161330 // Compare data type
1317- let op_dtype_str = match op. descriptor . data_type {
1318- rustnn:: graph:: DataType :: Float32 => "float32" ,
1319- rustnn:: graph:: DataType :: Int32 => "int32" ,
1320- _ => return Err ( Error :: Type ( c"Data type not supported" . to_owned ( ) ) ) ,
1321- } ;
1331+ let op_dtype_str = operand_data_type_name ( op. descriptor . data_type )
1332+ . ok_or_else ( || Error :: Type ( c"Data type not supported" . to_owned ( ) ) ) ?;
13221333 if tensor. data_type ( ) != op_dtype_str {
13231334 return Err ( Error :: Type ( c"input tensor descriptor mismatch" . to_owned ( ) ) ) ;
13241335 }
@@ -1349,11 +1360,8 @@ impl MLContextMethods<crate::DomTypeHolder> for MLContext {
13491360 } ;
13501361
13511362 if let Some ( op) = gi. operands . get ( op_id as usize ) {
1352- let op_dtype_str = match op. descriptor . data_type {
1353- rustnn:: graph:: DataType :: Float32 => "float32" ,
1354- rustnn:: graph:: DataType :: Int32 => "int32" ,
1355- _ => return Err ( Error :: Type ( c"Data type not supported" . to_owned ( ) ) ) ,
1356- } ;
1363+ let op_dtype_str = operand_data_type_name ( op. descriptor . data_type )
1364+ . ok_or_else ( || Error :: Type ( c"Data type not supported" . to_owned ( ) ) ) ?;
13571365 if tensor. data_type ( ) != op_dtype_str {
13581366 return Err ( Error :: Type ( c"output tensor descriptor mismatch" . to_owned ( ) ) ) ;
13591367 }
0 commit comments