ArmNN
 24.02
ClContextDeserializer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
8 
9 #include <armnn/Exceptions.hpp>
11 #include <armnn/Logging.hpp>
12 
13 #include <flatbuffers/flexbuffers.h>
14 
15 #include <fmt/format.h>
16 
17 #include <cstdlib>
18 #include <fstream>
19 #include <iostream>
20 #include <vector>
21 
22 
23 #if defined(__linux__)
24 #define SERIALIZER_USE_MMAP 1
25 #if SERIALIZER_USE_MMAP
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #endif
31 #endif
32 
33 namespace armnn
34 {
35 
36 void ClContextDeserializer::Deserialize(arm_compute::CLCompileContext& clCompileContext,
37  cl::Context& context,
38  cl::Device& device,
39  const std::string& filePath)
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 }
80 
81 void ClContextDeserializer::DeserializeFromBinary(arm_compute::CLCompileContext& clCompileContext,
82  cl::Context& context,
83  cl::Device& device,
84  const std::vector<uint8_t>& binaryContent)
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 }
116 
117 } // namespace armnn
armnn::GetClContext
const armnn::ClContext * GetClContext(const void *buf)
Definition: ClContextSchema_generated.h:140
ClContextSchema_generated.h
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::BoostLogSeverityMapping::error
@ error
NumericCast.hpp
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
Logging.hpp
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
ClContextDeserializer.hpp
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
armnn::ParseException
Definition: Exceptions.hpp:92
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ClContextDeserializer::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.
Definition: ClContextDeserializer.cpp:36