ArmNN
 24.02
ClContextDeserializer Class Reference

#include <ClContextDeserializer.hpp>

Public Member Functions

 ClContextDeserializer ()=default
 
 ~ClContextDeserializer ()=default
 
void Deserialize (arm_compute::CLCompileContext &clCompileContext, cl::Context &context, cl::Device &device, const std::string &filePath)
 Deserializes the CLCompileContext built-in programs from a binary file. More...
 
void DeserializeFromBinary (arm_compute::CLCompileContext &clCompileContext, cl::Context &context, cl::Device &device, const std::vector< uint8_t > &binaryContent)
 Deserializes the CLCompileContext built-in programs from binary file contents. More...
 

Detailed Description

Definition at line 13 of file ClContextDeserializer.hpp.

Constructor & Destructor Documentation

◆ ClContextDeserializer()

ClContextDeserializer ( )
default

◆ ~ClContextDeserializer()

~ClContextDeserializer ( )
default

Member Function Documentation

◆ Deserialize()

void Deserialize ( arm_compute::CLCompileContext &  clCompileContext,
cl::Context &  context,
cl::Device &  device,
const std::string &  filePath 
)

Deserializes the CLCompileContext built-in programs from a binary file.

Parameters
[in]clCompileContextThe CLCompileContext to be serialized
[in]contextThe CL Kernel context built-in program will be created from
[in]deviceThe CL Kernel device built-in program will be created from
[in]filePathThe serialized file

Definition at line 36 of file ClContextDeserializer.cpp.

40 {
41  std::vector<std::uint8_t> binaryContent;
42 #if !SERIALIZER_USE_MMAP
43  std::ifstream inputFileStream(filePath, std::ios::binary);
44  while (inputFileStream)
45  {
46  char input;
47  inputFileStream.get(input);
48  if (inputFileStream)
49  {
50  binaryContent.push_back(static_cast<std::uint8_t>(input));
51  }
52  }
53  inputFileStream.close();
54 #else
55  struct stat statbuf;
56  int fp = open(filePath.c_str(),O_RDONLY);
57  if (!fp)
58  {
59  ARMNN_LOG(error) << (std::string("Cannot open file ") + filePath);
60  return;
61  }
62  fstat(fp,&statbuf);
63  const unsigned long dataSize = static_cast<unsigned long>(statbuf.st_size);
64  binaryContent.resize(static_cast<long unsigned int>(dataSize));
65  void* ptrmem = mmap(NULL, dataSize,PROT_READ,MAP_PRIVATE,fp,0);
66  if(ptrmem!=MAP_FAILED)
67  {
68  memcpy (binaryContent.data(), ptrmem, dataSize);
69  }
70  close(fp);
71  if(ptrmem == MAP_FAILED)
72  {
73  ARMNN_LOG(error) << (std::string("Cannot map file ") + filePath);
74  return;
75  }
76 #endif
77 
78  DeserializeFromBinary(clCompileContext, context, device, binaryContent);
79 }

References ARMNN_LOG, ClContextDeserializer::DeserializeFromBinary(), and armnn::error.

◆ DeserializeFromBinary()

void DeserializeFromBinary ( arm_compute::CLCompileContext &  clCompileContext,
cl::Context &  context,
cl::Device &  device,
const std::vector< uint8_t > &  binaryContent 
)

Deserializes the CLCompileContext built-in programs from binary file contents.

Parameters
[in]clCompileContextThe CLCompileContext to be serialized
[in]contextThe CL Kernel context built-in program will be created from
[in]deviceThe CL Kernel device built-in program will be created from
[in]filePathThe serialized file

Definition at line 81 of file ClContextDeserializer.cpp.

85 {
86  if (binaryContent.data() == nullptr)
87  {
88  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
89  CHECK_LOCATION().AsString()));
90  }
91 
92  size_t binaryContentSize = binaryContent.size();
93  flatbuffers::Verifier verifier(binaryContent.data(), binaryContentSize);
94  if (verifier.VerifyBuffer<ClContext>() == false)
95  {
96  throw ParseException(fmt::format("Buffer doesn't conform to the expected Armnn "
97  "flatbuffers format. size:{0} {1}",
98  binaryContentSize,
99  CHECK_LOCATION().AsString()));
100  }
101  auto clContext = GetClContext(binaryContent.data());
102 
103  for (Program const* program : *clContext->programs())
104  {
105  const char* volatile programName = program->name()->c_str();
106  auto programBinary = program->binary();
107  std::vector<uint8_t> binary(programBinary->begin(), programBinary->begin() + programBinary->size());
108 
109  cl::Program::Binaries binaries{ binary };
110  std::vector<cl::Device> devices {device};
111  cl::Program theProgram(context, devices, binaries);
112  theProgram.build();
113  clCompileContext.add_built_program(programName, theProgram);
114  }
115 }

References CHECK_LOCATION, and armnn::GetClContext().

Referenced by ClContextDeserializer::Deserialize().


The documentation for this class was generated from the following files:
armnn::GetClContext
const armnn::ClContext * GetClContext(const void *buf)
Definition: ClContextSchema_generated.h:140
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::ClContextDeserializer::DeserializeFromBinary
void DeserializeFromBinary(arm_compute::CLCompileContext &clCompileContext, cl::Context &context, cl::Device &device, const std::vector< uint8_t > &binaryContent)
Deserializes the CLCompileContext built-in programs from binary file contents.
Definition: ClContextDeserializer.cpp:81