Skip to content

Commit

Permalink
got consistent 3 fps
Browse files Browse the repository at this point in the history
  • Loading branch information
eyaler committed Oct 20, 2024
1 parent 4d22af5 commit 16ca967
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 83 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

## This fork ([demo](https://eyaler.github.io/webnn-developer-preview/))
1. Added sd-turbo [image-to-image](demos/sd-turbo/index.js) mode using the [vae encoder](https://huggingface.co/eyaler/sd-turbo-webnn/tree/main/vae_encoder) to initialize latents
2. Added camera input option for the above (I can get up to 2 fps with an RTX 3070 TI laptop GPU on Windows)
2. Added camera input option for the above (I can get up to 3 fps with an RTX 3070 Ti laptop GPU on Windows)
3. Added a helper [script](demos/sd-turbo/fix_instance_norm.py) for casting instance-normalization nodes to float32 (which has critical numerical issues with float16)
4. Removed sd-turbo safety checks ;)

### Known issues:
1. Run times of VAE encoder and UNET are inconsistent and are sometimes slower by 10x
1. Run times of VAE encoder and UNET are inconsistent and are sometimes slower by 15x

### Example:

Expand Down
30 changes: 15 additions & 15 deletions assets/js/common_utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const KNOWN_COMPATIBLE_CHROMIUM_VERSION = {
'stable-diffusion-1.5': '129.0.6617.0',
'sd-turbo': '129.0.6617.0',
'segment-anything': '129.0.6617.0',
'whisper-base': '129.0.6617.0',
'image-classification': '129.0.6617.0'
'stable-diffusion-1.5': ['129.0.6617.0'],
'sd-turbo': ['129.0.6617.0', '130.0.6723.59'],
'segment-anything': ['129.0.6617.0'],
'whisper-base': ['129.0.6617.0'],
'image-classification': ['129.0.6617.0']
}

export const showCompatibleChromiumVersion = (key) => {
const version = KNOWN_COMPATIBLE_CHROMIUM_VERSION[key]
export const showCompatibleChromiumVersion = (key, branch) => {
const flag = (branch && !KNOWN_COMPATIBLE_ORT_VERSION[key][branch].startsWith('1.19') && KNOWN_COMPATIBLE_CHROMIUM_VERSION[key].length > 1) | 0
const version = KNOWN_COMPATIBLE_CHROMIUM_VERSION[key][flag]
if (version) {
const chromiumVersionElement = document.querySelector('#chromiumversion');
chromiumVersionElement.innerHTML = `Known compatible Chromium version:
Expand Down Expand Up @@ -123,13 +124,12 @@ export const getTime = () => {
};

const KNOWN_COMPATIBLE_ORT_VERSION = {
'stable-diffusion-1.5': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' },
'sd-turbo': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' },
//'sd-turbo': { 'dev': '1.19.2', 'stable': '1.20.0', 'test': 'test' },
//'sd-turbo': { 'dev': '1.20.0-dev.20240928-1bda91fc57', 'stable': '1.20.0', 'test': 'test' },
'segment-anything': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' },
'whisper-base': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' },
'image-classification': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' },
'stable-diffusion-1.5': { 'dev': '1.20.0-dev.20240919-bd60add8ce', 'stable': '1.20.0', 'test': 'test' },
'sd-turbo': { 'dev': '1.20.0-dev.20240919-bd60add8ce', 'stable': '1.20.0', 'test': 'test' },
//'sd-turbo': { 'dev': '1.21.0-dev.20241019-abad69b322', 'stable': '1.20.0', 'test': 'test' },
'segment-anything': { 'dev': '1.20.0-dev.20240919-bd60add8ce', 'stable': '1.20.0', 'test': 'test' },
'whisper-base': { 'dev': '1.20.0-dev.20240919-bd60add8ce', 'stable': '1.20.0', 'test': 'test' },
'image-classification': { 'dev': '1.20.0-dev.20240919-bd60add8ce', 'stable': '1.20.0', 'test': 'test' },
};

const ORT_BASE_URL = 'https://www.npmjs.com/package/onnxruntime-web/v/';
Expand Down Expand Up @@ -187,7 +187,7 @@ export const setupORT = async (key, branch) => {
ortVersionElement.innerHTML = versionHtml;
};

export const webNnStatus = async () => {
export const getWebnnStatus = async () => {
let result = {};
try {
const context = await navigator.ml.createContext();
Expand Down
4 changes: 2 additions & 2 deletions demos/image-classification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
log,
logError,
getQueryValue,
webNnStatus,
getWebnnStatus,
updateQueryStringParameter,
getMedian,
getAverage,
Expand Down Expand Up @@ -198,7 +198,7 @@ const main = async () => {
};

const checkWebNN = async () => {
let webnnStatus = await webNnStatus();
let webnnStatus = await getWebnnStatus();

if (
getQueryValue("provider") &&
Expand Down
11 changes: 7 additions & 4 deletions demos/sd-turbo/fix_instance_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import sys


assert len(sys.argv) == 3, 'Syntax: python fix_instance_norm.py INPUT_TXT_OR_ONNX OUTPUT_TXT_OR_ONNX'
assert len(sys.argv) >= 3, 'Syntax: python fix_instance_norm.py INPUT_TXT_OR_ONNX OUTPUT_TXT_OR_ONNX [location of onnx2text]'
input_file = sys.argv[1]
output_file = sys.argv[2]
onnx2text = 'onnx2text'
if len(sys.argv) > 3:
onnx2text = os.path.join(sys.argv[3], onnx2text)

if os.path.splitext(input_file)[-1] == '.onnx':
converted_input_file = input_file + '.txt'
os.system(f'onnx2text "{input_file}" "{converted_input_file}"')
os.system(f'{onnx2text} "{input_file}" "{converted_input_file}"')
input_file = converted_input_file

converted_output_file = None
Expand Down Expand Up @@ -49,7 +52,7 @@
total = 0
constants = []
for i, part in enumerate(parts[1 : -1], start=1):
part, subs = re.subn('(.*?input: ")([^"]*?)(_output[^"]*)?(".*?output: ")([^"]*?)(_output[^"]*)?(".*op_type: "InstanceNormalization".*)', replace, part, flags=re.DOTALL)
part, subs = re.subn('(.*?input: ")([^"]*?)(_output[^"]*)?(".*?output: ")([^"]*?)(_output[^"]*)?((?<!_tocast)".*op_type: "InstanceNormalization".*)', replace, part, flags=re.DOTALL)
if subs:
constants += re.findall('".*constant.*"', part, flags=re.IGNORECASE)
parts[i] = part
Expand All @@ -69,4 +72,4 @@
f.write(''.join(parts))

if converted_output_file:
os.system(f'onnx2text "{output_file}" "{converted_output_file}"')
os.system(f'{onnx2text} "{output_file}" "{converted_output_file}"')
Loading

0 comments on commit 16ca967

Please sign in to comment.