Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions paddle/cinn/operator_fusion/pattern_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ void PatternGraph<T>::HorizontalFusion() {
StmtPatternGraphMatcher<ReduceTreePattern<T>>>,
LiftToHorizontalFusionPatternOperation>(this);

GraphTransformer<NodePairPattern,
T,
HorizontalFusionConstrain<T>,
HorizontalFusionOperation>(this);
GraphTransformer<
NodePairPattern,
T,
And<HorizontalFusionConstrain<T>, HorizontalCheckMiddleOutputVar<T>>,
HorizontalFusionOperation>(this);
}

template <typename T>
Expand Down
44 changes: 41 additions & 3 deletions paddle/cinn/operator_fusion/pattern_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ struct CanFuseReduceTreeAndTrivialMatcher {
}
};

template <typename T>
struct HorizontalCheckMiddleOutputVar {
bool operator()(const PatternGraph<T>& graph,
const PatternNodePtr<T>& lhs,
const PatternNodePtr<T>& rhs) {
for (const auto& i : lhs->downstream()) {
if (i == rhs) return false;
}
for (const auto& i : lhs->upstream()) {
if (i == rhs) return false;
}
return true;
}
};

template <typename T>
struct HorizontalFusionConstrain {
bool operator()(const PatternGraph<T>& graph,
Expand Down Expand Up @@ -381,11 +396,34 @@ struct DownstreamSmallerThan {
}
};

template <typename A, typename B>
struct And {
template <typename... Args>
struct And {};

template <typename A>
struct And<A> {
template <typename T>
bool operator()(const PatternGraph<T>& graph, const PatternNodePtr<T>& node) {
return A()(graph, node);
}
template <typename T>
bool operator()(const PatternGraph<T>& graph,
const PatternNodePtr<T>& lhs,
const PatternNodePtr<T>& rhs) {
return A()(graph, lhs, rhs);
}
};

template <typename A, typename... Args>
struct And<A, Args...> {
template <typename T>
bool operator()(const PatternGraph<T>& graph, const PatternNodePtr<T>& node) {
return A()(graph, node) && B()(graph, node);
return A()(graph, node) && And<Args...>()(graph, node);
}
template <typename T>
bool operator()(const PatternGraph<T>& graph,
const PatternNodePtr<T>& lhs,
const PatternNodePtr<T>& rhs) {
return A()(graph, lhs, rhs) && And<Args...>()(graph, lhs, rhs);
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/ir/pir/cinn/inference/test_llama_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def prepare_data(self):
self.input_ids = paddle.randint(0, 512, [1, 32], dtype="int64")

def check_jit_kernel_info(self, static_fn):
utils.check_jit_kernel_number(static_fn, 4)
utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 4})
utils.check_jit_kernel_number(static_fn, 7)
utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 7})

def eval(self, use_cinn):
paddle.seed(2024)
Expand Down
4 changes: 2 additions & 2 deletions test/ir/pir/cinn/test_horizontal_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def prepare_data(self):
self.x.stop_gradient = True

def check_jit_kernel_info(self, static_fn):
utils.check_jit_kernel_number(static_fn, 1)
utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 1})
utils.check_jit_kernel_number(static_fn, 2)
utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 2})

def eval(self, use_cinn):
net = HorizontalSubGraph()
Expand Down