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

maths: switch dot to faster for loop #667

Merged
merged 2 commits into from
Apr 10, 2024
Merged

maths: switch dot to faster for loop #667

merged 2 commits into from
Apr 10, 2024

Conversation

pulsejet
Copy link
Contributor

Tiny optimization; the for loop warms up faster than the reduce.

import { dot } from "@xenova/transformers";

function dotNew(arr1, arr2) {
    let result = 0;
    for (let i = 0; i < arr1.length; i++) {
        result += arr1[i] * arr2[i];
    }
    return result;
}

const random1 = Array.from({ length: 10000 }, () => Array.from({ length: 512 }, () => Math.random()));
const random2 = Array.from({ length: 10000 }, () => Array.from({ length: 512 }, () => Math.random()));

// Replaced with dotNew for second run
console.time("dot");
for (let i = 0; i < 10000; i++) {
    dot(random1[i], random2[i]);
}
console.timeEnd("dot");

Output on node v20.5.1

dot: 43.757ms
dotNew: 11.982ms

Of course, eventually they're exactly the same. With 100k elements:

dot: 109.57ms
dotNew: 76.428ms

@xenova
Copy link
Collaborator

xenova commented Mar 27, 2024

Oh interesting! Thanks for this! Could you do some benchmarks with typed arrays? Specifically, Float32Array?

@pulsejet
Copy link
Contributor Author

Well the current dot function doesn't work at all on Float32Array (another reason to switch to the new one) because there's no reduce.

Typed arrays are extremely fast.

dot: 43.757ms
dotNew: 11.982ms
dotNew: 0.887ms  (Float32Array)
dotNew: 0.919ms (Float64Array)

With 100k elements

dot: 109.57ms
dotNew: 76.428ms
dotNew: 3.237ms (Float32Array)
dotNew: 3.167ms (Float64Array)

@xenova
Copy link
Collaborator

xenova commented Apr 4, 2024

Just an update: This will be merged and included in the next release. 😇 I'm just finalizing a few other things first.

@pulsejet
Copy link
Contributor Author

pulsejet commented Apr 4, 2024

No worries, take your time.

Thanks for this awesome library 😎

src/utils/maths.js Outdated Show resolved Hide resolved
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova
Copy link
Collaborator

xenova commented Apr 10, 2024

Just for the fun of it, I was doing some benchmarking myself (both in browser and node), and your update is consistently 5-10x faster (of course, depends on the array size)! Honestly, I'm quite surprised, since I would expect this to be a common pattern, and for reduce to be optimized for this case.

Thanks!

@xenova xenova merged commit 15b90b7 into huggingface:main Apr 10, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants