Create an optimized version of the network.
2031{
2033
2034
2035 auto profiler = inGraph.GetProfiler();
2037 profiler->EnableProfiling(options.GetProfilingEnabled());
2038
2040 if (backendPreferences.empty())
2041 {
2042 throw InvalidArgumentException("Invoked Optimize with no backends specified");
2043 }
2044
2045 if (options.GetReduceFp32ToBf16())
2046 {
2047 throw InvalidArgumentException("BFloat16 optimization is currently ignored. In order to use Bf16 optimization "
2048 "Please use the FastMathEnabled backend option for CpuAcc or GpuAcc.");
2049 }
2050
2051 if (options.GetReduceFp32ToFp16() && options.GetReduceFp32ToBf16())
2052 {
2053 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
2054 }
2055
2056
2057 inGraph.VerifyConstantLayerSetTensorInfo();
2058
2059 std::unique_ptr<Graph> graph = std::make_unique<Graph>(inGraph);
2060
2061
2062
2064 {{"ImportEnabled", options.GetImportEnabled()},
2065 {"ExportEnabled", options.GetExportEnabled()}});
2066 ModelOptions optimizedOptions(options.GetModelOptions());
2067 optimizedOptions.push_back(importExport);
2068
2071
2073
2074
2075 Graph& optGraph = optNetObjPtr->pOptimizedNetworkImpl->GetGraph();
2076
2078 {
2079
2080 optGraph.InferTensorInfos();
2081 }
2082
2083 using namespace optimizations;
2084
2085
2087
2088
2090
2092
2094 {
2095
2096 optGraph.InferTensorInfos();
2097 }
2098
2099
2100 BackendSettings backendSettings(backendPreferences, deviceSpec);
2101 auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends();
2102 if (availablePreferredBackends.empty())
2103 {
2104 std::stringstream failureMsg;
2105 failureMsg << "None of the preferred backends " << backendPreferences
2106 << " are supported. Current platform provides " << backendSettings.m_SupportedBackends;
2108 throw InvalidArgumentException(failureMsg.str());
2109 }
2110
2111
2112 TensorHandleFactoryRegistry tensorHandleFactoryRegistry;
2115
2116 bool reduceFp32ToFp16 = options.GetReduceFp32ToFp16();
2117
2118
2120 {
2122 reduceFp32ToFp16 = true;
2123 }
2124 else
2125 {
2127 }
2128
2129
2130
2131
2132
2134
2152
2154
2155
2156 for (auto backend : mappedGpuBackends)
2157 {
2158 if (std::count(backendPreferences.begin(), backendPreferences.end(), backend)
2159 && (backendPreferences[0] != backend) &&
2161 {
2162 std::stringstream failureMsg;
2163 failureMsg << backend << " backend cannot be specified as fallback.";
2165 throw InvalidArgumentException(failureMsg.str());
2166 }
2167 }
2168
2169 std::vector<BackendId> amendedBackendPreferences = backendPreferences;
2172 {
2173
2174 for (auto backend : mappedGpuBackends)
2175 {
2176 if (!std::count(amendedBackendPreferences.begin(), amendedBackendPreferences.end(), backend))
2177 {
2178 amendedBackendPreferences.insert(amendedBackendPreferences.begin(), backend);
2179 }
2180 }
2181 }
2182
2183 if (reduceFp32ToFp16 && hasFp16)
2184 {
2188 }
2189
2192 OptimizationResult assignBackendsResult =
AssignBackends(optNetObjPtr->pOptimizedNetworkImpl.get(),
2193 backendSettings,
2194 firstLayer,
2195 lastLayer,
2196 messages);
2197 if (assignBackendsResult.m_Error)
2198 {
2199
2200 throw InvalidArgumentException("Failed to assign a backend to each layer");
2201 }
2202
2205
2206
2208 backendSettings,
2209 backends,
2210 options.GetModelOptions(),
2211 messages);
2212 if (backendOptimizationResult.m_Error)
2213 {
2214
2215 throw InvalidArgumentException("Failed to apply the backend-specific optimizations");
2216 }
2217
2218
2219 {
2223 }
2224
2225
2226
2227
2228 if (options.GetDebugEnabled() && !options.GetDebugToFileEnabled())
2229 {
2231 }
2232 else if (options.GetDebugToFileEnabled())
2233 {
2234
2235 try
2236 {
2237#if !defined(ARMNN_DISABLE_FILESYSTEM)
2239 ARMNN_LOG(
info) <<
"Intermediate tensors will be written to: " << result;
2240#endif
2242 }
2244 {
2245
2247 }
2248 }
2249
2250
2252 backends,
2253 tensorHandleFactoryRegistry,
2254 options.GetImportEnabled(),
2255 options.GetExportEnabled(),
2256 messages);
2257
2258 if (strategyResult.m_Error)
2259 {
2260
2262 }
2263
2264
2265 {
2267 optGraph.AddCompatibilityLayers(backends, tensorHandleFactoryRegistry);
2268 }
2269
2270 return optNet;
2271}
#define ARMNN_LOG(severity)
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
BackendIdSet GetBackendIds() const
BackendIdVector GetMappedGpuBackends()
virtual const char * what() const noexcept override
LayerList::const_iterator Iterator
IOptimizedNetwork(const IOptimizedNetwork &other, const ModelOptions &modelOptions)
Creates a copy of the IOptimizedNetwork.
static void Destroy(IOptimizedNetwork *network)
static void Pass(Graph &graph, const Optimizations &optimizations)
void RegisterProfiler(IProfiler *profiler)
static ProfilerManager & GetInstance()
OptimizeForConnection< PermuteLayer, PermuteLayer, OptimizeInversePermutesImpl< PermuteLayer > > OptimizeInversePermutes
ConvertConstants< Float16ToFloat32, IsFloat32Layer > ConvertConstantsHalfToFloat
OptimizeForExclusiveConnection< ElementwiseBinaryLayer, ElementwiseBinaryLayer, MaxMinIntoBoundedReluImpl > MaxMinIntoBoundedRelu
OptimizeForConnection< Layer, TransposeLayer, SquashEqualSiblingsImpl< TransposeLayer > > SquashEqualTransposeSiblings
ConvertConstants< Float32ToFloat16, IsFloat16Layer > ConvertConstantsFloatToHalf
OptimizeForConnection< ConvertFp16ToFp32Layer, ConvertFp32ToFp16Layer, OptimizeInverseConversionsImpl > OptimizeInverseConversionsFp16
OptimizeForConnection< ConstantLayer, DequantizeLayer, ConvertConstDequantisationLayersToConstLayersImpl > ConvertConstDequantisationLayersToConstLayers
OptimizeForType< Layer, AddBroadcastReshapeLayerImpl > AddBroadcastReshapeLayer
OptimizeForExclusiveConnection< Convolution2dLayer, BatchNormalizationLayer, FuseBatchNorm< Convolution2dLayer, armnn::DataType::Float16 > > FuseBatchNormIntoConvolution2DFloat16
OptimizeForConnection< Layer, TransposeLayer, MoveTransposeUpImpl > MoveTransposeUp
OptimizeForConnection< PermuteLayer, BatchToSpaceNdLayer, PermuteAndBatchToSpaceAsDepthToSpaceImpl< PermuteLayer > > PermuteAndBatchToSpaceAsDepthToSpace
OptimizeForConnection< TransposeLayer, BatchToSpaceNdLayer, PermuteAndBatchToSpaceAsDepthToSpaceImpl< TransposeLayer > > TransposeAndBatchToSpaceAsDepthToSpace
OptimizeForConnection< TransposeLayer, TransposeLayer, OptimizeInversePermutesImpl< TransposeLayer > > OptimizeInverseTransposes
OptimizeForConnection< ConstantLayer, DequantizeLayer, TurboConvertConstDequantisationLayersToConstLayersImpl > TurboConvertConstDequantisationLayersToConstLayers
OptimizeForConnection< ReshapeLayer, ReshapeLayer, OptimizeConsecutiveReshapesImpl > OptimizeConsecutiveReshapes
OptimizeForConnection< Layer, ReshapeLayer, SquashEqualSiblingsImpl< ReshapeLayer > > SquashEqualReshapeSiblings
OptimizeForType< Layer, ConvertFp32NetworkToFp16Impl > Fp32NetworkToFp16Converter
OptimizeForConnection< Layer, PermuteLayer, SquashEqualSiblingsImpl< PermuteLayer > > SquashEqualPermuteSiblings
OptimizeForConnection< ConstantLayer, PermuteLayer, ConvertConstPermuteLayersToConstLayers > FusePermuteIntoConstLayer
OptimizeForType< Layer, AddDebugToFileImpl > InsertDebugToFileLayer
OptimizeForType< Layer, AddDebugImpl > InsertDebugLayer
OptimizeForConnection< Layer, PermuteLayer, MovePermuteUpImpl > MovePermuteUp
OptimizeForExclusiveConnection< DepthwiseConvolution2dLayer, BatchNormalizationLayer, FuseBatchNorm< DepthwiseConvolution2dLayer, armnn::DataType::Float16 > > FuseBatchNormIntoDepthwiseConvolution2DFloat16
OptimizeForExclusiveConnection< DepthwiseConvolution2dLayer, BatchNormalizationLayer, FuseBatchNorm< DepthwiseConvolution2dLayer, armnn::DataType::Float32 > > FuseBatchNormIntoDepthwiseConvolution2DFloat32
OptimizeForType< BroadcastToLayer, DeleteBroadcastToImpl > BroadcastToOptimizationLayer
OptimizeForType< TransposeLayer, TransposeAsReshapeImpl > TransposeAsReshape
OptimizeForExclusiveConnection< Convolution2dLayer, BatchNormalizationLayer, FuseBatchNorm< Convolution2dLayer, armnn::DataType::Float32 > > FuseBatchNormIntoConvolution2DFloat32
OptimizeForConnection< ConvertFp32ToFp16Layer, ConvertFp16ToFp32Layer, OptimizeInverseConversionsImpl > OptimizeInverseConversionsFp32
OptimizeForType< PermuteLayer, PermuteAsReshapeImpl > PermuteAsReshape
bool IsTfLiteTurboModel(const Graph &optGraph)
BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry &handleFactoryRegistry, BackendSettings &backendSettings)
Optimizer::Optimizations MakeOptimizations(Args &&... args)
bool CheckFp16Support(BackendsMap &backends, const std::vector< BackendId > &availablePreferredBackends)
bool CheckFastMathSupport(const std::vector< BackendId > &availablePreferredBackends, const ModelOptions &modelOptions)
std::vector< BackendOptions > ModelOptions
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
OptimizationResult AssignBackends(OptimizedNetworkImpl *optNetObjPtr, BackendSettings &backendSettings, Graph::Iterator &firstLayer, Graph::Iterator &lastLayer, Optional< std::vector< std::string > & > errMessages)
OptimizationResult ApplyBackendOptimizations(OptimizedNetworkImpl *optNetObjPtr, BackendSettings &backendSettings, BackendsMap &backends, const ModelOptions &modelOptions, Optional< std::vector< std::string > & > errMessages)
void ReportError(const std::string &errorMessage, Optional< std::vector< std::string > & > errorMessages)
BackendRegistry & BackendRegistryInstance()
OptimizationResult SelectTensorHandleStrategy(Graph &optGraph, BackendsMap &backends, TensorHandleFactoryRegistry ®istry, bool importEnabled, bool exportEnabled, Optional< std::vector< std::string > & > errMessages)
@ InferAndValidate
Infer missing output shapes and validate all output shapes.
@ ValidateOnly
Validate all output shapes.
std::map< BackendId, std::unique_ptr< class IBackendInternal > > BackendsMap
std::string CreateDirectory(std::string sPath)
Returns full path to temporary folder.
Struct for the users to pass backend specific options.