Skip to content
Draft
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
84b5731
[circle-resizer] Introduce a new tool to models resizing
mbencer Feb 21, 2025
5f4135b
input_shapes and output_shapes methods
mbencer Feb 21, 2025
4361786
styles applied
mbencer Feb 21, 2025
483a64c
working on tests
mbencer Feb 24, 2025
36df640
introduced model data
mbencer Feb 24, 2025
db6f420
refactor
mbencer Feb 25, 2025
195dad4
initial driver implementation
mbencer Feb 26, 2025
e83a955
initial version of python api
mbencer Mar 3, 2025
dd953aa
first working python api
mbencer Mar 5, 2025
c891819
initial version of setup.py
mbencer Mar 10, 2025
a34d7f5
missing copyright
mbencer Mar 10, 2025
28094bd
removed pybind11::module from linked libraries
mbencer Mar 10, 2025
472b854
shape bindings
mbencer Mar 11, 2025
79d9886
initial refactor
mbencer Mar 11, 2025
a0aa5d1
removed not used files
mbencer Mar 12, 2025
4d44e59
extend python api
mbencer Mar 13, 2025
8fea4f2
revert debug change
mbencer Mar 13, 2025
c569016
python api refactor
mbencer Mar 14, 2025
13bd9a4
Merge remote-tracking branch 'upstream/master' into mbencer/CircleRes…
mbencer Mar 18, 2025
61ab3fd
shape extraction refactor
mbencer Mar 19, 2025
f0b1b27
integrate circle-resizer with one-cmds
mbencer Apr 2, 2025
64188e2
Merge remote-tracking branch 'upstream/master' into mbencer/CircleRes…
mbencer Apr 2, 2025
4896da5
input_model and output_model names refactor
mbencer Apr 2, 2025
a7afdf2
remove build python file
mbencer Apr 2, 2025
e4af358
debian config
mbencer Apr 2, 2025
4149cda
test for one-resize with shortened flags
mbencer Apr 2, 2025
89d790f
driver refactor
mbencer Apr 3, 2025
a2c5ed6
test cut llama2 fragments
mbencer Apr 3, 2025
2454d0c
Merge remote-tracking branch 'upstream/master' into mbencer/CircleRes…
mbencer Apr 16, 2025
7a6fcda
model data doc + more tests
mbencer Apr 16, 2025
fd387ad
change model data
mbencer Apr 17, 2025
01263bb
styles applied
mbencer Apr 17, 2025
f12471b
add circle-resizer-dredd-recipe-test
mbencer Apr 23, 2025
a020aca
missing change in testall.sh (align to rule-lib.sh names)
mbencer Apr 23, 2025
d469df4
added more tests
mbencer Apr 24, 2025
c9229a5
added model editor doc
mbencer Apr 24, 2025
3d43630
styles applied
mbencer Apr 24, 2025
1e82cc6
Merge remote-tracking branch 'upstream/master' into mbencer/CircleRes…
mbencer Apr 28, 2025
9b0437b
resizer cmd app refactoring
mbencer Apr 28, 2025
b1b9811
readme update
mbencer Apr 28, 2025
5b19f13
Merge remote-tracking branch 'upstream/master' into mbencer/CircleRes…
mbencer May 8, 2025
8a45533
remove previous recipes
mbencer May 8, 2025
82a859a
added new recipes
mbencer May 8, 2025
b3781df
update test.lst
mbencer May 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions compiler/circle-resizer-dredd-recipe-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

nnas_include(TargetRequire)

unset(REQUIRED_TARGETS)
list(APPEND REQUIRED_TARGETS circle-inspect)
list(APPEND REQUIRED_TARGETS circle-verify)
list(APPEND REQUIRED_TARGETS circle_resizer)
list(APPEND REQUIRED_TARGETS dredd_rule_lib)
TargetRequire_Return(${REQUIRED_TARGETS})

unset(TEST_DEPS)
unset(TEST_NAMES)

get_target_property(ARTIFACTS_BIN_PATH testDataGenerator BINARY_DIR)

set(oneValueArgs NEW_INPUTS_SIZES)
set(multiValueArgs "")

macro(Add RECIPE)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

