Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Commit d9240b1

Browse files
William Yessenfacebook-github-bot
authored andcommitted
Generic model builder and inference engine (#3415)
Summary: Quite a bit to chew on in this PR. The main features are: - Generic model builder (x-model-builder) - Generic inference engine (x-infer) - Documentation for the generic model builder and the generic inference engine Pull Request resolved: #3415 Reviewed By: opti-mix Differential Revision: D17910520 Pulled By: jackm321 fbshipit-source-id: 44501ee29a32cde260069050776eaccd9eb039b7
1 parent ef39067 commit d9240b1

File tree

12 files changed

+2323
-2
lines changed

12 files changed

+2323
-2
lines changed

docs/InferenceEngines.md

Lines changed: 521 additions & 0 deletions
Large diffs are not rendered by default.

docs/XModelRunner.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## XModelRunner
2+
3+
Glow supplies a number of out-of-the-box model builders/runners, which include
4+
the `ImageClassifier` and the `ModelLoader`. The former is tailored towards specific
5+
networks built for image classification, while the latter is a generic model loader (see
6+
the corresponding documentation).
7+
8+
`XModelRunner` is a generic model builder/runner that is able to consume any model --
9+
either in the ONNX format, or as a Caffe2 protobuf -- and
10+
either compile it (producing a bundle if requested, along with profiling when requested),
11+
or run inference on it. `XModelRunner`
12+
is built along with the other models using the standard build instructions (see the
13+
building documentation). Below is a description of the command line options for `XModelRunner`.
14+
15+
### Command Line Options
16+
17+
In addition to all of the "standard" command line options (i.e. those that are common to
18+
all model builders/runners, and can be obtained with the `-help` option), a few
19+
options are supported by `XModelRunner`, some of which are mandatory and some are optional.
20+
21+
#### Required Options
22+
23+
| Option | Expected Values | Description | Required |
24+
| :------ | :------ | :------ | :------ |
25+
|`input-tensor-dims` | Comma-separated list of ints | Input tensor dimensions | Yes |
26+
|`output-tensor-names` | Comma-separated list of strings (no spaces) | Output tensor names (can be more than one) | Yes |
27+
|`write-output` | Boolean flag | Whether to write output to output files (only applicable when not building a bundle) | No (default is `False`) |
28+
29+
#### Expected Input
30+
31+
`XModelRunner` expects either a list of file names containing input (one input tensor per file), which are positional arguments, or a file name containing the list of file names that contain input (one file name per line), specified with the `input-file-list` option. Input specification is required when either profiling the network, or running inference (i.e. not building a bundle). Otherwise, input can be omitted.
32+
33+
#### Produced Output
34+
35+
When input files are specified, output is produced (saved into binary files) only when the `write-output` option is specified. In this case, the input file name acts as a base name for the output file name. The output file name is composed as `[input file name].out.dat`. When input is not specified and bundles are requested, the runner produces bundles in the specified output directory. Also, `write-output` may be omitted when profiling the network.
36+
37+
### Technical Notes
38+
39+
1. The runner currently supports only one (named) input tensor, but can work with multiple (named) output tensors.
40+
2. The runner does not currently support streaming input, but this feature is planned.
41+
42+
### A Note on the Initial Contribution
43+
44+
The initial contribution of this runner (as well as the corresponding documentation) was made as part of the open source contribution initiative by the XPERI Corporation (xperi.com).
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(x-infer)
3+
4+
option(LIB_ONLY "Only build the library" OFF)
5+
option(BUILD_FOR_DYNAMIC_LINKAGE "Whether to build for dynamic or static bundle linkage" ON)
6+
option(LINK_LIBS_STATICALLY "Whether to link in all the required libraries statically" ON)
7+
option(ENABLE_PERF_MONITORING "Whether to enable performance monitoring" OFF)
8+
9+
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
10+
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
11+
12+
if(LINK_LIBS_STATICALLY)
13+
message(WARNING "Static linking may cause issues with dlopen on target")
14+
message("Will statically link executable")
15+
set(TARGET_COMPILE_FLAGS "-static"
16+
)
17+
set(TARGET_LINKER_FLAGS "-static"
18+
)
19+
else()
20+
set(TARGET_COMPILE_FLAGS )
21+
endif()
22+
23+
if (DEFINED TARGET_OPTIONS)
24+
set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${TARGET_OPTIONS}")
25+
endif()
26+
27+
include_directories(${PROJECT_SOURCE_DIR})
28+
29+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${TARGET_COMPILE_FLAGS})
30+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${TARGET_COMPILE_FLAGS})
31+
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${TARGET_LINKER_FLAGS})
32+
33+
if(ENABLE_PERF_MONITORING)
34+
message("Will enable performance monitoring")
35+
message(WARNING "Performance monitoring is support only on Linux")
36+
message(WARNING "If you're running in a VM, performance monitoring will be "
37+
"broken, and will likely break inference.")
38+
add_definitions(-DENABLE_PERF_MONITORING)
39+
endif()
40+
41+
add_library(xinfer STATIC x_inference_lib.c x_perf_monitor.c)
42+
43+
if(LIB_ONLY)
44+
message("Will only build the library")
45+
else()
46+
message("Will build the library and the executable")
47+
if (BUILD_FOR_DYNAMIC_LINKAGE)
48+
message("Will build the executable for dynamic bundle linkage")
49+
set(OBJS )
50+
add_definitions(-DX_USE_DYNAMIC)
51+
else()
52+
if(NOT DEFINED LINKED_BUNDLE)
53+
message(FATAL_ERROR "When building with static bundle linkage, "
54+
"bundle must be specified with "
55+
"-DLINKED_BUNDLE=")
56+
endif()
57+
if (NOT DEFINED LINKED_MODEL_NAME)
58+
message(FATAL_ERROR "When building with static bundle linkage, "
59+
"model name must be specified with "
60+
"-DLINKED_MODEL_NAME=")
61+
endif()
62+
63+
add_definitions(-DX_MODEL_NAME=${LINKED_MODEL_NAME})
64+
set(OBJS ${LINKED_BUNDLE})
65+
set_source_files_properties(${OBJS} PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)
66+
endif()
67+
link_directories(${LIBRARY_OUTPUT_PATH})
68+
add_executable(x-infer main.c ${OBJS})
69+
target_link_libraries(x-infer xinfer dl m)
70+
endif()

0 commit comments

Comments
 (0)