ArmNN
 22.05
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>
16 
17 #include <doctest/doctest.h>
18 
19 namespace {
20 
21 const armnn::BackendId& GetCloneIdStatic()
22 {
23  static const armnn::BackendId s_Id{"Tests"};
24  return s_Id;
25 }
26 
27 template <typename T>
28 void DeleteAsType(const void* const blob)
29 {
30  delete static_cast<const T*>(blob);
31 }
32 
33 class TestWorkloadFactory : public armnn::WorkloadFactoryBase
34 {
35 public:
36 
37  TestWorkloadFactory(void* ptr)
38  : m_Ptr(ptr)
39  {}
40 
41  const armnn::BackendId& GetBackendId() const override
42  {
43  return GetCloneIdStatic();
44  }
45 
46  std::unique_ptr<armnn::IWorkload> CreatePreCompiled(const armnn::PreCompiledQueueDescriptor& descriptor,
47  const armnn::WorkloadInfo&) const override
48  {
49  CHECK(descriptor.m_PreCompiledObject == m_Ptr);
50  return nullptr;
51  }
52 
53  mutable void* m_Ptr;
54 };
55 
56 TEST_SUITE("CloneTests")
57 {
58 
59 TEST_CASE ("PreCompiledLayerClonePreservesObject")
60 {
61  armnn::Graph graph1;
62  armnn::Graph graph2;
63 
64  armnn::PreCompiledDescriptor descriptor(0u, 0u);
65 
66  armnn::Layer* const preCompiledLayer = graph1.AddLayer<armnn::PreCompiledLayer>(descriptor, "preCompiled");
67  armnn::PreCompiledLayer* layer = armnn::PolymorphicDowncast<armnn::PreCompiledLayer*>(preCompiledLayer);
68  std::unique_ptr<std::string> payload = std::make_unique<std::string>("Hello");
69 
70  armnn::PreCompiledObjectPtr payloadObject(payload.release(), DeleteAsType<std::string>);
71  TestWorkloadFactory factory(payloadObject.get());
72 
73  layer->SetPreCompiledObject(std::move(payloadObject));
74  layer->CreateWorkload(factory);
75 
76  armnn::PreCompiledLayer* clone = layer->Clone(graph2);
77  CHECK(std::strcmp(clone->GetName(), "preCompiled") == 0);
78  clone->CreateWorkload(factory);
79 }
80 
81 TEST_CASE ("PreCompiledLayerCloneNoObject")
82 {
83  armnn::Graph graph1;
84 
85  armnn::Graph graph2;
86 
87  armnn::PreCompiledDescriptor descriptor(0u, 0u);
88 
89  armnn::Layer* const preCompiledLayer = graph1.AddLayer<armnn::PreCompiledLayer>(descriptor, "preCompiled");
90  armnn::PreCompiledLayer* layer = armnn::PolymorphicDowncast<armnn::PreCompiledLayer*>(preCompiledLayer);
91 
92  TestWorkloadFactory factory(nullptr);
93  layer->CreateWorkload(factory);
94 
95  armnn::PreCompiledLayer* clone = layer->Clone(graph2);
96  CHECK(std::strcmp(clone->GetName(), "preCompiled") == 0);
97  clone->CreateWorkload(factory);
98 }
99 
100 }
101 
102 } // 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:425
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:317
A PreCompiledDescriptor for the PreCompiledLayer.