Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/relax/op/op_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,28 @@ Optional<Array<PrimExpr>> InferBinaryBroadcastShape(const Call& call, const Bloc
const auto* int_dim0 = dim0.as<IntImmNode>();
const auto* int_dim1 = dim1.as<IntImmNode>();
if (int_dim0 != nullptr && int_dim0->value == 1) {
// static dim(1)
output_shape.push_back(dim1);
} else if (int_dim1 != nullptr && int_dim1->value == 1) {
// static dim(1)
output_shape.push_back(dim0);
} else if (analyzer->CanProveEqual(dim0, dim1)) {
// equal static dims or equal symbolic dims
output_shape.push_back(dim0);
} else if (int_dim0 && int_dim1 && int_dim0->value != int_dim1->value) {
// different static dims
ctx->ReportFatal(Diagnostic::Error(call)
<< "In " << call->op << ", the first input shape at dim " << x1_ndim - i
<< " is " << dim0 << " and the second input shape at dim " << x2_ndim - i
<< " is " << dim1 << ", which are not broadcastable.");
} else if (int_dim0 == nullptr && int_dim1) {
// symbolic dim and static dim
output_shape.push_back(dim1);
} else if (int_dim0 && int_dim1 == nullptr) {
// static dim and symbolic dim
output_shape.push_back(dim0);
} else {
// Use simple fallback when shape mismatch.
// Use simple fallback when shapes mismatch.
return std::nullopt;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/python/relax/test_op_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ def test_binary_infer_struct_info_shape_symbolic(binary_arith_op: Callable):
y2 = relax.Var("y", R.Tensor((4, k, m, 1), "float32"))
y3 = relax.Var("y", R.Tensor("float32", ndim=2))
y4 = relax.Var("y", R.Tensor("float32", ndim=-1))
y5 = relax.Var("y", R.Tensor((m, 3), "float32"))
_check_inference(bb, binary_arith_op(x0, y0), relax.TensorStructInfo((m, n), "float32"))
_check_inference(bb, binary_arith_op(x0, y1), relax.TensorStructInfo(dtype="float32", ndim=2))
_check_inference(bb, binary_arith_op(x0, y5), relax.TensorStructInfo((m, 3), "float32"))
_check_inference(bb, binary_arith_op(x1, y0), relax.TensorStructInfo((m, n), "float32"))
_check_inference(bb, binary_arith_op(x1, y2), relax.TensorStructInfo((4, k, m, n), "float32"))
_check_inference(bb, binary_arith_op(x2, y2), relax.TensorStructInfo(dtype="float32", ndim=4))
Expand Down