bytecodealliance / wasi-nn

High-level bindings for wasi-nn system calls
Apache License 2.0
44 stars 33 forks source link

Troubleshoot AssemblyScript performance #12

Closed abrown closed 3 years ago

abrown commented 3 years ago

Why is the AssemblyScript example slower than the Rust version? There may be additional things on the Wasm side that could make the execution time faster. E.g., I noticed that loading the big weights files with readBytes seemed to take 20-25 seconds (a as-wasi issue?).

MaxGraey commented 3 years ago

sort may be a problem. Using Weak Heap Sorting algorithm isn't stable while ECMAScript currently specify Array#sort should be stable. That's why we currently temporary fallback to simple insertion sort O(N^2) for all references. It may be a problem for huge size of entries (> 1k)

MaxGraey commented 3 years ago

Also as-wasi uses push for read/readAll. Recently we fixed some huge perf regression which relate to buffer's growing for Array#push. So I recommend update to v0.18.31

brianjjones commented 3 years ago

@MaxGraey Thanks for the heads up, it does run several seconds faster with that fix.

MaxGraey commented 3 years ago

sort may be a problem

this issue also will be fixed soon

abrown commented 3 years ago

@MaxGraey, just wanted to say thanks for tracking this project and looking into these AssemblyScript issues. Let us know if you have any suggestions anywhere! (E.g., hope we're doing "the right thing" to get pointers here).

MaxGraey commented 3 years ago

(E.g., hope we're doing "the right thing" to get pointers here).

Arrays and TypedArrays have special property for point to started data called dataStart so you could use this:

// @ts-ignore: decorator
@inline 
function getArrayPtr<T>(data: T[]): usize {
    return data.dataStart;
}

// @ts-ignore: decorator
@inline 
function getTypedArrayPtr(data: ArrayBufferView): usize { // ArrayBufferView is base class for Uint8Array and etc
    return data.dataStart; // it also take into account data.byteOffset
}
abrown commented 3 years ago

Good to know! I think had read somewhere (or deduced from https://github.com/AssemblyScript/assemblyscript/issues/743) that it was an unstable, undocumented thing. But now I actually see it in the the docs so it looks like we can simplify a bit.

MaxGraey commented 3 years ago

@abrown 0.19.7 now contain new stable sorting algorithm. So this should be fixed