docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
CustomSpecializeBatchNorm.h
Go to the documentation of this file.
1#pragma once
2
3#include "mlir/Dialect/Linalg/IR/Linalg.h"
4#include "mlir/Dialect/Tensor/IR/Tensor.h"
5#include "mlir/IR/PatternMatch.h"
6
7namespace mlir {
8namespace linalg {
9
13struct LinalgGenericToLinalgCustomBatchNorm2DNchw : public OpRewritePattern<GenericOp> {
14 using OpRewritePattern<GenericOp>::OpRewritePattern;
15
16 bool is4dIdentityMap(Attribute attr) const;
17 bool is3dIdentityMap(Attribute attr) const;
18 bool checkIndexingMaps(ArrayAttr array_attr) const;
19
20 bool checkIteratorTypes(ArrayAttr array_attr) const;
21
22 template<typename ElemOp>
23 bool isElementwiseRegion(Region& region) const {
24 if (region.getBlocks().size() != 1) {
25 return false;
26 }
27 auto& block = region.getBlocks().front();
28 return (
29 block.getOperations().size() == 2 && llvm::dyn_cast_or_null<ElemOp>(block.getOperations().front()) &&
30 llvm::dyn_cast_or_null<YieldOp>(block.getOperations().back())
31 );
32 }
33
34 template<typename ElemOp>
35 bool checkGenericOp(GenericOp generic_op) const {
36 return (
37 generic_op.getInputs().size() == 2 && generic_op.getOutputs().size() == 1 &&
38 this->checkIndexingMaps(generic_op.getIndexingMaps()) &&
39 this->checkIteratorTypes(generic_op.getIteratorTypes()) &&
40 this->isElementwiseRegion<ElemOp>(generic_op.getRegion())
41 );
42 }
43
44 bool isTypeNCHW(Type type, Type element_type, int64_t n, int64_t c, int64_t h, int64_t w) const;
45 bool isTypeC11(Type type, Type element_type, int64_t c) const;
46 bool isTypeC(Type type, Type element_type, int64_t c) const;
47
48 bool checkExpandShape(tensor::ExpandShapeOp expand_shape_op, int64_t c) const;
49
50 LogicalResult matchAndRewrite(GenericOp generic_op, PatternRewriter& rewriter) const override;
51};
52
53} // namespace linalg
54} // namespace mlir
Definition CustomSpecializeBatchNorm.h:8
Definition CustomPasses.h:6
y = (x - E[x]) / sqrt(Var[x] + eps) * gamma + beta
Definition CustomSpecializeBatchNorm.h:13
bool isTypeC11(Type type, Type element_type, int64_t c) const
bool checkIndexingMaps(ArrayAttr array_attr) const
bool checkExpandShape(tensor::ExpandShapeOp expand_shape_op, int64_t c) const
LogicalResult matchAndRewrite(GenericOp generic_op, PatternRewriter &rewriter) const override
bool checkGenericOp(GenericOp generic_op) const
Definition CustomSpecializeBatchNorm.h:35
bool isTypeC(Type type, Type element_type, int64_t c) const
bool checkIteratorTypes(ArrayAttr array_attr) const
bool isElementwiseRegion(Region &region) const
Definition CustomSpecializeBatchNorm.h:23
bool isTypeNCHW(Type type, Type element_type, int64_t n, int64_t c, int64_t h, int64_t w) const