//
// Copyright (C) 2022-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "vpux/compiler/dialect/IE/IR/ops/specialized.hpp"
#include "vpux/compiler/utils/error.hpp"

using namespace vpux;

mlir::LogicalResult vpux::IE::ReorgYoloOp::verify() {
    if (getStride() <= 0) {
        return errorAt(*this, "Stride should be a natural number, while it is {0}", getStride());
    }
    return mlir::success();
}

mlir::LogicalResult vpux::IE::ReorgYoloOp::inferReturnTypeComponents(
        mlir::MLIRContext* ctx, std::optional<mlir::Location> optLoc, mlir::ValueShapeRange operands,
        mlir::DictionaryAttr attrs, mlir::OpaqueProperties prop, mlir::RegionRange,
        SmallVectorImpl<mlir::ShapedTypeComponents>& inferredReturnShapes) {
    const auto loc = optLoc.value_or(mlir::UnknownLoc::get(ctx));

    IE::ReorgYoloOpAdaptor reorgYolo(operands, attrs, prop);
    if (mlir::failed(reorgYolo.verify(loc))) {
        return mlir::failure();
    }

    const auto inType = mlir::cast<mlir::ShapedType>(reorgYolo.getInput().getType());
    auto stride = reorgYolo.getStride();

    if (stride <= 0) {
        return errorAt(loc, "Stride should be a natural number");
    }
    if (inType.getShape()[2] % stride != 0) {
        return errorAt(loc, "Input H should be divisible by stride.");
    }
    if (inType.getShape()[3] % stride != 0) {
        return errorAt(loc, "Input W should be divisible by stride.");
    }
    if (inType.getShape()[1] < stride * stride) {
        return errorAt(loc, "Input C >= (stride*stride) is required.");
    }

    SmallVector<int64_t> outputShape{inType.getShape()[0], inType.getShape()[1]};
    for (size_t i = 2; i < inType.getShape().size(); i++) {
        outputShape.push_back(inType.getShape()[i] / stride);
        outputShape[1] *= stride;
    }

    inferredReturnShapes.emplace_back(outputShape, inType.getElementType());
    return mlir::success();
}
