-
Notifications
You must be signed in to change notification settings - Fork 44
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
[compiler] add interReturnType function for byteir.reshape and byteir.one_hot #451
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
56dfda0
[tf-frontend] add pattern to convert dynamcic shape reshapeOp to byte…
heromapwrd f29284c
[compiler] add inferReturnType function for byteir.reshape and byteir…
Vremold 5dc2dfb
[tf-frontend] fix bug of fuse_tf_ops test
heromapwrd 1fd08fe
Merge branch 'main' into tf-frontend
heromapwrd 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 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
76 changes: 76 additions & 0 deletions
76
compiler/lib/Dialect/mhlo/DynamicShapeOpRegister/OneHot.cpp
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,76 @@ | ||
//===- OneHot.cpp ---------------------------------------------*--- C++ -*-===// | ||
// | ||
// Copyright 2024 ByteDance Ltd. and/or its affiliates. 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 "byteir/Dialect/mhlo/DynamicShapeOpRegister/Register.h" | ||
#include "byteir/Dialect/mhlo/Util/CustomCallUtil.h" | ||
#include "byteir/Dialect/mhlo/Util/ShapeInferUtil.h" | ||
#include "mhlo/IR/hlo_ops.h" | ||
#include "mlir/Dialect/Shape/IR/Shape.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
#define DEBUG_TYPE "dynamic-shape-op-register" | ||
|
||
using namespace mlir; | ||
|
||
void mlir::registerOneHotInferReturnTypeComponents() { | ||
static InferReturnTypeComponentsRegistration shapeRegister( | ||
getOneHotName(), | ||
[](MLIRContext *context, std::optional<Location> loc, | ||
ValueShapeRange operands, DictionaryAttr attr, | ||
OpaqueProperties properties, RegionRange, | ||
SmallVectorImpl<ShapedTypeComponents> &inferredReturnTypes) { | ||
ShapedType dataType = dyn_cast<ShapedType>(operands[0].getType()); | ||
if (!dataType) { | ||
LLVM_DEBUG(llvm::dbgs() << loc << ": get dataType failed\n"); | ||
return failure(); | ||
} | ||
int64_t axis = attr.getAs<DictionaryAttr>(getCustomCallAttrName()) | ||
.getAs<IntegerAttr>("axis") | ||
.getInt(); | ||
int64_t depth = attr.getAs<DictionaryAttr>(getCustomCallAttrName()) | ||
.getAs<IntegerAttr>("depth") | ||
.getInt(); | ||
Attribute onValue = attr.getAs<DictionaryAttr>(getCustomCallAttrName()) | ||
.getAs<Attribute>("on_value"); | ||
Type onValueType; | ||
if (dyn_cast<IntegerAttr>(onValue)) { | ||
onValueType = dyn_cast<IntegerAttr>(onValue).getType(); | ||
} else if (dyn_cast<FloatAttr>(onValue)) { | ||
onValueType = dyn_cast<FloatAttr>(onValue).getType(); | ||
} else { | ||
LLVM_DEBUG(llvm::dbgs() | ||
<< loc << ": get output element type failed\n"); | ||
return failure(); | ||
} | ||
|
||
auto dataShape = dataType.getShape(); | ||
llvm::SmallVector<int64_t> outShape; | ||
for (int64_t i = 0; i < dataShape.size(); ++i) { | ||
if (axis == i) { | ||
outShape.push_back(depth); | ||
} | ||
outShape.push_back(dataShape[i]); | ||
} | ||
if (-1 == axis || axis >= dataShape.size()) { | ||
outShape.push_back(depth); | ||
} | ||
inferredReturnTypes.emplace_back(outShape, onValueType); | ||
return success(); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// RUN: tf-ext-opt | ||
// -rewrite-to-custom-call="ops=softmax,log_softmax,gelu,erf,arg_max,arg_min,top_k,layer_norm,l2_norm,addn,one_hot,DynamicMaskStitch,DynamicPartition,DynamicStitch" | ||
// -rewrite-to-custom-call="ops=softmax,log_softmax,gelu,erf,arg_max,arg_min,top_k,layer_norm,l2_norm,addn,one_hot,reshape,DynamicMaskStitch,DynamicPartition,DynamicStitch" | ||
// %s | FileCheck %s | ||
|
||
func.func @softmax_case0(%arg0: tensor<100x100xf32>) -> tensor<100x100xf32> { | ||
|
@@ -731,3 +731,12 @@ func.func @onehot_case0(%arg0: tensor<150xi32>) -> tensor<150x16xf32> { | |
// tensor<150x16xf32> { CHECK: mhlo.custom_call CHECK-SAME: @byteir.one_hot | ||
// CHECK-SAME: byteir_attrs = {axis = 1 : i64, depth = 16 : i64, off_value = | ||
// 0.000000e+00 : f32, on_value = 1.000000e+00 : f32} | ||
|
||
func.func @reshape_case0(%arg0: tensor<?x24xf16>) -> tensor<?x24x1xf16> { | ||
%cst = "tf.Const"() <{value = dense<[-1, 24, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> | ||
%0 = "tf.Reshape"(%arg0, %cst) : (tensor<?x24xf16>, tensor<3xi64>) -> tensor<?x24x1xf16> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. => compute_reshape_shape + mhlo.dynamic_reshape |
||
return %0 : tensor<?x24x1xf16> | ||
} | ||
// CHECK-LABEL: func.func @reshape_case0 | ||
// CHECK: mhlo.custom_call | ||
// CHECK-SAME: @byteir.reshape |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to add
byteir.reshape
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are four reasons for switching to byteir.reshape instead of retaining the original tf.Reshape form: