onnxruntime
7881e011 - Validate initializer types for transpose optimizer (#32647)

Commit
9 days ago
Validate initializer types for transpose optimizer (#32647) This pull request improves the robustness and correctness of the transpose optimization logic in ONNX Runtime by adding stricter type checks and handling unexpected or unsupported tensor data types gracefully. It also expands the test suite to cover these new behaviors, ensuring that optimizations are only applied when input types are as expected. **Key changes include:** ### Type Safety and Robustness * Updated utility functions like `DataInt64`, `DataInt32`, and `TensorIntData` to return `std::optional` and check tensor data types before accessing data, returning `std::nullopt` if the type does not match expectations. This prevents undefined behavior when tensor types are incorrect. [[1]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL34-R49) [[2]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdR2154-R2181) * Added explicit data type checks in optimization handlers (e.g., for Reduce, Slice, Reshape, and Tile ops) to ensure that only supported types (typically `INT32` or `INT64`) are processed; handlers now bail out early if an unexpected type is encountered. [[1]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL1951-R1963) [[2]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2199-R2224) [[3]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2645-R2684) ### Defensive Programming in Optimization Handlers * Modified all usage of data extraction helpers to properly handle and propagate `std::nullopt` cases, ensuring that optimizations are not applied when the input constant tensors are of the wrong type or missing. [[1]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL1977-R1992) [[2]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2210-R2239) [[3]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2223-R2262) [[4]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2306-R2353) [[5]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdR2572-R2577) [[6]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2554-R2592) ### Testing Improvements * Added utility `ReplaceInitializerWithInt8` to allow tests to inject initializers with unsupported data types, simulating error cases. * Added new tests (`TestSliceUnexpectedAxesTypeNoOpt`, `TestReshapeUnexpectedShapeTypeNoOpt`) that verify the optimizer does not apply transformations when tensor data types are not as expected, increasing test coverage for edge cases. [[1]](diffhunk://#diff-4d8d2dcea7a3e4f5d77f7013252af876e48fc00058ee5b3d4ae6eb0b02b6bbc1R2067-R2102) [[2]](diffhunk://#diff-4d8d2dcea7a3e4f5d77f7013252af876e48fc00058ee5b3d4ae6eb0b02b6bbc1R4392-R4432) These changes collectively ensure that the transpose optimizer only modifies the graph when it is safe and valid to do so, and that it gracefully skips optimization when encountering unsupported or unexpected tensor types. **References:** [[1]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL34-R49) [[2]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL1951-R1963) [[3]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL1977-R1992) [[4]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdR2154-R2181) [[5]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2199-R2224) [[6]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2210-R2239) [[7]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2223-R2262) [[8]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2306-R2353) [[9]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdR2572-R2577) [[10]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2554-R2592) [[11]](diffhunk://#diff-add01cb10f69ee3e3272b7508ee68ebb98dbbd2060b1385efa7f6f1c1bbe78bdL2645-R2684) [[12]](diffhunk://#diff-4d8d2dcea7a3e4f5d77f7013252af876e48fc00058ee5b3d4ae6eb0b02b6bbc1R47-R59) [[13]](diffhunk://#diff-4d8d2dcea7a3e4f5d77f7013252af876e48fc00058ee5b3d4ae6eb0b02b6bbc1R2067-R2102) [[14]](diffhunk://#diff-4d8d2dcea7a3e4f5d77f7013252af876e48fc00058ee5b3d4ae6eb0b02b6bbc1R4392-R4432)
Author
Parents
Loading