Skip to content

Commit

Permalink
Added Asymmetric perchannel quantization support (#682)
Browse files Browse the repository at this point in the history
Type: New Feature

Signed-off-by: Feiyue Chen <[email protected]>
  • Loading branch information
chenfeiyue-cfy authored Feb 27, 2024
1 parent 3b74cf0 commit 8ca1382
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion include/tim/vx/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ enum class DataType {
UINT4
};

enum class QuantType { NONE, ASYMMETRIC, SYMMETRIC_PER_CHANNEL, DYNAMIC_FIXED_POINT };
enum class QuantType {
NONE,
ASYMMETRIC,
SYMMETRIC_PER_CHANNEL,
ASYMMETRIC_PER_CHANNEL,
DYNAMIC_FIXED_POINT
};

enum TensorAttribute {
CONSTANT = 1 << 0,
Expand Down
21 changes: 20 additions & 1 deletion src/tim/vx/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,26 @@ void PackTensorDtype(tim::vx::TensorSpec& spec, vsi_nn_dtype_t* dtype) {
break;
case tim::vx::QuantType::SYMMETRIC_PER_CHANNEL: {
dtype->scales = spec.quantization_.Scales().data();
dtype->scale_dim = spec.quantization_.ZeroPoints().size();
dtype->scale_dim = spec.quantization_.Scales().size();
#if (VSI_NN_VERSION_MAJOR == 1 && VSI_NN_VERSION_MINOR == 1 && \
VSI_NN_VERSION_PATCH <= 18)
{
std::vector<float> zps(spec.quantization_.ZeroPoints().size());
std::transform(spec.quantization_.ZeroPoints().begin(),
spec.quantization_.ZeroPoints().end(), zps.begin(),
[](const int& it) { return static_cast<float>(it); });
dtype->zero_points = zps.data();
}
#else
dtype->zero_points = spec.quantization_.ZeroPoints().data();
#endif
dtype->zero_points_dim = spec.quantization_.ZeroPoints().size();
dtype->channel_dim = spec.quantization_.ChannelDim();
break;
}
case tim::vx::QuantType::ASYMMETRIC_PER_CHANNEL: {
dtype->scales = spec.quantization_.Scales().data();
dtype->scale_dim = spec.quantization_.Scales().size();
#if (VSI_NN_VERSION_MAJOR == 1 && VSI_NN_VERSION_MINOR == 1 && \
VSI_NN_VERSION_PATCH <= 18)
{
Expand Down
2 changes: 2 additions & 0 deletions src/tim/vx/type_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ vsi_nn_qnt_type_e TranslateQuantType(QuantType qtype) {
return VSI_NN_QNT_TYPE_AFFINE_ASYMMETRIC;
case QuantType::SYMMETRIC_PER_CHANNEL:
return VSI_NN_QNT_TYPE_AFFINE_PERCHANNEL_SYMMETRIC;
case QuantType::ASYMMETRIC_PER_CHANNEL:
return VSI_NN_QNT_TYPE_AFFINE_PERCHANNEL_ASYMMETRIC;
case QuantType::DYNAMIC_FIXED_POINT:
return VSI_NN_QNT_TYPE_DFP;
default:
Expand Down

0 comments on commit 8ca1382

Please sign in to comment.