2021-06-15 02:30:10 +08:00
function ( tf_get_absolute_path path base final_path )
if ( IS_ABSOLUTE ${ path } )
set ( ${ final_path } ${ path } PARENT_SCOPE )
2021-02-12 14:17:59 +08:00
else ( )
2021-06-15 02:30:10 +08:00
set ( ${ final_path } ${ base } / ${ path } PARENT_SCOPE )
2021-02-12 14:17:59 +08:00
endif ( )
endfunction ( )
2021-06-15 02:30:10 +08:00
function ( tf_get_model model final_path )
string ( FIND ${ model } "http:" pos_http )
string ( FIND ${ model } "https:" pos_https )
if ( ${ pos_http } EQUAL 0 OR ${ pos_https } EQUAL 0 )
message ( "Downloading model " ${ model } )
string ( FIND ${ model } "/" fname_start REVERSE )
math ( EXPR fname_start "${fname_start}+1" )
string ( SUBSTRING ${ model } ${ fname_start } +1 -1 fname )
message ( "Model archive: " ${ fname } )
file ( DOWNLOAD ${ model } ${ CMAKE_CURRENT_BINARY_DIR } / ${ fname } )
file ( ARCHIVE_EXTRACT INPUT
$ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { f n a m e }
D E S T I N A T I O N $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { f n a m e } _ m o d e l )
set ( ${ final_path } ${ CMAKE_CURRENT_BINARY_DIR } / ${ fname } _model/model PARENT_SCOPE )
else ( )
tf_get_absolute_path ( ${ model } ${ CMAKE_CURRENT_BINARY_DIR } model_path )
set ( ${ final_path } ${ model_path } PARENT_SCOPE )
endif ( )
endfunction ( )
# Generate a mock model for tests.
2021-12-23 05:17:10 +08:00
function ( generate_mock_model generator output )
tf_get_absolute_path ( ${ generator } ${ CMAKE_CURRENT_SOURCE_DIR } generator_absolute_path )
tf_get_absolute_path ( ${ output } ${ CMAKE_CURRENT_BINARY_DIR } output_absolute_path )
2021-06-15 02:30:10 +08:00
message ( WARNING "Autogenerated mock models should not be used in production builds." )
execute_process ( COMMAND python3
2021-12-23 05:17:10 +08:00
$ { g e n e r a t o r _ a b s o l u t e _ p a t h }
$ { o u t p u t _ a b s o l u t e _ p a t h }
2021-06-15 02:30:10 +08:00
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R }
)
endfunction ( )
2021-04-06 07:15:36 +08:00
# Run the tensorflow compiler (saved_model_cli) on the saved model in the
2020-06-10 05:50:50 +08:00
# ${model} directory, looking for the ${tag_set} tag set, and the SignatureDef
# ${signature_def_key}.
2021-04-06 07:15:36 +08:00
# Produce a pair of files called ${fname}.h and ${fname}.o in the
2020-06-10 05:50:50 +08:00
# ${CMAKE_CURRENT_BINARY_DIR}. The generated header will define a C++ class
# called ${cpp_class} - which may be a namespace-qualified class name.
function ( tfcompile model tag_set signature_def_key fname cpp_class )
set ( prefix ${ CMAKE_CURRENT_BINARY_DIR } / ${ fname } )
set ( obj_file ${ prefix } .o )
set ( hdr_file ${ prefix } .h )
2021-04-06 07:15:36 +08:00
string ( TOUPPER ${ fname } fname_allcaps )
set ( override_header ${ LLVM_OVERRIDE_MODEL_HEADER_${fname_allcaps } } )
set ( override_object ${ LLVM_OVERRIDE_MODEL_OBJECT_${fname_allcaps } } )
if ( EXISTS "${override_header}" AND EXISTS "${override_object}" )
configure_file ( ${ override_header } ${ hdr_file } COPYONLY )
configure_file ( ${ override_object } ${ obj_file } COPYONLY )
message ( "Using provided header "
$ { h d r _ f i l e } " a n d o b j e c t " $ { o b j _ f i l e }
" f i l e s f o r m o d e l " $ { m o d e l } )
else ( )
2021-06-15 02:30:10 +08:00
tf_get_absolute_path ( ${ model } ${ CMAKE_CURRENT_BINARY_DIR } LLVM_ML_MODELS_ABSOLUTE )
2021-04-06 07:15:36 +08:00
message ( "Using model at " ${ LLVM_ML_MODELS_ABSOLUTE } )
add_custom_command ( OUTPUT ${ obj_file } ${ hdr_file }
2021-06-15 07:21:39 +08:00
C O M M A N D $ { T E N S O R F L O W _ A O T _ C O M P I L E R } a o t _ c o m p i l e _ c p u
- - m u l t i t h r e a d i n g f a l s e
2021-04-06 07:15:36 +08:00
- - d i r $ { L L V M _ M L _ M O D E L S _ A B S O L U T E }
- - t a g _ s e t $ { t a g _ s e t }
- - s i g n a t u r e _ d e f _ k e y $ { s i g n a t u r e _ d e f _ k e y }
- - o u t p u t _ p r e f i x $ { p r e f i x }
- - c p p _ c l a s s $ { c p p _ c l a s s }
- - t a r g e t _ t r i p l e $ { L L V M _ H O S T _ T R I P L E }
)
endif ( )
2020-06-10 05:50:50 +08:00
# Aggregate the objects so that results of different tfcompile calls may be
# grouped into one target.
set ( GENERATED_OBJS ${ GENERATED_OBJS } ${ obj_file } PARENT_SCOPE )
set_source_files_properties ( ${ obj_file } PROPERTIES
G E N E R A T E D 1 E X T E R N A L _ O B J E C T 1 )
set ( GENERATED_HEADERS ${ GENERATED_HEADERS } ${ hdr_file } PARENT_SCOPE )
set_source_files_properties ( ${ hdr_file } PROPERTIES
G E N E R A T E D 1 )
2021-06-15 02:30:10 +08:00
endfunction ( )
2021-12-23 05:17:10 +08:00
function ( tf_find_and_compile model default_url default_path test_model_generator tag_set signature_def_key fname cpp_class )
2021-06-15 02:30:10 +08:00
if ( "${model}" STREQUAL "download" )
2021-06-23 13:22:18 +08:00
# Crash if the user wants to download a model but a URL is set to "TO_BE_UPDATED"
2021-12-23 04:54:37 +08:00
if ( "${default_url}" STREQUAL "TO_BE_UPDATED" )
message ( FATAL_ERROR "Default URL was set to 'download' but there is no model url currently specified in cmake - likely, the model interface recently changed, and so there is not a released model available." )
2021-06-23 13:22:18 +08:00
endif ( )
2021-06-15 02:30:10 +08:00
set ( model ${ default_url } )
endif ( )
if ( "${model}" STREQUAL "autogenerate" )
2021-12-23 05:17:10 +08:00
set ( model ${ default_path } -autogenerated )
generate_mock_model ( ${ test_model_generator } ${ model } )
2021-06-15 02:30:10 +08:00
endif ( )
tf_get_model ( ${ model } LLVM_ML_MODELS_ABSOLUTE )
tfcompile ( ${ LLVM_ML_MODELS_ABSOLUTE } ${ tag_set } ${ signature_def_key } ${ fname } ${ cpp_class } )
set ( GENERATED_OBJS ${ GENERATED_OBJS } ${ obj_file } PARENT_SCOPE )
set_source_files_properties ( ${ obj_file } PROPERTIES
G E N E R A T E D 1 E X T E R N A L _ O B J E C T 1 )
set ( GENERATED_HEADERS ${ GENERATED_HEADERS } ${ hdr_file } PARENT_SCOPE )
set_source_files_properties ( ${ hdr_file } PROPERTIES
G E N E R A T E D 1 )
set ( GeneratedMLSources ${ GeneratedMLSources } ${ GENERATED_HEADERS } PARENT_SCOPE )
set ( MLDeps ${ MLDeps } tf_xla_runtime PARENT_SCOPE )
set ( MLLinkDeps ${ MLLinkDeps } tf_xla_runtime ${ GENERATED_OBJS } PARENT_SCOPE )
2020-06-10 05:50:50 +08:00
endfunction ( )