ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ModelBuilder Class Reference

#include <ModelBuilder.h>

Public Member Functions

virtual ~ModelBuilder ()=default
 
int addOperand (const ANeuralNetworksOperandType &type)
 
int setOperandValue (uint32_t index, const void *buffer, size_t length)
 
int setOperandValueFromMemory (uint32_t index, const Memory *memory, uint32_t offset, size_t length)
 
int addOperation (OperationType type, uint32_t inputCount, const uint32_t *inputs, uint32_t outputCount, const uint32_t *outputs)
 
int identifyInputsAndOutputs (uint32_t inputCount, const uint32_t *inputs, uint32_t outputCount, const uint32_t *outputs)
 
int finish ()
 
bool isFinished () const
 
int createCompilation (CompilationBuilder **compilation)
 
void publish (Model *model) const
 
uint32_t operandCount () const
 
uint32_t operationCount () const
 
uint32_t inputCount () const
 
uint32_t getInputOperandIndex (uint32_t i) const
 
const OperandgetInputOperand (uint32_t i) const
 
uint32_t outputCount () const
 
uint32_t getOutputOperandIndex (uint32_t i) const
 
const OperandgetOutputOperand (uint32_t i) const
 
const OperandgetOperand (uint32_t index) const
 
const OperationgetOperation (uint32_t index) const
 
const MemoryTrackergetMemories () const
 
const std::vector< Operation > & getOperations () const
 

Detailed Description

Definition at line 33 of file ModelBuilder.h.

Constructor & Destructor Documentation

◆ ~ModelBuilder()

virtual ModelBuilder::~ModelBuilder ( )
virtualdefault

Member Function Documentation

◆ addOperand()

int ModelBuilder::addOperand ( const ANeuralNetworksOperandType type)

Definition at line 83 of file ModelBuilder.cpp.

84{
85 if (badState("addOperand"))
86 {
88 }
89
90 int n = validateOperandType(type, "ANeuralNetworksModel_addOperand", true);
92 {
93 return n;
94 }
95 size_t idx = mOperands.size();
96 if (idx >= MAX_NUMBER_OF_OPERANDS)
97 {
98 LOG(ERROR) << "ANeuralNetworksModel_addOperand exceed max operands";
100 }
101 mOperands.resize(idx + 1);
102 auto &operand = mOperands[idx];
103 operand.type = static_cast<OperandType>(type.type);
104 setFromIntList(&operand.dimensions, type.dimensionCount, type.dimensions);
105 operand.numberOfConsumers = 0;
106 operand.scale = type.scale;
107 operand.zeroPoint = type.zeroPoint;
108 operand.lifetime = OperandLifeTime::TEMPORARY_VARIABLE;
109 operand.location = {.poolIndex = 0, .offset = 0, .length = 0};
111}
const uint32_t MAX_NUMBER_OF_OPERANDS
OperandType
Definition OperandType.h:24
int validateOperandType(const ANeuralNetworksOperandType &type, const char *tag, bool allowPartial)
#define LOG(...)
Definition Logging.h:36
@ ANEURALNETWORKS_BAD_DATA
@ ANEURALNETWORKS_NO_ERROR
@ ANEURALNETWORKS_BAD_STATE
type
Definition infer.py:18

References ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, LOG, MAX_NUMBER_OF_OPERANDS, TEMPORARY_VARIABLE, and validateOperandType().

Referenced by ANeuralNetworksModel_addOperand().

◆ addOperation()

int ModelBuilder::addOperation ( OperationType  type,
uint32_t  inputCount,
const uint32_t *  inputs,
uint32_t  outputCount,
const uint32_t *  outputs 
)

Definition at line 214 of file ModelBuilder.cpp.

216{
217
218 if (badState("addOperation"))
219 {
221 }
222
223 if (!validateOperationType(type))
224 {
225 LOG(ERROR) << "ANeuralNetworksModel_addOperation invalid operations type "
226 << static_cast<uint32_t>(type);
228 }
230 "ANeuralNetworksModel_addOperation inputs");
232 {
233 return n;
234 }
236 "ANeuralNetworksModel_addOperation outputs");
238 {
239 return n;
240 }
241
242 uint32_t operationIndex = operationCount();
243 if (operationIndex >= MAX_NUMBER_OF_OPERATIONS)
244 {
245 LOG(ERROR) << "ANeuralNetworksModel_addOperation exceed max operations";
247 }
248 mOperations.resize(operationIndex + 1);
249 auto &entry = mOperations[operationIndex];
250 entry.type = type;
251
252 setFromIntList(&entry.inputs, inputCount, inputs);
253 setFromIntList(&entry.outputs, outputCount, outputs);
254 for (uint32_t i : entry.inputs)
255 {
256 mOperands[i].numberOfConsumers++;
257 // TODO mOperands[i].consumers.push_back(operationIndex);
258 }
260}
const uint32_t MAX_NUMBER_OF_OPERATIONS
int validateOperationType(const OperationType &type)
int validateOperandList(uint32_t count, const uint32_t *list, uint32_t operandCount, const char *tag)
uint32_t operandCount() const
uint32_t inputCount() const
uint32_t operationCount() const
uint32_t outputCount() const
int entry(const int argc, char **argv)
Definition Driver.cpp:53

References ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, entry(), inputCount(), LOG, MAX_NUMBER_OF_OPERATIONS, operandCount(), operationCount(), outputCount(), validateOperandList(), and validateOperationType().

Referenced by ANeuralNetworksModel_addOperation().

◆ createCompilation()

int ModelBuilder::createCompilation ( CompilationBuilder **  compilation)

Definition at line 322 of file ModelBuilder.cpp.

323{
324 if (!mCompletedModel || mInvalidModel)
325 {
326 LOG(ERROR) << "ANeuralNetworksCompilation_create passed an unfinished model";
327 *compilation = nullptr;
329 }
330 *compilation = new CompilationBuilder(this);
332}
@ ANEURALNETWORKS_OUT_OF_MEMORY

References ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, ANEURALNETWORKS_OUT_OF_MEMORY, and LOG.

◆ finish()

int ModelBuilder::finish ( )

Definition at line 334 of file ModelBuilder.cpp.

335{
336 if (mCompletedModel)
337 {
338 LOG(ERROR) << "ANeuralNetworksModel_finish called more than once";
340 }
341 if (mInvalidModel)
342 {
343 LOG(ERROR) << "ANeuralNetworksModel_finish called on an invalid model";
345 }
346
347 int n = copyLargeValuesToMemory();
349 {
350 return n;
351 }
352
353 Model modelForValidation;
354 publish(&modelForValidation);
355 if (!validateModel(modelForValidation))
356 {
357 LOG(ERROR) << "ANeuralNetworksModel_finish called on invalid model";
358 mInvalidModel = true;
360 }
361
362 // We sort the operations so that they will be in the appropriate
363 // order for a single-threaded, op at a time execution.
364 // TODO: we don't need this if we always run the partitioner.
365 sortIntoRunOrder();
366 mCompletedModel = true;
368}
bool validateModel(const Model &model)
void publish(Model *model) const
Definition Model.h:27

References ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, LOG, publish(), and validateModel().

Referenced by ANeuralNetworksModel_finish().

◆ getInputOperand()

const Operand & ModelBuilder::getInputOperand ( uint32_t  i) const
inline

Definition at line 80 of file ModelBuilder.h.

80{ return mOperands[getInputOperandIndex(i)]; }
uint32_t getInputOperandIndex(uint32_t i) const

References getInputOperandIndex().

Referenced by ExecutionBuilder::setInput(), and ExecutionBuilder::setInputFromMemory().

◆ getInputOperandIndex()

uint32_t ModelBuilder::getInputOperandIndex ( uint32_t  i) const
inline

Definition at line 79 of file ModelBuilder.h.

79{ return mInputIndexes[i]; }

Referenced by getInputOperand().

◆ getMemories()

const MemoryTracker & ModelBuilder::getMemories ( ) const
inline

Definition at line 92 of file ModelBuilder.h.

92{ return mMemories; }

◆ getOperand()

const Operand & ModelBuilder::getOperand ( uint32_t  index) const
inline

Definition at line 88 of file ModelBuilder.h.

88{ return mOperands[index]; }
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

◆ getOperation()

const Operation & ModelBuilder::getOperation ( uint32_t  index) const
inline

Definition at line 89 of file ModelBuilder.h.

89{ return mOperations[index]; }

◆ getOperations()

const std::vector< Operation > & ModelBuilder::getOperations ( ) const
inline

Definition at line 93 of file ModelBuilder.h.

93{ return mOperations; }

◆ getOutputOperand()

const Operand & ModelBuilder::getOutputOperand ( uint32_t  i) const
inline

Definition at line 85 of file ModelBuilder.h.

85{ return mOperands[getOutputOperandIndex(i)]; }
uint32_t getOutputOperandIndex(uint32_t i) const

References getOutputOperandIndex().

Referenced by ExecutionBuilder::setOutput(), and ExecutionBuilder::setOutputFromMemory().

◆ getOutputOperandIndex()

uint32_t ModelBuilder::getOutputOperandIndex ( uint32_t  i) const
inline

Definition at line 84 of file ModelBuilder.h.

84{ return mOutputIndexes[i]; }

Referenced by getOutputOperand().

◆ identifyInputsAndOutputs()

