Skip to content

Commit

Permalink
Fix CI errors introduced by supporting loading keywords from buffers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Sep 20, 2024
1 parent 06b61cc commit d8809b5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 7 deletions.
9 changes: 8 additions & 1 deletion flutter/sherpa_onnx/lib/src/keyword_spotter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class KeywordSpotterConfig {
this.keywordsScore = 1.0,
this.keywordsThreshold = 0.25,
this.keywordsFile = '',
this.keywordsBuf = '',
this.keywordsBufSize = 0,
});

@override
String toString() {
return 'KeywordSpotterConfig(feat: $feat, model: $model, maxActivePaths: $maxActivePaths, numTrailingBlanks: $numTrailingBlanks, keywordsScore: $keywordsScore, keywordsThreshold: $keywordsThreshold, keywordsFile: $keywordsFile)';
return 'KeywordSpotterConfig(feat: $feat, model: $model, maxActivePaths: $maxActivePaths, numTrailingBlanks: $numTrailingBlanks, keywordsScore: $keywordsScore, keywordsThreshold: $keywordsThreshold, keywordsFile: $keywordsFile, keywordsBuf: $keywordsBuf, keywordsBufSize: $keywordsBufSize)';
}

final FeatureConfig feat;
Expand All @@ -35,6 +37,8 @@ class KeywordSpotterConfig {
final double keywordsScore;
final double keywordsThreshold;
final String keywordsFile;
final String keywordsBuf;
final int keywordsBufSize;
}

class KeywordResult {
Expand Down Expand Up @@ -89,9 +93,12 @@ class KeywordSpotter {
c.ref.keywordsScore = config.keywordsScore;
c.ref.keywordsThreshold = config.keywordsThreshold;
c.ref.keywordsFile = config.keywordsFile.toNativeUtf8();
c.ref.keywordsBuf = config.keywordsBuf.toNativeUtf8();
c.ref.keywordsBufSize = config.keywordsBufSize;

final ptr = SherpaOnnxBindings.createKeywordSpotter?.call(c) ?? nullptr;

calloc.free(c.ref.keywordsBuf);
calloc.free(c.ref.keywordsFile);
calloc.free(c.ref.model.bpeVocab);
calloc.free(c.ref.model.modelingUnit);
Expand Down
5 changes: 5 additions & 0 deletions flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct {
external double keywordsThreshold;

external Pointer<Utf8> keywordsFile;

external Pointer<Utf8> keywordsBuf;

@Int32()
external int keywordsBufSize;
}

final class SherpaOnnxOfflinePunctuation extends Opaque {}
Expand Down
7 changes: 7 additions & 0 deletions scripts/dotnet/KeywordSpotterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public KeywordSpotterConfig()
KeywordsScore = 1.0F;
KeywordsThreshold = 0.25F;
KeywordsFile = "";
KeywordsBuf= "";
KeywordsBufSize= 0;
}
public FeatureConfig FeatConfig;
public OnlineModelConfig ModelConfig;
Expand All @@ -28,5 +30,10 @@ public KeywordSpotterConfig()

[MarshalAs(UnmanagedType.LPStr)]
public string KeywordsFile;

[MarshalAs(UnmanagedType.LPStr)]
public string KeywordsBuf;

public int KeywordsBufSize;
}
}
2 changes: 1 addition & 1 deletion scripts/go/_internal/build_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

package sherpa_onnx

// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-unknown-linux-gnu -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-native-fbank-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -lssentencepiece_core -Wl,-rpath,${SRCDIR}/lib/aarch64-unknown-linux-gnu
// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-unknown-linux-gnu -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/aarch64-unknown-linux-gnu
import "C"
6 changes: 6 additions & 0 deletions scripts/node-addon-api/src/keyword-spotting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_score, keywordsScore);
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_threshold, keywordsThreshold);
SHERPA_ONNX_ASSIGN_ATTR_STR(keywords_file, keywordsFile);
SHERPA_ONNX_ASSIGN_ATTR_STR(keywords_buf, keywordsBuf);
SHERPA_ONNX_ASSIGN_ATTR_INT32(keywords_buf_size, keywordsBufSize);

SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c);

Expand Down Expand Up @@ -86,6 +88,10 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
delete[] c.keywords_file;
}

if (c.keywords_buf) {
delete[] c.keywords_buf;
}

if (!kws) {
Napi::TypeError::New(env, "Please check your config!")
.ThrowAsJavaScriptException();
Expand Down
8 changes: 6 additions & 2 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,9 @@ func sherpaOnnxKeywordSpotterConfig(
maxActivePaths: Int = 4,
numTrailingBlanks: Int = 1,
keywordsScore: Float = 1.0,
keywordsThreshold: Float = 0.25
keywordsThreshold: Float = 0.25,
keywordsBuf: String = "",
keywordsBufSize: Int = 0
) -> SherpaOnnxKeywordSpotterConfig {
return SherpaOnnxKeywordSpotterConfig(
feat_config: featConfig,
Expand All @@ -975,7 +977,9 @@ func sherpaOnnxKeywordSpotterConfig(
num_trailing_blanks: Int32(numTrailingBlanks),
keywords_score: keywordsScore,
keywords_threshold: keywordsThreshold,
keywords_file: toCPointer(keywordsFile)
keywords_file: toCPointer(keywordsFile),
keywords_buf: toCPointer(keywordsBuf),
keywords_buf_size: Int32(keywordsBufSize)
)
}

Expand Down
23 changes: 21 additions & 2 deletions wasm/kws/sherpa-onnx-kws.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,18 @@ function initKwsConfig(config, Module) {
};
}

if (!('keywordsBuf' in config)) {
config.keywordsBuf = '';
}

if (!('keywordsBufSize' in config)) {
config.keywordsBufSize = 0;
}

let featConfig = initFeatureExtractorConfig(config.featConfig, Module);

let modelConfig = initModelConfig(config.modelConfig, Module);
let numBytes = featConfig.len + modelConfig.len + 4 * 5;
let numBytes = featConfig.len + modelConfig.len + 4 * 7;

let ptr = Module._malloc(numBytes);
let offset = 0;
Expand All @@ -198,11 +206,22 @@ function initKwsConfig(config, Module) {
offset += 4;

let keywordsLen = Module.lengthBytesUTF8(config.keywords) + 1;
let keywordsBuffer = Module._malloc(keywordsLen);
let keywordsBufLen = Module.lengthBytesUTF8(config.keywordsBuf) + 1;

let keywordsBuffer = Module._malloc(keywordsLen + keywordsBufLen);
Module.stringToUTF8(config.keywords, keywordsBuffer, keywordsLen);
Module.stringToUTF8(
config.keywordsBuf, keywordsBuffer + keywordsLen, keywordsBufLen);

Module.setValue(ptr + offset, keywordsBuffer, 'i8*');
offset += 4;

Module.setValue(ptr + offset, keywordsBuffer + keywordsLen, 'i8*');
offset += 4;

Module.setValue(ptr + offset, config.keywordsBufLen, 'i32');
offset += 4;

return {
ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig,
keywordsBuffer: keywordsBuffer
Expand Down
2 changes: 1 addition & 1 deletion wasm/kws/sherpa-onnx-wasm-main-kws.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static_assert(sizeof(SherpaOnnxOnlineModelConfig) ==
static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, "");
static_assert(sizeof(SherpaOnnxKeywordSpotterConfig) ==
sizeof(SherpaOnnxFeatureConfig) +
sizeof(SherpaOnnxOnlineModelConfig) + 5 * 4,
sizeof(SherpaOnnxOnlineModelConfig) + 7 * 4,
"");

void CopyHeap(const char *src, int32_t num_bytes, char *dst) {
Expand Down

0 comments on commit d8809b5

Please sign in to comment.