// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include #include #include // implementation details of the transpose optimizer API defined in optimizer_api.h. // This exposes some internals so they can be extended as needed. #include "optimizer_api.h" namespace onnx_transpose_optimization { struct OptimizerCtx; // Struct containing information passed to op handlers. struct HandlerArgs { OptimizerCtx& ctx; api::NodeRef& transpose; // Transpose node we are considering moving past `node` api::NodeRef& node; const std::vector& perm; // perm attribute from Transpose const std::vector& perm_inv; // inverse of perm. // Cached result from calling HandlerInfo.transposible_inputs_fn std::vector& transposible_inputs; const std::unordered_set& outputs_leading_to_transpose; }; // Each op handler points to a (potentially shared) function for determining which input indices are eligible for // optimization. Handlers are only called if a transpose is on an eligible index, and if the optimization heuristics // predict that pushing the transpose will be beneficial. Most of the time this function returns a static value, but // for Sum/Concat/QLinearConcat it needs to be dynamic. using TransposibleInputsFn = std::vector (*)(OptimizerCtx& ctx, api::NodeRef& node); using HandlerFunction = bool (*)(HandlerArgs& args); struct HandlerInfo { TransposibleInputsFn transposible_inputs_fn; HandlerFunction handler_fn; // Does the handler have to transpose outputs? Used for cost estimation. bool transposes_outputs = true; }; using NodeIdToInputIdxsMap = std::unordered_map>; struct OptimizerCtx { int64_t opset; api::GraphRef& graph; const std::string provider_type; CostCheckFn cost_check_fn; // Handlers for ops that are not in the ONNX opset, or for ONNX ops where special handling is required. // If a handler is not found in this map, the default handlers will be used. const HandlerMap& extended_handlers; }; /// /// TransposibleInputsFn that returns the first input index. /// /// {0} inline std::vector FirstInput(OptimizerCtx&, api::NodeRef&) { return {0}; } std::vector InvertPerm(gsl::span perm); // Transpose all inputs and all outputs bool HandleSimpleNode(HandlerArgs& args); // Node with all inputs broadcastable bool HandleSimpleNodeBroadcast(HandlerArgs& args); // Transposes all inputs and all outputs. Updates axis attribute. bool HandleSimpleNodeWithAxis(HandlerArgs& args, std::optional default_axis = std::nullopt); bool HandleConcat(HandlerArgs& args); bool HandleSoftHardMax(HandlerArgs& args); // base handlers that are used by extended handlers. add from transpose_optimizer.cc as needed. bool HandleReduceOps(HandlerArgs& args); bool HandleReshape(HandlerArgs& args); bool HandleResize([[maybe_unused]] HandlerArgs& args); bool CanTransposeInput(const api::GraphRef& graph, const api::NodeRef& node, size_t i, const std::vector& perm_inv); bool TransposeInput(api::GraphRef& graph, api::NodeRef& node, size_t i, const std::vector& perm, const std::vector& perm_inv); // Transposes specified inputs according to perm. // NOTE: if a Transpose is expected to be above an input to this node, use the inverse of its permutation to cancel it. void TransposeInputs(OptimizerCtx& ctx, api::NodeRef& node, const std::vector& perm, const std::vector& input_indices); inline void TransposeFirstInput(OptimizerCtx& ctx, api::NodeRef& node, const std::vector& perm) { std::vector indices{0}; TransposeInputs(ctx, node, perm, indices); } // Inserts a Transpose op on the ith output of a node. Returns the new, transposed output. // Updates shape information assuming that the output from the node will have a transposed shape (using perm_inv) // but the overall (returned) output will match the initial shape. std::string_view TransposeOutput(api::GraphRef& graph, api::NodeRef& node, size_t i, const std::vector& perm, const std::vector& perm_inv); void TransposeOutputs(OptimizerCtx& ctx, api::NodeRef& node, const std::vector& perm); /// /// Computes the perm attribute needed to transpose a tensor from channel-first ordering (NCHW or NCD...D) to /// channel-last ordering (NHWC or ND...DC). rank must be >= 2. /// /// Rank of the tensor /// perm attribute to transpose from channel first to channel last. Ex: [0, 2, 3, 1] std::vector ChannelFirstToLastPerm(size_t rank); /// /// Computes the perm attribute needed to transpose a tensor from channel-last ordering (NHWC or ND...DC) to /// channel-last ordering (NCHW or NCD...D). rank must be >= 2. /// /// Rank of the tensor /// perm attribute to transpose from channel last to channel first. Ex: [0, 3, 1, 2] std::vector ChannelLastToFirstPerm(size_t rank); /// /// Updates the axis attribute of QuantizeLinear or DequantizeLinear operators according to the /// provided transposition permutation. Only applies to per-axis (de)quantization. /// /// The graph containing the node /// The transpose permutation /// The QuantizeLinear or DequantizeLinear node /// True if the axis attribute remains valid bool TransposeQuantizeDequantizeAxis(const api::GraphRef& graph, const std::vector& perm, api::NodeRef& node); } // namespace onnx_transpose_optimization