Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support iobinding for Whisper Base demo on WebNN GPU #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion demos/whisper-base/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const SpeechStates = {
let speechState = SpeechStates.UNINITIALIZED;

let mask4d = true; // use 4D mask input for decoder models
let iobinding = true;
miaobin marked this conversation as resolved.
Show resolved Hide resolved
let streamingNode = null;
let sourceNode = null;
let audioChunks = []; // member {isSubChunk: boolean, data: Float32Array}
Expand Down Expand Up @@ -161,6 +162,9 @@ function updateConfig() {
if (pair[0] == "mask_4d") {
mask4d = pair[1].toLowerCase() === "true";
}
if (pair[0] == 'iobinding') {
miaobin marked this conversation as resolved.
Show resolved Hide resolved
iobinding = pair[1].toLowerCase() === 'true';
}
}
}

Expand Down Expand Up @@ -664,7 +668,7 @@ const main = async () => {
const whisper_url = location.href.includes("github.io")
? "https://huggingface.co/microsoft/whisper-base-webnn/resolve/main/"
: "./models/";
whisper = new Whisper(whisper_url, provider, deviceType, dataType, mask4d);
whisper = new Whisper(whisper_url, provider, deviceType, dataType, mask4d, iobinding);
await whisper.create_whisper_processor();
await whisper.create_whisper_tokenizer();
await whisper.create_ort_sessions();
Expand Down
86 changes: 63 additions & 23 deletions demos/whisper-base/whisper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ if (

// wrapper around onnxruntime and model
export class Whisper {
constructor(url, provider, deviceType = "gpu", dataType, mask_4d = true) {
constructor(url, provider, deviceType = "gpu", dataType, mask_4d = true, iobinding) {
this.url = url;
this.provider = provider;
this.deviceType = deviceType;
this.dataType = dataType;
this.mask_4d = mask_4d;
this.iobinding = iobinding && deviceType == 'gpu';
ort.env.wasm.simd = true;

this.models = {
Expand Down Expand Up @@ -101,6 +102,19 @@ export class Whisper {
deviceType: this.deviceType,
},
],
preferredOutputLocation: this.iobinding ? (() => {
const pairs = {};
miaobin marked this conversation as resolved.
Show resolved Hide resolved
pairs['last_hidden_state'] = "ml-tensor";
for (let i = 0; i < 6; i++) {
pairs[`padded_present_key_values.${i}.decoder.key`] = "ml-tensor";
pairs[`padded_present_key_values.${i}.decoder.value`] = "ml-tensor";
pairs[`present_key_values.${i}.encoder.key`] = "ml-tensor";
pairs[`present_key_values.${i}.encoder.value`] = "ml-tensor";
pairs[`updated_present_key_values.${i}.decoder.key`] = "ml-tensor";
pairs[`updated_present_key_values.${i}.decoder.value`] = "ml-tensor";
}
return pairs;
})() : undefined,
logSeverityLevel: 0,
};

Expand All @@ -111,16 +125,22 @@ export class Whisper {
let url = this.url + this.models[name]["url"];
if (this.dataType == "float16") {
url = url.replace(".onnx", "_fp16_layernorm_gelu.onnx");
if (name.includes("decoder") && this.mask_4d) {
url = url.replace(".onnx", "_4dmask.onnx");
if (name.includes("decoder")) {
if (this.mask_4d) {
url = url.replace(".onnx", "_4dmask.onnx");
}
if (this.iobinding) {
url = url.replace(".onnx", "_iobinding.onnx");
}
}
log(`Loading ${this.models[name]["title"]} · ${this.dataType} · ${this.models[name]["fp16size"]}`);
} else {
url = url.replace(".onnx", "_layernorm.onnx");
log(`Loading ${this.models[name]["title"]} · ${this.dataType} · ${this.models[name]["fp32size"]}`);
}

const modelBuffer = await getModelOPFS(`${this.deviceType}_${name}_${this.dataType}`, url, false);
const modelBuffer = await getModelOPFS(`${this.iobinding}_${this.deviceType}_${name}_${this.dataType}`,
url, false);
log(`${this.models[name]["title"]} loaded`);

log(`Creating session for ${this.models[name]["title"]}`);
Expand Down Expand Up @@ -271,15 +291,24 @@ export class Whisper {
}

// modify the self attention kv cache in place
cache_update(
decoder_input,
decoder_output,
0,
this.max_sequence_length,
this.num_init_tokens,
this.num_init_tokens,
this.dataType,
);
if (this.iobinding) {
for (let i = 0; i < 6; i++) {
decoder_input[`past_key_values.${i}.decoder.key`] =
decoder_output[`padded_present_key_values.${i}.decoder.key`];
decoder_input[`past_key_values.${i}.decoder.value`] =
decoder_output[`padded_present_key_values.${i}.decoder.value`];
}
} else {
cache_update(
decoder_input,
decoder_output,
0,
this.max_sequence_length,
this.num_init_tokens,
this.num_init_tokens,
this.dataType
);
}

const position_ids = new Int32Array(decoder_input["position_ids"].cpuData.buffer);
// run complete inference for every item in dataset
Expand Down Expand Up @@ -319,16 +348,27 @@ export class Whisper {
position_ids[0],
this.mask_4d,
);
// modify the kv cache in place
cache_update(
decoder_input,
decoder_cached_output,
i,
this.max_sequence_length,
this.num_init_tokens,
position_ids[0],
this.dataType,
);

// modify the kv cache in place
if (this.iobinding) {
for (let i = 0; i < 6; i++) {
decoder_input[`past_key_values.${i}.decoder.key`] =
decoder_cached_output[`updated_present_key_values.${i}.decoder.key`];
decoder_input[`past_key_values.${i}.decoder.value`] =
decoder_cached_output[`updated_present_key_values.${i}.decoder.value`];
miaobin marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
cache_update(
decoder_input,
decoder_cached_output,
i,
this.max_sequence_length,
this.num_init_tokens,
position_ids[0],
this.dataType
);
}

}

// add token to sentence decode time
Expand Down