onnxruntime/onnxruntime/test/framework/test_utils.h
Scott McKay 86dc3b4360
Fix bug in the transformer that removes unnecessary Cast nodes where it was re-processing removed nodes leading to multiple calls to RemoveNode for the same node. (#1291)
Description:
The remove duplicate Cast logic was processing a node already removed, leading to multiple calls to remove the same node causing an error. Add a check so that nodes marked for removal are skipped.

Motivation and Context
If a model has 3 Cast nodes in a row the bug would cause an exception to be thrown due to multiple calls to remove the same node. This causes the latest optimized tf2onnx conversion of ssd_mobilenet to break.
2019-06-25 15:17:08 +10:00

77 lines
2.7 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <map>
#include <string>
#include "core/framework/allocatormgr.h"
#include "core/framework/execution_provider.h"
#include "core/providers/cpu/cpu_execution_provider.h"
#include "core/framework/ml_value.h"
#ifdef USE_CUDA
#include "core/providers/cuda/cuda_execution_provider.h"
#endif
#ifdef USE_TENSORRT
#include "core/providers/tensorrt/tensorrt_execution_provider.h"
#endif
#ifdef USE_OPENVINO
#include "core/providers/openvino/openvino_execution_provider.h"
#endif
namespace onnxruntime {
class Graph;
namespace test {
// Doesn't work with ExecutionProviders class and KernelRegistryManager
IExecutionProvider* TestCPUExecutionProvider();
#ifdef USE_CUDA
// Doesn't work with ExecutionProviders class and KernelRegistryManager
IExecutionProvider* TestCudaExecutionProvider();
#endif
#ifdef USE_TENSORRT
// Doesn't work with ExecutionProviders class and KernelRegistryManager
IExecutionProvider* TestTensorrtExecutionProvider();
#endif
#ifdef USE_OPENVINO
IExecutionProvider* TestOpenVINOExecutionProvider();
#endif
template <typename T>
void CreateMLValue(AllocatorPtr alloc, const std::vector<int64_t>& dims, const std::vector<T>& value,
OrtValue* p_mlvalue) {
TensorShape shape(dims);
auto element_type = DataTypeImpl::GetType<T>();
std::unique_ptr<Tensor> p_tensor = std::make_unique<Tensor>(element_type,
shape,
alloc);
if (value.size() > 0) {
memcpy(p_tensor->MutableData<T>(), &value[0], element_type->Size() * shape.Size());
}
p_mlvalue->Init(p_tensor.release(),
DataTypeImpl::GetType<Tensor>(),
DataTypeImpl::GetType<Tensor>()->GetDeleteFunc());
}
template <typename T>
void AllocateMLValue(AllocatorPtr alloc, const std::vector<int64_t>& dims, OrtValue* p_mlvalue) {
TensorShape shape(dims);
auto element_type = DataTypeImpl::GetType<T>();
std::unique_ptr<Tensor> p_tensor = std::make_unique<Tensor>(element_type,
shape,
alloc);
p_mlvalue->Init(p_tensor.release(),
DataTypeImpl::GetType<Tensor>(),
DataTypeImpl::GetType<Tensor>()->GetDeleteFunc());
}
// Returns a map with the number of occurrences of each operator in the graph.
// Helper function to check that the graph transformations have been successfully applied.
std::map<std::string, int> CountOpsInGraph(const Graph& graph);
} // namespace test
} // namespace onnxruntime