set(NEW_INPUTS_SIZES "")
if(ARG_NEW_INPUTS_SIZES)
set(NEW_INPUTS_SIZES "--input_shapes" "${ARG_NEW_INPUTS_SIZES}")
endif()

set(CIRCLE_PATH "${ARTIFACTS_BIN_PATH}/${RECIPE}.circle")
set(RESIZED_CIRCLE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${RECIPE}.resized.circle")

# Generate resized .circle
add_custom_command(OUTPUT ${RESIZED_CIRCLE_PATH}
COMMAND $<TARGET_FILE:circle_resizer> --input_path ${CIRCLE_PATH} --output_path ${RESIZED_CIRCLE_PATH} ${NEW_INPUTS_SIZES}
DEPENDS
circle_resizer
${CIRCLE_PATH}
COMMENT "Generate ${RECIPE}.resized.circle"
)

list(APPEND TEST_DEPS ${RESIZED_CIRCLE_PATH})
list(APPEND TEST_NAMES ${RECIPE})
endmacro(Add)


# Read "test.lst"
include("test.lst")

##
## Copy testall
##
set(TEST_RUNNER "${CMAKE_CURRENT_BINARY_DIR}/testall.sh")
set(TEST_RUNNER_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/testall.sh")

add_custom_command(
OUTPUT ${TEST_RUNNER}
COMMAND ${CMAKE_COMMAND} -E copy "${TEST_RUNNER_SOURCE}" "${TEST_RUNNER}"
DEPENDS ${TEST_RUNNER_SOURCE}
COMMENT "Generate test runner"
)

list(APPEND TEST_DEPS "${TEST_RUNNER}")

###
### Generate test.config
###
set(TEST_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/test.config")

add_custom_command(
OUTPUT ${TEST_CONFIG}
COMMAND ${CMAKE_COMMAND} -E remove -f ${TEST_CONFIG}
COMMAND ${CMAKE_COMMAND} -E echo 'CIRCLE_INSPECT_PATH=\"$<TARGET_FILE:circle-inspect>\"' >> ${TEST_CONFIG}
COMMAND ${CMAKE_COMMAND} -E echo 'CIRCLE_VERIFY_PATH=\"$<TARGET_FILE:circle-verify>\"' >> ${TEST_CONFIG}
COMMAND ${CMAKE_COMMAND} -E echo 'CIRCLE_RESIZER_PATH=\"$<TARGET_FILE:circle_resizer>\"' >> ${TEST_CONFIG}
DEPENDS
circle-inspect
circle-verify
circle_resizer
COMMENT "Generate test configuration"
)

list(APPEND TEST_DEPS "${TEST_CONFIG}")

#
# copy rule-lib.sh (a library of shell script functions)
#

# getting path for rule-lib.sh in dredd-rule-lib
get_target_property(DREDD_RULE_LIB_DIR dredd_rule_lib BINARY_DIR)

set(RULE_LIB_SOURCE_PATH "${DREDD_RULE_LIB_DIR}/rule-lib.sh")
set(RULE_LIB_BINARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/rule-lib.sh")

add_custom_command(
OUTPUT ${RULE_LIB_BINARY_PATH}
COMMAND ${CMAKE_COMMAND} -E copy "${RULE_LIB_SOURCE_PATH}" "${RULE_LIB_BINARY_PATH}"
DEPENDS ${RULE_LIB_SOURCE_PATH}
COMMENT "Generate rule lib"
)

list(APPEND TEST_DEPS "${RULE_LIB_BINARY_PATH}")

# Generate dependencies
add_custom_target(circle-resizer_dredd_recipe_test ALL DEPENDS ${TEST_DEPS})
add_dependencies(circle-resizer_dredd_recipe_test common_artifacts_deps)

get_target_property(ARTIFACTS_BIN_PATH testDataGenerator BINARY_DIR)

# Run tests
add_test(
NAME resizer_dredd_recipe_test
COMMAND "${TEST_RUNNER}"
"${TEST_CONFIG}"
"${ARTIFACTS_BIN_PATH}"
${TEST_NAMES}
)
31 changes: 31 additions & 0 deletions compiler/circle-resizer-dredd-recipe-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# circle-resizer-dredd-recipe-test

It tests non-functional conditions of a circle model resized by circle-resizer.

## How to add a test?

1. Create a directory under `res/TensorFlowLiteRecipes/` or `res/CircleRecipes/`.

2. Make a recipe (`test.recipe`) for a model under the directory.

3. Make a rule (`test.rule`) you want to test under the directory. Note, that you can find more information about dredd-test-rules in _dredd-rule-lib_ module.

4. Add a test to `test.lst` in this module using `Add` macro.
```
Add(RECIPE_DIR NEW_INPUTS_SIZES)
```
- `NEW_INPUTS_SIZES`: New shapes of Circle model inputs in comma-separated format like `[1,2,3],[4,5]` for a model with 2 inputs.

## Example

```
# TensorFlowLiteRecipes
res/TensorFlowLiteRecipes/PRelu_000
├── test.recipe # What you want to test
└── test.rule # Non-functional conditions to be satisfied

# test.lst
...
Add(PRelu_000 NEW_INPUTS_SIZES [1,4,4,5],[1,1,5])
...
```
5 changes: 5 additions & 0 deletions compiler/circle-resizer-dredd-recipe-test/requires.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("circle-inspect")
require("circle-resizer")
require("circle-verify")
require("common-artifacts")
require("dredd-rule-lib")
19 changes: 19 additions & 0 deletions compiler/circle-resizer-dredd-recipe-test/test.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## EXAMPLE
#
# Add(RECIPE_DIR NEW_INPUTS_SIZES [1,2,3])
#

## TFLITE RECIPE

# two inputs, one output
Add(PRelu_000 NEW_INPUTS_SIZES [1,4,4,5],[1,1,5])
# scalar output
Add(ReduceAny_dynamic_000 NEW_INPUTS_SIZES [4,5,6])
# change rank
Add(GreaterEqual_000 NEW_INPUTS_SIZES [1,2,3],[1,2,3])
# one inputs, two outputs
Add(Split_000 NEW_INPUTS_SIZES [8,1,2])
# bigger graph
Add(Net_FullyConnected_Gelu_000 NEW_INPUTS_SIZES [2,16])
# from dynamic to static
Add(Inf_StridedSlice_002 NEW_INPUTS_SIZES [1,10,10,5])
99 changes: 99 additions & 0 deletions compiler/circle-resizer-dredd-recipe-test/testall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

# Need at least 2 arguments
if [[ $# -lt 2 ]]; then
echo "USAGE: $0 ..."
echo
echo "ARGUMENTS:"
echo " [test.config path]"
echo " [WORKDIR]"
echo " [Prefix1]"
echo " [Prefix2]"
echo " ..."
exit 255
fi

WORKDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CONFIG_PATH="$1"; shift
RESOURCE_DIR="$1"; shift

source "${CONFIG_PATH}"

echo "-- Found circle-resizer: ${CIRCLE_RESIZER_PATH}"
echo "-- Found circle-inspect: ${CIRCLE_INSPECT_PATH}"
echo "-- Found circle-verify: ${CIRCLE_VERIFY_PATH}"
echo "-- Found common-artifacts: ${RESOURCE_DIR}"

TESTED=()
PASSED=()
FAILED=()

pushd ${WORKDIR}
while [[ $# -ne 0 ]]; do
PREFIX="$1"; shift

TESTED+=("${PREFIX}")

PASSED_TAG="${PREFIX}.passed"

rm -f "${PASSED_TAG}"

cat > "${PREFIX}.log" <(
exec 2>&1

echo "-- Found circle: ${PREFIX}.resized.circle"

# Exit immediately if any command fails
set -e
# Show commands
set -x

#
# Check if rule is satisfied
#

# Note: turn off 'command printing'. Otherwise printing will be so messy
set +x

# (RESIZED_FILE, INSPECT_PROG_PATH, VERIFY_PROG_PATH, ERROR_LOG) must be set for rule-lib.sh
RESIZED_FILE="${PREFIX}.resized.circle"
INSPECT_PROG_PATH=${CIRCLE_INSPECT_PATH}
VERIFY_PROG_PATH=${CIRCLE_VERIFY_PATH}
ERROR_LOG="${PREFIX}.error"

rm -f "${ERROR_LOG}"

# in case error while running rule-lib.sh, prints error msg
trap 'echo "** ERROR **" ; cat "${ERROR_LOG}"' ERR

source rule-lib.sh
source "${RESOURCE_DIR}/${PREFIX}.rule"

# unset
trap - ERR
set -x

# At this point, the exit code of all commands is 0
# If not 0, execution of this script ends because of "set -e"
touch "${PASSED_TAG}"
)

if [[ -f "${PASSED_TAG}" ]]; then
PASSED+=("$PREFIX")
else
FAILED+=("$PREFIX")
fi
done
popd

if [[ ${#TESTED[@]} -ne ${#PASSED[@]} ]]; then
echo "FAILED"
for TEST in "${FAILED[@]}"
do
echo "- ${TEST}"
done
exit 255
fi

echo "PASSED"
exit 0
1 change: 1 addition & 0 deletions compiler/circle-resizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(src)
add_subdirectory(app)
add_subdirectory(python)
add_subdirectory(tests)
8 changes: 8 additions & 0 deletions compiler/circle-resizer/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# circle-resizer

_circle-resizer provides capabilities to change the shapes of models inputs.

## How to build Python API
```py
CMAKE_INSTALL_PREFIX=~/workspace/one_install_nncc python setup.py bdist_wheel
```
1 change: 1 addition & 0 deletions compiler/circle-resizer/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_executable(circle_resizer CircleResizer.cpp)
target_link_libraries(circle_resizer PRIVATE circle_resizer_core)
target_link_libraries(circle_resizer PRIVATE arser)
target_link_libraries(circle_resizer PRIVATE safemain)
target_link_libraries(circle_resizer PRIVATE vconone)
Expand Down
60 changes: 59 additions & 1 deletion compiler/circle-resizer/app/CircleResizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
* limitations under the License.
*/

#include "ModelEditor.h"
#include "ShapeParser.h"

#include <arser/arser.h>
#include <vconone/vconone.h>

#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <memory>

using namespace circle_resizer;

namespace
{
Expand All @@ -35,6 +42,24 @@ int entry(const int argc, char **argv)
{
arser::Arser arser("circle-resizer provides capabilities to change inputs of the models");

arser.add_argument("--input_path")
.nargs(1)
.type(arser::DataType::STR)
.required(true)
.help("The path to the input model (.circle)");

arser.add_argument("--output_path")
.nargs(1)
.type(arser::DataType::STR)
.required(true)
.help("The path to the resized model (.circle)");

arser.add_argument("--input_shapes")
.nargs(1)
.type(arser::DataType::STR)
.required(true)
.help("New inputs shapes in in comma separated format. An example for 2 inputs: [1,2],[3,4].");

arser.add_argument("--version")
.nargs(0)
.required(false)
Expand All @@ -45,6 +70,39 @@ int entry(const int argc, char **argv)
try
{
arser.parse(argc, argv);

const auto input_path = arser.get<std::string>("--input_path");

auto circle_model = std::make_shared<CircleModel>(input_path);
ModelEditor resizer(circle_model);
const auto input_shapes = circle_model->input_shapes();
std::cout << "Input shapes before resizing:" << std::endl;
for (size_t in_idx = 0; in_idx < input_shapes.size(); ++in_idx)
{
std::cout << in_idx + 1 << ". " << input_shapes[in_idx] << std::endl;
}

auto output_shapes = circle_model->output_shapes();
std::cout << "Output shapes before resizing:" << std::endl;
for (size_t out_idx = 0; out_idx < output_shapes.size(); ++out_idx)
{
std::cout << out_idx + 1 << ". " << output_shapes[out_idx] << std::endl;
}

const auto output_path = arser.get<std::string>("--output_path");
const auto new_input_shapes_str = arser.get<std::string>("--input_shapes");

resizer.resize_inputs(parse_shapes(new_input_shapes_str));

output_shapes = circle_model->output_shapes();
std::cout << "Output shapes after resizing:" << std::endl;
for (size_t out_idx = 0; out_idx < output_shapes.size(); ++out_idx)
{
std::cout << out_idx + 1 << ". " << output_shapes[out_idx] << std::endl;
}

circle_model->save(output_path);
std::cout << "Resizing complete, the model saved to: " << output_path << std::endl;
}
catch (const std::runtime_error &err)
{
Expand Down
Loading
Loading