ArmNN
 21.11
CloneTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <Graph.hpp>
7 #include <Layer.hpp>
8 
9 #include <armnn/TypesUtils.hpp>
10 #include <armnn/Exceptions.hpp>
11 #include <armnn/Optional.hpp>
14 
17 
18 #include <doctest/doctest.h>
19 
20 namespace {
21 
22 const armnn::BackendId& GetCloneIdStatic()
23 {
24  static const armnn::BackendId s_Id{"Tests"};
25  return s_Id;
26 }
27 
28 template <typename T>
29 void DeleteAsType(const void* const blob)
30 {
31  delete static_cast<const T*>(blob);
32 }
33 
34 class TestWorkloadFactory : public armnn::WorkloadFactoryBase
35 {
36 public:
37 
38  TestWorkloadFactory(void* ptr)
39  : m_Ptr(ptr)
40  {}
41 
42  const armnn::BackendId& GetBackendId() const override
43  {
44  return GetCloneIdStatic();
45  }
46 
47  std::unique_ptr<armnn::IWorkload> CreatePreCompiled(const armnn::PreCompiledQueueDescriptor& descriptor,
48  const armnn::WorkloadInfo&) const override
49  {
50  CHECK(descriptor.m_PreCompiledObject == m_Ptr);
51  return nullptr;
52  }
53 
54  mutable void* m_Ptr;
55 };
56 
57 TEST_SUITE("CloneTests")
58 {
59 
60 TEST_CASE ("PreCompiledLayerClonePreservesObject")
61 {
62  armnn::Graph graph1;
63  armnn::Graph graph2;
64 
65  armnn::PreCompiledDescriptor descriptor(0u, 0u);
66 
67  armnn::Layer* const preCompiledLayer = graph1.AddLayer<armnn::PreCompiledLayer>(descriptor, "preCompiled");
68  armnn::PreCompiledLayer* layer = armnn::PolymorphicDowncast<armnn::PreCompiledLayer*>(preCompiledLayer);
69  std::unique_ptr<std::string> payload = std::make_unique<std::string>("Hello");
70 
71  armnn::PreCompiledObjectPtr payloadObject(payload.release(), DeleteAsType<std::string>);
72  TestWorkloadFactory factory(payloadObject.get());
73 
74  layer->SetPreCompiledObject(std::move(payloadObject));
75  layer->CreateWorkload(factory);
76 
77  armnn::PreCompiledLayer* clone = layer->Clone(graph2);
78  CHECK(std::strcmp(clone->GetName(), "preCompiled") == 0);
79  clone->CreateWorkload(factory);
80 }
81 
82 TEST_CASE ("PreCompiledLayerCloneNoObject")
83 {
84  armnn::Graph graph1;
85 
86  armnn::Graph graph2;
87 
88  armnn::PreCompiledDescriptor descriptor(0u, 0u);
89 
90  armnn::Layer* const preCompiledLayer = graph1.AddLayer<armnn::PreCompiledLayer>(descriptor, "preCompiled");
91  armnn::PreCompiledLayer* layer = armnn::PolymorphicDowncast<armnn::PreCompiledLayer*>(preCompiledLayer);
92 
93  TestWorkloadFactory factory(nullptr);
94  layer->CreateWorkload(factory);
95 
96  armnn::PreCompiledLayer* clone = layer->Clone(graph2);
97  CHECK(std::strcmp(clone->GetName(), "preCompiled") == 0);
98  clone->CreateWorkload(factory);
99 }
100 
101 }
102 
103 } // end anonymous namespace
TEST_SUITE("TestConstTensorLayerVisitor")
virtual const BackendId & GetBackendId() const =0
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:417
std::unique_ptr< void, PreCompiledObjectDeleter > PreCompiledObjectPtr
PreCompiledLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
void SetPreCompiledObject(PreCompiledObjectPtr preCompiledObject)
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
std::unique_ptr< IWorkload > CreatePreCompiled(const PreCompiledQueueDescriptor &, const WorkloadInfo &) const override
Contains information about TensorInfos of a layer.
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
A PreCompiledDescriptor for the PreCompiledLayer.