@@ -6,6 +6,7 @@ import {buildConstantByNpy} from '../common/utils.js';
66export class MobileNetV2Nchw {
77 constructor ( ) {
88 this . context_ = null ;
9+ this . devicePreference_ = null ;
910 this . builder_ = null ;
1011 this . graph_ = null ;
1112 this . weightsUrl_ = '../test-data/models/mobilenetv2_nchw/weights/' ;
@@ -30,10 +31,16 @@ export class MobileNetV2Nchw {
3031 await buildConstantByNpy ( this . builder_ , biasName ) ;
3132 options . bias = bias ;
3233 if ( relu6 ) {
33- // implement `relu6` by `clamp` of WebNN API
34- options . activation = this . builder_ . clamp ( { minValue : 0 , maxValue : 6 } ) ;
35- } else {
36- options . activation = undefined ;
34+ // TODO: Set clamp activation to options once it's supported in
35+ // WebNN DML backend.
36+ // Implement `clip` by `clamp` of WebNN API
37+ if ( this . devicePreference_ == 'gpu' ) {
38+ return this . builder_ . clamp (
39+ this . builder_ . conv2d ( input , weights , options ) ,
40+ { minValue : 0 , maxValue : 6 } ) ;
41+ } else {
42+ options . activation = this . builder_ . clamp ( { minValue : 0 , maxValue : 6 } ) ;
43+ }
3744 }
3845 return this . builder_ . conv2d ( input , weights , options ) ;
3946 }
@@ -69,6 +76,7 @@ export class MobileNetV2Nchw {
6976
7077 async load ( contextOptions ) {
7178 this . context_ = await navigator . ml . createContext ( contextOptions ) ;
79+ this . devicePreference_ = contextOptions . devicePreference ;
7280 this . builder_ = new MLGraphBuilder ( this . context_ ) ;
7381 const data = this . builder_ . input ( 'input' ,
7482 { type : 'float32' , dimensions : this . inputOptions . inputDimensions } ) ;
0 commit comments