24 struct DepthwiseConv2DIsMul :
public OpRewritePattern<tosa::DepthwiseConv2DOp> {
28 LogicalResult matchAndRewrite(tosa::DepthwiseConv2DOp op,
30 Value input = op.getInput();
31 Value weight = op.getWeight();
32 ShapedType inputType = cast<ShapedType>(input.
getType());
33 ShapedType weightType = cast<ShapedType>(weight.
getType());
34 ShapedType resultType = cast<ShapedType>(op.getOutput().getType());
36 if (!(inputType.hasStaticShape() && weightType.hasStaticShape() &&
37 resultType.hasStaticShape())) {
41 if (!llvm::all_of(op.getStride(), [](int64_t v) { return v == 1; }))
46 if (weightShape[0] != 1 || weightShape[1] != 1) {
50 Type inputETy = inputType.getElementType();
51 Type weightETy = weightType.getElementType();
56 FailureOr<int64_t> maybeIZp = op.getInputZeroPoint();
59 op,
"input zero point cannot be statically determined");
61 FailureOr<int64_t> maybeWZp = op.getWeightZeroPoint();
64 op,
"weight zero point cannot be statically determined");
66 int64_t iZp = *maybeIZp;
67 int64_t wZp = *maybeWZp;
68 if (op.verifyInputZeroPoint(iZp).failed())
70 op,
"input zero point must be zero for non-int8 integer types");
71 if (op.verifyWeightZeroPoint(wZp).failed())
73 op,
"weight zero point must be zero for non-int8 integer types");
78 inputShape[0], inputShape[1], inputShape[2], inputShape[3], 1};
81 dyn_cast<RankedTensorType>(input.
getType()).getElementType());
82 auto revisedInputShapeValue =
84 input = tosa::ReshapeOp::create(rewriter, op.getLoc(), inputType, input,
85 revisedInputShapeValue)
88 Type resultETy = resultType.getElementType();
90 if (inputETy != resultETy) {
91 inputType = inputType.clone(resultETy);
92 input = tosa::CastOp::create(rewriter, op.getLoc(), inputType, input);
95 if (weightETy != resultETy) {
96 weightType = weightType.clone(resultETy);
97 weight = tosa::CastOp::create(rewriter, op.getLoc(), weightType, weight);
100 if (iZp != 0 || wZp != 0) {
102 auto applyZp = [&](
Value val, int64_t zp) ->
Value {
105 auto ety = cast<ShapedType>(val.
getType()).getElementType();
106 std::vector<int64_t> shape(cast<ShapedType>(val.
getType()).getRank(),
111 auto zpVal = tosa::ConstOp::create(rewriter, op.getLoc(), zpTy, zpAttr);
112 return tosa::SubOp::create(rewriter, op.getLoc(), val.
getType(), val,
116 input = applyZp(input, iZp);
117 weight = applyZp(weight, wZp);
123 pad[it.index() + 2] = it.value();
125 if (llvm::any_of(pad, [](int64_t p) {
return p != 0; })) {
129 for (
int i = 0, s = pad.size(); i < s; ++i) {
130 if (newShape[i / 2] != ShapedType::kDynamic) {
131 newShape[i / 2] += pad[i];
140 tosa::ConstOp::create(rewriter, op->getLoc(), padTy, padAttr);
142 input = tosa::PadOp::create(rewriter, op->getLoc(), inputType, input,
148 inputType.getDimSize(0), inputType.getDimSize(1),
149 inputType.getDimSize(2), inputType.getDimSize(3), weightShape[3]};
152 dyn_cast<RankedTensorType>(weight.
getType()).getElementType());
154 if (
EqualizeRanks(rewriter, op.getLoc(), input, weight).failed()) {
163 tosa::ConstOp::create(rewriter, op.getLoc(), shiftType, shiftZeroAttr);
164 Value mulValue = tosa::MulOp::create(rewriter, op.getLoc(), mulShapeType,
165 input, weight, constZero)
169 auto outputShape = cast<ShapedType>(op.getOutput().getType()).getShape();
172 dyn_cast<RankedTensorType>(input.
getType()).getElementType());
173 auto outputShapeValue =
175 Value outputValue = tosa::ReshapeOp::create(
176 rewriter, op.getLoc(), outputShapeType, mulValue, outputShapeValue);
178 Value bias = op.getBias();
179 if (
EqualizeRanks(rewriter, op.getLoc(), outputValue, bias).failed()) {
195 patterns.add<DepthwiseConv2DIsMul>(ctx);
Attributes are known-constant values of operations.
IntegerAttr getIntegerAttr(Type type, int64_t value)
TypedAttr getZeroAttr(Type type)
MLIRContext * getContext() const
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
MLIRContext is the top-level object for a collection of MLIR operations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
void populateTosaDecomposeDepthwise(MLIRContext *ctx, RewritePatternSet &patterns)
LogicalResult EqualizeRanks(PatternRewriter &rewriter, Location loc, Value &input1, Value &input2)
Common code to create the reshape op where necessary to make the rank of two values equal.
Value getTosaConstShape(ImplicitLocOpBuilder &builder, llvm::ArrayRef< int64_t > shape)
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...