-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[luci] Introduce Compress weights pass
This commit introduces CopressWeightsPass for Conv2D ONE-DCO-1.0-Signed-off-by: Vyacheslav Bazhenov <[email protected]>
- Loading branch information
Vyacheslav Bazhenov
committed
Jul 31, 2024
1 parent
98ed924
commit 98fc07b
Showing
14 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
compiler/luci/lang/include/luci/IR/AttrWeightCompression.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 | ||
}; | ||
|
||
} // namespace luci | ||
|
||
#endif // __LUCI_IR_ATTRWEIGHTCOMPRESSION_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
compiler/luci/pass/include/luci/Pass/CompressWeightsPass.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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_COMPRESS_WEIGHTS_PASS_H__ | ||
#define __LUCI_COMPRESS_WEIGHTS_PASS_H__ | ||
|
||
#include <logo/Pass.h> | ||
|
||
namespace luci | ||
{ | ||
|
||
/** | ||
* @brief Class to generate FC/CONV with compressed weights | ||
* | ||
* To see the target Op pattern, please visit implementation. | ||
*/ | ||
struct CompressWeightsPass final : public logo::Pass | ||
{ | ||
const char *name(void) const final { return "luci::CompressWeightsPass"; } | ||
|
||
bool run(loco::Graph *g) final; | ||
}; | ||
|
||
} // namespace luci | ||
|
||
#endif // __LUCI_COMPRESS_WEIGHTS_PASS_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "luci/Pass/CompressWeightsPass.h" | ||
#include "helpers/HuffmanEncoder.h" | ||
#include "helpers/NodeFiller.h" | ||
|
||
#include <luci/IR/CircleNodes.h> | ||
#include <luci/Profile/CircleNodeOrigin.h> | ||
#include <luci/Service/CircleNodeClone.h> | ||
#include <luci/Service/Nodes/CircleConst.h> | ||
|
||
#include <cmath> | ||
#include <cassert> | ||
|
||
namespace | ||
{ | ||
template <loco::DataType T> class TypeSelector; | ||
|
||
template <> class TypeSelector<loco::DataType::U8> | ||
{ | ||
public: | ||
using Type = uint8_t; | ||
}; | ||
template <> class TypeSelector<loco::DataType::S8> | ||
{ | ||
public: | ||
using Type = int8_t; | ||
}; | ||
|
||
template <loco::DataType DT> bool compress_weights_huffman_conv2d(luci::CircleConv2D *conv2d) | ||
{ | ||
using T = typename TypeSelector<DT>::Type; | ||
assert(conv2d); | ||
if (conv2d->weightCompression() == luci::WeightCompression::HUFFMAN) | ||
return false; | ||
luci::huffman::HuffmanEncoder<T> encoder; | ||
auto weights = loco::must_cast<luci::CircleConst *>(conv2d->filter()); | ||
auto new_weights = luci::clone(weights); | ||
std::vector<T> tmp_buf{}; | ||
for (int i = 0; i < weights->size<DT>(); ++i) | ||
{ | ||
T data = weights->at<DT>(i); | ||
tmp_buf.push_back(data); | ||
} | ||
std::vector<uint8_t> encoded = encoder.encode(tmp_buf); | ||
new_weights->dtype(DT); | ||
new_weights->size<DT>(encoded.size()); | ||
for (int i = 0; i < new_weights->size<DT>(); ++i) | ||
{ | ||
new_weights->at<DT>(i) = encoded[i]; | ||
} | ||
conv2d->filter(new_weights); | ||
conv2d->weightCompression(luci::WeightCompression::HUFFMAN); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace luci | ||
{ | ||
|
||
bool CompressWeightsPass::run(loco::Graph *g) | ||
{ | ||
bool changed = false; | ||
for (auto node : loco::active_nodes(loco::output_nodes(g))) | ||
{ | ||
auto conv2d = dynamic_cast<luci::CircleConv2D *>(node); | ||
if (not conv2d) | ||
continue; | ||
loco::DataType weights_dtype = loco::must_cast<luci::CircleConst *>(conv2d->filter())->dtype(); | ||
if (weights_dtype == loco::DataType::S8) | ||
{ | ||
if (compress_weights_huffman_conv2d<loco::DataType::S8>(conv2d)) | ||
changed = true; | ||
} | ||
else if (weights_dtype == loco::DataType::U8) | ||
{ | ||
if (compress_weights_huffman_conv2d<loco::DataType::U8>(conv2d)) | ||
changed = true; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error("Huffman weights compression supports s8 and u8"); | ||
} | ||
} | ||
|
||
return changed; | ||
} | ||
|
||
} // namespace luci |
Oops, something went wrong.