/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Barcode quant 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:
#if !defined(BUILD_EMITC)
#include "samples/quant_model/barcode_bytecode_module_static.h"
#include "samples/quant_model/barcode_bytecode_module_static_c.h"
#else
#include "samples/quant_model/barcode_c_module_static_c.h"
#include "samples/quant_model/barcode_c_module_static_emitc.h"
#endif

const MlModel kModel = {
    .num_input = 1,
    .num_input_dim = {4},
    .input_shape = {{1, 320, 320, 1}},
    .input_length = {320 * 320 * 1},
    .input_size_bytes = {sizeof(uint8_t)},
    .num_output = 12,
    .output_length = {20 * 20 * 36, 20 * 20 * 9, 10 * 10 * 72, 10 * 10 * 18,
                      5 * 5 * 72, 5 * 5 * 18, 3 * 3 * 72, 3 * 3 * 18,
                      2 * 2 * 72, 2 * 2 * 18, 72, 18},
    .output_size_bytes = sizeof(uint8_t),
    .hal_element_type = IREE_HAL_ELEMENT_TYPE_UINT_8,
    .entry_func = "module.main",
    .model_name = "barcode_quant",
};

iree_status_t create_module(iree_vm_module_t **module) {
#if !defined(BUILD_EMITC)
  const struct iree_file_toc_t *module_file_toc =
      samples_quant_model_barcode_bytecode_module_static_create();
  return iree_vm_bytecode_module_create(
      iree_make_const_byte_span(module_file_toc->data, module_file_toc->size),
      iree_allocator_null(), iree_allocator_system(), module);
#else
  return module_create(iree_allocator_system(), module);
#endif
}

const iree_hal_executable_library_header_t **library_query(
    iree_hal_executable_library_version_t max_version, void *reserved) {
#if !defined(BUILD_EMITC)
  return barcode_bytecode_module_static_linked_llvm_library_query(
      max_version,
      /*reserved=*/reserved);
#else
  return barcode_c_module_static_linked_llvm_library_query(
      max_version,
      /*reserved=*/reserved);
#endif
}

iree_status_t load_input_data(const MlModel *model, void **buffer,
                              iree_byte_span_t **byte_span) {
  iree_status_t result = alloc_input_buffer(model, buffer);
  // Populate initial value
  srand(768954);
  if (iree_status_is_ok(result)) {
    for (int i = 0; i < model->input_length[0]; ++i) {
      ((uint8_t *)*buffer)[i] = (uint8_t)rand();
    }
  }
  byte_span[0] = malloc(sizeof(iree_byte_span_t));
  *byte_span[0] = iree_make_byte_span(
      buffer[0], model->input_size_bytes[0] * model->input_length[0]);
  return result;
}

iree_status_t process_output(const MlModel *model,
                             iree_hal_buffer_mapping_t *buffers,
                             MlOutput *output) {
  return iree_ok_status();
}
