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

[luci] Introduce Compress weights pass #13521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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 @@ -343,6 +345,7 @@ int entry(int argc, char **argv)
option_str_to_enum["decompose_softmax"] = Algorithms::DecomposeSoftmaxPass;
option_str_to_enum["expand_broadcast_const"] = Algorithms::ExpandBroadcastConst;
option_str_to_enum["unroll_unidirseqlstm"] = Algorithms::UnrollUnidirSeqLSTM;
option_str_to_enum["compress_weights_huffman"] = Algorithms::CompressWeightsHuffman;
// clang-format on

if (arser.get<bool>("--verbose"))
Expand Down
15 changes: 15 additions & 0 deletions compiler/luci-interpreter/include/luci_interpreter/core/Tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define LUCI_INTERPRETER_CORE_TENSOR_H

#include "luci_interpreter/core/DataType.h"
#include <luci/IR/AttrWeightCompression.h>

#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -146,6 +147,8 @@ class Tensor

void resize(const Shape &new_shape);

void resize(const Shape &new_shape, size_t raw_size);

void set_data_buffer(uint8_t *buffer)
{
if (buffer == nullptr)
Expand Down Expand Up @@ -173,11 +176,21 @@ class Tensor

void set_offset(int32_t offset) { _offset = offset; }

luci::CompressionType get_compression() const { return _compression; }

void set_compression(luci::CompressionType compression) { _compression = compression; }

size_t get_raw_size(void) const { return _raw_size; }
void set_raw_size(size_t size) { _raw_size = size; }

private:
DataType _element_type;
Shape _shape;
AffineQuantization _quantization;
uint8_t *_data = nullptr;
// Used for compressed/sparsed tensors when size != WxHxLxD
size_t _raw_size{0};

std::string _name;
bool _data_allocated = false;
// Write of tensor is reported to registered Observers only if this tensor is observable
Expand All @@ -190,6 +203,8 @@ class Tensor
// Used by static memory manager.
// Stores the offset from the beginning of the allocated memory buffer.
int32_t _offset = -1;

luci::CompressionType _compression{luci::CompressionType::NONE};
};

} // namespace luci_interpreter
Expand Down
60 changes: 60 additions & 0 deletions compiler/luci-interpreter/pal/linux/HuffmanCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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_INTERPRETER_PAL_HUFFMAN_COMMON_H
#define LUCI_INTERPRETER_PAL_HUFFMAN_COMMON_H

#include <memory>

namespace luci_interpreter_pal
{
namespace huffman
{

// Node of prefix tree
template <typename T> struct Node
{
std::shared_ptr<Node<T>> p_left;
std::shared_ptr<Node<T>> p_right;
T data;
unsigned int freq;
};

// Compare functor for priority queue
template <typename T> struct CompareNodes
{
bool operator()(std::shared_ptr<Node<T>> l, std::shared_ptr<Node<T>> r)
{
return l->freq > r->freq;
}
};

template <typename T>
std::shared_ptr<Node<T>> createNode(T data, unsigned int freq, std::shared_ptr<Node<T>> p_left,
std::shared_ptr<Node<T>> p_right)
{
std::shared_ptr<Node<T>> node = std::make_unique<Node<T>>();
node->data = data;
node->freq = freq;
node->p_left = p_left;
node->p_right = p_right;
return node;
}

} // namespace huffman
} // namespace luci_interpreter_pal

#endif // LUCI_INTERPRETER_PAL_HUFFMAN_COMMON_H
Loading