|
| 1 | +// Copyright(C) 2025 Advanced Micro Devices, Inc. All rights reserved. |
| 2 | +#include "vitisai/whisper-vitisai-encoder.h" |
| 3 | +#include "FlexMLClient.h" |
| 4 | +#include "ggml.h" |
| 5 | +#include "ggml-backend.h" |
| 6 | + |
| 7 | +#include <cstdio> |
| 8 | +#include <cstdlib> |
| 9 | +#ifdef _WIN32 |
| 10 | + #include <windows.h> |
| 11 | +#else |
| 12 | + #include <sys/mman.h> |
| 13 | + #include <sys/stat.h> |
| 14 | + #include <fcntl.h> |
| 15 | +#endif |
| 16 | +#include <cstring> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +struct whisper_vitisai_context { |
| 20 | + std::string model_path; |
| 21 | + std::shared_ptr<flexmlrt::client::Model> runner; |
| 22 | + uint8_t * fbs_buffer; |
| 23 | + size_t fbs_buffer_size; |
| 24 | +}; |
| 25 | + |
| 26 | +// Function to mmap rai file for Linux and MapViewOfFile for Windows |
| 27 | +bool map_rai_file(const char * path, uint8_t ** buffer, size_t * size) { |
| 28 | +#ifdef _WIN32 |
| 29 | + // Open the file |
| 30 | + HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 31 | + if (hFile == INVALID_HANDLE_VALUE) { |
| 32 | + std::fprintf(stderr, "%s: %d: Failed to open rai file '%s'\n", __func__, __LINE__, path); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + // Get the file size |
| 37 | + LARGE_INTEGER fileSize; |
| 38 | + if (!GetFileSizeEx(hFile, &fileSize)) { |
| 39 | + CloseHandle(hFile); |
| 40 | + std::fprintf(stderr, "%s: %d: Failed to get file size for rai file '%s'\n", __func__, __LINE__, path); |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // Create a file mapping object |
| 45 | + HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, fileSize.QuadPart, NULL); |
| 46 | + if (hMapping == NULL) { |
| 47 | + CloseHandle(hFile); |
| 48 | + std::fprintf(stderr, "%s: %d: Failed to create file mapping for rai file '%s'\n", __func__, __LINE__, path); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Map the file |
| 53 | + *buffer = (uint8_t *)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, fileSize.QuadPart); |
| 54 | + if (*buffer == NULL) { |
| 55 | + CloseHandle(hMapping); |
| 56 | + CloseHandle(hFile); |
| 57 | + std::fprintf(stderr, "%s: %d: Failed to map rai file '%s'\n", __func__, __LINE__, path); |
| 58 | + return false; |
| 59 | + } |
| 60 | + *size = fileSize.QuadPart; |
| 61 | + return true; |
| 62 | +#else |
| 63 | + // Open the file |
| 64 | + FILE * fd = fopen(path, "rb"); |
| 65 | + if (!fd) { |
| 66 | + std::fprintf(stderr, "%s: %d: Failed to open rai file '%s'\n", __func__, __LINE__, path); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // Get the file size |
| 71 | + struct stat st; |
| 72 | + if (fstat(fileno(fd), &st) == -1) { |
| 73 | + fclose(fd); |
| 74 | + std::fprintf(stderr, "%s: %d: Failed to get file size for rai file '%s'\n", __func__, __LINE__, path); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + // Mmap the file |
| 79 | + *buffer = (uint8_t *)mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fileno(fd), 0); |
| 80 | + if (*buffer == MAP_FAILED) { |
| 81 | + fclose(fd); |
| 82 | + std::fprintf(stderr, "%s: %d: Failed to mmap rai file '%s'\n", __func__, __LINE__, path); |
| 83 | + return false; |
| 84 | + } |
| 85 | + *size = st.st_size; |
| 86 | + return true; |
| 87 | +#endif // _WIN32 |
| 88 | +} |
| 89 | + |
| 90 | +void unmap_rai_file(uint8_t * buffer, size_t size) { |
| 91 | +#ifdef _WIN32 |
| 92 | + UnmapViewOfFile(buffer); |
| 93 | +#else |
| 94 | + munmap(buffer, size); |
| 95 | +#endif // _WIN32 |
| 96 | +} |
| 97 | + |
| 98 | +struct whisper_vitisai_context * whisper_vitisai_init(const char * path_model) { |
| 99 | + if (!path_model) { |
| 100 | + std::fprintf(stderr, "%s: path_model is null\n", __func__); |
| 101 | + return nullptr; |
| 102 | + } |
| 103 | + |
| 104 | + auto * ctx = new whisper_vitisai_context; |
| 105 | + ctx->model_path = path_model; |
| 106 | + |
| 107 | + // Override the model path with the environment variable if it is set |
| 108 | + if (const char * env_model_path = std::getenv("OVERRIDE_VITISAI_MODEL_PATH")) { |
| 109 | + if (env_model_path[0] != '\0') { |
| 110 | + ctx->model_path = env_model_path; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Step 1: Set up the model |
| 115 | + flexmlrt::client::Options options; |
| 116 | + options.modelPath = ctx->model_path; |
| 117 | + options.deviceName = "stx"; |
| 118 | + options.debug = false; |
| 119 | + options.executeMode = 2; |
| 120 | + options.extOptions["ai_analyzer_profiling"] = true; // Enable AIA profiling |
| 121 | + options.extOptions["enable_preemption"] = true; |
| 122 | + |
| 123 | + // Check if model_path is rai file and if so, add fbs_buffer and fbs_buffer_size to the options |
| 124 | + if (ctx->model_path.find(".rai") != std::string::npos) { |
| 125 | + // mmap rai file for both Linux and Windows and pass the buffer to the options |
| 126 | + ctx->fbs_buffer = nullptr; |
| 127 | + ctx->fbs_buffer_size = 0; |
| 128 | + if (map_rai_file(ctx->model_path.c_str(), &ctx->fbs_buffer, &ctx->fbs_buffer_size)) { |
| 129 | + options.extOptions["fbs_buffer"] = ctx->fbs_buffer; |
| 130 | + options.extOptions["fbs_buffer_size"] = ctx->fbs_buffer_size; |
| 131 | + options.subgraphName = "vaiml_par_0"; |
| 132 | + options.extOptions["cache_dir"] = std::string("."); |
| 133 | + } else { |
| 134 | + std::fprintf(stderr, "%s: Failed to mmap rai file '%s'\n", __func__, ctx->model_path.c_str()); |
| 135 | + delete ctx; |
| 136 | + return nullptr; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + ctx->runner = std::make_shared<flexmlrt::client::Model>(options); |
| 142 | + |
| 143 | + if (!ctx->runner->good()) { |
| 144 | + throw std::runtime_error("Runner creation ran into an error"); |
| 145 | + } |
| 146 | + } catch (const std::exception & e) { |
| 147 | + std::fprintf(stderr, "%s: Exception during Vitis AI runner creation: %s\n", __func__, e.what()); |
| 148 | + delete ctx; |
| 149 | + return nullptr; |
| 150 | + } |
| 151 | + return ctx; |
| 152 | +} |
| 153 | + |
| 154 | +void whisper_vitisai_free(struct whisper_vitisai_context * ctx) { |
| 155 | + if (!ctx) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + std::fprintf(stderr, "%s: releasing Vitis AI encoder context for model '%s'\n", __func__, ctx->model_path.c_str()); |
| 160 | + if (ctx->fbs_buffer) { |
| 161 | + unmap_rai_file(ctx->fbs_buffer, ctx->fbs_buffer_size); |
| 162 | + } |
| 163 | + delete ctx; |
| 164 | +} |
| 165 | + |
| 166 | +int whisper_vitisai_encode(struct whisper_vitisai_context * ctx, struct ggml_tensor * mel, struct ggml_tensor * out) { |
| 167 | + if (!ctx || !mel || !out) { |
| 168 | + std::fprintf(stderr, "%s: ctx/mel/out must not be null\n", __func__); |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + if (ggml_n_dims(mel) != 2) { |
| 173 | + std::fprintf(stderr, "%s: mel tensor expected to have 2 dims, got %d\n", __func__, ggml_n_dims(mel)); |
| 174 | + return 0; |
| 175 | + } |
| 176 | + |
| 177 | + if (ggml_n_dims(out) != 2) { |
| 178 | + std::fprintf(stderr, "%s: out tensor expected to have 2 dims, got %d\n", __func__, ggml_n_dims(out)); |
| 179 | + return 0; |
| 180 | + } |
| 181 | + |
| 182 | + // setup input and output tensors for Vitis AI model |
| 183 | + std::vector<flexmlrt::client::ErtTensorType> input_tensors, output_tensors; |
| 184 | + auto model = ctx->runner; |
| 185 | + |
| 186 | + // Get tensors as CPU tensors (hwTensor = false) |
| 187 | + input_tensors = model->getIOTensors("input", false); |
| 188 | + output_tensors = model->getIOTensors("output", false); |
| 189 | + |
| 190 | + // TODO: add assert checks for tensor numbers and shapes |
| 191 | + |
| 192 | + input_tensors[0].data = mel->data; |
| 193 | + output_tensors[0].data = out->data; |
| 194 | + |
| 195 | + try { |
| 196 | + model->forward(input_tensors, output_tensors); |
| 197 | + std::fprintf(stdout, "%s: Vitis AI model inference completed.\n", __func__); |
| 198 | + } catch (const std::exception & e) { |
| 199 | + std::fprintf(stderr, "%s: Exception during model inference: %s\n", __func__, e.what()); |
| 200 | + return 0; |
| 201 | + } |
| 202 | + |
| 203 | + return 1; |
| 204 | +} |
0 commit comments