// Daredevil float model
// MlModel struct initialization to include model I/O info.
// Bytecode loading, input/output processes.

#include <springbok.h>

#include "iree/base/api.h"
#include "iree/hal/api.h"
#include "samples/util/util.h"

// Compiled module embedded here to avoid file IO:
#include "samples/float_model_embedding/daredevil_bytecode_module_dylib_c.h"

const MlModel kModel = {
    .num_input_dim = 3,
    .input_shape = {1, 96, 64},
    .input_length = 96 * 64,
    .input_size_bytes = sizeof(float),
    .num_output = 1,
    .output_length = {527},
    .output_size_bytes = sizeof(float),
    .hal_element_type = IREE_HAL_ELEMENT_TYPE_FLOAT_32,
    .entry_func = "module.main",
    .model_name = "daredevil_float",
};

const iree_const_byte_span_t load_bytecode_module_data() {
  const struct iree_file_toc_t *module_file_toc =
      samples_float_model_embedding_daredevil_bytecode_module_dylib_create();
  return iree_make_const_byte_span(module_file_toc->data,
                                   module_file_toc->size);
}

iree_status_t load_input_data(const MlModel *model, void **buffer) {
  // Populate initial value
  srand(3689964);
  for (int i = 0; i < model->input_length; ++i) {
    int x = rand();
    ((float *)*buffer)[i] = (float)x / (float)RAND_MAX;
  }
  return iree_ok_status();
}

iree_status_t check_output_data(const MlModel *model,
                                iree_hal_buffer_mapping_t *mapped_memory,
                                int index_output) {
  iree_status_t result = iree_ok_status();
  if (index_output > model->num_output ||
      mapped_memory->contents.data_length / model->output_size_bytes !=
          model->output_length[index_output]) {
    result = iree_make_status(IREE_STATUS_UNKNOWN, "output length mismatches");
  }
  LOG_INFO("Output #%d data length: %d\n", index_output,
           mapped_memory->contents.data_length / model->output_size_bytes);
  return result;
}
