license import * as tfc from '@tensorflow/tfjs-core' ; import { NodeJSKernelBackend } from '../nodejs_kernel_backend' ; import { createTensorsTypeOpAttr , createTypeOpAttr , ensureTensorflowBackend , getTFDType , nodeBackend } from './op_utils' ; describe ( 'Exposes Backend for internal Op execution.' , ( ) => { it ( 'Provides the Node backend over a function' , ( ) => { const backend = nodeBackend ( ) ; expect ( backend instanceof NodeJSKernelBackend ) . toBeTruthy ( ) ; } ) ; it ( 'Provides internal access to the binding' , ( ) => { expect ( nodeBackend ( ) . binding ) . toBeDefined ( ) ; } ) ; it ( 'throw error if backend is not tensorflow' , async ( done ) => { try { tfc . setBackend ( 'cpu' ) ; ensureTensorflowBackend ( ) ; done . fail ( ) ; } catch ( err ) { expect ( err . message ) . toBe ( 'Expect the current backend to be "tensorflow", but got "cpu"' ) ; tfc . setBackend ( 'tensorflow' ) ; done ( ) ; } } ) ; } ) ; describe ( 'getTFDType()' , ( ) => { const binding = nodeBackend ( ) . binding ; it ( 'handles float32' , ( ) => { expect ( getTFDType ( 'float32' ) ) . toBe ( binding . TF_FLOAT ) ; } ) ; it ( 'handles int32' , ( ) => { expect ( getTFDType ( 'int32' ) ) . toBe ( binding . TF_INT32 ) ; } ) ; it ( 'handles bool' , ( ) => { expect ( getTFDType ( 'bool' ) ) . toBe ( binding . TF_BOOL ) ; } ) ; it ( 'handles unknown types' , ( ) => { expect ( ( ) => getTFDType ( null ) ) . toThrowError ( ) ; } ) ; } ) ; describe ( 'createTypeOpAttr()' , ( ) => { const binding = nodeBackend ( ) . binding ; it ( 'Creates a valid type attribute' , ( ) => { const attr = createTypeOpAttr ( 'foo' , 'float32' ) ; expect ( attr . name ) . toBe ( 'foo' ) ; expect ( attr . type ) . toBe ( binding . TF_ATTR_TYPE ) ; expect ( attr . value ) . toBe ( binding . TF_FLOAT ) ; } ) ; it ( 'handles unknown dtypes' , ( ) => { expect ( ( ) => createTypeOpAttr ( 'foo' , null ) ) . toThrowError ( ) ; } ) ; } ) ; describe ( 'Returns TFEOpAttr for a Tensor or list of Tensors' , ( ) => { const binding = nodeBackend ( ) . binding ; it ( 'handles a single Tensor' , ( ) => { const result = createTensorsTypeOpAttr ( 'T' , tfc . scalar ( 13 , 'float32' ) ) ; expect ( result . name ) . toBe ( 'T' ) ; expect ( result . type ) . toBe ( binding . TF_ATTR_TYPE ) ; expect ( result . value ) . toBe ( binding . TF_FLOAT ) ; } ) ; it ( 'handles a list of Tensors' , ( ) => { const tensors = [ tfc . scalar ( 1 , 'int32' ) , tfc . scalar ( 20.1 , 'float32' ) ] ; const result = createTensorsTypeOpAttr ( 'T' , tensors ) ; expect ( result . name ) . toBe ( 'T' ) ; expect ( result . type ) . toBe ( binding . TF_ATTR_TYPE ) ; expect ( result . value ) . toBe ( binding . TF_INT32 ) ; } ) ; it ( 'handles null' , ( ) => { expect ( ( ) => createTensorsTypeOpAttr ( 'T' , null ) ) . toThrowError ( ) ; } ) ; it ( 'handles list of null' , ( ) => { const inputs = [ null , null ] as tfc . Tensor [ ] ; expect ( ( ) => createTensorsTypeOpAttr ( 'T' , inputs ) ) . toThrowError ( ) ; } ) ; } ) ;