ArmNN
 21.02
ClContextSerializerTests.cpp File Reference
#include <Filesystem.hpp>
#include <cl/test/ClContextControlFixture.hpp>
#include <boost/test/unit_test.hpp>
#include <fstream>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (ClContextSerializerTest)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( ClContextSerializerTest  )

Definition at line 71 of file ClContextSerializerTests.cpp.

References BOOST_AUTO_TEST_SUITE_END(), IRuntime::Create(), armnn::GpuAcc, OptimizerOptions::m_ModelOptions, armnnUtils::Filesystem::NamedTempFile(), armnn::Optimize(), and armnn::Success.

72 {
73  // Get tmp directory and create blank file.
74  fs::path filePath = armnnUtils::Filesystem::NamedTempFile("Armnn-CachedNetworkFileTest-TempFile.bin");
75  std::string const filePathString{filePath.string()};
76  std::ofstream file { filePathString };
77 
78  // Create runtime in which test will run
81 
82  std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc};
83 
84  // Create two networks.
85  // net1 will serialize and save context to file.
86  // net2 will deserialize context saved from net1 and load.
87  armnn::INetworkPtr net1 = CreateNetwork();
88  armnn::INetworkPtr net2 = CreateNetwork();
89 
90  // Add specific optimizerOptions to each network.
91  armnn::OptimizerOptions optimizerOptions1;
92  armnn::OptimizerOptions optimizerOptions2;
93  armnn::BackendOptions modelOptions1("GpuAcc",
94  {{"SaveCachedNetwork", true}, {"CachedNetworkFilePath", filePathString}});
95  armnn::BackendOptions modelOptions2("GpuAcc",
96  {{"SaveCachedNetwork", false}, {"CachedNetworkFilePath", filePathString}});
97  optimizerOptions1.m_ModelOptions.push_back(modelOptions1);
98  optimizerOptions2.m_ModelOptions.push_back(modelOptions2);
99 
101  *net1, backends, runtime->GetDeviceSpec(), optimizerOptions1);
103  *net2, backends, runtime->GetDeviceSpec(), optimizerOptions2);
104  BOOST_CHECK(optNet1);
105  BOOST_CHECK(optNet2);
106 
107  // Cached file should be empty until net1 is loaded into runtime.
108  BOOST_TEST(fs::is_empty(filePathString));
109 
110  // Load net1 into the runtime.
111  armnn::NetworkId netId1;
112  BOOST_TEST(runtime->LoadNetwork(netId1, std::move(optNet1)) == armnn::Status::Success);
113 
114  // File should now exist and not be empty. It has been serialized.
115  BOOST_TEST(fs::exists(filePathString));
116  std::vector<char> dataSerialized = ReadBinaryFile(filePathString);
117  BOOST_TEST(dataSerialized.size() != 0);
118 
119  // Load net2 into the runtime using file and deserialize.
120  armnn::NetworkId netId2;
121  BOOST_TEST(runtime->LoadNetwork(netId2, std::move(optNet2)) == armnn::Status::Success);
122 
123  // Run inference and get output data.
124  std::vector<uint8_t> outputData1(5);
125  RunInference(netId1, runtime, outputData1);
126 
127  std::vector<uint8_t> outputData2(5);
128  RunInference(netId2, runtime, outputData2);
129 
130  // Compare outputs from both networks.
131  BOOST_CHECK_EQUAL_COLLECTIONS(outputData1.begin(), outputData1.end(),
132  outputData2.begin(), outputData2.end());
133 
134  // Remove temp file created.
135  fs::remove(filePath);
136 }
ModelOptions m_ModelOptions
Definition: INetwork.hpp:168
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:37
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:26
int NetworkId
Definition: IRuntime.hpp:20
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1502
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:174
GPU Execution: OpenCL: ArmCompute.
Struct for the users to pass backend specific options.
fs::path NamedTempFile(const char *fileName)
Construct a temporary file name.
Definition: Filesystem.cpp:23
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173