Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[circle-resizer] Introduce a new tool to models resizing #14727

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 45 additions & 0 deletions compiler/circle-resizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
list(APPEND CIRCLE_RESIZER_SOURCES src/Shape.cpp)
list(APPEND CIRCLE_RESIZER_SOURCES src/ShapeParser.cpp)
list(APPEND CIRCLE_RESIZER_SOURCES src/ModelData.cpp)
list(APPEND CIRCLE_RESIZER_SOURCES src/CircleResizer.cpp)

add_library(circle_resizer_core STATIC "${CIRCLE_RESIZER_SOURCES}")

target_include_directories(circle_resizer_core PUBLIC include)

target_link_libraries(circle_resizer_core PRIVATE mio_circle08)
target_link_libraries(circle_resizer_core PRIVATE logo_core)
target_link_libraries(circle_resizer_core PRIVATE luci_pass)
target_link_libraries(circle_resizer_core PRIVATE luci_lang)
target_link_libraries(circle_resizer_core PRIVATE luci_export)
target_link_libraries(circle_resizer_core PRIVATE luci_import)
target_link_libraries(circle_resizer_core PRIVATE logo)
target_link_libraries(circle_resizer_core PRIVATE luci_log)

add_executable(circle_resizer src/Driver.cpp)
target_link_libraries(circle_resizer PRIVATE circle_resizer_core)
target_link_libraries(circle_resizer PRIVATE arser)
target_link_libraries(circle_resizer PRIVATE safemain)

set_target_properties(circle_resizer PROPERTIES
OUTPUT_NAME "circle-resizer"
)

install(TARGETS circle_resizer DESTINATION bin)

if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

list(APPEND CIRCLE_RESIZER_TEST_SOURCES tests/ShapeParser.test.cpp)
list(APPEND CIRCLE_RESIZER_TEST_SOURCES tests/CircleResizer.test.cpp)

nnas_find_package(GTest REQUIRED)
GTest_AddTest(circle_resizer_unit_test ${CIRCLE_RESIZER_TEST_SOURCES})
target_link_libraries(circle_resizer_unit_test circle_resizer_core)
target_link_libraries(circle_resizer_unit_test oops)

get_target_property(ARTIFACTS_PATH testDataGenerator BINARY_DIR)
set_tests_properties(circle_resizer_unit_test
PROPERTIES
ENVIRONMENT "ARTIFACTS_PATH=${ARTIFACTS_PATH}")
49 changes: 49 additions & 0 deletions compiler/circle-resizer/include/CircleResizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __CIRCLE_RESIZER_H__
#define __CIRCLE_RESIZER_H__

#include "Shape.h"
#include "ModelData.h"

#include <string>
#include <vector>

namespace circle_resizer
{
class CircleResizer
{
public:
explicit CircleResizer(const std::vector<uint8_t> &model_buffer);
explicit CircleResizer(const std::string &model_path);

public:
void resize_model(const std::vector<Shape> &shapes);
void save_model(std::ostream &stream);
void save_model(const std::string &output_path);

public:
// cannot be const because of loco::Graph limitations
std::vector<Shape> input_shapes();
std::vector<Shape> output_shapes();

private:
ModelData _model_data;
};
} // namespace circle_resizer

#endif // __CIRCLE_RESIZER_H__
54 changes: 54 additions & 0 deletions compiler/circle-resizer/include/ModelData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __CIRCLE_RESIZER_MODEL_H__
#define __CIRCLE_RESIZER_MODEL_H__

#include <string>
#include <memory>
#include <vector>

namespace luci
{
class Module;
}

namespace circle_resizer
{
// DESIGN NOTE: The purpose of the class is to keep buffer and module synchronized
class ModelData
{

public:
explicit ModelData(const std::vector<uint8_t> &buffer);
void invalidate_module();
void invalidate_buffer();
// to satisfy forward declaration + unique_ptr
~ModelData();

public:
std::vector<uint8_t> &buffer();
luci::Module *module();

private:
bool _module_invalidated = false, _buffer_invalidated = false;
std::vector<uint8_t> _buffer;
std::unique_ptr<luci::Module> _module;
};

} // namespace circle_resizer

#endif // __CIRCLE_RESIZER_H__
44 changes: 44 additions & 0 deletions compiler/circle-resizer/include/Shape.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __CIRCLE_RESIZER_SHAPE_H__
#define __CIRCLE_RESIZER_SHAPE_H__

#include <stdexcept>
#include <stdint.h>
#include <vector>

namespace circle_resizer
{
class Dim
{
public:
explicit Dim(int32_t dim);

public:
bool is_dynamic();
int32_t value() const;
bool operator==(const Dim &rhs) const;

private:
// Note that in the future, we might need to support dimension with lower and upper bounds
int32_t _dim_value;
};

using Shape = std::vector<Dim>;
} // namespace circle_resizer

#endif // __CIRCLE_RESIZER_SHAPE_H__
30 changes: 30 additions & 0 deletions compiler/circle-resizer/include/ShapeParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __CIRCLE_RESIZER_SHAPE_PARSER_H__
#define __CIRCLE_RESIZER_SHAPE_PARSER_H__

#include "Shape.h"

#include <string>
#include <vector>

namespace circle_resizer
{
std::vector<Shape> parse_shapes(std::string shapes_str);
} // namespace circle_resizer

#endif // __CIRCLE_RESIZER_SHAPE_PARSER_H__
7 changes: 7 additions & 0 deletions compiler/circle-resizer/requires.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("arser")
require("safemain")
require("mio-circle08")
require("logo-core")
require("luci")
require("logo")
require("common-artifacts")
Loading
Loading