From 678eecb52567351d45decf84c285126c7013e0d9 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Thu, 23 Oct 2025 18:53:21 +0800 Subject: [PATCH] [CINN] Fix bug of infer_symbol_shape for crop op (#75992) * fix bug of infer_symbol_shape for crop op * fix unittest --- .../group_merge/single_op_fallback_to_phi.cc | 1 + .../infer_symbolic_shape/unary_infer_sym.cc | 3 ++- .../pir/cinn/inference/test_llama_postprocess.py | 15 ++++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/paddle/cinn/hlir/dialect/operator/transforms/group_merge/single_op_fallback_to_phi.cc b/paddle/cinn/hlir/dialect/operator/transforms/group_merge/single_op_fallback_to_phi.cc index 8b5842e15d5210..a71352d0ddb4e6 100644 --- a/paddle/cinn/hlir/dialect/operator/transforms/group_merge/single_op_fallback_to_phi.cc +++ b/paddle/cinn/hlir/dialect/operator/transforms/group_merge/single_op_fallback_to_phi.cc @@ -150,6 +150,7 @@ class FusionOpPattern : public pir::OpRewritePattern { {paddle::dialect::AssignOut_Op::name(), &FusionOpPattern::AssignOutOpPattern}, {paddle::dialect::CastOp::name(), &FusionOpPattern::CastOpPattern}, + {cinn::dialect::ConcatOp::name(), &FusionOpPattern::ConcatOpPattern}, }; return handler_map; } diff --git a/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/unary_infer_sym.cc b/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/unary_infer_sym.cc index ab9e020aea41ea..2996a714caf266 100644 --- a/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/unary_infer_sym.cc +++ b/paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/unary_infer_sym.cc @@ -830,7 +830,8 @@ bool CropOpInferSymbolicShape(pir::Operation *op, for (size_t i = 0; i < in_shape.size(); ++i) { if (in_shape[i].isa()) { - if (x_shape[i].Get() == 0) { // x is 0-size + if (x_shape[i].isa() && + x_shape[i].Get() == 0) { // x is 0-size out_dims.push_back(symbol::DimExpr(x_shape[i])); } else if (in_shape[i].Get() == -1) { out_dims.push_back(symbol::DimExpr(x_shape[i] - offsets[i])); diff --git a/test/ir/pir/cinn/inference/test_llama_postprocess.py b/test/ir/pir/cinn/inference/test_llama_postprocess.py index f9b3adb7b71398..36e1828cc4a1b4 100644 --- a/test/ir/pir/cinn/inference/test_llama_postprocess.py +++ b/test/ir/pir/cinn/inference/test_llama_postprocess.py @@ -15,6 +15,8 @@ import unittest from os.path import dirname +import numpy as np + import paddle import paddle.nn.functional as F from paddle import nn @@ -93,8 +95,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, 5) - utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 5}) + utils.check_jit_kernel_number(static_fn, 4) + utils.check_jit_kernel_structure(static_fn, {utils.JIT_KERNEL_NAME: 4}) def eval(self, use_cinn): paddle.seed(2024) @@ -114,11 +116,10 @@ def eval(self, use_cinn): def test_eval(self): dy_out = self.eval(use_cinn=False) cinn_out = self.eval(use_cinn=True) - # TODO(Aurelius84): fix the precision with inf - # for i in range(len(dy_out)): - # np.testing.assert_allclose( - # cinn_out[i].numpy(), dy_out[i].numpy(), atol=1e-6, rtol=1e-6 - # ) + for i in range(len(dy_out)): + np.testing.assert_allclose( + cinn_out[i].numpy(), dy_out[i].numpy(), atol=1e-6, rtol=1e-6 + ) if __name__ == '__main__':