Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CINN / Symbolic]Fix gather infer symbolic bugs #63973

Merged
merged 4 commits into from
Apr 30, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,22 @@ bool GatherOpInferSymbolicShape(
return numel;
}();

const auto &axis_shape_or_data =
shape_analysis->GetShapeOrDataForValue(op->operand_source(2));
int axis = 0;
const auto &attributes = op->attributes();
if (op->HasAttribute("axis")) { // CINN Dialect
axis = attributes.at("axis").dyn_cast<pir::Int32Attribute>().data();
} else {
PADDLE_ENFORCE_EQ(
op->num_operands() == 3,
true,
phi::errors::InvalidArgument(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后续不建议使用phi了,推荐使用::common::errors

"in GatherOpInferSymbolicShape: The number of operands should be "
"3 when the axis is not set."));
const auto &axis_shape_or_data =
shape_analysis->GetShapeOrDataForValue(op->operand_source(2));
axis =
static_cast<int>(axis_shape_or_data.data().value()[0].Get<int64_t>());
}

const std::vector<symbol::DimExpr> &input_sym_shape =
input_shape_or_data.data().has_value()
Expand All @@ -248,8 +262,6 @@ bool GatherOpInferSymbolicShape(
? index_shape_or_data.data().value()
: index_shape_or_data.shape();

int axis =
static_cast<int>(axis_shape_or_data.data().value()[0].Get<int64_t>());
if (axis < 0) axis += input_sym_shape.size();

const auto &out_sym_shape = [&] {
Expand Down
73 changes: 37 additions & 36 deletions test/ir/pir/cinn/symbolic/test_cinn_transform_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,43 @@ def test_eval(self):
)


# class TestGatherAxisPosSymbolic(unittest.TestCase):
# def setUp(self):
# paddle.seed(2022)
# self.prepare_data()
#
# def prepare_data(self):
# self.shape = [None, 4 ]
# self.x = paddle.randn(self.shape, dtype="float32")
# self.x.stop_gradient = True
# self.index = paddle.to_tensor([1])
# self.index.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})
#
# def eval(self, use_cinn):
# net = GatherLayerAxisPos()
# input_spec = [
# InputSpec(shape=[None, 4], dtype='float32'),
# InputSpec(shape=[1], dtype='int32'),
# ]
# net = utils.apply_to_static(net, use_cinn, input_spec)
# net.eval()
# out = net(self.x, self.index)
# if use_cinn:
# self.check_jit_kernel_info(net.forward)
# return out
#
# def test_eval(self):
# cinn_out = self.eval(use_cinn=True)
# dy_out = self.eval(use_cinn=False)
# np.testing.assert_allclose(
# cinn_out.numpy(), dy_out.numpy(), atol=1e-6, rtol=1e-6
# )
#
class TestGatherAxisPosSymbolic(unittest.TestCase):
def setUp(self):
paddle.seed(2022)
self.prepare_data()

def prepare_data(self):
self.shape = [32, 4]
self.x = paddle.randn(self.shape, dtype="float32")
self.x.stop_gradient = True
self.index = paddle.to_tensor([1])
self.index.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})

def eval(self, use_cinn):
net = GatherLayerAxisPos()
input_spec = [
InputSpec(shape=[None, 4], dtype='float32'),
InputSpec(shape=[1], dtype='int32'),
]
net = utils.apply_to_static(net, use_cinn, input_spec)
net.eval()
out = net(self.x, self.index)
if use_cinn:
self.check_jit_kernel_info(net.forward)
return out

def test_eval(self):
cinn_out = self.eval(use_cinn=True)
dy_out = self.eval(use_cinn=False)
np.testing.assert_allclose(
cinn_out.numpy(), dy_out.numpy(), atol=1e-6, rtol=1e-6
)


class TestGatherAxisNegStatic(unittest.TestCase):
def setUp(self):
paddle.seed(2022)
Expand Down