tf.prg
- Code: Select all Expand view RUN
- #include "FiveWin.ch"
#define TF_STRING 6
static hDLL
function Main()
local oTF := TensorFlow():New()
local hTensor, hOperationDescription, hOperation
local hTensorOutput := 0
MsgInfo( oTF:Version() )
hTensor = oTF:TensorString( "Hello TensorFlow" )
hOperationDescription = TF_NewOperation( oTF:hGraph, "Const", "hello" )
TF_SetAttrTensor( hOperationDescription, "value", hTensor, oTF:hStatus )
TF_SetAttrType( hOperationDescription, "dtype", TF_TensorType( hTensor ) )
hOperation = TF_FinishOperation( hOperationDescription, oTF:hStatus )
TF_SessionRun( oTF:hSession, 0,;
0, 0, 0,; // Inputs
TF_Output( hOperation, 0 ), @hTensorOutput, 1,; // Outputs
hOperation, 1,; // Operations
0, oTF:hStatus )
MsgInfo( oTF:StatusCode(), "zero means ok" )
MsgInfo( TF_TensorType( hTensorOutput ) )
MsgInfo( L2Str( TF_TensorData( hTensorOutput ) ) )
oTF:End()
return nil
CLASS TensorFlow
DATA hGraph
DATA hOptions
DATA hStatus
DATA hSession
METHOD New()
METHOD Version() INLINE TF_Version()
METHOD ImportGraph( cFileName )
METHOD TensorString( cString )
METHOD End()
METHOD StatusCode() INLINE TF_GetCode( ::hStatus )
METHOD Variable( cVarName, nDataType )
ENDCLASS
METHOD New() CLASS TensorFlow
hDLL = LoadLibrary( "tensorflow.dll" )
::hGraph = TF_NewGraph()
::hOptions = TF_NewSessionOptions()
::hStatus = TF_NewStatus()
::hSession = TF_NewSession( ::hGraph, ::hOptions, ::hStatus )
return Self
METHOD End() CLASS TensorFlow
TF_CloseSession( ::hSession, ::hStatus )
TF_DeleteSession( ::hSession, ::hStatus )
TF_DeleteStatus( ::hStatus )
TF_DeleteSessionOptions( ::hOptions )
FreeLibrary( hDLL )
return nil
METHOD ImportGraph( cFileName ) CLASS TensorFlow
local cGraph := MemoRead( cFileName )
local hGraphDefOptions := TF_NewImportGraphDefOptions()
TF_GraphImportGraphDef( ::hGraph, TF_NewBufferFromString( cGraph, Len( cGraph ) ), hGraphDefOptions, ::hStatus )
if TF_GetCode( ::hStatus ) != 0
MsgAlert( TF_Message( ::hStatus ), "Error importing a Graph file" )
endif
TF_DeleteImportGraphDefOptions( hGraphDefOptions )
return nil
METHOD TensorString( cString ) CLASS TensorFlow
local hTensor := TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( Len( cString ) ) )
Memset( TF_TensorData( hTensor ), 0, 8 )
TF_StringEncode( cString, Len( cString ), 8 + TF_TensorData( hTensor ), TF_StringEncodedSize( Len( cString ) ), ::hStatus )
if TF_GetCode( ::hStatus ) != 0
MsgAlert( TF_Message( ::hStatus ), "Error creating a Tensor string" )
endif
return hTensor
METHOD Variable( cVarName, nDataType ) CLASS TensorFlow
local hOperationDescription := TF_NewOperation( ::hGraph, "Variable", cVarName )
TF_SetAttrType( hOperationDescription, "dtype", nDataType )
return TF_FinishOperation( hOperationDescription, ::hStatus )
DLL FUNCTION TF_Version() AS LPSTR LIB hDLL
DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL
DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL
DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL
DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_NewImportGraphDefOptions() AS LONG LIB hDLL
DLL FUNCTION TF_GraphImportGraphDef( hGraph AS LONG, hBuffer AS LONG, hGraphDefOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_NewBufferFromString( cString AS LPSTR, nLegth AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_GetCode( hStatus AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_DeleteImportGraphDefOptions( hGraphDefOptions AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_NewTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, pData AS LONG, nLength AS LONG, pDeallocator AS LONG,;
pDeallocatorArgs AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_AllocateTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, nLegth AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_StringEncodedSize( nLength AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_TensorData( hTensor AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_StringEncode( cString AS LPSTR, nLength AS LONG, pDest AS LONG, nDestLength AS LONG, hStatus AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_SessionRun( hSession AS LONG, hRunOptions AS LONG, hInputs AS LONG, @hInputValues AS LONG, nInputs AS LONG,;
hOutputs AS LONG, @hOutputValues AS LONG, nOutputs AS LONG, @hTargetOperations AS LONG, nTargets AS LONG,;
hRunMetadata AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_Message( hStatus AS LONG ) AS LPSTR LIB hDLL
DLL FUNCTION TF_NewOperation( hGraph AS LONG, cOperationType AS LPSTR, cOperationName AS LPSTR ) AS LONG LIB hDLL
DLL FUNCTION TF_SetAttrTensor( hOperationDescription AS LONG, cAttributeName AS LPSTR, hTensor AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_TensorType( hTensor AS LONG ) AS LONG LIB hDLL
DLL FUNCTION TF_SetAttrType( hOperationDescription AS LONG, cAttributeName AS LPSTR, nDataType AS LONG ) AS VOID LIB hDLL
DLL FUNCTION TF_FinishOperation( hOperationDescription AS LONG, hStatus AS LONG ) AS LONG LIB hDLL
#pragma BEGINDUMP
#include <hbapi.h>
typedef struct TF_Output {
void * oper;
int index;
} TF_Output;
HB_FUNC( TF_OUTPUT )
{
TF_Output * hOutput = ( TF_Output * ) hb_xgrab( sizeof( TF_Output ) );
hOutput->oper = ( void * ) hb_parnll( 1 );
hOutput->index = hb_parnl( 2 );
hb_retnll( ( HB_LONGLONG ) hOutput );
}
HB_FUNC( L2STR )
{
hb_retc( ( ( char * ) hb_parnll( 1 ) ) + 9 );
}
HB_FUNC( MEMSET )
{
hb_retnll( ( HB_LONGLONG ) memset( ( void * ) hb_parnll( 1 ), hb_parnl( 2 ), hb_parnll( 3 ) ) );
}
#pragma ENDDUMP