license import { Tensor } from '@tensorflow/tfjs-core' ; import { test_util } from '@tensorflow/tfjs-core' ; import * as tfOps from '@tensorflow/tfjs-core/dist/ops/ops_for_converter' ; import { ExecutionContext } from '../../executor/execution_context' ; import { Node } from '../types' ; import { executeOp } from './graph_executor' ; import { createNumberAttr , createStrAttr , createTensorAttr , createTensorsAttr } from './test_helper' ; describe ( 'graph' , ( ) => { let node ; const input1 = [ tfOps . tensor1d ( [ 1 ] ) ] ; const input2 = [ tfOps . tensor1d ( [ 1 ] ) ] ; const input3 = [ tfOps . tensor3d ( [ 1 , 1 , 1 , 2 , 2 , 2 ] , [ 1 , 2 , 3 ] ) ] ; const context = new ExecutionContext ( { } , { } , { } ) ; beforeEach ( ( ) => { node = { name : 'input1' , op : '' , category : 'graph' , inputNames : [ ] , inputs : [ ] , inputParams : { } , attrParams : { } , children : [ ] } ; } ) ; describe ( 'executeOp' , ( ) => { describe ( 'Const' , ( ) => { it ( 'should return input' , ( ) => { node . op = 'Const' ; expect ( executeOp ( node , { input1 } , context ) ) . toEqual ( input1 ) ; } ) ; } ) ; describe ( 'placeholder' , ( ) => { it ( 'should return input' , ( ) => { node . op = 'Placeholder' ; expect ( executeOp ( node , { input1 } , context ) ) . toEqual ( input1 ) ; } ) ; it ( 'should return default if input not set' , ( ) => { node . inputNames = [ 'input2' ] ; node . op = 'PlaceholderWithDefault' ; node . inputParams . default = createTensorAttr ( 0 ) ; expect ( executeOp ( node , { input2 } , context ) ) . toEqual ( input2 ) ; } ) ; } ) ; describe ( 'Identity' , ( ) => { it ( 'should return input' , async ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'Identity' ; test_util . expectArraysEqual ( await ( executeOp ( node , { input : input1 } , context ) as Tensor [ ] ) [ 0 ] . array ( ) , await input1 [ 0 ] . array ( ) ) ; } ) ; } ) ; describe ( 'IdentityN' , ( ) => { it ( 'should return inputs' , ( ) => { node . inputNames = [ 'input1' , 'input3' ] ; node . inputParams . x = createTensorsAttr ( 0 , 0 ) ; node . op = 'IdentityN' ; expect ( ( executeOp ( node , { input1 , input3 } , context ) as Tensor [ ] ) . map ( t => { return Array . prototype . slice . call ( t . dataSync ( ) ) ; } ) ) . toEqual ( [ [ 1 ] , [ 1 , 1 , 1 , 2 , 2 , 2 ] ] ) ; } ) ; } ) ; describe ( 'Snapshot' , ( ) => { it ( 'should return input' , async ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'Snapshot' ; const result = ( executeOp ( node , { input : input1 } , context ) as Tensor [ ] ) [ 0 ] ; expect ( result . rank ) . toEqual ( input1 [ 0 ] . rank ) ; test_util . expectArraysClose ( await result . data ( ) , [ 1 ] ) ; } ) ; } ) ; describe ( 'Shape' , ( ) => { it ( 'should return shape' , ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'Shape' ; expect ( Array . prototype . slice . call ( ( executeOp ( node , { input : input3 } , context ) as Tensor [ ] ) [ 0 ] . dataSync ( ) ) ) . toEqual ( [ 1 , 2 , 3 ] ) ; } ) ; } ) ; describe ( 'ShapeN' , ( ) => { it ( 'should return shapeN' , ( ) => { node . inputNames = [ 'input1' , 'input3' ] ; node . inputParams . x = createTensorsAttr ( 0 , 0 ) ; node . op = 'ShapeN' ; expect ( ( executeOp ( node , { input1 , input3 } , context ) as Tensor [ ] ) . map ( t => { return Array . prototype . slice . call ( t . dataSync ( ) ) ; } ) ) . toEqual ( [ [ 1 ] , [ 1 , 2 , 3 ] ] ) ; } ) ; } ) ; describe ( 'Size' , ( ) => { it ( 'should return size' , ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'Size' ; expect ( Array . prototype . slice . call ( ( executeOp ( node , { input : input3 } , context ) as Tensor [ ] ) [ 0 ] . dataSync ( ) ) ) . toEqual ( [ 6 ] ) ; } ) ; } ) ; describe ( 'Rank' , ( ) => { it ( 'should return rank' , ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'Rank' ; expect ( Array . prototype . slice . call ( ( executeOp ( node , { input : input3 } , context ) as Tensor [ ] ) [ 0 ] . dataSync ( ) ) ) . toEqual ( [ 3 ] ) ; } ) ; } ) ; describe ( 'NoOp' , ( ) => { it ( 'should return empty' , async ( ) => { node . op = 'NoOp' ; test_util . expectArraysClose ( await ( executeOp ( node , { } , context ) as Tensor [ ] ) [ 0 ] . data ( ) , [ 1 ] ) ; } ) ; } ) ; } ) ; describe ( 'Print' , ( ) => { it ( 'should return empty' , ( ) => { node . op = 'Print' ; node . inputNames = [ 'input1' , 'input2' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . inputParams . data = createTensorsAttr ( 1 , 2 ) ; node . attrParams . message = createStrAttr ( 'message' ) ; node . attrParams . summarize = createNumberAttr ( 1 ) ; spyOn ( console , 'log' ) . and . callThrough ( ) ; spyOn ( console , 'warn' ) . and . callThrough ( ) ; expect ( executeOp ( node , { input1 , input2 } , context ) ) . toEqual ( input1 ) ; expect ( console . warn ) . toHaveBeenCalled ( ) ; expect ( console . log ) . toHaveBeenCalledWith ( 'message' ) ; expect ( console . log ) . toHaveBeenCalledWith ( [ 1 ] ) ; } ) ; } ) ; describe ( 'StopGradient' , ( ) => { it ( 'should return input' , async ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'StopGradient' ; test_util . expectArraysClose ( await ( executeOp ( node , { input : input1 } , context ) as Tensor [ ] ) [ 0 ] . array ( ) , await input1 [ 0 ] . array ( ) ) ; } ) ; } ) ; describe ( 'FakeQuantWithMinMaxVars' , ( ) => { it ( 'should return input' , async ( ) => { node . inputNames = [ 'input' ] ; node . inputParams . x = createTensorAttr ( 0 ) ; node . op = 'FakeQuantWithMinMaxVars' ; test_util . expectArraysClose ( await ( executeOp ( node , { input : input1 } , context ) as Tensor [ ] ) [ 0 ] . array ( ) , await input1 [ 0 ] . array ( ) ) ; } ) ; } ) ; } ) ;