chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,61 @@
/* 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.
==============================================================================*/
// This is the optimization pattern definition file for TensorFlow Lite.
include "mlir/IR/OpBase.td"
include "mlir/IR/PatternBase.td"
include "mlir/Dialect/Arith/IR/ArithOps.td"
include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td"
include "tensorflow/compiler/mlir/lite/utils/utils.td"
// Returns Squeezed shape of a ranked-tensor.
// Squeezed, here, means eliminating any 1s' in the
// dimensions of the tensor
def GetSqueezedShape: NativeCodeCall<"GetSqueezedShape($0)">;
// This is a utility function to deduct the effective permutation to apply on
// TFL_TransposeOp when the tensor has some dimensions with value==1
def GetSqueezedPermutation: NativeCodeCall<"GetSqueezedPermutation($0, $1)">;
// Check to see if the tensor dimensions can be Squeezed by eliminating 1s'
def CanSqueezeTensor : Constraint<CPred<
"GetShapeAttr($0).getNumElements() > GetSqueezedShape($0).getNumElements()">>;
// Pattern to convert TFL_TransposeOp with rank>6 to rank<=6 if there are
// redundant dimensions in the tensor. For example- [2x1x3] == [2x3] and 1 is
// not contributing to the dimentionality. This will run if the rank>6
// Pattern will convert-
// %0 = "tfl.transpose"(%arg0, %cst) : (tensor<56x8x56x1x1x1x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32>
// to-
// %0 = "tfl.reshape"(%arg0, %cst) : (tensor<56x8x56x1x1x1x7xf32>, tensor<4xi32>) -> tensor<56x8x56x7xf32>
// %1 = "tfl.transpose"(%0, %cst_0) : (tensor<56x8x56x7xf32>, tensor<4xi32>) -> tensor<8x56x56x7xf32>
// %2 = "tfl.reshape"(%1, %cst_1) : (tensor<8x56x56x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32>
def ConvertTransposeToDecreaseRank : Pat<
(TFL_TransposeOp:$output_transpose $input, (Arith_ConstantOp:$permutation $_)),
(TFL_ReshapeOp
(TFL_TransposeOp
(TFL_ReshapeOp $input, (Arith_ConstantOp (GetSqueezedShape $input))),
(Arith_ConstantOp (GetSqueezedPermutation $input, $permutation))),
(Arith_ConstantOp (GetShapeAttr $output_transpose))),
[(AnyStaticShapeTensor $input),
(HasRankAtLeast<7> $input),
(CanSqueezeTensor $input)]>;
def RemoveNoopTranspose : Pat<
(TFL_TransposeOp $input, $perm),
(replaceWithValue $input),
[(IsTransposeNoop $perm)]>;
@@ -0,0 +1,181 @@
/* Copyright 2022 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.
==============================================================================*/
// This is the operation enums definition file for TensorFlow Lite.
#ifndef TFL_OP_ENUMS
#define TFL_OP_ENUMS
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"
include "tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td"
// A string attribute whose value are one of the values in `cases`.
// Referred TF_AnyStrAttrOf in tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td
class TFL_AnyStrAttrOf<list<string> cases> : StringBasedAttr<
CPred<!foldl(
"llvm::cast<StringAttr>($_self).getValue() == \"" # !head(cases) # "\"",
!foreach(case, !tail(cases),
"llvm::cast<StringAttr>($_self).getValue() == \"" # case # "\""),
prev, cur, prev # " || " # cur)>,
"string attribute whose value is " #
!foldl(/*init*/!head(cases), /*list*/!tail(cases),
prev, cur, prev # ", or " # cur)>;
// Attributes used for encoding sparse tensors.
// Please find detailed explanation of these parameters in the TFLite schema.
def TFL_DT_Dense : I32EnumAttrCase<"DENSE", 0>;
def TFL_DT_SparseCSR : I32EnumAttrCase<"SPARSE_CSR", 1>;
def TFL_DimensionType : I32EnumAttr<
"DimensionType", "dimension_type", [TFL_DT_Dense, TFL_DT_SparseCSR]> {
let genSpecializedAttr = 0;
let cppNamespace = "::mlir::TFL";
}
def TFL_DimensionTypeAttr : EnumAttr<TFL_Dialect, TFL_DimensionType,
"dimension_type_attr"> {
let convertFromStorage = "$_self";
}
// Allowed activation function cases
// These should match the ActivationFunctionType enum in TFLite schema.
def TFL_AFEnum_None : I32EnumAttrCase<"NONE", 0>;
def TFL_AFEnum_Relu : I32EnumAttrCase<"RELU", 1>;
def TFL_AFEnum_Relu1 : I32EnumAttrCase<"RELU_N1_TO_1", 2>;
def TFL_AFEnum_Relu6 : I32EnumAttrCase<"RELU6", 3>;
def TFL_AFEnum_Tanh : I32EnumAttrCase<"TANH", 4>;
def TFL_AFEnum_Sign : I32EnumAttrCase<"SIGN_BIT", 5>;
def TFL_AFAttr : TFL_AnyStrAttrOf<[
TFL_AFEnum_None.symbol, TFL_AFEnum_Relu.symbol, TFL_AFEnum_Relu1.symbol,
TFL_AFEnum_Relu6.symbol, TFL_AFEnum_Tanh.symbol, TFL_AFEnum_Sign.symbol
]>;
// Predefined constant attributes of activation function
def TFL_AF_None : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_None.symbol>;
def TFL_AF_Relu : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_Relu.symbol>;
def TFL_AF_Relu1 : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_Relu1.symbol>;
def TFL_AF_Relu6 : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_Relu6.symbol>;
def TFL_AF_Tanh : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_Tanh.symbol>;
def TFL_AF_Sign : ConstantStrAttr<TFL_AFAttr, TFL_AFEnum_Sign.symbol>;
// Allowed padding cases
// These should match the padding enum in TFLite schema.
def TFL_PADEnum_Same : I32EnumAttrCase<"SAME", 0>;
def TFL_PADEnum_Valid : I32EnumAttrCase<"VALID", 1>;
def TFL_PaddingAttr : TFL_AnyStrAttrOf<[
TFL_PADEnum_Same.symbol, TFL_PADEnum_Valid.symbol
]>;
def TFL_PAD_Same : ConstantStrAttr<TFL_PaddingAttr, TFL_PADEnum_Same.symbol>;
def TFL_PAD_Valid : ConstantStrAttr<TFL_PaddingAttr, TFL_PADEnum_Valid.symbol>;
// FullyConnectedOptionsWeightFormat attributes
def TFL_FCWOEnum_Default : I32EnumAttrCase<"DEFAULT", 0>;
def TFL_FCWOEnum_Shuffled4x16i8 : I32EnumAttrCase<"SHUFFLED4x16INT8", 1>;
def TFL_FullyConnectedOptionsWeightFormatAttr :
TFL_AnyStrAttrOf<[
TFL_FCWOEnum_Default.symbol,
TFL_FCWOEnum_Shuffled4x16i8.symbol
]>;
def TFL_FCWO_Default : ConstantStrAttr<
TFL_FullyConnectedOptionsWeightFormatAttr, TFL_FCWOEnum_Default.symbol>;
def TFL_FCWO_Shuffled4x16i8 : ConstantStrAttr<
TFL_FullyConnectedOptionsWeightFormatAttr, TFL_FCWOEnum_Shuffled4x16i8.symbol>;
// MirrorPadding type attributes
def TFL_MIRRORPAD_Reflect : I32EnumAttrCase<"REFLECT", 0>;
def TFL_MIRRORPAD_Symmetric : I32EnumAttrCase<"SYMMETRIC", 1>;
def TFL_MirrorPaddingType : I32EnumAttr<"MirrorPaddingType", "mirror_pad_enum", [
TFL_MIRRORPAD_Reflect, TFL_MIRRORPAD_Symmetric
]> {
let genSpecializedAttr = 0;
let cppNamespace = "::mlir::TFL";
}
def TFL_MirrorPaddingAttr : EnumAttr<TFL_Dialect, TFL_MirrorPaddingType,
"mirror_pad_attr">;
// LSTM Kernel Type attributes
def TFL_LSTM_KT_FULL : I32EnumAttrCase<"FULL", 0>;
def TFL_LSTM_KT_BASIC : I32EnumAttrCase<"BASIC", 1>;
def TFL_LSTMKernelType : I32EnumAttr<"LSTMKernelType", "lstm_kernel_type",
[
TFL_LSTM_KT_FULL, TFL_LSTM_KT_BASIC
]>{
let genSpecializedAttr = 0;
let cppNamespace = "::mlir::TFL";
}
def TFL_LSTMKernelTypeAttr : EnumAttr<TFL_Dialect, TFL_LSTMKernelType,
"lstm_kernel_type_attr">;
def I32ArrayParameter :
AttrOrTypeParameter<"::llvm::ArrayRef<int32_t>", ""> {
let allocator = [{$_dst = $_allocator.copyInto($_self);}];
let cppStorageType = "::llvm::SmallVector<int32_t>";
let parser = "::mlir::TFL::parseI32Array($_parser)";
let printer = "$_printer << '[' << $_self << ']'";
}
def DimensionMetadataAttr : AttrDef<TFL_Dialect, "DimensionMetadata"> {
let mnemonic = "dimension_metadata";
let parameters = (ins
TFL_DimensionTypeAttr:$format,
"int32_t":$dense_size,
I32ArrayParameter:$segments,
I32ArrayParameter:$indices
);
let summary = "Dimension metadata.";
let assemblyFormat = "`<` struct(params) `>`";
}
def SparsityParameterAttr : AttrDef<TFL_Dialect, "SparsityParameter"> {
let mnemonic = "sparsity_parameter";
let parameters = (ins
I32ArrayParameter:$traversal_order,
I32ArrayParameter:$block_map,
ArrayRefParameter<"DimensionMetadataAttr">:$dim_metadata
);
let summary = "Sparsity parameter.";
let assemblyFormat = "`<` struct(params) `>`";
}
def TFL_ConstBytesAttr : AttrDef<TFL_Dialect, "ConstBytes"> {
let summary = "A string attribute representation of compiled bytes";
let description = [{
Syntax Examples:
```mlir
#tfl<const_bytes : "0xDEADBEEF">
```
}];
let mnemonic = "const_bytes";
let parameters = (ins StringRefParameter<"">:$value);
let hasCustomAssemblyFormat = 1;
}
def TFL_ExternalBufferAttr : AttrDef<TFL_Dialect, "ExternalBuffer"> {
let mnemonic = "external_buffer";
let parameters = (ins
"::mlir::StringAttr":$group_name,
"uint64_t":$offset,
"uint64_t":$length,
"::mlir::StringAttr":$packing
);
let summary = "Flatbuffer external buffer metadata.";
let assemblyFormat = "`<` struct(params) `>`";
}
#endif // TFL_OP_ENUMS
@@ -0,0 +1,134 @@
/* Copyright 2020 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.
==============================================================================*/
// This is the operation interface definition file for TensorFlow Lite.
#ifndef TFL_OP_INTERFACES
#define TFL_OP_INTERFACES
include "mlir/IR/OpBase.td"
def TFL_Dialect : Dialect {
let name = "tfl";
let description = [{
The TensorFlow Lite dialect.
This dialect maps to TensorFlow Lite operations.
Invariants:
* All values are of Tensor type (in particular, scalars are
represented using zero-dimensional tensors);
}];
let cppNamespace = "::mlir::TFL";
let useDefaultAttributePrinterParser = 1;
let useDefaultTypePrinterParser = 1;
let extraClassDeclaration = [{
ParseResult parseOneResultSameOperandTypeOp(OpAsmParser &parser,
OperationState &result);
void printOneResultOp(Operation *op, OpAsmPrinter &p);
// Registered hook to materialize a constant operation from a given
// attribute value with the desired resultant type.
Operation *materializeConstant(OpBuilder &builder, Attribute value,
Type type, Location loc) override;
}];
}
//===----------------------------------------------------------------------===//
// TFL op interface for stateful operands.
def TFL_StatefulOp : OpInterface<"StatefulOpInterface"> {
let description = [{
Interface for ops that are stateful and need to identify stateful operands.
Stateful operands correspond to TF's variables semantics. An op that has 1
or more stateful operands is a stateful op.
}];
let methods = [
InterfaceMethod<
[{Returns the indices of stateful operands.}],
"std::vector<int>", "GetStatefulOperands", (ins)
>,
];
}
//===----------------------------------------------------------------------===//
// TFL op interface for sparse operands.
def TFL_SparseOp : OpInterface<"SparseOpInterface"> {
let description = [{
Interface for ops that support sparse computation.
}];
let methods = [
InterfaceMethod<
[{Returns the indices of sparse operands.}],
"std::vector<int>", "GetSparseOperands", (ins)
>,
InterfaceMethod<
[{Returns the supported block size of float sparse operands.}],
"std::vector<std::vector<int>>", "GetFloatBlockSize", (ins)
>,
InterfaceMethod<
[{Returns the supported block size of quantized sparse operands.}],
"std::vector<std::vector<int>>", "GetQuantizedBlockSize", (ins)
>,
];
}
//===----------------------------------------------------------------------===//
// TFL runtime type verification of operand/result types.
def TFL_RuntimeVerification : OpInterface<"TflRuntimeVerifyOpInterface"> {
let description = [{
Interface to verify TFLite runtime op verification.
This verifies that the converted TFLite ops has operand/result type
supported by the TFLite runtime.
}];
let methods = [
StaticInterfaceMethod<
[{Returns whether the op's operands/results are supported by runtime.}],
"LogicalResult", "VerifyTflRuntimeConstraints",
(ins "Operation*":$op, "bool":$emit_error_on_verify_fail)
>,
];
}
//===----------------------------------------------------------------------===//
// TFL arithmetic count interface.
def TFL_ArithmeticCount : OpInterface<"TflArithmeticCountOpInterface"> {
let description = [{
Interface for TFLite ops to calculate arithmetic count (Multiply-Add Count).
}];
let methods = [
StaticInterfaceMethod<
[{Returns an integer representing the op's arithmetic count (Multiply-Add
Count), return -1 if the arithmetic count cannot be determined.}],
"int64_t", "GetArithmeticCount", (ins "Operation*":$op)
>,
];
}
#endif // TFL_OP_INTERFACES
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,66 @@
/* Copyright 2019 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.
==============================================================================*/
// This file defines the operations used in the MLIR TensorFlow Lite dialect.
#ifndef TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_
#define TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_
#include "mlir/Dialect/Traits.h" // from @llvm-project
#include "mlir/IR/Attributes.h" // from @llvm-project
#include "mlir/IR/Builders.h" // from @llvm-project
#include "mlir/IR/BuiltinTypes.h" // from @llvm-project
#include "mlir/IR/Dialect.h" // from @llvm-project
#include "mlir/IR/DialectImplementation.h" // from @llvm-project
#include "mlir/IR/OpImplementation.h" // from @llvm-project
#include "mlir/IR/TypeSupport.h" // from @llvm-project
#include "mlir/IR/Types.h" // from @llvm-project
#include "mlir/Interfaces/DerivedAttributeOpInterface.h" // from @llvm-project
#include "mlir/Interfaces/InferTypeOpInterface.h" // from @llvm-project
#include "mlir/Interfaces/LoopLikeInterface.h" // from @llvm-project
#include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project
#include "mlir/Support/LLVM.h" // from @llvm-project
#include "mlir/Support/TypeID.h" // from @llvm-project
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_dialect.h.inc"
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_enums.h.inc"
#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" // IWYU pragma: keep
#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h"
#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h"
#include "tensorflow/compiler/mlir/lite/utils/utils.h"
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_traits.h"
#define GET_ATTRDEF_CLASSES
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_attrdefs.h.inc"
namespace mlir {
namespace TFL {
typedef TFLDialect TensorFlowLiteDialect;
// The Control type is a token-like value that models control dependencies
class ControlType : public Type::TypeBase<ControlType, Type, TypeStorage> {
public:
using Base::Base;
static constexpr StringLiteral name = "tfl.control";
};
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_interface.h.inc"
} // end namespace TFL
} // end namespace mlir
#define GET_OP_CLASSES
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h.inc"
#endif // TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_
File diff suppressed because it is too large Load Diff