Skip to content

Commit

Permalink
[luci] Introduce Compress weights pass
Browse files Browse the repository at this point in the history
This commit introduces CopressWeightsPass for Conv2D

ONE-DCO-1.0-Signed-off-by: Vyacheslav Bazhenov <[email protected]>
  • Loading branch information
Vyacheslav Bazhenov committed Aug 9, 2024
1 parent 98ed924 commit 3030741
Show file tree
Hide file tree
Showing 48 changed files with 3,006 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile.template
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ prepare_nncc_internal: $(WORKSPACE)
ifeq (,$(findstring android,$(TARGET_OS)))
EXTERNAL_FLATC=$(BUILDTOOL_PATH)/bin/flatc ./nncc configure -DBUILD_GTEST=OFF -DENABLE_TEST=OFF -DEXTERNALS_BUILD_THREADS=$(NPROCS) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-DCMAKE_INSTALL_PREFIX=$(OVERLAY_FOLDER) \
-DBUILD_WHITELIST="luci;foder;pepper-csv2vec;loco;locop;logo;logo-core;mio-circle08;luci-compute;oops;hermes;hermes-std;angkor;pp;pepper-strcast;pepper-str" \
-DBUILD_WHITELIST="luci;foder;pepper-csv2vec;loco;locop;logo;logo-core;mio-circle09;luci-compute;oops;hermes;hermes-std;angkor;pp;pepper-strcast;pepper-str" \
$(OPTIONS_NNCC)
./nncc build -j$(NPROCS)
cmake --install $(NNCC_FOLDER) $(INSTALL_OPTIONS)
Expand Down
8 changes: 4 additions & 4 deletions compiler/circle-inspect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(NOT TARGET mio_circle08)
if(NOT TARGET mio_circle09)
return()
endif(NOT TARGET mio_circle08)
endif(NOT TARGET mio_circle09)

set(DRIVER "driver/Driver.cpp")

Expand All @@ -10,6 +10,6 @@ add_executable(circle-inspect ${DRIVER} ${SOURCES})
target_include_directories(circle-inspect PRIVATE src)
target_link_libraries(circle-inspect arser)
target_link_libraries(circle-inspect foder)
target_link_libraries(circle-inspect mio_circle08)
target_link_libraries(circle-inspect mio_circle08_helper)
target_link_libraries(circle-inspect mio_circle09)
target_link_libraries(circle-inspect mio_circle09_helper)
target_link_libraries(circle-inspect safemain)
2 changes: 1 addition & 1 deletion compiler/circle-inspect/requires.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("arser")
require("foder")
require("mio-circle08")
require("mio-circle09")
require("safemain")
8 changes: 4 additions & 4 deletions compiler/circle-operator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(NOT TARGET mio_circle08)
if(NOT TARGET mio_circle09)
return()
endif(NOT TARGET mio_circle08)
endif(NOT TARGET mio_circle09)

set(DRIVER "driver/Driver.cpp")

Expand All @@ -10,8 +10,8 @@ add_executable(circle-operator ${DRIVER} ${SOURCES})
target_include_directories(circle-operator PRIVATE src)
target_link_libraries(circle-operator arser)
target_link_libraries(circle-operator foder)
target_link_libraries(circle-operator mio_circle08)
target_link_libraries(circle-operator mio_circle08_helper)
target_link_libraries(circle-operator mio_circle09)
target_link_libraries(circle-operator mio_circle09_helper)
target_link_libraries(circle-operator safemain)

install(TARGETS circle-operator DESTINATION bin)
2 changes: 1 addition & 1 deletion compiler/circle-operator/requires.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("arser")
require("foder")
require("mio-circle08")
require("mio-circle09")
require("safemain")
8 changes: 4 additions & 4 deletions compiler/circle-tensordump/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(NOT TARGET mio_circle08)
if(NOT TARGET mio_circle09)
return()
endif(NOT TARGET mio_circle08)
endif(NOT TARGET mio_circle09)

