-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flexmlrt.cpp
More file actions
134 lines (115 loc) · 5.4 KB
/
Copy pathtest_flexmlrt.cpp
File metadata and controls
134 lines (115 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#endif
#define FLEXML_LOADER_DEBUG 1
#include "FlexMLClient.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stdexcept>
#ifdef _WIN32
static bool map_file(const char * path, uint8_t ** buf, size_t * sz) {
HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "open failed: %s\n", path); return false; }
LARGE_INTEGER fs;
if (!GetFileSizeEx(hFile, &fs)) { CloseHandle(hFile); return false; }
HANDLE hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, fs.QuadPart, NULL);
if (!hMap) { CloseHandle(hFile); return false; }
*buf = (uint8_t *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, (SIZE_T)fs.QuadPart);
if (!*buf) { CloseHandle(hMap); CloseHandle(hFile); return false; }
*sz = (size_t)fs.QuadPart;
return true;
}
static void unmap_file(uint8_t * buf, size_t) { UnmapViewOfFile(buf); }
#endif
int main(int argc, char ** argv) {
if (argc < 2) {
fprintf(stderr, "Usage: test_flexmlrt <model_dir> [subgraph_name]\n");
fprintf(stderr, " model_dir: directory containing the compiled .rai model\n");
fprintf(stderr, " e.g.: test_flexmlrt models vaiml_par_0\n");
return 1;
}
const char * rai_path = argv[1];
const char * subgraph = argc > 2 ? argv[2] : "vaiml_par_0";
fprintf(stderr, "=== FlexmlRT standalone test (fbs_buffer mode) ===\n");
fprintf(stderr, " rai : %s\n", rai_path);
fprintf(stderr, " subgraph : %s\n", subgraph);
uint8_t * buf = nullptr;
size_t sz = 0;
if (!map_file(rai_path, &buf, &sz)) {
fprintf(stderr, "FAIL: could not mmap %s\n", rai_path);
return 1;
}
fprintf(stderr, " mmap ok : %zu bytes\n", sz);
flexmlrt::client::Options opts;
opts.modelPath = rai_path;
opts.deviceName = "stx";
opts.debug = true;
opts.executeMode = 2;
opts.extOptions["enable_preemption"] = true;
opts.extOptions["fbs_buffer"] = buf;
opts.extOptions["fbs_buffer_size"] = sz;
opts.extOptions["cache_dir"] = std::string(".");
opts.subgraphName = subgraph;
fprintf(stderr, " calling Model() constructor ...\n");
try {
auto model = std::make_shared<flexmlrt::client::Model>(opts);
fprintf(stderr, " constructor returned, good()=%s\n", model->good() ? "true" : "false");
if (!model->good()) {
fprintf(stderr, "FAIL: good() returned false\n");
return 1;
}
auto inputs = model->getIOTensors("input", false);
auto outputs = model->getIOTensors("output", false);
fprintf(stderr, " inputs : %zu\n", inputs.size());
fprintf(stderr, " outputs: %zu\n", outputs.size());
auto dump_meta = [](const char * tag, size_t i, flexmlrt::client::ErtTensorType & t) {
try {
const auto & m = t.getMetadata();
std::string shp;
for (auto d : m.shape) shp += std::to_string(d) + ",";
fprintf(stderr, " %s[%zu] idx=%d size=%zu type=%s fmt=%s shape=[%s]\n",
tag, i, t.idx, m.size,
flexmlrt::client::toString(m.type).c_str(),
flexmlrt::client::toString(m.format).c_str(),
shp.c_str());
} catch (const std::exception & e) {
fprintf(stderr, " %s[%zu] idx=%d (getMetadata failed: %s)\n", tag, i, t.idx, e.what());
}
};
for (size_t i = 0; i < inputs.size(); ++i) dump_meta("in", i, inputs[i]);
for (size_t i = 0; i < outputs.size(); ++i) dump_meta("out", i, outputs[i]);
// --- forward with dummy buffers matching the COMPILED model metadata ---
// in[0] = bool mask [1,188] (188 bytes) -- length encoded as a frame mask
// in[1] = audio fp32 [1,128,1498] (766976 bytes)
// out[0]= fp32 [1,1024,188] (770048 bytes)
const int M = 128, T = 1498, D = 1024, Tp = 188;
const int n_mel_real = 1101;
const int subsampl = 8;
const int n_valid = (n_mel_real + subsampl - 1) / subsampl; // ceil -> 138
std::vector<float> audio((size_t)M * T, 0.1f);
std::vector<uint8_t> mask((size_t)Tp, 0); // 1 byte per bool
for (int i = 0; i < Tp; ++i) mask[i] = (i < n_valid) ? 1 : 0; // true = valid frame
std::vector<float> out_buf((size_t)D * Tp, 0.0f);
if (inputs.size() < 2 || outputs.empty()) {
fprintf(stderr, "FAIL: unexpected IO counts\n");
return 1;
}
inputs[0].data = mask.data(); // bool mask
inputs[1].data = audio.data(); // audio_signal
outputs[0].data = out_buf.data();
fprintf(stderr, " calling forward (n_valid=%d/%d) ...\n", n_valid, Tp);
model->forward(inputs, outputs);
fprintf(stderr, " forward returned\n");
// print a few output values to confirm it actually wrote something
fprintf(stderr, " out[0..4] = %f %f %f %f %f\n",
out_buf[0], out_buf[1], out_buf[2], out_buf[3], out_buf[4]);
fprintf(stderr, "SUCCESS\n");
} catch (const std::exception & e) {
fprintf(stderr, "FAIL exception: %s\n", e.what());
return 1;
}
return 0;
}