sw:vec_iree: Fix model_input CMake rule Split the gen_mlmodel_input script into two parts: Download URL and generate the c-embedded library. In this way, we can add the proper output file dependency of the CMake rule, so --target clean can clean the generated files, and any update of the source path can trigger the rebuild. Change-Id: I96b89e99f7b722a56be4dea7151c4b8fd495bd7a
diff --git a/build_tools/download_url.py b/build_tools/download_url.py new file mode 100755 index 0000000..e8aa650 --- /dev/null +++ b/build_tools/download_url.py
@@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +"""Download file from URL.""" +import argparse +import urllib.request + +args = parser = argparse.ArgumentParser( + description='Download files from URL.') +parser.add_argument('--u', dest='img_url', required=True, + help='Input image URL') +parser.add_argument('--o', dest='output_file', + help='Output binary name', required=True) +args = parser.parse_args() + +if __name__ == '__main__': + urllib.request.urlretrieve(args.img_url, args.output_file)
diff --git a/build_tools/gen_mlmodel_input.py b/build_tools/gen_mlmodel_input.py index 5bd8918..5a910a1 100755 --- a/build_tools/gen_mlmodel_input.py +++ b/build_tools/gen_mlmodel_input.py
@@ -1,8 +1,7 @@ #!/usr/bin/env python3 -"""Generate ML model inputs from external images.""" +"""Generate ML model inputs from images.""" import argparse import os -import sys import struct import urllib.request @@ -20,7 +19,6 @@ help='Model input shape (example: "1, 224, 224, 3")', required=True) parser.add_argument('--q', dest='is_quant', action='store_true', help='Indicate it is quant model (default: False)') -parser.add_argument('--u', dest='img_url', help='Input image URL') args = parser.parse_args() @@ -33,9 +31,9 @@ file.write(struct.pack("<f", d)) -def gen_mlmodel_input(input_name, output_file, input_shape, is_quant, img_url): +def gen_mlmodel_input(input_name, output_file, input_shape, is_quant): if not os.path.exists(input_name): - urllib.request.urlretrieve(img_url, input_name) + raise RuntimeError("Input file %s doesn't exist" % {input_name}) if len(input_shape) < 3: raise ValueError("Input shape < 3 dimensions") resized_img = Image.open(input_name).resize( @@ -49,7 +47,5 @@ if __name__ == '__main__': # convert input shape to a list input_shape = [int(x) for x in args.input_shape.split(',')] - # remove whitespace in image URL if any - img_url = args.img_url.replace(' ', '') gen_mlmodel_input(args.input_name, args.output_file, - input_shape, args.is_quant, img_url) + input_shape, args.is_quant)
diff --git a/cmake/iree_model_input.cmake b/cmake/iree_model_input.cmake index 9d66348..bb28d37 100644 --- a/cmake/iree_model_input.cmake +++ b/cmake/iree_model_input.cmake
@@ -32,14 +32,32 @@ ${ARGN} ) - get_filename_component(EXT_STR "${_RULE_SRC}" LAST_EXT) + string(REGEX REPLACE "[ \t\r\n]" "" _RULE_SRC_TRIM ${_RULE_SRC}) + string(REGEX MATCH "^https:" _RULE_SRC_URL ${_RULE_SRC_TRIM}) + if (_RULE_SRC_URL) + get_filename_component(_INPUT_FILENAME "${_RULE_SRC}" NAME) + set(_ARGS) + set(_DOWNLOAD_SCRIPT "${CMAKE_SOURCE_DIR}/build_tools/download_url.py") + list(APPEND _ARGS "--u=${_RULE_SRC_TRIM}") + list(APPEND _ARGS "--o=${_INPUT_FILENAME}") + add_custom_command( + OUTPUT + ${_INPUT_FILENAME} + COMMAND + ${_DOWNLOAD_SCRIPT} ${_ARGS} + DEPENDS + ${_DOWNLOAD_SCRIPT} + ) + else() + set(_INPUT_FILENAME ${_RULE_SRC_TRIM}) + endif() + set(_GEN_INPUT_SCRIPT "${CMAKE_SOURCE_DIR}/build_tools/gen_mlmodel_input.py") set(_OUTPUT_BINARY ${_RULE_NAME}.bin) set(_ARGS) - list(APPEND _ARGS "--i=${_RULE_NAME}${EXT_STR}") + list(APPEND _ARGS "--i=${_INPUT_FILENAME}") list(APPEND _ARGS "--o=${_OUTPUT_BINARY}") list(APPEND _ARGS "--s=${_RULE_SHAPE}") - list(APPEND _ARGS "--u=${_RULE_SRC}") if(_RULE_QUANT) list(APPEND _ARGS "--q") endif() @@ -49,6 +67,9 @@ ${_OUTPUT_BINARY} COMMAND ${_GEN_INPUT_SCRIPT} ${_ARGS} + DEPENDS + ${_GEN_INPUT_SCRIPT} + ${_INPUT_FILENAME} ) iree_c_embed_data(