diff --git a/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h b/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h index afc8ae0cd7..250626fd93 100644 --- a/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h +++ b/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h @@ -70,8 +70,8 @@ static const char* const kOrtSessionOptionsConfigAllowIntraOpSpinning = "session static const char* const kOrtSessionOptionsConfigUseORTModelBytesDirectly = "session.use_ort_model_bytes_directly"; // It controls whether qdq is int8 allowed in QDQ format model or not. -// "0": not allowed; "1": allowed. It is used as a forced option when exporting to ort format model do disable certain -// usage of unsupported qdq transformers(in minimal build), such as QDQS8ToU8Transformer. +// "0": not allowed; "1": allowed. It's used as a forced option when exporting to ort model to disable certain usage of +// unsupported qdq transformers (in minimal build), such as QDQS8ToU8Transformer and also passed into several qdq selectors. static const char* const kOrtSessionOptionsQDQIsInt8Allowed = "session.qdqisint8allowed"; // Save information for replaying graph optimizations later instead of applying them directly. diff --git a/onnxruntime/core/optimizer/graph_transformer_utils.cc b/onnxruntime/core/optimizer/graph_transformer_utils.cc index 27025170f4..469a87a774 100644 --- a/onnxruntime/core/optimizer/graph_transformer_utils.cc +++ b/onnxruntime/core/optimizer/graph_transformer_utils.cc @@ -6,6 +6,7 @@ #include #include "core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.h" +#include "core/optimizer/selectors_actions/selector_action_transformer_apply_contexts.h" #include "core/session/onnxruntime_session_options_config_keys.h" #if !defined(ORT_MINIMAL_BUILD) @@ -159,7 +160,7 @@ InlinedVector> GenerateTransformers( const bool disable_quant_qdq = session_options.config_options.GetConfigOrDefault(kOrtSessionOptionsDisableQuantQDQ, "0") == "1"; const bool qdq_is_int8_allowed = - session_options.config_options.GetConfigOrDefault(kOrtSessionOptionsQDQIsInt8Allowed, "1") == "1"; + session_options.config_options.GetConfigOrDefault(kOrtSessionOptionsQDQIsInt8Allowed, QDQIsInt8Allowed() ? "1" : "0") == "1"; #ifndef DISABLE_CONTRIB_OPS const bool enable_gelu_approximation = session_options.config_options.GetConfigOrDefault(kOrtSessionOptionsEnableGeluApproximation, "0") == "1"; @@ -213,7 +214,7 @@ InlinedVector> GenerateTransformers( if (!qdq_is_int8_allowed) { transformers.emplace_back(std::make_unique(cpu_ep)); } - transformers.emplace_back(std::make_unique()); + transformers.emplace_back(std::make_unique(SatApplyContextVariant{}, qdq_is_int8_allowed)); } transformers.emplace_back(std::make_unique(cpu_ep)); diff --git a/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.cc b/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.cc index ab1fcdcec3..b60f6f8de8 100644 --- a/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.cc +++ b/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.cc @@ -204,10 +204,10 @@ SelectorActionRegistry CreateSelectorActionRegistry(bool is_int8_allowed) { } // namespace -QDQSelectorActionTransformer::QDQSelectorActionTransformer(const SatApplyContextVariant& apply_context) +QDQSelectorActionTransformer::QDQSelectorActionTransformer(const SatApplyContextVariant& apply_context, bool is_int8_allowed) : SelectorActionTransformer{ "QDQSelectorActionTransformer", - CreateSelectorActionRegistry(QDQIsInt8Allowed()), + CreateSelectorActionRegistry(is_int8_allowed), apply_context} { } diff --git a/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.h b/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.h index b7d7e026bc..e89d2bba3f 100644 --- a/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.h +++ b/onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selector_action_transformer.h @@ -22,7 +22,7 @@ Transformer that fuses QDQ and fp32 ops into quantized ops. */ class QDQSelectorActionTransformer : public SelectorActionTransformer { public: - QDQSelectorActionTransformer(const SatApplyContextVariant& apply_context = SatApplyContextVariant{}); + QDQSelectorActionTransformer(const SatApplyContextVariant& apply_context = SatApplyContextVariant{}, bool is_int8_allowed = false); }; } // namespace onnxruntime diff --git a/tools/python/util/convert_onnx_models_to_ort.py b/tools/python/util/convert_onnx_models_to_ort.py index 19589ecaf2..4a10de71d2 100644 --- a/tools/python/util/convert_onnx_models_to_ort.py +++ b/tools/python/util/convert_onnx_models_to_ort.py @@ -198,10 +198,11 @@ def parse_args(): 'In particular, specify the value of the "ep.nnapi.partitioning_stop_ops" session ' 'options config entry.') - parser.add_argument('--target_platform', type=str, default='arm', choices=['arm', 'amd64'], + parser.add_argument('--target_platform', type=str, default=None, choices=['arm', 'amd64'], help='Specify the target platform where the exported model will be used.' - 'This parameter can be used to choose between platform specifically related options,' - 'such as QDQIsInt8Allowed or not, NCHWc (amd64) and NHWC (arm) format optimizer level options,etc.') + 'This parameter can be used to choose between platform specific options,' + 'such as QDQIsInt8Allowed(arm), NCHWc (amd64) and NHWC (arm/amd64) format different' + 'optimizer level options,etc.') parser.add_argument('model_path_or_dir', type=pathlib.Path, help='Provide path to ONNX model or directory containing ONNX model/s to convert. ' @@ -235,6 +236,8 @@ def convert_onnx_models_to_ort(): if args.target_platform == 'arm': session_options_config_entries["session.qdqisint8allowed"] = "1" + else: + session_options_config_entries["session.qdqisint8allowed"] = "0" for optimization_level in args.optimization_level: print(f"Converting models and creating configuration file for optimization level '{optimization_level}'")