Search code examples
androidc++ctensorflow-litennapi

How works ANeuralNetworksMemory_createFromFd?


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?


Solution

  • 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);
    
    • tensor0 - pointing at offset
    • tensor1 - pointing at offset+size