ArmNN
 21.08
ClContextSerializerTests.cpp File Reference
#include <armnnUtils/Filesystem.hpp>
#include <cl/test/ClContextControlFixture.hpp>
#include <doctest/doctest.h>
#include <fstream>

Go to the source code of this file.

Functions

 TEST_CASE_FIXTURE (ClContextControlFixture, "ClContextSerializerTest")
 

Function Documentation

◆ TEST_CASE_FIXTURE()

TEST_CASE_FIXTURE ( ClContextControlFixture  ,
"ClContextSerializerTest"   
)

Definition at line 69 of file ClContextSerializerTests.cpp.

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

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