GithubHelp home page GithubHelp logo

Comments (4)

LaurentMazare avatar LaurentMazare commented on July 2, 2024

Interesting, I would have hoped that the compiler uses constant propagation so as not to recompute this on all call, but rustc has quite a few shortcomings when it comes to float const functions that may prevent this from happening.
Would you mind trying to replace this with the hardcoded value for the thing and see if it has an impact? (and if that's the case, happy to get a PR for the change)

from candle.

j-baker avatar j-baker commented on July 2, 2024
fn crit(c: &mut Criterion) {
    let arg = 20.0;
    c.bench_function("gelu_f16", |c| {
        c.iter(|| Gelu::f16(black_box(f16::from_f32(arg))))
    });
}

leads to:

current implementation: 2.2172ns
below implementation: 1.6855ns

on my M1 mac.

I would expect that constant propagation is trickier on f16 as the compiler has to inline more to understand that it's safe.

This is the solution I used.

f16::from_f32_const((2.0 / PI).sqrt())

which does work with constant propgation. I can confirm that constant propagation does work on f32 and f64, validated on godbolt.

from candle.

LaurentMazare avatar LaurentMazare commented on July 2, 2024

Neat, would be great if you can make a PR for this (ideally with a comment explaining why it's useful). If you don't have the time I could take a stab at it.

from candle.

LaurentMazare avatar LaurentMazare commented on July 2, 2024

Made a PR for this, #2008

from candle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.