forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a convenience function for creating
i8:f32
uniform quant…
…ized type. Creates a factored out `CreateI8F32UniformQuantizedType` function, which creates a `!quant.uniform<i8:f32, scale:zp>` given scale and zp. PiperOrigin-RevId: 557705465
- Loading branch information
1 parent
404c29e
commit f21d60a
Showing
6 changed files
with
201 additions
and
27 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
40 changes: 40 additions & 0 deletions
40
tensorflow/compiler/mlir/quantization/stablehlo/uniform_quantized_types.cc
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,40 @@ | ||
/* Copyright 2023 The TensorFlow Authors. 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 "tensorflow/compiler/mlir/quantization/stablehlo/uniform_quantized_types.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "llvm/Support/MathExtras.h" | ||
#include "mlir/Dialect/Quant/QuantTypes.h" // from @llvm-project | ||
#include "mlir/IR/BuiltinTypes.h" // from @llvm-project | ||
#include "mlir/IR/Location.h" // from @llvm-project | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace quant { | ||
|
||
UniformQuantizedType CreateI8F32UniformQuantizedType(Location loc, | ||
MLIRContext& context, | ||
const float scale, | ||
const int8_t zero_point) { | ||
return UniformQuantizedType::getChecked( | ||
loc, /*flags=*/QuantizationFlags::Signed, | ||
/*storageType=*/IntegerType::get(&context, /*width=*/8), | ||
/*expressedType=*/FloatType::getF32(&context), scale, zero_point, | ||
/*storageTypeMin=*/llvm::minIntN(8), /*storageTypeMax=*/llvm::maxIntN(8)); | ||
} | ||
|
||
} // namespace quant | ||
} // namespace mlir |
38 changes: 38 additions & 0 deletions
38
tensorflow/compiler/mlir/quantization/stablehlo/uniform_quantized_types.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,38 @@ | ||
/* Copyright 2023 The TensorFlow Authors. 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 TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_UNIFORM_QUANTIZED_TYPES_H_ | ||
#define TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_UNIFORM_QUANTIZED_TYPES_H_ | ||
|
||
#include <cstdint> | ||
|
||
#include "mlir/Dialect/Quant/QuantTypes.h" // from @llvm-project | ||
#include "mlir/IR/Location.h" // from @llvm-project | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace quant { | ||
|
||
// Creates a `UniformQuantizedType` with the given `scale` and `zero_point` | ||
// values. The produced type has f32 as its expressed type and i8 as its | ||
// storage type. The available values use the full range of the storage value, | ||
// i.e. [-128, 127]. Assumes asymmetric quantization, meaning the zero point | ||
// values may be nonzero. | ||
quant::UniformQuantizedType CreateI8F32UniformQuantizedType( | ||
Location loc, MLIRContext& context, float scale, int8_t zero_point); | ||
|
||
} // namespace quant | ||
} // namespace mlir | ||
|
||
#endif // TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_UNIFORM_QUANTIZED_TYPES_H_ |
82 changes: 82 additions & 0 deletions
82
tensorflow/compiler/mlir/quantization/stablehlo/uniform_quantized_types_test.cc
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,82 @@ | ||
/* Copyright 2023 The TensorFlow Authors. 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 "tensorflow/compiler/mlir/quantization/stablehlo/uniform_quantized_types.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include "mlir/Dialect/Quant/QuantOps.h" // from @llvm-project | ||
#include "mlir/Dialect/Quant/QuantTypes.h" // from @llvm-project | ||
#include "mlir/IR/Location.h" // from @llvm-project | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace quant { | ||
namespace { | ||
|
||
using ::mlir::quant::UniformQuantizedType; | ||
|
||
class CreateI8F32UniformQuantizedTypeTest : public ::testing::Test { | ||
protected: | ||
CreateI8F32UniformQuantizedTypeTest() : ctx_() { | ||
ctx_.loadDialect<quant::QuantizationDialect>(); | ||
} | ||
|
||
MLIRContext ctx_; | ||
}; | ||
|
||
TEST_F(CreateI8F32UniformQuantizedTypeTest, HasI8StorageType) { | ||
const UniformQuantizedType quantized_type = | ||
CreateI8F32UniformQuantizedType(UnknownLoc::get(&ctx_), ctx_, | ||
/*scale=*/1.0, /*zero_point=*/0); | ||
|
||
EXPECT_TRUE(quantized_type.getStorageType().isSignlessInteger(8)); | ||
} | ||
|
||
TEST_F(CreateI8F32UniformQuantizedTypeTest, HasF32ExpressedType) { | ||
const UniformQuantizedType quantized_type = | ||
CreateI8F32UniformQuantizedType(UnknownLoc::get(&ctx_), ctx_, | ||
/*scale=*/1.0, /*zero_point=*/0); | ||
|
||
EXPECT_TRUE(quantized_type.getExpressedType().isF32()); | ||
} | ||
|
||
TEST_F(CreateI8F32UniformQuantizedTypeTest, IsSigned) { | ||
const UniformQuantizedType quantized_type = | ||
CreateI8F32UniformQuantizedType(UnknownLoc::get(&ctx_), ctx_, | ||
/*scale=*/1.0, /*zero_point=*/0); | ||
|
||
EXPECT_TRUE(quantized_type.isSigned()); | ||
} | ||
|
||
TEST_F(CreateI8F32UniformQuantizedTypeTest, SotrageTypeMinMaxEqualToI8MinMax) { | ||
const UniformQuantizedType quantized_type = | ||
CreateI8F32UniformQuantizedType(UnknownLoc::get(&ctx_), ctx_, | ||
/*scale=*/1.0, /*zero_point=*/0); | ||
|
||
EXPECT_EQ(quantized_type.getStorageTypeMin(), -128); | ||
EXPECT_EQ(quantized_type.getStorageTypeMax(), 127); | ||
} | ||
|
||
TEST_F(CreateI8F32UniformQuantizedTypeTest, HasScaleAndZeroPointProperlySet) { | ||
const UniformQuantizedType quantized_type = | ||
CreateI8F32UniformQuantizedType(UnknownLoc::get(&ctx_), ctx_, | ||
/*scale=*/8.0, /*zero_point=*/99); | ||
|
||
EXPECT_EQ(quantized_type.getScale(), 8.0); | ||
EXPECT_EQ(quantized_type.getZeroPoint(), 99); | ||
} | ||
|
||
} // namespace | ||
} // namespace quant | ||
} // namespace mlir |