Skip to content

Commit d91c62c

Browse files
committed
update whisper runner
1 parent d942981 commit d91c62c

565 files changed

Lines changed: 176846 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

projects/llm_framework/main_whisper/src/runner/AudioFile.h

Lines changed: 1293 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************************************************************
2+
*
3+
* Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
4+
*
5+
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
6+
* may not be copied or distributed in any isomorphic form without the prior
7+
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
8+
*
9+
**************************************************************************************************/
10+
11+
#pragma once
12+
13+
#include "EngineWrapper.hpp"
14+
15+
class DecoderLoop : public EngineWrapper {
16+
public:
17+
DecoderLoop() = default;
18+
~DecoderLoop() = default;
19+
20+
// int Run() {
21+
// int ret = 0;
22+
// ret = this->Run();
23+
// if (ret) {
24+
// return ret;
25+
// }
26+
27+
// // ret = SetInput(GetOutputPtr(1), 1);
28+
// // if (ret) {
29+
// // return ret;
30+
// // }
31+
32+
// // ret = SetInput(GetOutputPtr(2), 2);
33+
// // if (ret) {
34+
// // return ret;
35+
// // }
36+
37+
// return ret;
38+
// }
39+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**************************************************************************************************
2+
*
3+
* Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
4+
*
5+
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
6+
* may not be copied or distributed in any isomorphic form without the prior
7+
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
8+
*
9+
**************************************************************************************************/
10+
11+
#pragma once
12+
13+
#include "EngineWrapper.hpp"
14+
15+
class DecoderMain : public EngineWrapper {
16+
public:
17+
DecoderMain() = default;
18+
~DecoderMain() = default;
19+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**************************************************************************************************
2+
*
3+
* Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
4+
*
5+
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
6+
* may not be copied or distributed in any isomorphic form without the prior
7+
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
8+
*
9+
**************************************************************************************************/
10+
11+
#pragma once
12+
13+
#include <vector>
14+
#include "EngineWrapper.hpp"
15+
16+
class Encoder : public EngineWrapper {
17+
public:
18+
Encoder() = default;
19+
~Encoder() = default;
20+
};
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/**************************************************************************************************
2+
*
3+
* Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
4+
*
5+
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
6+
* may not be copied or distributed in any isomorphic form without the prior
7+
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
8+
*
9+
**************************************************************************************************/
10+
#include "EngineWrapper.hpp"
11+
#include "utils/io.hpp"
12+
13+
#include <cstdlib>
14+
15+
16+
static const char *strAlgoModelType[AX_ENGINE_MODEL_TYPE_BUTT] = {"HalfOCM", "FullOCM"};
17+
18+
19+
/// @brief npu type
20+
typedef enum axNPU_TYPE_E {
21+
AX_NPU_DEFAULT = 0, /* running under default NPU according to system */
22+
AX_STD_VNPU_1 = (1 << 0), /* running under STD VNPU1 */
23+
AX_STD_VNPU_2 = (1 << 1), /* running under STD VNPU2 */
24+
AX_STD_VNPU_3 = (1 << 2), /* running under STD VNPU3 */
25+
AX_BL_VNPU_1 = (1 << 3), /* running under BIG-LITTLE VNPU1 */
26+
AX_BL_VNPU_2 = (1 << 4) /* running under BIG-LITTLE VNPU2 */
27+
} AX_NPU_TYPE_E;
28+
29+
30+
static AX_S32 CheckModelVNpu(const std::string &strModel, const AX_ENGINE_MODEL_TYPE_T &eModelType, const AX_S32 &nNpuType, AX_U32 &nNpuSet) {
31+
AX_ENGINE_NPU_ATTR_T stNpuAttr;
32+
memset(&stNpuAttr, 0x00, sizeof(stNpuAttr));
33+
34+
auto ret = AX_ENGINE_GetVNPUAttr(&stNpuAttr);
35+
if (ret == 0) {
36+
// VNPU DISABLE
37+
if (stNpuAttr.eHardMode == AX_ENGINE_VIRTUAL_NPU_DISABLE) {
38+
nNpuSet = 0x01; // NON-VNPU (0b111)
39+
// ALOGN("%s will run under VNPU-DISABLE [%s]", strModel.c_str(), strAlgoModelType[eModelType]);
40+
}
41+
// STD VNPU
42+
else if (stNpuAttr.eHardMode == AX_ENGINE_VIRTUAL_NPU_ENABLE) {
43+
// full ocm model was no allowned
44+
if (eModelType == AX_ENGINE_MODEL_TYPE1) {
45+
// printf("%s model type%d: [%s], no allow run under STD VNPU", strModel.c_str(), eModelType, strAlgoModelType[eModelType]);
46+
return -1;
47+
}
48+
49+
// default STD VNPU2
50+
if (nNpuType == 0) {
51+
nNpuSet = 0x02; // VNPU2 (0b010)
52+
// printf("%s will run under default STD-VNPU2 [%s]", strModel.c_str(), strAlgoModelType[eModelType]);
53+
}
54+
else {
55+
if (nNpuType & AX_STD_VNPU_1) {
56+
nNpuSet |= 0x01; // VNPU1 (0b001)
57+
// printf("%s will run under STD-VNPU1 [%s]", strModel.c_str(), strAlgoModelType[eModelType]);
58+
}
59+
if (nNpuType & AX_STD_VNPU_2) {
60+
nNpuSet |= 0x02; // VNPU2 (0b010)
61+
// printf("%s will run under STD-VNPU2 [%s]", strModel.c_str(), strAlgoModelType[eModelType]);
62+
}
63+
}
64+
}
65+
}
66+
else {
67+
printf("AX_ENGINE_GetVNPUAttr fail ret = %x", ret);
68+
}
69+
70+
return ret;
71+
}
72+
73+
int EngineWrapper::Init(const char* strModelPath, uint32_t nNpuType)
74+
{
75+
AX_S32 ret = 0;
76+
77+
// 1. load model
78+
AX_BOOL bLoadModelUseCmm = AX_FALSE;
79+
AX_CHAR *pModelBufferVirAddr = nullptr;
80+
AX_U64 u64ModelBufferPhyAddr = 0;
81+
AX_U32 nModelBufferSize = 0;
82+
83+
std::vector<char> model_buffer;
84+
85+
if (bLoadModelUseCmm) {
86+
if (!utils::read_file(strModelPath, (AX_VOID **)&pModelBufferVirAddr, u64ModelBufferPhyAddr, nModelBufferSize)) {
87+
printf("ALGO read model(%s) fail\n", strModelPath);
88+
return -1;
89+
}
90+
}
91+
else {
92+
if (!utils::read_file(strModelPath, model_buffer)) {
93+
printf("ALGO read model(%s) fail\n", strModelPath);
94+
return -1;
95+
}
96+
97+
pModelBufferVirAddr = model_buffer.data();
98+
nModelBufferSize = model_buffer.size();
99+
}
100+
101+
auto freeModelBuffer = [&]() {
102+
if (bLoadModelUseCmm) {
103+
if (u64ModelBufferPhyAddr != 0) {
104+
AX_SYS_MemFree(u64ModelBufferPhyAddr, &pModelBufferVirAddr);
105+
}
106+
}
107+
else {
108+
std::vector<char>().swap(model_buffer);
109+
}
110+
return;
111+
};
112+
113+
// 1.1 Get Model Type
114+
AX_ENGINE_MODEL_TYPE_T eModelType = AX_ENGINE_MODEL_TYPE0;
115+
ret = AX_ENGINE_GetModelType(pModelBufferVirAddr, nModelBufferSize, &eModelType);
116+
if (0 != ret || eModelType >= AX_ENGINE_MODEL_TYPE_BUTT) {
117+
printf("%s AX_ENGINE_GetModelType fail ret=%x, eModelType=%d\n", strModelPath, eModelType);
118+
freeModelBuffer();
119+
return -1;
120+
}
121+
122+
// 1.2 Check VNPU
123+
AX_ENGINE_NPU_SET_T nNpuSet = 0;
124+
ret = CheckModelVNpu(strModelPath, eModelType, nNpuType, nNpuSet);
125+
if (0 != ret) {
126+
printf("ALGO CheckModelVNpu fail\n");
127+
freeModelBuffer();
128+
return -1;
129+
}
130+
131+
// 2. create handle
132+
AX_ENGINE_HANDLE handle = nullptr;
133+
ret = AX_ENGINE_CreateHandle(&handle, pModelBufferVirAddr, nModelBufferSize);
134+
auto deinit_handle = [&handle]() {
135+
if (handle) {
136+
AX_ENGINE_DestroyHandle(handle);
137+
}
138+
return -1;
139+
};
140+
141+
freeModelBuffer();
142+
143+
if (0 != ret || !handle) {
144+
printf("ALGO Create model(%s) handle fail\n", strModelPath);
145+
146+
return deinit_handle();
147+
}
148+
149+
// 3. create context
150+
ret = AX_ENGINE_CreateContext(handle);
151+
if (0 != ret) {
152+
return deinit_handle();
153+
}
154+
155+
// 4. set io
156+
m_io_info = nullptr;
157+
ret = AX_ENGINE_GetIOInfo(handle, &m_io_info);
158+
if (0 != ret) {
159+
return deinit_handle();
160+
}
161+
m_input_num = m_io_info->nInputSize;
162+
m_output_num = m_io_info->nOutputSize;
163+
164+
// 4.1 query io
165+
// AX_IMG_FORMAT_E eDtype;
166+
// ret = utils::query_model_input_size(m_io_info, m_input_size, eDtype);//FIXME.
167+
// if (0 != ret) {
168+
// printf("model(%s) query model input size fail\n", strModelPath.c_str());
169+
// return deinit_handle();
170+
// }
171+
172+
// if (!(eDtype == AX_FORMAT_YUV420_SEMIPLANAR || eDtype == AX_FORMAT_YUV420_SEMIPLANAR_VU ||
173+
// eDtype == AX_FORMAT_RGB888 || eDtype == AX_FORMAT_BGR888)) {
174+
// printf("model(%s) data type is: 0x%02X, unsupport\n", strModelPath, eDtype);
175+
// return deinit_handle();
176+
// }
177+
178+
// 4.2 brief io
179+
#ifdef __DEBUG__
180+
printf("brief_io_info\n");
181+
utils::brief_io_info(strModelPath, m_io_info);
182+
#endif
183+
184+
//5. Config VNPU
185+
// printf("model(%s) nNpuSet: 0x%08X\n", strModelPath.c_str(), nNpuSet);
186+
// will do nothing for using create handle v2 api
187+
188+
// 6. prepare io
189+
// AX_U32 nIoDepth = (stCtx.vecOutputBufferFlag.size() == 0) ? 1 : stCtx.vecOutputBufferFlag.size();
190+
ret = utils::prepare_io(strModelPath, m_io_info, m_io, utils::IO_BUFFER_STRATEGY_CACHED);
191+
if (0 != ret) {
192+
printf("prepare io failed!\n");
193+
utils::free_io(m_io);
194+
return deinit_handle();
195+
}
196+
197+
m_handle = handle;
198+
m_hasInit = true;
199+
200+
return 0;
201+
}
202+
203+
int EngineWrapper::SetInput(void* pInput, int index) {
204+
return utils::push_io_input(pInput, index, m_io);
205+
}
206+
207+
int EngineWrapper::Run()
208+
{
209+
if (!m_hasInit)
210+
return -1;
211+
212+
// 7.3 run & benchmark
213+
auto ret = AX_ENGINE_RunSync(m_handle, &m_io);
214+
if (0 != ret) {
215+
printf("AX_ENGINE_RunSync failed. ret=0x%x\n", ret);
216+
return ret;
217+
}
218+
219+
return 0;
220+
}
221+
222+
int EngineWrapper::GetOutput(void* pOutput, int index) {
223+
return utils::push_io_output(pOutput, index, m_io);
224+
}
225+
226+
int EngineWrapper::GetInputSize(int index) {
227+
return m_io.pInputs[index].nSize;
228+
}
229+
230+
int EngineWrapper::GetOutputSize(int index) {
231+
return m_io.pOutputs[index].nSize;
232+
}
233+
234+
void* EngineWrapper::GetOutputPtr(int index) {
235+
utils::cache_io_flush(&m_io.pOutputs[index]);
236+
return m_io.pOutputs[index].pVirAddr;
237+
}
238+
239+
int EngineWrapper::Release()
240+
{
241+
if (m_handle) {
242+
utils::free_io(m_io);
243+
AX_ENGINE_DestroyHandle(m_handle);
244+
m_handle = nullptr;
245+
}
246+
return 0;
247+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**************************************************************************************************
2+
*
3+
* Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
4+
*
5+
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
6+
* may not be copied or distributed in any isomorphic form without the prior
7+
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
8+
*
9+
**************************************************************************************************/
10+
11+
#pragma once
12+
13+
#include <string>
14+
#include <vector>
15+
#include <cstring>
16+
#include <array>
17+
#include <cstdint>
18+
19+
#include "ax_engine_api.h"
20+
21+
22+
class EngineWrapper {
23+
public:
24+
EngineWrapper() :
25+
m_hasInit(false),
26+
m_handle(nullptr) {}
27+
28+
~EngineWrapper() {
29+
Release();
30+
}
31+
32+
int Init(const char* strModelPath, uint32_t nNpuType = 0);
33+
34+
int SetInput(void* pInput, int index);
35+
36+
int Run();
37+
38+
int GetOutput(void* pOutput, int index);
39+
40+
int GetInputSize(int index);
41+
int GetOutputSize(int index);
42+
43+
void* GetOutputPtr(int index);
44+
45+
int Release();
46+
47+
protected:
48+
bool m_hasInit;
49+
AX_ENGINE_HANDLE m_handle;
50+
AX_ENGINE_IO_INFO_T *m_io_info{};
51+
AX_ENGINE_IO_T m_io{};
52+
int m_input_num{}, m_output_num{};
53+
};

0 commit comments

Comments
 (0)