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

【Infer Symbolic Shape No.120】[BUAA] affine_grid #67364

Merged
merged 12 commits into from
Aug 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// distributed under the License is distributed on an "AS IS" BASIS,affine
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Expand Down Expand Up @@ -85,13 +85,6 @@ std::vector<symbol::DimExpr> GetRealPadding(
return real_padding;
}

// bool AffineGridOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }

symbol::ShapeOrDataDimExprs Pool2dRawInferSymbolicShape(
pir::Operation *op,
const std::vector<symbol::DimExpr> &kernel_size,
Expand Down Expand Up @@ -228,6 +221,60 @@ symbol::ShapeOrDataDimExprs Pool2dRawInferSymbolicShape(
namespace paddle::dialect {
using paddle::dialect::details::CreateShapeOrDataForXShape;

bool AffineGridOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &input_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
std::vector<symbol::DimExpr> input_dims = input_shape_or_data.shape();

const auto &attributes = op->attributes();
int output_shape_size;
std::vector<symbol::DimExpr> output_shape_data;
if (attributes.find("output_shape") != attributes.end()) {
std::vector<int64_t> output_shape =
op->attribute<paddle::dialect::IntArrayAttribute>("output_shape")
.data()
.GetData();
output_shape_size = output_shape.size();
for (const auto &i : output_shape) {
output_shape_data.push_back(symbol::DimExpr{i});
}
} else if (op->operand_source(1)) {
const auto &output_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));

output_shape_data = details::GetOrCreateExprVecFromData(
output_shape_or_data, infer_context);
output_shape_size = output_shape_data.size();
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"The input arguments must have the shape of output, please check!"));
}

std::vector<symbol::DimExpr> output_dims;
output_dims.push_back(input_dims[0]); // N

if (output_shape_size == 4) {
// N * H * W * 2
output_dims.push_back(output_shape_data[2]); // H
output_dims.push_back(output_shape_data[3]); // W
output_dims.push_back(symbol::DimExpr(2)); // 2
} else {
// N * D * H * W * 3
output_dims.push_back(output_shape_data[2]); // D
output_dims.push_back(output_shape_data[3]); // H
output_dims.push_back(output_shape_data[4]); // W
output_dims.push_back(symbol::DimExpr(3)); // 3
}

infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{
symbol::TensorShapeOrDataDimExprs(output_dims)});

return true;
}

bool AllOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
const auto &axis = details::GetVectorAttr(op, "axis");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "paddle/pir/include/dialect/shape/utils/shape_analysis.h"

namespace paddle::dialect {
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(AffineGrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(AffineGrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(All)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amin)
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
param : [input, output_shape, align_corners]
data_type : input
backward : affine_grid_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : all
args : (Tensor x, int64_t[] axis={}, bool keepdim=false)
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_affine_grid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def setUp(self):
}

def test_check_output(self):
self.check_output(check_pir=True)
self.check_output(check_pir=True, check_symbol_infer=False)

def test_check_grad_normal(self):
self.check_grad(
Expand Down