int ModelBuilder::identifyInputsAndOutputs ( uint32_t  inputCount,
const uint32_t *  inputs,
uint32_t  outputCount,
const uint32_t *  outputs 
)

Definition at line 262 of file ModelBuilder.cpp.

264{
265 if (badState("identifyInputsAndOutputs"))
266 {
268 }
269
271 "ANeuralNetworksModel_identifyInputsAndOutputs inputs");
273 {
274 return n;
275 }
277 "ANeuralNetworksModel_identifyInputsAndOutputs outputs");
279 {
280 return n;
281 }
282
283 // Makes a copy of the index list, validates the arguments, and changes
284 // the lifetime info of the corresponding operand.
285 auto setArguments = [&](std::vector<uint32_t> *indexVector, uint32_t indexCount,
286 const uint32_t *indexList, OperandLifeTime lifetime) -> bool {
287 indexVector->resize(indexCount);
288 for (uint32_t i = 0; i < indexCount; i++)
289 {
290 const uint32_t operandIndex = indexList[i];
291 if (operandIndex >= mOperands.size())
292 {
293 LOG(ERROR) << "ANeuralNetworksModel_identifyInputsAndOutputs Can't set input or output "
294 "to be "
295 << operandIndex << " as this exceeds the numbe of operands " << mOperands.size();
296 return false;
297 }
298 (*indexVector)[i] = operandIndex;
299 Operand &operand = mOperands[operandIndex];
301 {
302 LOG(ERROR) << "ANeuralNetworksModel_identifyInputsAndOutputs Can't set operand "
303 << operandIndex
304 << " to be an input or output. Check that it's not a constant or "
305 "already an input or output";
306 return false;
307 }
308 operand.lifetime = lifetime;
309 }
310 return true;
311 };
312
313 if (!setArguments(&mInputIndexes, inputCount, inputs, OperandLifeTime::MODEL_INPUT) ||
314 !setArguments(&mOutputIndexes, outputCount, outputs, OperandLifeTime::MODEL_OUTPUT))
315 {
317 }
318
320}
OperandLifeTime
Definition Operand.h:26
OperandLifeTime lifetime
Definition Operand.h:51

References ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, inputCount(), Operand::lifetime, LOG, MODEL_INPUT, MODEL_OUTPUT, operandCount(), outputCount(), TEMPORARY_VARIABLE, and validateOperandList().

Referenced by ANeuralNetworksModel_identifyInputsAndOutputs().

◆ inputCount()

uint32_t ModelBuilder::inputCount ( ) const
inline

Definition at line 78 of file ModelBuilder.h.

78{ return static_cast<uint32_t>(mInputIndexes.size()); }

Referenced by addOperation(), and identifyInputsAndOutputs().

◆ isFinished()

bool ModelBuilder::isFinished ( ) const
inline

Definition at line 57 of file ModelBuilder.h.

57{ return mCompletedModel; }

◆ operandCount()

uint32_t ModelBuilder::operandCount ( ) const
inline

Definition at line 66 of file ModelBuilder.h.

67 {
68 // We don't allow more than uint32_t worth of operands
69 return static_cast<uint32_t>(mOperands.size());
70 }

Referenced by addOperation(), identifyInputsAndOutputs(), setOperandValue(), and setOperandValueFromMemory().

◆ operationCount()

uint32_t ModelBuilder::operationCount ( ) const
inline

Definition at line 71 of file ModelBuilder.h.

72 {
73 // We don't allow more than uint32_t worth of operations
74 return static_cast<uint32_t>(mOperations.size());
75 }

Referenced by addOperation().

◆ outputCount()

uint32_t ModelBuilder::outputCount ( ) const
inline

Definition at line 83 of file ModelBuilder.h.

83{ return static_cast<uint32_t>(mOutputIndexes.size()); }

Referenced by addOperation(), and identifyInputsAndOutputs().

◆ publish()

void ModelBuilder::publish ( Model model) const

Definition at line 425 of file ModelBuilder.cpp.

426{
427 model->operands = mOperands;
428 model->operations = mOperations;
429 model->inputIndexes = mInputIndexes;
430 model->outputIndexes = mOutputIndexes;
431 model->operandValues = mSmallOperandValues;
432
433 uint32_t count = mMemories.size();
434 model->pools.resize(count);
435 for (uint32_t i = 0; i < count; i++)
436 {
437 uint8_t *buffer;
438 mMemories[i]->getPointer(&buffer);
439 model->pools[i] = buffer;
440 }
441}
uint32_t size() const

References MemoryTracker::size().

Referenced by finish(), and ExecutionBuilder::startCompute().

◆ setOperandValue()

int ModelBuilder::setOperandValue ( uint32_t  index,
const void *  buffer,
size_t  length 
)

