Reputation: 3381
In Android Neural Network API docs says: Creates a shared memory object from a file descriptor.
But I can't find any place that specifies how is the format of this file, on TFL source code:
allocation.cc:
MMAPAllocation::MMAPAllocation(const char* filename,
ErrorReporter* error_reporter)
: Allocation(error_reporter), mmapped_buffer_(MAP_FAILED) {
mmap_fd_ = open(filename, O_RDONLY);
if (mmap_fd_ == -1) {
error_reporter_->Report("Could not open '%s'.", filename);
return;
}
struct stat sb;
fstat(mmap_fd_, &sb);
buffer_size_bytes_ = sb.st_size;
mmapped_buffer_ =
mmap(nullptr, buffer_size_bytes_, PROT_READ, MAP_SHARED, mmap_fd_, 0);
if (mmapped_buffer_ == MAP_FAILED) {
error_reporter_->Report("Mmap of '%s' failed.", filename);
return;
}
}
nnapi_delegate.cc
NNAPIAllocation::NNAPIAllocation(const char* filename,
ErrorReporter* error_reporter)
: MMAPAllocation(filename, error_reporter) {
if (mmapped_buffer_ != MAP_FAILED)
CHECK_NN(ANeuralNetworksMemory_createFromFd(buffer_size_bytes_, PROT_READ,
mmap_fd_, 0, &handle_));
}
It means, TFL opens the file, and give this file to NNAPI. What I need is what is the format of this file that store the tensors, is it a flatbuffers file like TFL format?
Edit: This is a sample from NNAPI doc:
ANeuralNetworksMemory* mem1 = NULL;
int fd = open("training_data", O_RDONLY);
ANeuralNetworksMemory_createFromFd(file_size, PROT_READ, fd, 0, &mem1);
This file training_data
, how must its content be structured to NNAPI understand?
Upvotes: 0
Views: 433
Reputation: 123
ANeuralNetworksMemory_createFromFd(file_size, PROT_READ, fd, 0, &mem1) - This API maps the model file in to ANeuralNetworksMemory.
The mapped address is stored in mem1 (pass by reference!)
Further, trained values that are stored in mem1 (ANeuralNetworksMemory object) is read by pointing to the appropriate offset value and copied in to the tensors of NeuralNetwork model.
ANeuralNetworksModel_setOperandValueFromMemory(model_, tensor0, mem1, offset, size);
ANeuralNetworksModel_setOperandValueFromMemory(model_, tensor1, mem1, offset+size, size);
Upvotes: 1
Reputation: 66
The loading of the model file and the parsing of it are done separately. This makes it easier to mix-and-match between different memory models and different file formats. It also makes it possible to use these building blocks for other functions, like loading inputs from a file.
ANeuralNetworksMemory_createFromFd() is just used to load the model file into memory.
FlatBufferModel::BuildFromFile() gets an allocation (memory block) that represents the model. This is where ANeuralNetworksMemory_createFromFd() gets called. Then it news up a FlatBufferModel object. This calls tflite::GetModel() which is in the schema subdirectory. The schema subdirectory deserializes the flat-buffer-model from the .tflite model loaded into memory.
When NNAPIDelegate::Invoke() is called, the schema Model object is used to build the model in the Android-NN layer using calls like ANeuralNetworksModel_addOperand().
Upvotes: 1