Jan Eilers | 146b2e1 | 2019-08-16 10:45:30 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | |
| 8 | #include <cl/ClBackend.hpp> |
| 9 | #include <neon/NeonBackend.hpp> |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 10 | #include <reference/RefBackend.hpp> |
Jan Eilers | 146b2e1 | 2019-08-16 10:45:30 +0100 | [diff] [blame] | 11 | |
| 12 | #include <Network.hpp> |
| 13 | |
| 14 | #include <vector> |
| 15 | #include <string> |
| 16 | |
| 17 | using namespace armnn; |
| 18 | |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 19 | BOOST_AUTO_TEST_SUITE(BackendsCompatibility, * boost::unit_test::disabled()) |
Jan Eilers | 146b2e1 | 2019-08-16 10:45:30 +0100 | [diff] [blame] | 20 | |
| 21 | BOOST_AUTO_TEST_CASE(Neon_Cl_DirectCompatibility_Test) |
| 22 | { |
| 23 | auto neonBackend = std::make_unique<NeonBackend>(); |
| 24 | auto clBackend = std::make_unique<ClBackend>(); |
| 25 | |
| 26 | TensorHandleFactoryRegistry registry; |
| 27 | neonBackend->RegisterTensorHandleFactories(registry); |
| 28 | clBackend->RegisterTensorHandleFactories(registry); |
| 29 | |
| 30 | const BackendId& neonBackendId = neonBackend->GetId(); |
| 31 | const BackendId& clBackendId = clBackend->GetId(); |
| 32 | |
| 33 | BackendsMap backends; |
| 34 | backends[neonBackendId] = std::move(neonBackend); |
| 35 | backends[clBackendId] = std::move(clBackend); |
| 36 | |
| 37 | armnn::Graph graph; |
| 38 | |
| 39 | armnn::InputLayer* const inputLayer = graph.AddLayer<armnn::InputLayer>(0, "input"); |
| 40 | |
| 41 | inputLayer->SetBackendId(neonBackendId); |
| 42 | |
| 43 | armnn::SoftmaxDescriptor smDesc; |
| 44 | armnn::SoftmaxLayer* const softmaxLayer1 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax1"); |
| 45 | softmaxLayer1->SetBackendId(clBackendId); |
| 46 | |
| 47 | armnn::SoftmaxLayer* const softmaxLayer2 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax2"); |
| 48 | softmaxLayer2->SetBackendId(neonBackendId); |
| 49 | |
| 50 | armnn::SoftmaxLayer* const softmaxLayer3 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax3"); |
| 51 | softmaxLayer3->SetBackendId(clBackendId); |
| 52 | |
| 53 | armnn::SoftmaxLayer* const softmaxLayer4 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax4"); |
| 54 | softmaxLayer4->SetBackendId(neonBackendId); |
| 55 | |
| 56 | armnn::OutputLayer* const outputLayer = graph.AddLayer<armnn::OutputLayer>(0, "output"); |
| 57 | outputLayer->SetBackendId(clBackendId); |
| 58 | |
| 59 | inputLayer->GetOutputSlot(0).Connect(softmaxLayer1->GetInputSlot(0)); |
| 60 | softmaxLayer1->GetOutputSlot(0).Connect(softmaxLayer2->GetInputSlot(0)); |
| 61 | softmaxLayer2->GetOutputSlot(0).Connect(softmaxLayer3->GetInputSlot(0)); |
| 62 | softmaxLayer3->GetOutputSlot(0).Connect(softmaxLayer4->GetInputSlot(0)); |
| 63 | softmaxLayer4->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 64 | |
| 65 | graph.TopologicalSort(); |
| 66 | |
| 67 | std::vector<std::string> errors; |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 68 | auto result = SelectTensorHandleStrategy(graph, backends, registry, true, errors); |
Jan Eilers | 146b2e1 | 2019-08-16 10:45:30 +0100 | [diff] [blame] | 69 | |
| 70 | BOOST_TEST(result.m_Error == false); |
| 71 | BOOST_TEST(result.m_Warning == false); |
| 72 | |
| 73 | OutputSlot& inputLayerOut = inputLayer->GetOutputSlot(0); |
| 74 | OutputSlot& softmaxLayer1Out = softmaxLayer1->GetOutputSlot(0); |
| 75 | OutputSlot& softmaxLayer2Out = softmaxLayer2->GetOutputSlot(0); |
| 76 | OutputSlot& softmaxLayer3Out = softmaxLayer3->GetOutputSlot(0); |
| 77 | OutputSlot& softmaxLayer4Out = softmaxLayer4->GetOutputSlot(0); |
| 78 | |
| 79 | // Check that the correct factory was selected |
| 80 | BOOST_TEST(inputLayerOut.GetTensorHandleFactoryId() == "Arm/Cl/TensorHandleFactory"); |
| 81 | BOOST_TEST(softmaxLayer1Out.GetTensorHandleFactoryId() == "Arm/Cl/TensorHandleFactory"); |
| 82 | BOOST_TEST(softmaxLayer2Out.GetTensorHandleFactoryId() == "Arm/Cl/TensorHandleFactory"); |
| 83 | BOOST_TEST(softmaxLayer3Out.GetTensorHandleFactoryId() == "Arm/Cl/TensorHandleFactory"); |
| 84 | BOOST_TEST(softmaxLayer4Out.GetTensorHandleFactoryId() == "Arm/Cl/TensorHandleFactory"); |
| 85 | |
| 86 | // Check that the correct strategy was selected |
| 87 | BOOST_TEST((inputLayerOut.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility)); |
| 88 | BOOST_TEST((softmaxLayer1Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility)); |
| 89 | BOOST_TEST((softmaxLayer2Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility)); |
| 90 | BOOST_TEST((softmaxLayer3Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility)); |
| 91 | BOOST_TEST((softmaxLayer4Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility)); |
| 92 | |
| 93 | graph.AddCompatibilityLayers(backends, registry); |
| 94 | |
| 95 | // Test for copy layers |
| 96 | int copyCount= 0; |
| 97 | graph.ForEachLayer([©Count](Layer* layer) |
| 98 | { |
| 99 | if (layer->GetType() == LayerType::MemCopy) |
| 100 | { |
| 101 | copyCount++; |
| 102 | } |
| 103 | }); |
| 104 | BOOST_TEST(copyCount == 0); |
| 105 | |
| 106 | // Test for import layers |
| 107 | int importCount= 0; |
| 108 | graph.ForEachLayer([&importCount](Layer *layer) |
| 109 | { |
| 110 | if (layer->GetType() == LayerType::MemImport) |
| 111 | { |
| 112 | importCount++; |
| 113 | } |
| 114 | }); |
| 115 | BOOST_TEST(importCount == 0); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE_END() |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 119 | |
| 120 | BOOST_AUTO_TEST_SUITE(BackendCapability) |
| 121 | |
Colm Donelan | 92c5072 | 2021-03-29 14:59:04 +0100 | [diff] [blame] | 122 | #if defined(ARMNNREF_ENABLED) |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 123 | |
Colm Donelan | 92c5072 | 2021-03-29 14:59:04 +0100 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(Ref_Backends_Capability_Test) |
| 125 | { |
| 126 | auto refBackend = std::make_unique<RefBackend>(); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 127 | BOOST_CHECK(refBackend->HasCapability(armnn::BackendCapability::NonConstWeights)); |
Sadik Armagan | aede8ca | 2021-03-31 16:12:13 +0100 | [diff] [blame] | 128 | |
| 129 | BOOST_CHECK(!refBackend->HasCapability(armnn::BackendCapability::AsyncExecution)); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Colm Donelan | 92c5072 | 2021-03-29 14:59:04 +0100 | [diff] [blame] | 132 | #endif |
| 133 | |
| 134 | #if defined(ARMCOMPUTENEON_ENABLED) |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(Neon_Backends_Capability_Test) |
| 137 | { |
| 138 | auto neonBackend = std::make_unique<NeonBackend>(); |
| 139 | BOOST_CHECK(!neonBackend->HasCapability(armnn::BackendCapability::NonConstWeights)); |
| 140 | } |
| 141 | |
| 142 | #endif |
| 143 | |
| 144 | #if defined(ARMCOMPUTECL_ENABLED) |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(Cl_Backends_Capability_Test) |
| 147 | { |
| 148 | auto clBackend = std::make_unique<ClBackend>(); |
| 149 | BOOST_CHECK(!clBackend->HasCapability(armnn::BackendCapability::NonConstWeights)); |
| 150 | } |
| 151 | |
| 152 | #endif |
| 153 | |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 154 | BOOST_AUTO_TEST_SUITE_END() |