Definition at line 113 of file ModelBuilder.cpp.

114{
115 if (badState("setOperandValue"))
116 {
118 }
119
120 VLOG(MODEL) << __func__ << " for operand " << index << " size " << length;
121 if (index >= operandCount())
122 {
123 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue setting operand " << index << " of "
124 << operandCount();
126 }
127 Operand &operand = mOperands[index];
128 if (buffer == nullptr)
129 {
130 if (length)
131 {
132 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue buffer is nullptr but length is "
133 "not 0";
135 }
137 // The location is unused and is set to zeros.
138 operand.location = {.poolIndex = 0, .offset = 0, .length = 0};
139 }
140 else
141 {
142 if (length > 0xFFFFFFFF)
143 {
144 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue value length of " << length
145 << " exceeds max size";
147 }
148 uint32_t valueLength = static_cast<uint32_t>(length);
149 uint32_t neededLength = sizeOfData(operand.type, operand.dimensions);
150 if (neededLength != valueLength)
151 {
152 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue setting " << valueLength
153 << " bytes when needing " << neededLength;
155 }
157 {
158 uint32_t existingSize = static_cast<uint32_t>(mSmallOperandValues.size());
159 uint32_t extraBytes = alignBytesNeeded(existingSize, valueLength);
160 mSmallOperandValues.resize(existingSize + extraBytes + valueLength);
162 operand.location = {
163 .poolIndex = 0, .offset = existingSize + extraBytes, .length = neededLength};
164 memcpy(&mSmallOperandValues[operand.location.offset], buffer, valueLength);
165 VLOG(MODEL) << "Copied small value to offset " << operand.location.offset;
166 }
167 else
168 {
169 VLOG(MODEL) << "Saving large value";
171 // The values for poolIndex and offset will be set when the model is finished.
172 operand.location = {.poolIndex = 0, .offset = 0, .length = valueLength};
173 // We keep track of the buffers. We'll allocate the shared memory only
174 // once we know the total size, to avoid needless copies.
175 mLargeOperandValues.push_back(LargeValue{.operandIndex = index, .buffer = buffer});
176 }
177 }
179}
uint32_t alignBytesNeeded(uint32_t index, size_t length)
#define VLOG(...)
Definition Logging.h:37
uint32_t sizeOfData(const Operand &operand)
Definition Operand.h:56
@ ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES
uint32_t poolIndex
Definition Operand.h:36
uint32_t offset
Definition Operand.h:37
std::vector< uint32_t > dimensions
Definition Operand.h:46
OperandType type
Definition Operand.h:42
DataLocation location
Definition Operand.h:48

References alignBytesNeeded(), ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES, ANEURALNETWORKS_NO_ERROR, CONSTANT_COPY, CONSTANT_REFERENCE, Operand::dimensions, Operand::lifetime, Operand::location, LOG, NO_VALUE, DataLocation::offset, operandCount(), DataLocation::poolIndex, sizeOfData(), Operand::type, and VLOG.

Referenced by ANeuralNetworksModel_setOperandValue().

◆ setOperandValueFromMemory()

int ModelBuilder::setOperandValueFromMemory ( uint32_t  index,
const Memory memory,
uint32_t  offset,
size_t  length 
)

Definition at line 181 of file ModelBuilder.cpp.

183{
184 VLOG(MODEL) << __func__ << " for operand " << index << " offset " << offset << " size " << length;
185 if (badState("setOperandValueFromMemory"))
186 {
188 }
189
190 if (index >= operandCount())
191 {
192 LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromMemory setting operand " << index
193 << " of " << operandCount();
195 }
196 Operand &operand = mOperands[index];
197 uint32_t neededLength = sizeOfData(operand.type, operand.dimensions);
198 if (neededLength != length)
199 {
200 LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromMemory setting " << length
201 << " bytes when needing " << neededLength;
203 }
204 if (!memory->validateSize(offset, length))
205 {
207 }
208 // TODO validate does not exceed length of memory
210 operand.location = {.poolIndex = mMemories.add(memory), .offset = offset, .length = neededLength};
212}
uint32_t add(const Memory *memory)
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
virtual bool validateSize(uint32_t offset, uint32_t length) const =0

References MemoryTracker::add(), ANEURALNETWORKS_BAD_DATA, ANEURALNETWORKS_BAD_STATE, ANEURALNETWORKS_NO_ERROR, CONSTANT_REFERENCE, Operand::dimensions, Operand::lifetime, Operand::location, LOG, offset(), operandCount(), DataLocation::poolIndex, sizeOfData(), Operand::type, Memory::validateSize(), and VLOG.

Referenced by ANeuralNetworksModel_setOperandValueFromMemory().


The documentation for this class was generated from the following files: