Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into prathikrao/where-op-bfloat16
Browse files Browse the repository at this point in the history
  • Loading branch information
Prathik Rao committed Oct 30, 2023
2 parents 7913384 + 348a963 commit aa76302
Showing 1 changed file with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ namespace DmlGraphFusionHelper
ORT_THROW_IF_FAILED(resourceUnk->QueryInterface(resource));
}

std::tuple<std::unique_ptr<std::byte[]>, std::vector<uint8_t>, std::byte*, size_t> UnpackInitializer(
const onnxruntime::Graph& graph,
const ONNX_NAMESPACE::TensorProto* initializer)
{
std::unique_ptr<std::byte[]> unpackedTensor;
std::vector<uint8_t> unpackedExternalTensor;
std::byte* tensorPtr = nullptr;
size_t tensorByteSize = 0;

// The tensor may be stored as raw data or in typed fields.
if (initializer->data_location() == onnx::TensorProto_DataLocation_EXTERNAL)
{
THROW_IF_NOT_OK(onnxruntime::utils::UnpackInitializerData(*initializer, graph.ModelPath(), unpackedExternalTensor));
tensorPtr = reinterpret_cast<std::byte*>(unpackedExternalTensor.data());
tensorByteSize = unpackedExternalTensor.size();
}
else if (initializer->has_raw_data())
{
tensorPtr = (std::byte*)(initializer->raw_data().c_str());
tensorByteSize = initializer->raw_data().size();
}
else
{
std::tie(unpackedTensor, tensorByteSize) = Windows::AI::MachineLearning::Adapter::UnpackTensor(*initializer, graph.ModelPath());
tensorPtr = unpackedTensor.get();
}

return std::make_tuple(std::move(unpackedTensor), std::move(unpackedExternalTensor), tensorPtr, tensorByteSize);
}

void ProcessInputData(
const ExecutionProviderImpl* providerImpl,
const std::vector<uint8_t>& isInputsUploadedByDmlEP,
Expand Down Expand Up @@ -161,32 +191,11 @@ namespace DmlGraphFusionHelper
auto iter = initializerNameToInitializerMap.find(subGraphInputArgNames[i]);
if (iter != initializerNameToInitializerMap.end())
{
std::byte* tensorPtr = nullptr;
size_t tensorByteSize = 0;
std::vector<uint8_t> unpackedExternalTensor;

std::unique_ptr<std::byte[]> unpackedTensor;

//auto& initializer = iter->second;
auto* initializer = iter->second.first;
auto [unpackedTensor, unpackedExternalTensor, tensorPtr, tensorByteSize] = UnpackInitializer(graph, initializer);

// The tensor may be stored as raw data or in typed fields.
if (initializer->data_location() == onnx::TensorProto_DataLocation_EXTERNAL)
{
THROW_IF_NOT_OK(onnxruntime::utils::UnpackInitializerData(*initializer, graph.ModelPath(), unpackedExternalTensor));
tensorPtr = reinterpret_cast<std::byte*>(unpackedExternalTensor.data());
tensorByteSize = unpackedExternalTensor.size();
}
else if (initializer->has_raw_data())
if (initializer->data_location() != onnx::TensorProto_DataLocation_EXTERNAL && !initializer->has_raw_data())
{
tensorPtr = (std::byte*)(initializer->raw_data().c_str());
tensorByteSize = initializer->raw_data().size();
}
else
{
std::tie(unpackedTensor, tensorByteSize) = Windows::AI::MachineLearning::Adapter::UnpackTensor(*initializer, graph.ModelPath());
tensorPtr = unpackedTensor.get();

// Free the initializer if this is the last usage of it.
if (initializerToLastInputIndexMap[initializer] == i)
{
Expand Down Expand Up @@ -592,9 +601,11 @@ namespace DmlGraphFusionHelper

for (auto& kvp : isInitializerTransferable)
{
auto [unpackedTensor, unpackedExternalTensor, tensorPtr, tensorByteSize] = UnpackInitializer(graph, kvp.second.first);

ONNX_NAMESPACE::TensorProto tensorProto;
tensorProto.set_data_type(kvp.second.first->data_type());
tensorProto.set_raw_data(kvp.second.first->raw_data());
tensorProto.set_raw_data(tensorPtr, tensorByteSize);
tensorProto.set_name(kvp.second.first->name());

for (int i = 0; i < kvp.second.first->dims_size(); ++i)
Expand Down

0 comments on commit aa76302

Please sign in to comment.