matthiastl's picture
SAVA exported to ONNX: full precision and 8-bit, with model card, runner, benchmarks and scripts
cadfcff
Raw History Blame Contribute Delete
4.41 kB
#include "Bench.h"
#include <mach/mach_time.h>
#include <onnxruntime/onnxruntime_c_api.h>
#include <stdio.h>
#include <stdlib.h>
static char error_text[512];
static double now_ms(void) {
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) mach_timebase_info(&timebase);
return (double)mach_absolute_time() * timebase.numer / timebase.denom / 1e6;
}
#define CHECK(call) \
do { \
OrtStatus *status = (call); \
if (status != NULL) { \
snprintf(error_text, sizeof error_text, "%s", ort->GetErrorMessage(status)); \
ort->ReleaseStatus(status); \
failed = 1; \
goto done; \
} \
} while (0)
const char *sava_bench(const char *model_path, const float *audio, int frames, int threads, int passes,
float *times_ms, float *lookahead, float *turn, double *load_ms) {
const OrtApi *ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
OrtEnv *env = NULL;
OrtSessionOptions *options = NULL;
OrtSession *session = NULL;
OrtMemoryInfo *memory = NULL;
OrtValue *state = NULL;
float *zeros = NULL;
int failed = 0;
const char *input_names[] = {"pcm", "state"};
const char *output_names[] = {"speech", "lookahead", "turn", "new_state"};
const int64_t pcm_shape[] = {1, SAVA_FRAME_SAMPLES};
const int64_t state_shape[] = {1, SAVA_STATE_SIZE};
double load_start = now_ms();
CHECK(ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "savabench", &env));
CHECK(ort->CreateSessionOptions(&options));
CHECK(ort->SetIntraOpNumThreads(options, threads));
CHECK(ort->SetInterOpNumThreads(options, 1));
CHECK(ort->CreateSession(env, model_path, options, &session));
*load_ms = now_ms() - load_start;
CHECK(ort->CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory));
for (int pass = 0; pass < passes; pass++) {
// A stream starts from an all-zero state; after the first frame the model's own output
// tensor is fed straight back in, so no state is copied.
if (state != NULL) ort->ReleaseValue(state);
state = NULL;
free(zeros);
zeros = calloc(SAVA_STATE_SIZE, sizeof(float));
CHECK(ort->CreateTensorWithDataAsOrtValue(memory, zeros, SAVA_STATE_SIZE * sizeof(float), state_shape, 2,
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &state));
for (int i = 0; i < frames; i++) {
OrtValue *pcm = NULL;
OrtValue *outputs[4] = {NULL, NULL, NULL, NULL};
float *value = NULL;
double start = now_ms();
CHECK(ort->CreateTensorWithDataAsOrtValue(memory, (void *)(audio + (size_t)i * SAVA_FRAME_SAMPLES),
SAVA_FRAME_SAMPLES * sizeof(float), pcm_shape, 2,
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &pcm));
const OrtValue *inputs[] = {pcm, state};
CHECK(ort->Run(session, NULL, input_names, inputs, 2, output_names, 4, outputs));
times_ms[i] = (float)(now_ms() - start);
CHECK(ort->GetTensorMutableData(outputs[1], (void **)&value));
lookahead[i] = value[0];
CHECK(ort->GetTensorMutableData(outputs[2], (void **)&value));
turn[i] = value[0];
ort->ReleaseValue(pcm);
ort->ReleaseValue(outputs[0]);
ort->ReleaseValue(outputs[1]);
ort->ReleaseValue(outputs[2]);
ort->ReleaseValue(state);
state = outputs[3];
}
}
done:
if (state != NULL) ort->ReleaseValue(state);
if (memory != NULL) ort->ReleaseMemoryInfo(memory);
if (session != NULL) ort->ReleaseSession(session);
if (options != NULL) ort->ReleaseSessionOptions(options);
if (env != NULL) ort->ReleaseEnv(env);
free(zeros);
return failed ? error_text : NULL;
}