-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #922 from xmos/replace-mean
Replace mean
- Loading branch information
Showing
33 changed files
with
264 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow import lite as tfl | ||
|
||
i = 0 | ||
|
||
|
||
def generate_mean_model(input_shape, axes): | ||
input_data = tf.keras.Input(shape=input_shape, dtype=tf.int8, batch_size=1) | ||
mean_output = tf.keras.backend.mean(input_data, axis=axes) | ||
model = tf.keras.Model(inputs=input_data, outputs=mean_output) | ||
converter = tfl.TFLiteConverter.from_keras_model(model) | ||
|
||
def representative_dataset_gen(): | ||
for _ in range(100): | ||
yield [ | ||
np.random.uniform(low=-127, high=127, size=input_shape).astype(np.int8) | ||
] | ||
|
||
converter.optimizations = [tf.lite.Optimize.DEFAULT] | ||
converter.representative_dataset = representative_dataset_gen | ||
converter.target_spec.supported_ops = [tfl.OpsSet.TFLITE_BUILTINS_INT8] | ||
converter.inference_input_type = tf.int8 | ||
converter.inference_output_type = tf.int8 | ||
|
||
tflite_model = converter.convert() | ||
global i | ||
model_name = f"test_mean_{i}.tflite" | ||
i += 1 | ||
with open(model_name, "wb") as f: | ||
f.write(tflite_model) | ||
print(f"Model saved: {model_name}") | ||
|
||
|
||
input_shapes_and_axes = [ | ||
((10,), [0]), | ||
((8, 16), [0]), | ||
((8, 16), [1]), | ||
((8, 16), [0, 1]), | ||
((8, 15, 32), [0]), | ||
((8, 15, 32), [1]), | ||
((8, 15, 32), [2]), | ||
((8, 15, 32), [0, 2]), | ||
] | ||
|
||
for shape, axes in input_shapes_and_axes: | ||
generate_mean_model(shape, axes) |
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 @@ | ||
MAX_ABS_ERROR: 1.0 |
Binary file not shown.
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,5 @@ | ||
func.func @main(%arg0: tensor<1x5x8x16x!quant.uniform<i8:f32, 0.0078426999971270561:-1>> {tf_saved_model.index_path = ["input_2"]}) -> (tensor<1x5x1x16x!quant.uniform<i8:f32, 0.0078426999971270561:-1>> {tf_saved_model.index_path = ["tf.mean_1"]}) attributes {tf.entry_function = {inputs = "serving_default_input_2:0", outputs = "PartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<1xi32>, value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = true} : (tensor<1x5x8x16x!quant.uniform<i8:f32, 0.0078426999971270561:-1>>, tensor<1xi32>) -> tensor<1x5x1x16x!quant.uniform<i8:f32, 0.0078426999971270561:-1>> | ||
return %1 : tensor<1x5x1x16x!quant.uniform<i8:f32, 0.0078426999971270561:-1>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 2nd and 3rd axes of a 4D tensor with consecutive axes and keep_dims = true. | ||
func.func @main(%arg0: tensor<8x5x10x12x!quant.uniform<i8:f32, 0.008:2>>) -> (tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<2xi32>, value = dense<[1, 2]> : tensor<2xi32>} : () -> tensor<2xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = true} : (tensor<8x5x10x12x!quant.uniform<i8:f32, 0.008:2>>, tensor<2xi32>) -> tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>> | ||
return %1 : tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 2nd and 3rd axes of a 4D tensor with consecutive axes and keep_dims = true. | ||
func.func @main(%arg0: tensor<8x5x10x12x!quant.uniform<i8:f32, 0.008:2>>) -> (tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<2xi32>, value = dense<[1, 2]> : tensor<2xi32>} : () -> tensor<2xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = true} : (tensor<8x5x10x12x!quant.uniform<i8:f32, 0.008:2>>, tensor<2xi32>) -> tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>> | ||
return %1 : tensor<8x1x1x12x!quant.uniform<i8:f32, 0.008:2>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 3rd axis of a 4D tensor without keeping dimensions. | ||
func.func @main(%arg0: tensor<2x3x4x5x!quant.uniform<i8:f32, 0.005:-128>>) -> (tensor<2x3x5x!quant.uniform<i8:f32, 0.006:-127>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<1xi32>, value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor<2x3x4x5x!quant.uniform<i8:f32, 0.005:-128>>, tensor<1xi32>) -> tensor<2x3x5x!quant.uniform<i8:f32, 0.006:-127>> | ||
return %1 : tensor<2x3x5x!quant.uniform<i8:f32, 0.006:-127>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 2nd and 4th axes of a 5D tensor while keeping dimensions. | ||
func.func @main(%arg0: tensor<4x3x5x7x6x!quant.uniform<i8:f32, 0.0045:0>>) -> (tensor<4x1x5x1x6x!quant.uniform<i8:f32, 0.0045:0>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<2xi32>, value = dense<[1, 3]> : tensor<2xi32>} : () -> tensor<2xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = true} : (tensor<4x3x5x7x6x!quant.uniform<i8:f32, 0.0045:0>>, tensor<2xi32>) -> tensor<4x1x5x1x6x!quant.uniform<i8:f32, 0.0045:0>> | ||
return %1 : tensor<4x1x5x1x6x!quant.uniform<i8:f32, 0.0045:0>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 1st axis of a 3D tensor without keeping dimensions. | ||
func.func @main(%arg0: tensor<10x20x30x!quant.uniform<i8:f32, 0.003:-5>>) -> (tensor<20x30x!quant.uniform<i8:f32, 0.003:-5>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<1xi32>, value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor<10x20x30x!quant.uniform<i8:f32, 0.003:-5>>, tensor<1xi32>) -> tensor<20x30x!quant.uniform<i8:f32, 0.003:-5>> | ||
return %1 : tensor<20x30x!quant.uniform<i8:f32, 0.003:-5>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces all axes of a 2D tensor while keeping dimensions. | ||
func.func @main(%arg0: tensor<5x7x!quant.uniform<i8:f32, 0.002:-3>>) -> (tensor<1x1x!quant.uniform<i8:f32, 0.002:-3>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<2xi32>, value = dense<[0, 1]> : tensor<2xi32>} : () -> tensor<2xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = true} : (tensor<5x7x!quant.uniform<i8:f32, 0.002:-3>>, tensor<2xi32>) -> tensor<1x1x!quant.uniform<i8:f32, 0.002:-3>> | ||
return %1 : tensor<1x1x!quant.uniform<i8:f32, 0.002:-3>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces a 1D tensor to a scalar. | ||
func.func @main(%arg0: tensor<15x!quant.uniform<i8:f32, 0.009:0>>) -> (tensor<!quant.uniform<i8:f32, 0.009:0>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<1xi32>, value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor<15x!quant.uniform<i8:f32, 0.009:0>>, tensor<1xi32>) -> tensor<!quant.uniform<i8:f32, 0.009:0>> | ||
return %1 : tensor<!quant.uniform<i8:f32, 0.009:0>> | ||
} |
Binary file not shown.
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,6 @@ | ||
// This test reduces the 2nd and 3rd axes of a 3D tensor with different input/output quantization parameters. | ||
func.func @main(%arg0: tensor<5x6x7x!quant.uniform<i8:f32, 0.004:-2>>) -> (tensor<5x!quant.uniform<i8:f32, 0.0035:-1>>) { | ||
%0 = "tfl.pseudo_qconst"() {qtype = tensor<2xi32>, value = dense<[1, 2]> : tensor<2xi32>} : () -> tensor<2xi32> | ||
%1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor<5x6x7x!quant.uniform<i8:f32, 0.004:-2>>, tensor<2xi32>) -> tensor<5x!quant.uniform<i8:f32, 0.0035:-1>> | ||
return %1 : tensor<5x!quant.uniform<i8:f32, 0.0035:-1>> | ||
} |
Binary file not shown.
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 |
---|---|---|
@@ -1 +1 @@ | ||
MAX_ABS_ERROR: 0.0 | ||
MAX_ABS_ERROR: 1.0 |
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
Submodule lib_tflite_micro
updated
5 files
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2021 XMOS LIMITED. This Software is subject to the terms of the | ||
// XMOS Public License: Version 1 | ||
|
||
#include "IR/XCoreOps.h" | ||
#include "Utils/Util.h" | ||
|
||
extern "C" { | ||
#include "lib_nn/api/nn_layers.h" | ||
} | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" | ||
#include "tensorflow/compiler/mlir/lite/utils/validators.h" | ||
|
||
namespace mlir::xcore { | ||
|
||
namespace { | ||
// Replace TFL Mean with Mean for XCore. | ||
struct ReplaceMean | ||
: public PassWrapper<ReplaceMean, OperationPass<func::FuncOp>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ReplaceMean) | ||
|
||
void getDependentDialects(DialectRegistry ®istry) const final { | ||
registry.insert<TFL::TensorFlowLiteDialect>(); | ||
} | ||
StringRef getArgument() const final { return "xcore-replace-mean"; } | ||
StringRef getDescription() const final { | ||
return "Replace TFL Mean with Mean for XCore."; | ||
} | ||
void runOnOperation() override; | ||
}; | ||
|
||
struct ReplaceMeanPattern : public OpRewritePattern<TFL::MeanOp> { | ||
using OpRewritePattern<TFL::MeanOp>::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(TFL::MeanOp meanOp, | ||
PatternRewriter &rewriter) const override { | ||
|
||
auto input = meanOp.getInput(); | ||
auto output = meanOp.getOutput(); | ||
|
||
DenseElementsAttr axisAttr; | ||
matchPattern(meanOp.getAxis(), m_Constant(&axisAttr)); | ||
auto axisValues = axisAttr.getValues<int32_t>(); | ||
std::vector<int32_t> axis(axisValues.begin(), axisValues.end()); | ||
int32_t minAxis = *std::min_element(axis.begin(), axis.end()); | ||
int32_t maxAxis = *std::max_element(axis.begin(), axis.end()); | ||
if (maxAxis - minAxis > axis.size() - 1) { | ||
return failure(); | ||
} | ||
|
||
auto inputType = input.getType().cast<ShapedType>(); | ||
auto outputType = output.getType().cast<ShapedType>(); | ||
if (!utils::isNBitSignedQType<8>(inputType.getElementType()) || | ||
!utils::isNBitSignedQType<8>(outputType.getElementType())) { | ||
return failure(); | ||
} | ||
|
||
auto inputShape = inputType.getShape(); | ||
auto outputShape = outputType.getShape(); | ||
|
||
int rank = inputShape.size(); | ||
|
||
int beginDims = 1; | ||
for (int i = 0; i < minAxis; i++) { | ||
beginDims *= inputShape[i]; | ||
} | ||
|
||
int endDims = 1; | ||
for (int i = maxAxis + 1; i < rank; i++) { | ||
endDims *= inputShape[i]; | ||
} | ||
|
||
int meanDims = 1; | ||
for (int i = minAxis; i <= maxAxis; i++) { | ||
meanDims *= inputShape[i]; | ||
} | ||
|
||
auto inputQType = utils::getQType(input); | ||
auto outputQType = utils::getQType(output); | ||
|
||
float inZeroPoint = static_cast<float>(inputQType.getZeroPoint()); | ||
float outZeroPoint = static_cast<float>(outputQType.getZeroPoint()); | ||
float scaleMul = inputQType.getScale() / outputQType.getScale() / | ||
static_cast<float>(meanDims); | ||
|
||
auto beginDimsAttr = rewriter.getI32IntegerAttr(beginDims); | ||
auto endDimsAttr = rewriter.getI32IntegerAttr(endDims); | ||
auto meanDimsAttr = rewriter.getI32IntegerAttr(meanDims); | ||
auto inZeroPointAttr = rewriter.getF32FloatAttr(inZeroPoint); | ||
auto outZeroPointAttr = rewriter.getF32FloatAttr(outZeroPoint); | ||
auto scaleMulAttr = rewriter.getF32FloatAttr(scaleMul); | ||
|
||
auto xcMeanOp = rewriter.create<MeanOp>( | ||
meanOp.getLoc(), meanOp.getType(), meanOp.getInput(), beginDimsAttr, | ||
meanDimsAttr, endDimsAttr, inZeroPointAttr, outZeroPointAttr, | ||
scaleMulAttr); | ||
rewriter.replaceOp(meanOp, xcMeanOp.getOutput()); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
void ReplaceMean::runOnOperation() { | ||
auto *ctx = &getContext(); | ||
func::FuncOp func = getOperation(); | ||
RewritePatternSet patterns(ctx); | ||
patterns.insert<ReplaceMeanPattern>(ctx); | ||
(void)applyPatternsAndFoldGreedily(func, std::move(patterns)); | ||
} | ||
} // namespace | ||
|
||
// Creates an instance of the ReplaceMean pass. | ||
std::unique_ptr<OperationPass<func::FuncOp>> createReplaceMeanPass() { | ||
return std::make_unique<ReplaceMean>(); | ||
} | ||
|
||
static PassRegistration<ReplaceMean> pass; | ||
|
||
} // namespace mlir::xcore |
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