nnas_find_package(HDF5 COMPONENTS STATIC QUIET)

Expand All @@ -19,8 +19,8 @@ target_include_directories(circle-tensordump PRIVATE ${HDF5_INCLUDE_DIRS})
target_link_libraries(circle-tensordump PRIVATE ${HDF5_CXX_LIBRARIES})
target_link_libraries(circle-tensordump PRIVATE arser)
target_link_libraries(circle-tensordump PRIVATE foder)
target_link_libraries(circle-tensordump PRIVATE mio_circle08)
target_link_libraries(circle-tensordump PRIVATE mio_circle08_helper)
target_link_libraries(circle-tensordump PRIVATE mio_circle09)
target_link_libraries(circle-tensordump PRIVATE mio_circle09_helper)
target_link_libraries(circle-tensordump PRIVATE safemain)

install(TARGETS circle-tensordump DESTINATION bin)
2 changes: 1 addition & 1 deletion compiler/circle-tensordump/requires.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("arser")
require("foder")
require("mio-circle08")
require("mio-circle09")
require("safemain")
4 changes: 4 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ int entry(int argc, char **argv)
"This will convert single input Transpose to Reshape");
add_switch(arser, "--expand_broadcast_const", "This will expand broadcastable constant inputs");
add_switch(arser, "--unroll_unidirseqlstm", "Unroll UnidirectionalSequenceLSTM operator.");
add_switch(arser, "--compress_weights_huffman",
"Loseless weights compression with Huffman encoding.");
add_switch(arser, "--convert_nchw_to_nhwc",
"Experimental: This will convert NCHW operators to NHWC under the assumption that "
"input model is NCHW.");
Expand Down Expand Up @@ -405,6 +407,8 @@ int entry(int argc, char **argv)
options->enable(Algorithms::ExpandBroadcastConst);
if (arser.get<bool>("--unroll_unidirseqlstm"))
options->enable(Algorithms::UnrollUnidirSeqLSTM);
if (arser.get<bool>("--compress_weights_huffman"))
options->enable(Algorithms::CompressWeightsHuffman);

