1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
17 * 1.1 - added memory information
20 #define HEADER_MAJOR 1
21 #define HEADER_MINOR 1
24 * @brief Header version 1.0
28 *@brief MagicNumber – GNAM in ascii table, equals to hex 0x474e414d
32 * @brief if header size is not equal to sizeof ModelHeader - some reserved data append in the end of header
33 * usually it is an indicator of working with version of model different that is current export function produce
35 uint32_t headerSize = 0u;
38 * @details Version of format Major – unsigned int, ex: 0x0001
39 * every change in the header or in the layers definition should be reflected in version change
40 * for backward compatibility new parsers can read old versions of model with certain restrictions
44 * @details Version of Format Minor – unsigned int, corresponding to build revision for example
45 * changes in minor version are not affected layout of model
50 * @brief Memory required to be allocated using GNAAlloc()
52 uint64_t gnaMemSize = 0ull;
54 * @brief Number of GNA Layers
56 uint64_t layersCount = 0ull;
59 * @brief Grouping level
64 * Convolution related setting - they are affecting input transformation
66 uint32_t nRotateRows = 0u;
67 uint32_t nRotateColumns = 0u;
72 * if scale factor is different then pased into infer , network might need to be requantized
74 float scaleFactor = 0.f;
76 * Offset in bytes of pointer descriptor
78 uint64_t descriptor_offset = 0ull;
80 * Endpoint resolution in bytes.
82 uint32_t element_size = 0u;
86 uint32_t elements_count = 0u;
92 * Reserved Data might be here
98 * @brief implements serialisation tasks for GNAGraph
100 class GNAModelSerial {
103 * In runtime endpoint mostly same as in serial version, except pf descriptor field
105 struct RuntimeEndPoint {
107 * if scale factor is different then pased into infer , network might need to be requantized
109 float scaleFactor = 0;
113 void* descriptor_ptr = nullptr;
115 * Endpoint resolution in bytes.
117 uint32_t element_size = 0;
121 uint32_t elements_count = 0;
123 RuntimeEndPoint() = default;
124 RuntimeEndPoint(double scaleFactor,
125 void* descriptor_ptr,
126 uint32_t element_size,
127 uint32_t elements_count) : scaleFactor(scaleFactor),
128 descriptor_ptr(descriptor_ptr),
129 element_size(element_size),
130 elements_count(elements_count) {
133 using MemoryType = std::vector<std::pair<void*, uint32_t>>;
136 intel_nnet_type_t *ptr_nnet;
137 RuntimeEndPoint input, output;
138 uint32_t nRotateRows = 0;
139 uint32_t nRotateColumns = 0;
141 MemoryType states, *pstates = nullptr;
146 * @brief Used for import/export
148 * @param inputScale - in/out parameter representing input scale factor
149 * @param outputScale - in/out parameter representing output scale factor
151 GNAModelSerial(intel_nnet_type_t *ptr_nnet, MemoryType &states_holder)
152 : ptr_nnet(ptr_nnet) , pstates(&states_holder) {
156 * @brief used for export only since runtime params are not passed by pointer
161 intel_nnet_type_t *ptr_nnet,
162 RuntimeEndPoint input,
163 RuntimeEndPoint output) : ptr_nnet(ptr_nnet), input(input), output(output) {
166 GNAModelSerial & SetInputRotation(uint32_t nRotateRows, uint32_t nRotateColumns) {
167 this->nRotateColumns = nRotateColumns;
168 this->nRotateRows = nRotateRows;
173 * mark certain part of gna_blob as state (in future naming is possible)
174 * @param descriptor_ptr
178 GNAModelSerial & AddState(void* descriptor_ptr, size_t size) {
179 states.emplace_back(descriptor_ptr, size);
184 * @brief calculate memory required for import gna graph
185 * @param is - opened input stream
188 static ModelHeader ReadHeader(std::istream &is);
191 * @brief Import model from FS into preallocated buffer,
192 * buffers for pLayers, and pStructs are allocated here and required manual deallocation using mm_free
195 * @param is - stream without header structure - TBD heder might be needed
197 void Import(void *basePointer, size_t gnaGraphSize, std::istream &is);
200 * save gna graph to an outpus stream
203 * @param gnaGraphSize
206 void Export(void *basePtr,
208 std::ostream &os) const;