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/pass] introduce quantize weight with GPTQ pass #14285

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
82 changes: 82 additions & 0 deletions compiler/luci/pass/include/luci/Pass/QuantizeWeightsWithGPTQPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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_QUANTIZE_WEIGHTS_WITH_GPTQ_PASS_H__
#define __LUCI_QUANTIZE_WEIGHTS_WITH_GPTQ_PASS_H__

#include <luci/Pass/QuantizationParameters.h>
#include <luci/IR/CircleNode.h>

#include <logo/Pass.h>
#include <loco.h>

#include <unordered_map>

namespace luci
{

using HessianMap = std::unordered_map<const luci::CircleNode *, std::vector<float>>;

/**
* @brief Pass to quantize weights
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this brief doesn't give any help. plz remove or give more useful description.

*/
class QuantizeWeightsWithGPTQPass : public logo::Pass
{
public:
struct Context
{
loco::DataType input_model_dtype = loco::DataType::Unknown;
loco::DataType output_model_dtype = loco::DataType::Unknown;
QuantizationGranularity granularity = QuantizationGranularity::ChannelWise;
std::vector<LayerInfo> layers_info;
};

public:
QuantizeWeightsWithGPTQPass(std::unique_ptr<Context> &&ctx) : _ctx{std::move(ctx)}
{
// DO NOTHING
}

QuantizeWeightsWithGPTQPass(std::unique_ptr<Context> &&ctx, HessianMap *hessian_map)
: _ctx{std::move(ctx)}, _hessian_map{hessian_map}
{
// DO NOTHING
}

public:
QuantizeWeightsWithGPTQPass(loco::DataType input_model_dtype, loco::DataType output_model_dtype,
QuantizationGranularity granularity)
{
_ctx = std::make_unique<Context>();
{
_ctx->input_model_dtype = input_model_dtype;
_ctx->output_model_dtype = output_model_dtype;
_ctx->granularity = granularity;
}
}
virtual const char *name(void) const { return "luci::QuantizeWeightsWithGPTQPass"; }

public:
bool run(loco::Graph *graph);

private:
std::unique_ptr<Context> _ctx;
HessianMap *_hessian_map;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HessianMap *_hessian_map;
HessianMap *_hessian_map = nullptr;

};

} // namespace luci

#endif //__LUCI_QUANTIZE_WEIGHTS_WITH_GPTQ_PASS_H__
5 changes: 5 additions & 0 deletions compiler/luci/pass/src/QuantizationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ uint32_t cal_offset(loco::TensorShape &dimension, uint32_t *indices)
indices[2] * dimension.dim(3).value() + indices[3];
}

uint32_t cal_offset_2d(loco::TensorShape &dimension, uint32_t *indices)
{
return indices[0] * dimension.dim(1).value() + indices[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know caller is calling with safe index.
It would be better to add some assertion check before accessing ptr and array.

}

// Activation (ofm) qtype is determined in different ways.
// 1. Pre-defined values: Some Ops have pre-defined qparams (ex: LOGISTIC, TANH)
// 2. Integer scale: Output of some Ops should be integers (ex: FLOOR, CEIL)
Expand Down
3 changes: 3 additions & 0 deletions compiler/luci/pass/src/QuantizationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ bool get_channel_dim_index(CircleConst *node, loco::TensorShape &dimension,

// Calculate offset of the given indices in dimension
uint32_t cal_offset(loco::TensorShape &dimension, uint32_t *indices);
uint32_t cal_offset_2d(loco::TensorShape &dimension, uint32_t *indices);

// Backward propagation of concatenation qparam
void propagate_concat_quantparam(luci::CircleConcatenation *concat);
Expand All @@ -63,6 +64,8 @@ void propagate_pad_v2_quantparam(luci::CirclePadV2 *pad_v2);
// Return true if the node is quantized
bool is_quantized(const CircleNode *node);

uint8_t fp32_to_uint8_cast(float f);

// Return true if the node is fp32
bool is_fp32(const CircleNode *node);

Expand Down
Loading