// NOTE Experimental options; these will be removed someday
// Add experimental options here
Expand Down
4 changes: 2 additions & 2 deletions compiler/luci/export/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ target_include_directories(luci_export PUBLIC include)
target_link_libraries(luci_export PRIVATE luci_lang)
target_link_libraries(luci_export PRIVATE luci_service)
target_link_libraries(luci_export PRIVATE luci_pass)
target_link_libraries(luci_export PRIVATE mio_circle08)
target_link_libraries(luci_export PRIVATE mio_circle09)
target_link_libraries(luci_export PRIVATE luci_env)
target_link_libraries(luci_export PRIVATE luci_log)
target_link_libraries(luci_export PRIVATE luci_logex)
Expand All @@ -36,6 +36,6 @@ target_include_directories(luci_export_test PRIVATE src)
target_link_libraries(luci_export_test luci_export)
target_link_libraries(luci_export_test luci_plan)
target_link_libraries(luci_export_test luci_lang)
target_link_libraries(luci_export_test mio_circle08)
target_link_libraries(luci_export_test mio_circle09)
target_link_libraries(luci_export_test luci_env)
target_link_libraries(luci_export_test oops)
8 changes: 4 additions & 4 deletions compiler/luci/export/src/CircleBuiltinTypesExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ class BuiltinOptionsExtractor final
// flatbuffers::Offset<void> visit(luci::CircleConst *)
flatbuffers::Offset<void> visit(luci::CircleConv2D *node)
{
return circle::CreateConv2DOptions(_builder, getOpPadding(node->padding()), node->stride()->w(),
node->stride()->h(),
to_circle_actfunc(node->fusedActivationFunction()),
node->dilation()->w(), node->dilation()->h())
return circle::CreateConv2DOptions(
_builder, getOpPadding(node->padding()), node->stride()->w(), node->stride()->h(),
to_circle_actfunc(node->fusedActivationFunction()), node->dilation()->w(),
node->dilation()->h(), to_circle_weightcompressiontype(node->weightCompression()))
.Union();
}
flatbuffers::Offset<void> visit(luci::CircleCos *)
Expand Down
20 changes: 20 additions & 0 deletions compiler/luci/export/src/CircleExporterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
namespace luci
{

circle::WeightCompressionType to_circle_weightcompressiontype(luci::WeightCompression type)
{
switch (type)
{
case luci::WeightCompression::NONE:
return circle::WeightCompressionType_NONE;
case luci::WeightCompression::HUFFMAN:
return circle::WeightCompressionType_HUFFMAN;
case luci::WeightCompression::RLE:
return circle::WeightCompressionType_RLE;
case luci::WeightCompression::HUFFMAN_RLE:
return circle::WeightCompressionType_HUFFMAN_RLE;
case luci::WeightCompression::RLE_HUFFMAN:
return circle::WeightCompressionType_RLE_HUFFMAN;
default:
INTERNAL_EXN_V("trying to convert unsupported luci::WeightCompression",
oops::to_uint32(type));
}
}

circle::ActivationFunctionType to_circle_actfunc(luci::FusedActFunc func)
{
switch (func)
Expand Down
1 change: 1 addition & 0 deletions compiler/luci/export/src/CircleExporterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace luci
{

circle::WeightCompressionType to_circle_weightcompressiontype(luci::WeightCompression type);
circle::ActivationFunctionType to_circle_actfunc(luci::FusedActFunc func);
circle::TensorType to_circle_tensortype(loco::DataType type);
circle::MirrorPadMode to_circle_mirrorpadmode(luci::MirrorPadMode mode);
Expand Down
4 changes: 4 additions & 0 deletions compiler/luci/export/src/CircleTensorExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ bool has_same_values(luci::CircleConst *lhs, luci::CircleConst *rhs)
return has_same_elements<loco::DataType::S4>(lhs, rhs);

case loco::DataType::S8:
if (lhs->size<loco::DataType::S8>() != rhs->size<loco::DataType::S8>())
return false;
return has_same_elements<loco::DataType::S8>(lhs, rhs);

case loco::DataType::S16:
Expand All @@ -574,6 +576,8 @@ bool has_same_values(luci::CircleConst *lhs, luci::CircleConst *rhs)
return has_same_elements<loco::DataType::U4>(lhs, rhs);

case loco::DataType::U8:
if (lhs->size<loco::DataType::U8>() != rhs->size<loco::DataType::U8>())
return false;
return has_same_elements<loco::DataType::U8>(lhs, rhs);

case loco::DataType::BOOL:
Expand Down
4 changes: 2 additions & 2 deletions compiler/luci/import/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ target_include_directories(luci_import PUBLIC include)
target_link_libraries(luci_import PUBLIC luci_lang)
target_link_libraries(luci_import PUBLIC luci_profile)
target_link_libraries(luci_import PUBLIC luci_plan)
target_link_libraries(luci_import PUBLIC mio_circle08)
target_link_libraries(luci_import PUBLIC mio_circle09)
target_link_libraries(luci_import PRIVATE luci_env)
target_link_libraries(luci_import PRIVATE luci_log)
target_link_libraries(luci_import PRIVATE luci_logex)
target_link_libraries(luci_import PRIVATE nncc_common)
target_link_libraries(luci_import PRIVATE locop)
target_link_libraries(luci_import PRIVATE foder)
target_link_libraries(luci_import PRIVATE oops)
target_link_libraries(luci_import PRIVATE mio_circle08_helper)
target_link_libraries(luci_import PRIVATE mio_circle09_helper)
install(TARGETS luci_import DESTINATION lib)
install(DIRECTORY include/ DESTINATION include
FILES_MATCHING PATTERN "*.h")
Expand Down
32 changes: 32 additions & 0 deletions compiler/luci/import/include/luci/Import/CircleImporterUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 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_IMPORTER_UTILS_H__
#define __CIRCLE_IMPORTER_UTILS_H__

#include <luci/IR/CircleNodes.h>

#include <loco.h>

#include <mio/circle/schema_generated.h>

namespace luci
{
luci::WeightCompression from_circle_weightcompressiontype(circle::WeightCompressionType type);

} // namespace luci

#endif // __CIRCLE_IMPORTER_UTILS_H__
42 changes: 42 additions & 0 deletions compiler/luci/import/src/CircleImporterUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 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.
*/

#include "luci/Import/CircleImporterUtils.h"

#include <oops/InternalExn.h>

namespace luci
{
luci::WeightCompression from_circle_weightcompressiontype(circle::WeightCompressionType type)
{
switch (type)
{
case circle::WeightCompressionType_NONE:
return luci::WeightCompression::NONE;
case circle::WeightCompressionType_HUFFMAN:
return luci::WeightCompression::HUFFMAN;
case circle::WeightCompressionType_RLE:
return luci::WeightCompression::RLE;
case circle::WeightCompressionType_HUFFMAN_RLE:
return luci::WeightCompression::HUFFMAN_RLE;
case circle::WeightCompressionType_RLE_HUFFMAN:
return luci::WeightCompression::RLE_HUFFMAN;
default:
INTERNAL_EXN_V("trying to convert unsupported luci::WeightCompression",
oops::to_uint32(type));
}
}
} // namespace luci
3 changes: 2 additions & 1 deletion compiler/luci/import/src/Nodes/CircleConv2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

#include "luci/Import/CircleImporterUtils.h"
#include "luci/Import/Nodes/CircleConv2D.h"

#include <luci/IR/Nodes/CircleConv2D.h>

#include <loco.h>

#include <cassert>
Expand Down Expand Up @@ -49,6 +49,7 @@ CircleNode *CircleConv2DGraphBuilder::build_node(const circle::OperatorT &op,
node->fusedActivationFunction(luci_actfunc(options->fused_activation_function));
node->dilation()->w(options->dilation_w_factor);
node->dilation()->h(options->dilation_h_factor);
node->weightCompression(from_circle_weightcompressiontype(options->weight_compression_type));

return node;
}
Expand Down
36 changes: 36 additions & 0 deletions compiler/luci/lang/include/luci/IR/AttrWeightCompression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 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 __LUCI_IR_ATTRWEIGHTCOMPRESSION_H__
#define __LUCI_IR_ATTRWEIGHTCOMPRESSION_H__

namespace luci
{

enum class WeightCompression
{
UNDEFINED, // This is not defined by TFLite or Circle. This was added to
// prevent programming error.
NONE,
HUFFMAN,
RLE,
HUFFMAN_RLE,
RLE_HUFFMAN
};

} // namespace luci

#endif // __LUCI_IR_ATTRWEIGHTCOMPRESSION_H__
20 changes: 19 additions & 1 deletion compiler/luci/lang/include/luci/IR/CircleNodeMixins.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define __LUCI_IR_CIRCLE_NODE_MIXINS_H__

#include "luci/IR/AttrFusedActFunc.h"
#include "luci/IR/AttrWeightCompression.h"

#include <loco/IR/Node.h>
#include <loco/IR/NodeMixins.h>
Expand All @@ -31,11 +32,28 @@ namespace luci
enum class CircleNodeTrait
{
FusedActFunc,
Bias
Bias,
WeightCompression
};

template <CircleNodeTrait T> class CircleNodeMixin;

template <> class CircleNodeMixin<CircleNodeTrait::WeightCompression>
{
public:
CircleNodeMixin() = default;

public:
WeightCompression weightCompression() const { return _weight_compression; }
void weightCompression(WeightCompression weight_compression)
{
_weight_compression = weight_compression;
}

private:
WeightCompression _weight_compression = WeightCompression::UNDEFINED;
};

template <> class CircleNodeMixin<CircleNodeTrait::FusedActFunc>
{
public:
Expand Down
Loading

0 comments on commit 3030741

Please sign in to comment.