Skip to content
Permalink
Browse files

Fix AOT bundle CMake build and regression tests (#3027)

Summary:
The AOT bundle examples had multiple makefiles, of which the CMake version did not work properly. This fixes the CMakeLists for the two bundle examples (plus the two extra quantized versions) and deletes the extra Makefile.

We did regression test the mnist bundle using the non-CMake makefile, but not resnet50 - I've modified the CI script to test both bundles using the CMakeList version.

Documentation: updated AOT.md to remove references to the Run*Bundle targets which never worked as described and I have removed.

fixes #3013
Pull Request resolved: #3027

Differential Revision: D15587378

Pulled By: nickgg

fbshipit-source-id: 1ec718d4c1ee4128dbae804dbee51fb37b7efbd7
  • Loading branch information...
nickgg authored and facebook-github-bot committed May 31, 2019
1 parent 8c78b96 commit 1af031c9c040dd4ff843d9b50f83e71f6e605631
@@ -1,3 +1,4 @@
Result: 0
Result: 1
Result: 2
Result: 3
@@ -0,0 +1,3 @@
Result: 285
Result: 207
Result: 340
@@ -75,6 +75,7 @@ elif [[ "$CIRCLE_JOB" == "RELEASE_WITH_EXPENSIVE_TESTS" ]]; then
)
CMAKE_ARGS+=("-DGLOW_MODELS_DIR=$MODELS_DIR")
CMAKE_ARGS+=("-DGLOW_WITH_OPENCL=OFF")
CMAKE_ARGS+=("-DGLOW_WITH_BUNDLES=ON")
CMAKE_ARGS+=("-DCMAKE_BUILD_TYPE=Release")
elif [[ "$CIRCLE_JOB" == "COVERAGE" ]]; then
sudo apt-get install wget
@@ -9,21 +9,41 @@ export GLOW_BUILD_DIR=${GLOW_SRC}/build
export LOADER=${GLOW_BUILD_DIR}/bin/image-classifier
export LSAN_OPTIONS="suppressions=$GLOW_SRC/.circleci/suppressions.txt"
export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer
export IMAGES_DIR=${GLOW_SRC}/tests/images/

# Pass in which tests to run (one of {test, test_unopt}).
run_unit_tests() {
CTEST_PARALLEL_LEVEL=4 ninja "${1}" || ( cat Testing/Temporary/LastTest.log && exit 1 )
}

