mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-30 20:18:08 +00:00
Merge remote-tracking branch 'upstream/master' into DmlDev
This commit is contained in:
commit
72908b146e
13 changed files with 84 additions and 25 deletions
|
|
@ -162,6 +162,7 @@ if(NOT WIN32)
|
|||
set(onnxruntime_ENABLE_INSTRUMENT OFF)
|
||||
endif()
|
||||
else()
|
||||
add_compile_definitions(WINVER=0x0601) # Support Windows 7 and newer
|
||||
if(WINDOWS_STORE)
|
||||
# cmake/external/protobuf/src/google/protobuf/compiler/subprocess.cc and onnxruntime/core/platform/windows/env.cc call a bunch of Win32 APIs.
|
||||
# For now, we'll set the API family to desktop globally to expose Win32 symbols in headers; this must be fixed!
|
||||
|
|
|
|||
|
|
@ -190,6 +190,9 @@ target_compile_options(winml_lib_ort PRIVATE /GR- /await /wd4238)
|
|||
target_compile_definitions(winml_lib_ort PRIVATE WINML_ROOT_NS=${winml_root_ns})
|
||||
target_compile_definitions(winml_lib_ort PRIVATE PLATFORM_WINDOWS)
|
||||
target_compile_definitions(winml_lib_ort PRIVATE _SCL_SECURE_NO_WARNINGS) # remove warnings about unchecked iterators
|
||||
if (onnxruntime_WINML_NAMESPACE_OVERRIDE STREQUAL "Windows")
|
||||
target_compile_definitions(winml_lib_ort PRIVATE "BUILD_INBOX=1")
|
||||
endif()
|
||||
|
||||
# Specify the usage of a precompiled header
|
||||
target_precompiled_header(winml_lib_ort pch.h)
|
||||
|
|
@ -622,7 +625,6 @@ add_dependencies(winml_dll winml_api_native)
|
|||
add_dependencies(winml_dll winml_api_native_internal)
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(winml_dll PRIVATE onnxruntime)
|
||||
target_link_libraries(winml_dll PRIVATE re2)
|
||||
target_link_libraries(winml_dll PRIVATE wil)
|
||||
target_link_libraries(winml_dll PRIVATE winml_lib_api)
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ Environment:
|
|||
|
||||
// Configuration macro for use in TRACELOGGING_DEFINE_PROVIDER. The definition
|
||||
// in this file configures the provider as a normal (non-telemetry) provider.
|
||||
#ifndef ENABLE_TELEMETRY
|
||||
#ifndef TraceLoggingOptionMicrosoftTelemetry
|
||||
#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
TraceLoggingOptionGroup(0000000000, 00000, 00000, 0000, 0000, 0000, 0000, 0000, 000, 0000, 0000)
|
||||
// Empty definition for TraceLoggingOptionMicrosoftTelemetry
|
||||
|
|
|
|||
|
|
@ -127,15 +127,17 @@ def optimize_onnx_model_by_ort(onnx_model_path, ort_model_path, use_gpu, overwri
|
|||
|
||||
|
||||
def optimize_onnx_model(onnx_model_path, optimized_model_path, model_type, num_attention_heads, hidden_size, use_gpu,
|
||||
fp16, use_raw_attention_mask, overwrite, model_fusion_statistics):
|
||||
precision, use_raw_attention_mask, overwrite, model_fusion_statistics):
|
||||
if overwrite or not os.path.exists(optimized_model_path):
|
||||
from optimizer import optimize_model
|
||||
from onnx_model_bert import BertOptimizationOptions
|
||||
optimization_options = BertOptimizationOptions(model_type)
|
||||
if use_raw_attention_mask:
|
||||
optimization_options.use_raw_attention_mask()
|
||||
if fp16:
|
||||
if Precision.FLOAT16 == precision:
|
||||
optimization_options.enable_gelu_approximation = True
|
||||
if Precision.INT8 == precision:
|
||||
optimization_options.enable_embed_layer_norm = False
|
||||
|
||||
# Use script to optimize model.
|
||||
# Use opt_level <= 1 for models to be converted to fp16, because some fused op (like FusedGemm) has only fp32 and no fp16.
|
||||
|
|
@ -150,7 +152,7 @@ def optimize_onnx_model(onnx_model_path, optimized_model_path, model_type, num_a
|
|||
only_onnxruntime=False)
|
||||
model_fusion_statistics[optimized_model_path] = opt_model.get_fused_operator_statistics()
|
||||
|
||||
if fp16:
|
||||
if Precision.FLOAT16 == precision:
|
||||
opt_model.convert_model_float32_to_float16()
|
||||
opt_model.save_model_to_file(optimized_model_path)
|
||||
else:
|
||||
|
|
@ -215,7 +217,7 @@ def export_onnx_model(model_name, opset_version, use_external_data_format, model
|
|||
optimized_model_path = get_onnx_file_path(onnx_dir, model_name, len(input_names), True, use_gpu, precision,
|
||||
False, use_external_data_format)
|
||||
optimize_onnx_model(onnx_model_path, optimized_model_path, model_type, config.num_attention_heads,
|
||||
config.hidden_size, use_gpu, precision == Precision.FLOAT16, use_raw_attention_mask,
|
||||
config.hidden_size, use_gpu, precision, use_raw_attention_mask,
|
||||
overwrite, model_fusion_statistics)
|
||||
|
||||
onnx_model_path = optimized_model_path
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
import logging
|
||||
import torch
|
||||
import onnx
|
||||
import os
|
||||
from transformers.modeling_utils import Conv1D
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -34,6 +35,12 @@ def conv1d_to_linear(model):
|
|||
conv1d_to_linear(module)
|
||||
|
||||
|
||||
def _get_size_of_pytorch_model(model):
|
||||
torch.save(model.state_dict(), "temp.p")
|
||||
size = os.path.getsize("temp.p")/(1024*1024)
|
||||
os.remove('temp.p')
|
||||
return size
|
||||
|
||||
class QuantizeHelper:
|
||||
@staticmethod
|
||||
def quantize_torch_model(model, dtype=torch.qint8):
|
||||
|
|
@ -43,11 +50,15 @@ class QuantizeHelper:
|
|||
TODO: mix of in-place and return, but results are different
|
||||
'''
|
||||
conv1d_to_linear(model)
|
||||
return torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=dtype)
|
||||
quantized_model = torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=dtype)
|
||||
logger.info(f'Size of full precision Torch model(MB):{_get_size_of_pytorch_model(model)}')
|
||||
logger.info(f'Size of quantized Torch model(MB):{_get_size_of_pytorch_model(quantized_model)}')
|
||||
return quantized_model
|
||||
|
||||
@staticmethod
|
||||
def quantize_onnx_model(onnx_model_path, quantized_model_path):
|
||||
from onnxruntime.quantization import quantize, QuantizationMode
|
||||
logger.info(f'Size of full precision ONNX model(MB):{os.path.getsize(onnx_model_path)/(1024*1024)}')
|
||||
onnx_opt_model = onnx.load(onnx_model_path)
|
||||
quantized_onnx_model = quantize(onnx_opt_model,
|
||||
quantization_mode=QuantizationMode.IntegerOps,
|
||||
|
|
@ -55,3 +66,4 @@ class QuantizeHelper:
|
|||
force_fusions=True)
|
||||
onnx.save(quantized_onnx_model, quantized_model_path)
|
||||
logger.info(f"quantized model saved to:{quantized_model_path}")
|
||||
logger.info(f'Size of quantized ONNX model(MB):{os.path.getsize(quantized_model_path)/(1024*1024)}')
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ GraphAugmenter::GraphDefs BertLoss::operator()(const Graph& graph, const LossFun
|
|||
"Reshape_label"));
|
||||
|
||||
std::vector<AttributeProto> attrs;
|
||||
attrs.push_back(ONNX_NAMESPACE::MakeAttribute("ignore_index", static_cast<int64_t>(-1)));
|
||||
attrs.push_back(ONNX_NAMESPACE::MakeAttribute("ignore_index", static_cast<int64_t>(0)));
|
||||
attrs.push_back(ONNX_NAMESPACE::MakeAttribute("reduction", "mean"));
|
||||
|
||||
new_nodes.emplace_back(NodeDef("SoftmaxCrossEntropyLoss",
|
||||
|
|
|
|||
|
|
@ -49,15 +49,15 @@ Status PrepareForTrainingCompute(const TensorShape& input_shape, int num_outputs
|
|||
// populate split_sizes with the same size for each output
|
||||
split_sizes = std::vector<int64_t>(static_cast<size_t>(num_outputs), split_dim_size / num_outputs);
|
||||
} else {
|
||||
if (split_sizes_values.size() != static_cast<size_t>(num_outputs) || split_size_sum != split_dim_size)
|
||||
if (split_sizes_values.size() != static_cast<size_t>(num_outputs) || split_size_sum != split_dim_size) {
|
||||
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
|
||||
"Cannot split using values in 'split' input. Axis=", axis_value,
|
||||
" Input shape=", input_shape,
|
||||
" NumOutputs=", num_outputs,
|
||||
" Num entries in 'split' (must equal number of outputs) was ", split_sizes_values.size(),
|
||||
" Sum of sizes in 'split' (must equal size of selected axis) was ", split_size_sum);
|
||||
|
||||
split_sizes = split_sizes_values;
|
||||
}
|
||||
split_sizes = split_sizes_values;
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ steps:
|
|||
|
||||
- powershell: |
|
||||
$length = $env:TELEMETRYGUID.length
|
||||
$fileContent = "#define ENABLE_TELEMETRY`n#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
$fileContent = "#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
TraceLoggingOptionGroup("+$env:TELEMETRYGUID.substring(1, $length-2)+")"
|
||||
New-Item -Path "$(Build.SourcesDirectory)\include\onnxruntime\core\platform\windows\TraceLoggingConfigPrivate.h" -ItemType "file" -Value "$fileContent" -Force
|
||||
displayName: 'Create TraceLoggingConfigPrivate.h For WinML Telemetry'
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ jobs:
|
|||
if($env:TELEMETRYGUID)
|
||||
{
|
||||
$length = $env:TELEMETRYGUID.length
|
||||
$fileContent = "#define ENABLE_TELEMETRY`n#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
$fileContent = "#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
TraceLoggingOptionGroup("+$env:TELEMETRYGUID.substring(1, $length-2)+")"
|
||||
New-Item -Path "$(Build.SourcesDirectory)\include\onnxruntime\core\platform\windows\TraceLoggingConfigPrivate.h" -ItemType "file" -Value "$fileContent" -Force
|
||||
Write-Output "Enabling TELEMETRY"
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ jobs:
|
|||
if($env:TELEMETRYGUID)
|
||||
{
|
||||
$length = $env:TELEMETRYGUID.length
|
||||
$fileContent = "#define ENABLE_TELEMETRY`n#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
$fileContent = "#define TraceLoggingOptionMicrosoftTelemetry() \
|
||||
TraceLoggingOptionGroup("+$env:TELEMETRYGUID.substring(1, $length-2)+")"
|
||||
New-Item -Path "$(Build.SourcesDirectory)\include\onnxruntime\core\platform\windows\TraceLoggingConfigPrivate.h" -ItemType "file" -Value "$fileContent" -Force
|
||||
Write-Output "Enabling TELEMETRY"
|
||||
|
|
|
|||
|
|
@ -13,16 +13,6 @@
|
|||
|
||||
using namespace _winml;
|
||||
|
||||
static const OrtApi* GetVersionedOrtApi() {
|
||||
static const uint32_t ort_version = 2;
|
||||
const auto ort_api_base = OrtGetApiBase();
|
||||
return ort_api_base->GetApi(ort_version);
|
||||
}
|
||||
|
||||
static const WinmlAdapterApi* GetVersionedWinmlAdapterApi() {
|
||||
return OrtGetWinMLAdapter(GetVersionedOrtApi());
|
||||
}
|
||||
|
||||
static ONNXTensorElementDataType
|
||||
ONNXTensorElementDataTypeFromTensorKind(winml::TensorKind kind) {
|
||||
switch (kind) {
|
||||
|
|
|
|||
|
|
@ -7,10 +7,59 @@
|
|||
#include "core/platform/windows/TraceLoggingConfig.h"
|
||||
#include <evntrace.h>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
using namespace _winml;
|
||||
|
||||
static bool debug_output_ = false;
|
||||
|
||||
static HRESULT GetOnnxruntimeLibrary(HMODULE& module) {
|
||||
DWORD flags = 0;
|
||||
#ifdef BUILD_INBOX
|
||||
flags = LOAD_LIBRARY_SEARCH_SYSTEM32;
|
||||
#endif
|
||||
|
||||
auto out_module = LoadLibraryExA("onnxruntime.dll", nullptr, flags);
|
||||
if (out_module == nullptr) {
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
module = out_module;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
const OrtApi* _winml::GetVersionedOrtApi() {
|
||||
HMODULE onnxruntime_dll;
|
||||
FAIL_FAST_IF_FAILED(GetOnnxruntimeLibrary(onnxruntime_dll));
|
||||
|
||||
using OrtGetApiBaseSignature = decltype(OrtGetApiBase);
|
||||
auto ort_get_api_base_fn = reinterpret_cast<OrtGetApiBaseSignature*>(GetProcAddress(onnxruntime_dll, "OrtGetApiBase"));
|
||||
if (ort_get_api_base_fn == nullptr) {
|
||||
FAIL_FAST_HR(HRESULT_FROM_WIN32(GetLastError()));
|
||||
}
|
||||
|
||||
const auto ort_api_base = ort_get_api_base_fn();
|
||||
|
||||
static const uint32_t ort_version = 2;
|
||||
return ort_api_base->GetApi(ort_version);
|
||||
}
|
||||
|
||||
static const WinmlAdapterApi* GetVersionedWinmlAdapterApi(const OrtApi* ort_api) {
|
||||
HMODULE onnxruntime_dll;
|
||||
FAIL_FAST_IF_FAILED(GetOnnxruntimeLibrary(onnxruntime_dll));
|
||||
|
||||
using OrtGetWinMLAdapterSignature = decltype(OrtGetWinMLAdapter);
|
||||
auto ort_get_winml_adapter_fn = reinterpret_cast<OrtGetWinMLAdapterSignature*>(GetProcAddress(onnxruntime_dll, "OrtGetWinMLAdapter"));
|
||||
if (ort_get_winml_adapter_fn == nullptr) {
|
||||
FAIL_FAST_HR(HRESULT_FROM_WIN32(GetLastError()));
|
||||
}
|
||||
|
||||
return ort_get_winml_adapter_fn(ort_api);
|
||||
}
|
||||
|
||||
const WinmlAdapterApi* _winml::GetVersionedWinmlAdapterApi() {
|
||||
return GetVersionedWinmlAdapterApi(GetVersionedOrtApi());
|
||||
}
|
||||
|
||||
static void __stdcall WinmlOrtLoggingCallback(void* param, OrtLoggingLevel severity, const char* category,
|
||||
const char* logger_id, const char* code_location, const char* message) noexcept {
|
||||
UNREFERENCED_PARAMETER(param);
|
||||
|
|
@ -128,7 +177,7 @@ OnnxruntimeEnvironment::OnnxruntimeEnvironment(const OrtApi* ort_api) : ort_env_
|
|||
ort_env_ = UniqueOrtEnv(ort_env, ort_api->ReleaseEnv);
|
||||
|
||||
// Configure the environment with the winml logger
|
||||
auto winml_adapter_api = OrtGetWinMLAdapter(ort_api);
|
||||
auto winml_adapter_api = GetVersionedWinmlAdapterApi(ort_api);
|
||||
THROW_IF_NOT_OK_MSG(winml_adapter_api->EnvConfigureCustomLoggerAndProfiler(ort_env_.get(),
|
||||
&WinmlOrtLoggingCallback, &WinmlOrtProfileEventCallback, nullptr,
|
||||
OrtLoggingLevel::ORT_LOGGING_LEVEL_VERBOSE, "Default", &ort_env),
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ class OnnxruntimeEnvironment {
|
|||
UniqueOrtEnv ort_env_;
|
||||
};
|
||||
|
||||
const OrtApi* GetVersionedOrtApi();
|
||||
const WinmlAdapterApi* GetVersionedWinmlAdapterApi();
|
||||
|
||||
} // namespace _winml
|
||||
|
||||
#pragma warning(pop)
|
||||
Loading…
Reference in a new issue