| 290 | } |
| 291 | |
| 292 | OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph, |
| 293 | const ModelOptions& modelOptions) const |
| 294 | { |
| 295 | OptimizationViews optimizationViews(modelOptions); |
| 296 | |
| 297 | auto it = subgraph.end(); |
| 298 | bool isFastMathEnabled = false; |
| 299 | std::map<LayerGuid, Layer*> untouched; |
| 300 | |
| 301 | while (it != subgraph.begin()) |
| 302 | { |
| 303 | --it; |
| 304 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
| 305 | untouched.insert({base.GetGuid(), &base}); |
| 306 | } |
| 307 | |
| 308 | it = subgraph.end(); |
| 309 | #if defined(ARMCOMPUTECL_ENABLED) |
| 310 | IBackendInternal::IBackendSpecificModelContextPtr modelContextPtr = CreateBackendSpecificModelContext(modelOptions); |
| 311 | |
| 312 | if (modelContextPtr) |
| 313 | { |
| 314 | auto clModelOptions = dynamic_cast<ClBackendModelContext*>(modelContextPtr.get()); |
| 315 | if (clModelOptions) |
| 316 | { |
| 317 | isFastMathEnabled = clModelOptions->IsFastMathEnabled(); |
| 318 | } |
| 319 | } |
| 320 | #endif |
| 321 | while (it != subgraph.begin()) |
| 322 | { |
| 323 | --it; |
| 324 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
| 325 | |
| 326 | // Fuse activation into previous layer if supported by backend |
| 327 | if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d |
| 328 | || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected |
| 329 | || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication |
| 330 | || base.GetType() == LayerType::Subtraction || base.GetType() == LayerType::Division |
| 331 | || base.GetType() == LayerType::ElementwiseBinary) |
| 332 | && (base.GetAdditionalInformation<ActivationDescriptor>() == nullptr)) |
| 333 | { |
| 334 | for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output) |
| 335 | { |
| 336 | if (output->GetNumConnections() == 1) |
| 337 | { |
| 338 | for (auto&& childInput : output->GetConnections()) |
| 339 | { |
| 340 | if ((childInput->GetOwningLayer().GetType() == LayerType::Activation) && |
| 341 | (checkDataTypeInputandOutput(childInput->GetOwningLayer()))) |
| 342 | { |
| 343 | Layer& child = childInput->GetOwningLayer(); |
| 344 | |
| 345 | auto* activationLayer = PolymorphicDowncast<ActivationLayer*>(&child); |
| 346 | // Before we proceed make sure that this activation layer is in the subgraph. It could be |
| 347 | // the first layer in the next subgraph. |
| 348 | if (untouched.find(activationLayer->GetGuid()) == untouched.end()) |
| 349 | { |
nothing calls this directly
no test coverage detected