# Pass one of {YES, NO} for QUANTIZE.
run_and_check_bundle() {
echo "Checking lenet_mnist bundle with QUANTIZE=${1}"
cd "${GLOW_SRC}/examples/bundles/lenet_mnist/"
( QUANTIZE=${1} make &> raw_results.txt ) || ( cat raw_results.txt && exit 1 )
( tail -n72 raw_results.txt | grep -F "Result: " > results.txt ) || ( cat raw_results.txt && exit 1 )
diff results.txt "${GLOW_SRC}/.ci/lenet_mnist_expected_output.txt"
rm results.txt raw_results.txt
echo "Successfully completed checking lenet_mnist bundle with QUANTIZE=${1}"
run_and_check_lenet_mnist_bundle() {
for q in "" "quantized_"
do
cd "${GLOW_BUILD_DIR}/bundles/${q}lenet_mnist/"
rm -f raw_results.txt
for f in ${IMAGES_DIR}/mnist/*
do
# Assume that there is only one file with this format (prepended with Quantized or not)
./*LeNetMnistBundle ${f} | grep "Result: " >> raw_results.txt
done
diff raw_results.txt "${GLOW_SRC}/.ci/lenet_mnist_expected_output.txt"
cd -
done
}

run_and_check_resnet50_bundle() {
for q in "" "quantized_"
do
cd "${GLOW_BUILD_DIR}/bundles/${q}resnet50/"
rm -f raw_results.txt
for f in ${IMAGES_DIR}/imagenet/*
do
# Assume that there is only one file with this format (prepended with Quantized or not)
./*ResNet50Bundle ${f} | grep "Result: " >> raw_results.txt
done
diff raw_results.txt "${GLOW_SRC}/.ci/resnet50_expected_output.txt"
cd -
done
}

# Run unit tests and bundle tests.
@@ -41,8 +61,6 @@ case ${CIRCLE_JOB} in
DEBUG)
run_unit_tests check
run_unit_tests test_unopt
run_and_check_bundle YES
run_and_check_bundle NO
;;

SHARED)
@@ -51,6 +69,8 @@ case ${CIRCLE_JOB} in

RELEASE_WITH_EXPENSIVE_TESTS)
run_unit_tests check_expensive
run_and_check_lenet_mnist_bundle
run_and_check_resnet50_bundle
;;
COVERAGE)
cd "${GLOW_SRC}"
@@ -20,7 +20,7 @@ executables. Example:
$ninja ResNet50Bundle
...
$./bin/resnet50 cat.png
$./resnet50 cat.png
Result: 285
```

@@ -170,13 +170,10 @@ The CMakeLists.txt provides the following targets:
* Once everything is setup, it invokes the compiled network model by calling the
`resnet50` function from the `resnet50.o` object file.
* `ResNet50Bundle`: it links the user-defined `main.o` and auto-generated `resnet50.o` into a standalone executable file called `resnet50`
* `RunResNet50Bundle`: it runs this standalone executable with imagenet images as inputs and outputs the results of the network model execution.
### Quantized network
All of the aforementioned targets have quantized versions in CMakeLists.txt named
`QuantizedResNet50BundleNet`, `QuantizedResNet50Bundle`, `RunQuantizedResNet50Bundle`.
For example, to run the quantized bundle, you just need to execute:
* `ninja RunQuantizedResNet50Bundle`
`QuantizedResNet50BundleNet`, `QuantizedResNet50Bundle`.
This run performs almost the same steps as non-quantized Resnet50 version
except it emits bundle based on the quantization profile:
@@ -1,67 +1,54 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${GLOW_BINARY_DIR}/bundles)
set(LENET_MNIST_BUNDLE_DIR ${GLOW_BINARY_DIR}/examples/bundles/lenet_mnist)
set(LENET_MNIST_GLOW_S3 "http://fb-glow-assets.s3.amazonaws.com/models/lenet_mnist")
set(LENET_MNIST_NET_FILES predict_net.pbtxt predict_net.pb init_net.pb)
set(MODEL_INPUT_NAME "data")
set(IMAGES ${GLOW_SOURCE_DIR}/tests/images/mnist)
set(RUNTIME_OUTPUT_DIRECTORY ${LENET_MNIST_BUNDLE_DIR})

# Run Commands
# ============
# Regular
add_custom_command(
OUTPUT
RunBundleCommand
COMMAND
LeNetMnistBundle ${IMAGES}/*.png
DEPENDS
LeNetMnistBundle
)
add_custom_target(RunLeNetMnistBundle DEPENDS RunBundleCommand LeNetMnistBundle)
# Output directories for regular and quantized outputs
set(BUNDLE_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lenet_mnist)
set(QUANTIZED_BUNDLE_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/quantized_lenet_mnist)

# Quantized
add_custom_command(
OUTPUT
RunQuantizedBundleCommand
COMMAND
QuantizedLeNetMnistBundle ${IMAGES}/*.png
DEPENDS
QuantizedLeNetMnistBundle
add_custom_target(LeNetMnistBundleDir ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${BUNDLE_OUTPUT_DIRECTORY}
COMMAND ${CMAKE_COMMAND} -E make_directory ${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY}
)
add_custom_target(RunQuantizedLeNetMnistBundle DEPENDS RunQuantizedBundleCommand QuantizedLeNetMnistBundle)

# Final Executables
# =================
# Regular
add_executable(LeNetMnistBundle $<TARGET_OBJECTS:LeNetMnistBundleMain>)
target_link_libraries(LeNetMnistBundle ${LENET_MNIST_BUNDLE_DIR}/lenet_mnist.o png)
set_target_properties(LeNetMnistBundle PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BUNDLE_OUTPUT_DIRECTORY})
target_link_libraries(LeNetMnistBundle ${BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o png)
add_dependencies(LeNetMnistBundle LeNetMnistBundleMain LeNetMnistBundleNet)

# Quantized
add_executable(QuantizedLeNetMnistBundle $<TARGET_OBJECTS:LeNetMnistBundleMain>)
target_link_libraries(QuantizedLeNetMnistBundle ${LENET_MNIST_BUNDLE_DIR}/quantized_lenet_mnist.o png)
set_target_properties(QuantizedLeNetMnistBundle PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY})
target_link_libraries(QuantizedLeNetMnistBundle ${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o png)
add_dependencies(QuantizedLeNetMnistBundle LeNetMnistBundleMain QuantizedLeNetMnistBundleNet)

# Glow Bundles
# ============
# Regular Bundle
add_custom_command(
OUTPUT
${LENET_MNIST_BUNDLE_DIR}/lenet_mnist.o
${BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o
COMMAND
image-classifier ${IMAGES}/3_1020.png -g -image-mode=0to1
image-classifier ${IMAGES}/3_1020.png -g -i=0to1
-m=${LENET_MNIST_BUNDLE_DIR}/lenet_mnist -model-input-name=${MODEL_INPUT_NAME}
-cpu -emit-bundle ${LENET_MNIST_BUNDLE_DIR}
-cpu -emit-bundle ${BUNDLE_OUTPUT_DIRECTORY}
DEPENDS
image-classifier
image-classifier LeNetMnistBundleDir
)
add_custom_target(LeNetMnistBundleNet DEPENDS ${LENET_MNIST_BUNDLE_DIR}/lenet_mnist.o LeNetMnistBundleNetFiles)
add_custom_target(LeNetMnistBundleNet DEPENDS ${BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o LeNetMnistBundleNetFiles)

# Quantization Profile
add_custom_command(
OUTPUT
${LENET_MNIST_BUNDLE_DIR}/profile.yml
COMMAND
image-classifier ${IMAGES}/*.png -image-mode=0to1
image-classifier ${IMAGES}/*.png -i=0to1
-dump-profile=${LENET_MNIST_BUNDLE_DIR}/profile.yml
-m=${LENET_MNIST_BUNDLE_DIR}/lenet_mnist -model-input-name=${MODEL_INPUT_NAME}
DEPENDS
@@ -72,15 +59,15 @@ add_custom_target(LeNetMnistBundleQuantizationProfile DEPENDS ${LENET_MNIST_BUND
# Quantized Bundle
add_custom_command(
OUTPUT
${LENET_MNIST_BUNDLE_DIR}/quantized_lenet_mnist.o
${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o
COMMAND
image-classifier ${IMAGES}/3_1020.png -g -image-mode=0to1 -load-profile=profile.yml
image-classifier ${IMAGES}/3_1020.png -g -i=0to1 -load-profile=profile.yml
-m=${LENET_MNIST_BUNDLE_DIR}/lenet_mnist -model-input-name=${MODEL_INPUT_NAME}
-cpu -emit-bundle ${LENET_MNIST_BUNDLE_DIR} && mv ${LENET_MNIST_BUNDLE_DIR}/lenet_mnist.o ${LENET_MNIST_BUNDLE_DIR}/quantized_lenet_mnist.o
-cpu -emit-bundle ${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY}
DEPENDS
image-classifier
image-classifier LeNetMnistBundleDir
)
add_custom_target(QuantizedLeNetMnistBundleNet DEPENDS ${LENET_MNIST_BUNDLE_DIR}/quantized_lenet_mnist.o LeNetMnistBundleQuantizationProfile)
add_custom_target(QuantizedLeNetMnistBundleNet DEPENDS ${QUANTIZED_BUNDLE_OUTPUT_DIRECTORY}/lenet_mnist.o LeNetMnistBundleQuantizationProfile)

# Other
# =====
@@ -100,5 +87,3 @@ foreach(file ${LENET_MNIST_NET_FILES})
)
endforeach()
add_custom_target(LeNetMnistBundleNetFiles DEPENDS ${LENET_MNIST_NET_FILES})

unset(RUNTIME_OUTPUT_DIRECTORY)

This file was deleted.

@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <assert.h>
#include <inttypes.h>
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
@@ -97,6 +98,7 @@ bool readPngImage(const char *filename, std::pair<float, float> range,
(void)bit_depth;
assert(bit_depth == 8 && "Invalid image");
assert(isGray && "Invalid image");
(void)isGray;
bool hasAlpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);

int number_of_passes = png_set_interlace_handling(png_ptr);
@@ -170,6 +172,7 @@ static void loadImagesAndPreprocess(const std::vector<std::string> &filenames,
size_t dims[3];
bool loadSuccess = !readPngImage(filenames[n].c_str(), range, imageT, dims);
assert(loadSuccess && "Error reading input image.");
(void)loadSuccess;

assert((dims[0] == DEFAULT_HEIGHT && dims[1] == DEFAULT_WIDTH) &&
"All images must have the same Height and Width");
@@ -258,13 +261,13 @@ const SymbolTableEntry &getMutableWeightVar(const BundleConfig &config,

/// Allocate an aligned block of memory.
void *alignedAlloc(const BundleConfig &config, size_t size) {
const size_t alignment = 64;
void *ptr;
// Properly align the memory region.
int res = posix_memalign(&ptr, config.alignment, size);
assert(res == 0 && "posix_memalign failed");
assert((size_t)ptr % alignment == 0 && "Wrong alignment");
assert((size_t)ptr % config.alignment == 0 && "Wrong alignment");
memset(ptr, 0, size);
(void)res;
return ptr;
}

@@ -284,7 +287,8 @@ static uint8_t *initConstantWeights(const char *weightsFileName,
uint8_t *baseConstantWeightVarsAddr =
static_cast<uint8_t *>(alignedAlloc(config, fileSize));
printf("Allocated weights of size: %lu\n", fileSize);
printf("Expected weights of size: %lu\n", config.constantWeightVarsMemSize);
printf("Expected weights of size: %" PRIu64 "\n",
config.constantWeightVarsMemSize);
assert(fileSize == config.constantWeightVarsMemSize &&
"Wrong weights file size");
int result = fread(baseConstantWeightVarsAddr, fileSize, 1, weightsFile);
@@ -303,7 +307,7 @@ static uint8_t *initConstantWeights(const char *weightsFileName,
static uint8_t *allocateMutableWeightVars(const BundleConfig &config) {
auto *weights = static_cast<uint8_t *>(
alignedAlloc(config, config.mutableWeightVarsMemSize));
printf("Allocated mutable weight variables of size: %lu\n",
printf("Allocated mutable weight variables of size: %" PRIu64 "\n",
config.mutableWeightVarsMemSize);
return weights;
}

0 comments on commit 1af031c

Please sign in to comment.
You can’t perform that action at this time.