-
Notifications
You must be signed in to change notification settings - Fork 35
Initial commit for tfl custom op translation #698
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b2c8d34
Initial commit for tfl custom op translation
panickal 4ccabc1
Update larq_compute_engine/mlir/transforms/translate_tflite.cc
panickal-xmos dab20a6
Update larq_compute_engine/mlir/transforms/translate_tflite.cc
panickal-xmos 2065616
Update larq_compute_engine/mlir/transforms/translate_tflite.cc
panickal-xmos f3e42cc
Update test to check both passes of larq to tfl to larq
panickal ae29d9d
Update tests
panickal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| #include "flatbuffers/flexbuffers.h" | ||
| #include "larq_compute_engine/mlir/ir/lce_ops.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" | ||
|
|
||
| namespace mlir { | ||
| namespace TFL { | ||
|
|
||
| namespace { | ||
|
|
||
| struct TranslateToLCE : public PassWrapper<TranslateToLCE, FunctionPass> { | ||
| void getDependentDialects(DialectRegistry& registry) const override { | ||
| registry.insert<mlir::TFL::TensorFlowLiteDialect, mlir::lq::LarqDialect>(); | ||
| } | ||
| void runOnFunction() override; | ||
| }; | ||
|
|
||
| struct TranslateToLCEPattern : public OpRewritePattern<TFL::CustomOp> { | ||
| using OpRewritePattern<TFL::CustomOp>::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(TFL::CustomOp custom_op, | ||
| PatternRewriter& rewriter) const override { | ||
| auto stringData = custom_op.custom_option().getValue(); | ||
|
|
||
| // Replace CustomOp with relevant LarqOp | ||
| if (custom_op.custom_code() == "LceQuantize") { | ||
| rewriter.replaceOpWithNewOp<lq::QuantizeOp>( | ||
| custom_op, custom_op->getResultTypes(), custom_op->getOperands()); | ||
| } else if (custom_op.custom_code() == "LceDequantize") { | ||
| rewriter.replaceOpWithNewOp<lq::DequantizeOp>( | ||
| custom_op, custom_op->getResultTypes(), custom_op->getOperands()); | ||
| } else if (custom_op.custom_code() == "LceBMaxPool2d") { | ||
| auto map = | ||
| flexbuffers::GetRoot((uint8_t*)stringData.data(), stringData.size()) | ||
| .AsMap(); | ||
| rewriter.replaceOpWithNewOp<lq::BMaxPool2dOp>( | ||
| custom_op, custom_op->getResultTypes(), custom_op->getOperand(0), | ||
| stringifyPadding(static_cast<Padding>(map["padding"].AsInt32())), | ||
| map["stride_width"].AsInt32(), map["stride_height"].AsInt32(), | ||
| map["filter_width"].AsInt32(), map["filter_height"].AsInt32()); | ||
| } else if (custom_op.custom_code() == "LceBconv2d") { | ||
| auto map = | ||
| flexbuffers::GetRoot((uint8_t*)stringData.data(), stringData.size()) | ||
| .AsMap(); | ||
| rewriter.replaceOpWithNewOp<lq::Bconv2dOp>( | ||
| custom_op, custom_op->getResultTypes(), custom_op->getOperand(0), | ||
| custom_op->getOperand(1), custom_op->getOperand(2), | ||
| custom_op->getOperand(3), custom_op->getOperand(4), | ||
| map["channels_in"].AsInt32(), map["dilation_height_factor"].AsInt32(), | ||
| map["dilation_width_factor"].AsInt32(), | ||
| stringifyActivationFunctionType(static_cast<ActivationFunctionType>( | ||
| map["fused_activation_function"].AsInt32())), | ||
| map["pad_values"].AsInt32(), | ||
| stringifyPadding(static_cast<Padding>(map["padding"].AsInt32())), | ||
| map["stride_height"].AsInt32(), map["stride_width"].AsInt32()); | ||
| } | ||
|
|
||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| void TranslateToLCE::runOnFunction() { | ||
| OwningRewritePatternList patterns(&getContext()); | ||
| auto* ctx = &getContext(); | ||
| auto func = getFunction(); | ||
| patterns.insert<TranslateToLCEPattern>(ctx); | ||
| (void)applyPatternsAndFoldGreedily(func, std::move(patterns)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // Creates an instance of the TranslateToLCE pass. | ||
| std::unique_ptr<OperationPass<FuncOp>> CreateTranslateToLCEPass() { | ||
| return std::make_unique<TranslateToLCE>(); | ||
| } | ||
|
|
||
| static PassRegistration<TranslateToLCE> pass( | ||
| "lce-translate-tfl", "Translate TFL custom ops to LCE ops"); | ||
|
|
||
| } // namespace TFL | ||
| } // namespace mlir |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.