[GPU][MLIR][2/3] Add 'request_input_order' mechanism to MLIR converters #39622
The new request-to-layout path lacks regression coverage: mlir_primitive_layouts_test.cpp sets required_input_fmts directly, bypassing memref stride generation and input-order remapping. Add tests that request a static rank-3 order and verify the strides and propagated primitive format, index remapping after splat/unused inputs are removed, and fallback for conflicting or unsupported requests, including dynamic shapes. The Testing section of src/common/transformations/docs/writing_transformations.md requires coverage for behavioral changes; please check the remaining new branches against that requirement too.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The new request-to-layout path lacks regression coverage for stride generation, input-order remapping, and fallback behavior for conflicting or unsupported requests, including dynamic shapes.
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
This PR lets GPU MLIR converters request physical input layouts while keeping their generated tensor operations layout-independent. It connects the layout enforcement from #39423 to the compressed grouped-matmul support planned in #39624.
Changes:
MLIROp into required GPU primitive formats.| File | Description |
| ---- | ----------- |
| src/plugins/intel_gpu/src/plugin/transformations/op/mlir_op.cpp | Preserve input orders during construction and cloning. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/graph_converter.hpp | Declare order-request tracking and lookup. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/graph_converter.cpp | Record requests and discard conflicting orders. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/convert.cpp | Apply memref strides and remap retained input orders. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/common/README.md | Document layout hints and driver responsibilities. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/common/conversion_context.hpp | Expose the input-order callback to converters. |
| src/plugins/intel_gpu/src/plugin/transformations/mlir/common/conversion_context.cpp | Initialize the new callback. |
| src/plugins/intel_gpu/src/plugin/ops/mlir_op.cpp | Translate applied orders into required primitive formats. |
| src/plugins/intel_gpu/include/intel_gpu/primitives/mlir_primitive.hpp | Map dimension orders to supported GPU formats. |
| src/plugins/intel_gpu/include/intel_gpu/op/mlir_op.hpp | Define and expose input-order metadata. |
</details>
<sub>Review effort: Balanced</sub>
this is the place we propagate orders requested by request_input_order to mlir-primitive's input formats
I was initially not sure if it's okay to call cldnn-primitives from the ov-model-level transformations, but then found one example where transpose_fusion does the same. Hope it's okay.
We do it here to make sure that the requested order can be later satisfied at the primitive's level. If we would remove this check then instead of ignoring unsupported requested order we would propagate it to the primitives layer where it would fail at this assertion.
Since we control ov->mlir converters there _shouldn't_ be a case where this check fails, so removing this check at ov-model-level is not critical, if we would decide to do so during the review
Unfortunately we can't add a unit-test for the request_input_order mechanism without linking openvino_intel_gpu_mlir_obj/gc/llvm to ov_gpu_unit_tests and I wouldn't do it right now. This mechanism will be tested by e2e tests with new converters using this feature (starting from #39624). I've manually verified that the new grouped-mm-compressed tests from there work properly.
As for this PR I've added a unit test to verify the format_from_dim_order` primitive's functionality. It doesn't cover the whole flow, but that's all we can test right now
I know it doesn't cover the whole flow, but that's all we can test in this PR
<!-- ccr-overview-v2 -->
🟢 Approval recommended
No active review issues are recorded for the GPU MLIR input-order layout changes.
No open issues 🎉
<details>
<summary><strong>1 resolved since last review</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
isIdentity() is always true here (the arguments come from tensorsToMemRefs), so it can go. The rank and permutation checks can only fail on a malformed request from a converter: let's assert them in request_input_order instead of silently ignoring the hint, so such a bug fails right away. Only the static shape and the cldnn format availability are valid fallbacks
Can we simplify this helper? find_format throws only if there is no format for the order, and a converter requesting such an order is a bug that should fail right away (as the other converter asserts do) instead of silently falling back to the plain layout. Then is_supported_order doesn't need the format check, the has_value() assertion in CreateMLIROp goes away, and the order can be validated once in GraphConverter::request_input_order. The std::nullopt cases in the unit test become EXPECT_THROW
// Returns the cldnn format with the given physical dimension order (e.g. {0, 2, 1} -> byfx).
// Throws if there is no such format. The order must not be empty (the rank comes from it).
static format::type format_from_dim_order(std::vector<size_t> order) {
OPENVINO_ASSERT(!order.empty(), "[GPU] mlir_primitive: empty physical dimension order");
// Ranks below 4 are represented as 4D by cldnn: the missing trailing dimensions keep their place.
for (size_t i = order.size(); i < 4; ++i) {
order.push_back(i);
}
return static_cast<format::type>(format::find_format(order, {}));
}
Minor: it's not clear from this comment what happens when the order is not honored (does the converter have to reorder the input itself?), and driver can be read as the GPU driver. Let's make it explicit
// A hint to pass the source of `input` in the given physical order. The graph converter may ignore it, so the IR
// must always be built on logical tensors, with no reordering of the input: only the function argument layout depends on it.
Let's replace conflicting_orders with an assert: the real cases (GroupedMatMul) don't have conflicting requests, and silently dropping a request would only hide a converter bug. conflicting_orders and #include <set> can be removed from the header then
void GraphConverter::request_input_order(const ov::Input<ov::Node>& input, DimOrder order) {
const auto source = input.get_source_output();
const auto [it, inserted] = requested_orders.emplace(source, order);
OPENVINO_ASSERT(inserted || it->second == order, "Conflicting physical orders are requested for ", source);
}
Do we need the orders on the node? This is a layout detail of the compiled function, which the program already bakes in, and here it is plumbed through the constructor, clone_with_new_inputs and a getter just to reach CreateMLIROp. Can we keep it in MLIRGpuProgram instead (passed to create_gpu_program, read in CreateMLIROp via op->get_program())? MLIROp would stay free from the layout info, and clone_with_new_inputs would be correct by construction since the program is shared. What do you think?
applied in 3de687f5846a26ce2db17b556ed3d09124cef1e4
agree, throw on conflicting orders since: 9eb15537eebb0e6f6c2be2308ab500ce9bbd81b1
Agree. Simplified the format_from_dim_order method and moved the 'cldnn-layout-check' to request_input_order function (inside of validate_requested_order): 3de687f5846a26ce2db17b556ed3d09124cef1e4
Added the 'order-validation' to request_input_order. The is_supported_order still check whether the order is valid for a <i>memref-type</i> (since in request_input_order we validate against ov-input) but now it throws an exception instead of ignoring an invalid order.
0fddb024ada8734772f6a31a6c37ec40e435cfc0
Thanks, it's indeed a more clean approach that I haven't considered!
Moved the input_order map to MLIRGpuProgram in ef6072968520857567de938eb1c1c4befbba8fd1
This is not actual anymore: an order without a cldnn format and conflicting requests now throw instead of being ignored
- The graph-converter honours it only for inputs coming from outside of the subgraph and with a static shape,
otherwise the plain order is used, and is responsible for providing the buffer in the chosen layout. A malformed
order, an order with no cldnn format and conflicting requests for the same input are converter bugs and throw.
Can we remove the default value for input_orders here and in the MLIRGpuProgram constructor? The only caller (and GcGpuProgram) passes it explicitly, and a forgotten argument would silently make all inputs plain
std::shared_ptr<MLIRGpuProgram> create_gpu_program(::mlir::OwningOpRef<::mlir::ModuleOp> module, uint32_t device_id, InputOrders input_orders);
Approved these changes.
v-Golubev
dchigarev
The PR adds a mechanism for an mlir-converter to request an arbitrary physical memory order for a certain input via
ConversionContext.ConversionContextandGraphConvertergot a new methodrequest_input_order(ov::Input<ov::Node>& input, DimOrder order)that take anov::Node's input (eventually becomes an input ofop::MLIROp) and a requested order (e.g.{0, 2, 1}to transpose last two dimensions). GraphConverter records the requested order to apply it to the inputmlir::memreflater when the wholemlir.funcis materialized. It's important to note, that _op-converters_ (that actually callrequest_input_orderfunc) operate on the _mlir-tensor-level_ and thus generate an IR that is layout-agnostic. The generated IR should work properly regardless of whether the requested memory-order was honored by the graph-converter or not.After all nodes in the subgraph are converted to mlir and the final
mlir.funcis materialized, thengraph_to_mlirlogic applies requested memory orders (apply_requested_orders):1. Applying custom strided-layout to the input memref type:
%arg0: memref<512x32xf16>->%arg0: memref<512x32xf16, strided<[1, 512]>>. After the strided-layout was baked into memref it must be honored bycldnn::reorder_inputs, otherwise an assertion inexecute_implfails.2. Pass the orders-map to
MLIRGpuProgramconstructor askeptInputOrdersThen in mlir-primitive's builder (
CreateMLIROp) (that has access toMLIRGpuProgramviaov::op::MLIROpwithkeptInputOrdersinformation) the input orders are converted tocldnn::formats and stored intoprimitive.required_input_fmts(that are then handled by cldnn-layouts propagation logic from #39423)----
<b>Current limitations:</b>
1. An input we request custom memory-layout for must have static shapes. It's true for a case that we want to support in the first place (compressed grouped-matmul from qwen), so it's fine for now. It's not a fundamental limitation, memrefs with custom dynamic strides are just not supported by our graph-compiler pipeline yet.
2. There is no "bubble-up" propagation of the requested layout, meaning it will only be honored if requested for a producer that is located outside of the captured MLIROp. It's also not a problem for our compressed-grouped-matmul-qwen case, since we plan to use for scales/zp that are represented as constants/params that are never part of MLIR subgraph.
<details><summary>img example</summary>
<img width="1234" height="613" alt="image" src="https://github.com/user-attachments/assets/7bb50a1a-ca55-4474-bd85-35b8e74e1f67" />
</details>
----
<b>Follow-up PR</b>:
GroupedMatmulCompressedconverter