Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct OrtCompileApi
public IntPtr ModelCompilationOptions_SetGraphOptimizationLevel;
public IntPtr ModelCompilationOptions_SetOutputModelWriteFunc;
public IntPtr ModelCompilationOptions_SetOutputModelGetInitializerLocationFunc;
public IntPtr ModelCompilationOptions_SetInputModel;
}

internal class NativeMethods
Expand Down Expand Up @@ -136,6 +137,12 @@ public DOrtModelCompilationOptions_SetOutputModelWriteFunc
public DOrtModelCompilationOptions_SetOutputModelGetInitializerLocationFunc
OrtModelCompilationOptions_SetOutputModelGetInitializerLocationFunc;

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /* OrtStatus* */ DOrtModelCompilationOptions_SetInputModel(
IntPtr /* OrtModelCompilationOptions* */ options,
IntPtr /* const OrtModel* */ inputModel);
public DOrtModelCompilationOptions_SetInputModel OrtModelCompilationOptions_SetInputModel;

internal NativeMethods(OnnxRuntime.NativeMethods.DOrtGetCompileApi getCompileApi)
{

Expand Down Expand Up @@ -217,6 +224,11 @@ internal NativeMethods(OnnxRuntime.NativeMethods.DOrtGetCompileApi getCompileApi
_compileApi.ModelCompilationOptions_SetOutputModelGetInitializerLocationFunc,
typeof(DOrtModelCompilationOptions_SetOutputModelGetInitializerLocationFunc));

OrtModelCompilationOptions_SetInputModel =
(DOrtModelCompilationOptions_SetInputModel)Marshal.GetDelegateForFunctionPointer(
_compileApi.ModelCompilationOptions_SetInputModel,
typeof(DOrtModelCompilationOptions_SetInputModel));

}
}
}
23 changes: 23 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8015,6 +8015,29 @@ struct OrtCompileApi {
ORT_API2_STATUS(ModelCompilationOptions_SetOutputModelGetInitializerLocationFunc,
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ OrtGetInitializerLocationFunc get_initializer_location_func, _In_ void* state);

/** \brief Sets the OrtModel to compile.
*
* Sets an OrtModel created via the Model Editor API as the input for compilation.
*
* The input model's source (file path, memory buffer, or OrtModel) must be set with
* one of: ModelCompilationOptions_SetInputModelPath, ModelCompilationOptions_SetInputModelFromBuffer,
* or ModelCompilationOptions_SetInputModel.
*
* The OrtModel must have a complete graph with inputs, outputs, and nodes defined.
* The caller retains ownership of the OrtModel and must not release it until after
* CompileModel returns.
*
* \param[in] model_compile_options The OrtModelCompilationOptions instance.
* \param[in] model The OrtModel to compile. The model is borrowed (not copied or owned).
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.24.
*/
ORT_API2_STATUS(ModelCompilationOptions_SetInputModel,
_In_ OrtModelCompilationOptions* model_compile_options,
_In_ const OrtModel* model);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,8 @@ struct ModelCompilationOptions : detail::Base<OrtModelCompilationOptions> {
ModelCompilationOptions& SetFlags(uint32_t flags); ///< Wraps OrtApi::ModelCompilationOptions_SetFlags

ModelCompilationOptions& SetGraphOptimizationLevel(GraphOptimizationLevel graph_optimization_level); ///< Wraps OrtApi::ModelCompilationOptions_SetGraphOptimizationLevel

ModelCompilationOptions& SetInputModel(const OrtModel* model); ///< Wraps OrtCompileApi::ModelCompilationOptions_SetInputModel
};

/** \brief Compiles an input model to generate a model with EPContext nodes that execute EP-specific kernels. Wraps OrtApi::CompileModels.
Expand Down
5 changes: 5 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,11 @@ inline ModelCompilationOptions& ModelCompilationOptions::SetGraphOptimizationLev
return *this;
}

inline ModelCompilationOptions& ModelCompilationOptions::SetInputModel(const OrtModel* model) {
Ort::ThrowOnError(GetCompileApi().ModelCompilationOptions_SetInputModel(this->p_, model));
return *this;
}

namespace detail {

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ HRESULT STDMETHODCALLTYPE AbiCustomRegistry::RegisterOperatorKernel(
InferAndVerifyOutputSizes(node, &defaultAttributesCapture, shapeInferrerCapture.Get(), constantCpuInputCapture, constantInputGetter, inputShapesOverrides, *outputShapes);

// Create the kernel while allowing input shape and output shape queries according to options
ComPtr<DmlGraphOpKernelInfoWrapper> kernelInfoWrapper = wil::MakeOrThrow<DmlGraphOpKernelInfoWrapper>(
ComPtr<DmlGraphOpKernelInfoWrapper> kernelInfoWrapper = Dml::SafeMakeOrThrow<DmlGraphOpKernelInfoWrapper>(
&protoHelper,
executionHandle,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace Dml
assert(resourceWrapper->GetD3D12Resource()->GetDesc().Width == bucketSize);
assert(resourceWrapper != nullptr);

ComPtr<AllocationInfo> allocInfo = wil::MakeOrThrow<AllocationInfo>(
ComPtr<AllocationInfo> allocInfo = Dml::SafeMakeOrThrow<AllocationInfo>(
this,
++m_currentAllocationId,
resourceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
));

ComPtr<DmlResourceWrapper> resourceWrapper;
wil::MakeOrThrow<DmlCommittedResourceWrapper>(std::move(resource)).As(&resourceWrapper);
Dml::SafeMakeOrThrow<DmlCommittedResourceWrapper>(std::move(resource)).As(&resourceWrapper);

Check warning on line 25 in onnxruntime/core/providers/dml/DmlExecutionProvider/src/DmlCommittedResourceAllocator.cpp

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <utility> for move [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/dml/DmlExecutionProvider/src/DmlCommittedResourceAllocator.cpp:25: Add #include <utility> for move [build/include_what_you_use] [4]
return resourceWrapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
constexpr uint64_t pooledResourceId = 0; // Not a pooled resource

Microsoft::WRL::ComPtr<DmlResourceWrapper> resourceWrapper;
wil::MakeOrThrow<DmlCommittedResourceWrapper>(std::move(resource)).As(&resourceWrapper);
Dml::SafeMakeOrThrow<DmlCommittedResourceWrapper>(std::move(resource)).As(&resourceWrapper);

Check warning on line 51 in onnxruntime/core/providers/dml/DmlExecutionProvider/src/DmlExternalBufferAllocator.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <utility> for move [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/dml/DmlExecutionProvider/src/DmlExternalBufferAllocator.h:51: Add #include <utility> for move [build/include_what_you_use] [4]

Microsoft::WRL::ComPtr<AllocationInfo> allocInfo = wil::MakeOrThrow<AllocationInfo>(
Microsoft::WRL::ComPtr<AllocationInfo> allocInfo = Dml::SafeMakeOrThrow<AllocationInfo>(
nullptr,
0,
pooledResourceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ namespace DmlGraphFusionHelper
}
}

// Tensor sizes in DML must be a multiple of 4 bytes large.
tensorByteSize = AlignToPow2<size_t>(tensorByteSize, 4);
if(graphSerializationEnabled)
{
WriteToFile(modelName, ConvertToWString(iter->first) + L".bin", reinterpret_cast<uint8_t*>(tensorPtr), tensorByteSize);
Expand Down Expand Up @@ -264,9 +262,10 @@ namespace DmlGraphFusionHelper
initializeInputBuffer = CreateCpuResource(providerImpl, tensorPtr, tensorByteSize);
}

// Set the binding for operator initialization to the buffer
// Set the binding for operator initialization to the buffer.
// DML requires buffer binding sizes to be a multiple of 4 bytes.
initInputBindings[i].Buffer = initializeInputBuffer.Get();
initInputBindings[i].SizeInBytes = tensorByteSize;
initInputBindings[i].SizeInBytes = AlignToPow2<size_t>(tensorByteSize, 4);
initializeResourceRefs.push_back(std::move(initializeInputBuffer));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
_Out_ std::shared_ptr<onnxruntime::KernelRegistry>* registry,
_Out_ std::shared_ptr<const InternalRegistrationInfoMap>* internalRegInfoMap)
{
ComPtr<AbiCustomRegistry> abiRegistry = wil::MakeOrThrow<AbiCustomRegistry>();
ComPtr<AbiCustomRegistry> abiRegistry = Dml::SafeMakeOrThrow<AbiCustomRegistry>();
Dml::RegisterDmlOperators(abiRegistry.Get());

assert(abiRegistry->GetRegistries().size() == 1);
Expand Down Expand Up @@ -88,7 +88,7 @@
ComPtr<ID3D12Device> device;
GRAPHICS_THROW_IF_FAILED(dmlDevice->GetParentDevice(IID_GRAPHICS_PPV_ARGS(device.GetAddressOf())));

m_impl = wil::MakeOrThrow<ExecutionProviderImpl>(dmlDevice, device.Get(), executionContext, enableMetacommands,
m_impl = Dml::SafeMakeOrThrow<ExecutionProviderImpl>(dmlDevice, device.Get(), executionContext, enableMetacommands,
enableGraphCapture, enableSyncSpinning, disableMemoryArena);
}

Expand Down Expand Up @@ -1298,9 +1298,9 @@
uint64_t pooledResourceId = 0; // Not a pooled resource

ComPtr<DmlResourceWrapper> resourceWrapper;
wil::MakeOrThrow<DmlCommittedResourceWrapper>(pResource).As(&resourceWrapper);
Dml::SafeMakeOrThrow<DmlCommittedResourceWrapper>(pResource).As(&resourceWrapper);

ComPtr<AllocationInfo> allocInfo = wil::MakeOrThrow<AllocationInfo>(nullptr, 0, pooledResourceId, resourceWrapper.Get(), (size_t)pResource->GetDesc().Width);
ComPtr<AllocationInfo> allocInfo = Dml::SafeMakeOrThrow<AllocationInfo>(nullptr, 0, pooledResourceId, resourceWrapper.Get(), (size_t)pResource->GetDesc().Width);

Check warning on line 1303 in onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Using C-style cast. Use static_cast<size_t>(...) instead [readability/casting] [4] Raw Output: onnxruntime/core/providers/dml/DmlExecutionProvider/src/ExecutionProvider.cpp:1303: Using C-style cast. Use static_cast<size_t>(...) instead [readability/casting] [4]
return allocInfo.Detach();
}
void FreeGPUAllocation(void* ptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ namespace Dml::GraphDescBuilder
if (iter != isInitializerTransferable.end())
{
// Using const_cast here is simpler than making surrounding code const correct.
tensorWrapper = wil::MakeOrThrow<OnnxTensorWrapper>(const_cast<ONNX_NAMESPACE::TensorProto*>(iter->second.first), modelPath);
tensorWrapper = Dml::SafeMakeOrThrow<OnnxTensorWrapper>(const_cast<ONNX_NAMESPACE::TensorProto*>(iter->second.first), modelPath);
}
return tensorWrapper;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ namespace Windows::AI::MachineLearning::Adapter
const onnx::TensorProto* tensorProto = &attributeProto->t();

// An empty path is used as external weights are not currently supported in this case
Microsoft::WRL::ComPtr<IMLOperatorTensor> tensorWrapper = wil::MakeOrThrow<OnnxTensorWrapper>(const_cast<onnx::TensorProto*>(tensorProto), std::filesystem::path());
Microsoft::WRL::ComPtr<IMLOperatorTensor> tensorWrapper = Dml::SafeMakeOrThrow<OnnxTensorWrapper>(const_cast<onnx::TensorProto*>(tensorProto), std::filesystem::path());
*tensor = tensorWrapper.Detach();
return S_OK;
}
Expand Down Expand Up @@ -1977,7 +1977,7 @@ namespace Windows::AI::MachineLearning::Adapter
auto inputTensor = m_impl->Input<onnxruntime::Tensor>(gsl::narrow_cast<int>(inputIndex));
if (inputTensor != nullptr)
{
ComPtr<TensorWrapper> tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
ComPtr<TensorWrapper> tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
const_cast<onnxruntime::Tensor*>(inputTensor),
IsAllocationInterface(inputTensor->Location()),
m_winmlProvider.Get(),
Expand Down Expand Up @@ -2019,7 +2019,7 @@ namespace Windows::AI::MachineLearning::Adapter
auto elemTensor = const_cast<onnxruntime::Tensor*>(&inputTensorSeq->Get(sequenceIndex));
if (elemTensor != nullptr)
{
ComPtr<TensorWrapper> tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
ComPtr<TensorWrapper> tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
elemTensor,
IsAllocationInterface(elemTensor->Location()),
m_winmlProvider.Get(),
Expand Down Expand Up @@ -2119,7 +2119,7 @@ namespace Windows::AI::MachineLearning::Adapter
auto elemTensor = const_cast<onnxruntime::Tensor*>(&outputTensorSeq->Get(sequenceIndex));
if (elemTensor != nullptr)
{
ComPtr<TensorWrapper> tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
ComPtr<TensorWrapper> tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
elemTensor,
IsAllocationInterface(elemTensor->Location()),
m_winmlProvider.Get(),
Expand Down Expand Up @@ -2212,7 +2212,7 @@ namespace Windows::AI::MachineLearning::Adapter
auto outputTensor = m_impl->Output(outputIndex, shape);
if (outputTensor)
{
ComPtr<TensorWrapper> tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
ComPtr<TensorWrapper> tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
const_cast<onnxruntime::Tensor*>(outputTensor),
IsAllocationInterface(outputTensor->Location()),
m_winmlProvider.Get(),
Expand Down Expand Up @@ -2377,7 +2377,7 @@ namespace Windows::AI::MachineLearning::Adapter
const onnxruntime::Tensor* tensor = nullptr;
if (kerneInfo.TryGetConstantInput(index, &tensor))
{
tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
const_cast<onnxruntime::Tensor*>(tensor),
IsAllocationInterface(tensor->Location()),
winmlProviderCapture.Get(),
Expand All @@ -2396,7 +2396,7 @@ namespace Windows::AI::MachineLearning::Adapter
}

// Create the kernel while allowing input shape and output shape queries according to options
ComPtr<OpKernelInfoWrapper> kernelInfoWrapper = wil::MakeOrThrow<OpKernelInfoWrapper>(
ComPtr<OpKernelInfoWrapper> kernelInfoWrapper = Dml::SafeMakeOrThrow<OpKernelInfoWrapper>(
&kerneInfo,
m_abiExecutionObject.Get(),
nullptr,
Expand Down Expand Up @@ -2443,7 +2443,7 @@ namespace Windows::AI::MachineLearning::Adapter
const auto* tensor = context->Input<onnxruntime::Tensor>(gsl::narrow_cast<int>(index));
if (tensor != nullptr)
{
tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
const_cast<onnxruntime::Tensor*>(tensor),
IsAllocationInterface(tensor->Location()),
winmlProviderCapture.Get(),
Expand All @@ -2464,7 +2464,7 @@ namespace Windows::AI::MachineLearning::Adapter
for (uint32_t sequenceIndex = 0; sequenceIndex < tensorSequence->Size(); ++sequenceIndex)
{
auto& tensor = tensorSequence->Get(sequenceIndex);
auto tensorWrapper = wil::MakeOrThrow<TensorWrapper>(
auto tensorWrapper = Dml::SafeMakeOrThrow<TensorWrapper>(
const_cast<onnxruntime::Tensor*>(&tensor),
IsAllocationInterface(tensor.Location()),
winmlProviderCapture.Get(),
Expand All @@ -2491,7 +2491,7 @@ namespace Windows::AI::MachineLearning::Adapter
}

// Create the kernel while allowing input shape and output shape queries according to options
ComPtr<OpKernelInfoWrapper> kernelInfoWrapper = wil::MakeOrThrow<OpKernelInfoWrapper>(
ComPtr<OpKernelInfoWrapper> kernelInfoWrapper = Dml::SafeMakeOrThrow<OpKernelInfoWrapper>(
&Info(),
m_abiExecutionObject.Get(),
&inputShapes,
Expand Down Expand Up @@ -2569,7 +2569,7 @@ namespace Windows::AI::MachineLearning::Adapter
EdgeShapes localInferredOutputShapes;
ComPtr<IMLOperatorKernel> localKernel = inferShapesAndCreateKernel(local_input_shapes, localInferredOutputShapes);

ComPtr<OpKernelContextWrapper> kernelContextWrapper = wil::MakeOrThrow<OpKernelContextWrapper>(
ComPtr<OpKernelContextWrapper> kernelContextWrapper = Dml::SafeMakeOrThrow<OpKernelContextWrapper>(
context,
Info().GetExecutionProvider(),
m_internalOperator,
Expand All @@ -2588,7 +2588,7 @@ namespace Windows::AI::MachineLearning::Adapter
}
}

ComPtr<OpKernelContextWrapper> kernelContextWrapper = wil::MakeOrThrow<OpKernelContextWrapper>(
ComPtr<OpKernelContextWrapper> kernelContextWrapper = Dml::SafeMakeOrThrow<OpKernelContextWrapper>(
context,
Info().GetExecutionProvider(),
m_internalOperator,
Expand Down Expand Up @@ -2811,7 +2811,7 @@ namespace Windows::AI::MachineLearning::Adapter
onnxruntime::ProtoHelperNodeContext protoContext(node);
onnxruntime::OpNodeProtoHelper<onnxruntime::ProtoHelperNodeContext> info(&protoContext);

ComPtr<MLKernelInferenceContext> inferenceContext = wil::MakeOrThrow<MLKernelInferenceContext>(&info, inputShapes, outputShapes, defaultAttributes, requiredConstantCpuInputs, constantInputGetter);
ComPtr<MLKernelInferenceContext> inferenceContext = Dml::SafeMakeOrThrow<MLKernelInferenceContext>(&info, inputShapes, outputShapes, defaultAttributes, requiredConstantCpuInputs, constantInputGetter);

outputShapes.Reset(info.GetOutputCount());

Expand Down Expand Up @@ -2865,13 +2865,13 @@ namespace Windows::AI::MachineLearning::Adapter
[ctx](uint32_t index)
{
// An empty path is used as external weights are not currently supported in this case
Microsoft::WRL::ComPtr<IMLOperatorTensor> tensorWrapper = wil::MakeOrThrow<OnnxTensorWrapper>(
Microsoft::WRL::ComPtr<IMLOperatorTensor> tensorWrapper = Dml::SafeMakeOrThrow<OnnxTensorWrapper>(
const_cast<onnx::TensorProto*>(ctx->getInputData(index)), std::filesystem::path());
return tensorWrapper;
}
);

return wil::MakeOrThrow<MLSchemaInferenceContext>(info, ctx, requiredConstantCpuInputs, mlOperatorTensorGetter);
return Dml::SafeMakeOrThrow<MLSchemaInferenceContext>(info, ctx, requiredConstantCpuInputs, mlOperatorTensorGetter);
}

MLSchemaInferenceContext::MLSchemaInferenceContext(
Expand Down Expand Up @@ -2952,7 +2952,7 @@ namespace Windows::AI::MachineLearning::Adapter
const AttributeMap* defaultAttributes)
{
MLOperatorTensorGetter mLOperatorTensorGetter = MLOperatorTensorGetter();
return wil::MakeOrThrow<MLSupportQueryContext>(info, defaultAttributes, mLOperatorTensorGetter);
return Dml::SafeMakeOrThrow<MLSupportQueryContext>(info, defaultAttributes, mLOperatorTensorGetter);
}

MLSupportQueryContext::MLSupportQueryContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ class GpuDFTOperatorFactory : public WRL::Base<IMLOperatorKernelFactory>
version = 20;
}

auto dftOperator = wil::MakeOrThrow<GpuDFTOperator>(context, version);
auto dftOperator = Dml::SafeMakeOrThrow<GpuDFTOperator>(context, version);
dftOperator.CopyTo(kernel);
return S_OK;
}
Expand Down Expand Up @@ -1177,8 +1177,8 @@ class GpuDFTOperatorFactory : public WRL::Base<IMLOperatorKernelFactory>
kernelDescription.options = MLOperatorKernelOptions::None;
kernelDescription.executionOptions = 0;

auto shareInferrer = wil::MakeOrThrow<DFTShapeInferrer>();
auto factory = wil::MakeOrThrow<GpuDFTOperatorFactory>();
auto shareInferrer = Dml::SafeMakeOrThrow<DFTShapeInferrer>();
auto factory = Dml::SafeMakeOrThrow<GpuDFTOperatorFactory>();

std::array<uint32_t, 2> requiredConstantCpuInputs = { 1, 2 };

Expand Down
Loading
Loading