Add support for semantic_lift_quant model to build/run on springbok

semantic_lift_quant can successfully run on qemu/renode with springbok.

Change-Id: Iafceed9100b55d4aa031ca29697d0c93a301ae4f
diff --git a/samples/quant_model_embedding/CMakeLists.txt b/samples/quant_model_embedding/CMakeLists.txt
index 5bf4381..333c243 100644
--- a/samples/quant_model_embedding/CMakeLists.txt
+++ b/samples/quant_model_embedding/CMakeLists.txt
@@ -118,6 +118,21 @@
 
 springbok_bytecode_module(
   NAME
+    semantic_lift_bytecode_module_dylib
+  SRC
+    "$ENV{ROOTDIR}/ml/ml-models/quant_models/semantic_lift_quant.tflite"
+  C_IDENTIFIER
+    "samples_quant_model_embedding_semantic_lift_bytecode_module_dylib"
+  FLAGS
+    "-iree-input-type=tosa"
+    "-riscv-v-vector-bits-min=512"
+    "-riscv-v-fixed-length-vector-lmul-max=8"
+    "-riscv-v-fixed-length-vector-elen-max=32"
+  PUBLIC
+)
+
+springbok_bytecode_module(
+  NAME
     voice_commands_bytecode_module_dylib
   SRC
     "$ENV{ROOTDIR}/ml/ml-models/quant_models/voice_commands_quant.tflite"
@@ -145,7 +160,7 @@
 #
 # to increase it.
 
-# Currently only the daredevil model can run with springbok (b/196910893)
+# Currently only daredevil and semantic_lift can run with springbok (b/196910893)
 
 if(NOT ${BUILD_WITH_SPRINGBOK})
 # TODO(b/198476354): uncomment below after operator tosa.clz is supported
@@ -239,6 +254,18 @@
 
 iree_cc_binary(
   NAME
+    semantic_lift_embedded_sync
+  SRCS
+    "semantic_lift.c"
+  DEPS
+    ::semantic_lift_bytecode_module_dylib_c
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__stack_size__=20k"
+)
+
+iree_cc_binary(
+  NAME
     voice_commands_embedded_sync
   SRCS
     "voice_commands.c"
diff --git a/samples/quant_model_embedding/barcode.c b/samples/quant_model_embedding/barcode.c
index 4f89811..60b3e01 100644
--- a/samples/quant_model_embedding/barcode.c
+++ b/samples/quant_model_embedding/barcode.c
@@ -36,7 +36,7 @@
   // Populate initial value
   srand(768954);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/daredevil.c b/samples/quant_model_embedding/daredevil.c
index c234929..63aea14 100644
--- a/samples/quant_model_embedding/daredevil.c
+++ b/samples/quant_model_embedding/daredevil.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(3689964);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/fssd_25_8bit_v2.c b/samples/quant_model_embedding/fssd_25_8bit_v2.c
index ad72fac..3631f90 100644
--- a/samples/quant_model_embedding/fssd_25_8bit_v2.c
+++ b/samples/quant_model_embedding/fssd_25_8bit_v2.c
@@ -35,7 +35,7 @@
   // Populate initial value
   srand(11111111);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/mobilenet_v1.c b/samples/quant_model_embedding/mobilenet_v1.c
index b75aca5..780d143 100644
--- a/samples/quant_model_embedding/mobilenet_v1.c
+++ b/samples/quant_model_embedding/mobilenet_v1.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(33333333);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/mobilenet_v2.c b/samples/quant_model_embedding/mobilenet_v2.c
index 021171a..ec733e8 100644
--- a/samples/quant_model_embedding/mobilenet_v2.c
+++ b/samples/quant_model_embedding/mobilenet_v2.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(33333333);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/person_detection.c b/samples/quant_model_embedding/person_detection.c
index fc10fd9..a1ae000 100644
--- a/samples/quant_model_embedding/person_detection.c
+++ b/samples/quant_model_embedding/person_detection.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(44444444);
   for (int i = 0; i < model->input_length; ++i) {
-    ((int8_t *)*buffer)[i] = rand() % 256 - 128;
+    ((int8_t *)*buffer)[i] = (int8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/scenenet_v2.c b/samples/quant_model_embedding/scenenet_v2.c
index 9678eae..0e7edc7 100644
--- a/samples/quant_model_embedding/scenenet_v2.c
+++ b/samples/quant_model_embedding/scenenet_v2.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(55555555);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }
diff --git a/samples/quant_model_embedding/semantic_lift.c b/samples/quant_model_embedding/semantic_lift.c
new file mode 100644
index 0000000..eb29774
--- /dev/null
+++ b/samples/quant_model_embedding/semantic_lift.c
@@ -0,0 +1,54 @@
+// Semantic_lift 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:
+#include "samples/quant_model_embedding/semantic_lift_bytecode_module_dylib_c.h"
+
+const MlModel kModel = {
+    .num_input_dim = 4,
+    .input_shape = {1, 128, 128, 3},
+    .input_length = 128 * 128 * 3,
+    .input_size_bytes = sizeof(uint8_t),
+    .num_output = 3,
+    .output_length = {2, 2, 2},
+    .output_size_bytes = sizeof(uint8_t),
+    .hal_element_type = IREE_HAL_ELEMENT_TYPE_UINT_8,
+    .model_name = "semantic_lift_quant",
+};
+
+const iree_const_byte_span_t load_bytecode_module_data() {
+  const struct iree_file_toc_t *module_file_toc =
+      samples_quant_model_embedding_semantic_lift_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(66666666);
+  for (int i = 0; i < model->input_length; ++i) {
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
+  }
+  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;
+}
diff --git a/samples/quant_model_embedding/voice_commands.c b/samples/quant_model_embedding/voice_commands.c
index ca725f3..da4a9e8 100644
--- a/samples/quant_model_embedding/voice_commands.c
+++ b/samples/quant_model_embedding/voice_commands.c
@@ -34,7 +34,7 @@
   // Populate initial value
   srand(77777777);
   for (int i = 0; i < model->input_length; ++i) {
-    ((uint8_t *)*buffer)[i] = rand() % 256;
+    ((uint8_t *)*buffer)[i] = (uint8_t)rand();
   }
   return iree_ok_status();
 }