-
Notifications
You must be signed in to change notification settings - Fork 97
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
Simple version of @btime with automatic interpolation #118
Comments
Yeah, I'm definitely tired of (a) remembering to do the interpolation myself and (b) explaining it to others. I think the easiest thing to do is essentially what I proposed over in #65 and create a new package which just provides its own I just tried writing an implementation of this, and it turned out to be pretty simple given the available tools: import BenchmarkTools
using MacroTools: postwalk
function interpolate(expr)
postwalk(x -> x isa Symbol ? Expr(:$, x) : x, expr)
end
macro benchmark(expr, args...)
quote
BenchmarkTools.@benchmark($(interpolate(expr)), $(args)...)
end
end it's likewise pretty trivial to implement Any thoughts on a name? |
Why a new package rather than a new macro within this package? Is there resistance to merge something of that sort, or something more. Regardless, if it is in a new package (and your name sounds fine) then I don't think it should clashes of named macros with this package, just in case something uses both, or gets confused on behaviour |
I mean it's really up to the maintainers whether they want to add more functionality to this package or to keep it separate. But one way or another I'm definitely done with |
I'm all for this if somebody can come up with a correct implementation that doesn't end up being too magical. AFAICT the proposed implementation above is unfortunately broken, for example it would try to mark setup variable usages with It's basically just a matter of switching the "default" scope of variables. Right now, by default, variables in benchmark kernels just follow normal Julia scoping rules. One can then use Either way, normal usage should probably see variables being Honestly, I think the root of the problem is just that scope is a very important part of benchmarking in Julia, and one cannot author a well-defined benchmark (or correctly interpret results) without explicitly understanding the scope of data manipulated by the benchmark. This comprehension barrier is going to exist regardless of which syntax is chosen. |
I think you are exactly right. And that is plenty of reason that it shouldn't replace The issue is that Right now the heuristic people like myself are using is "always put |
Yeah, I noticed the issue with @btime f(x, y) setup=(x = 10) I can produce: @btime f(x, y) setup=(x = 10; y = $y; f = $f) I've got that version mostly working here: https://github.com/rdeits/LocalScopeBenchmarks.jl/blob/b7407351be6f99be6116ce1f28514278f65c3fac/src/LocalScopeBenchmarks.jl |
This keeps coming up when trying to get beginners up and running, and suggest to them to explore things with simple benchmarks. Splicing is not as obvious to people as seasoned Julia may think. Are they any thoughts on whether we need a new benchmarking library, or try to add one here? It sure would simplify things (and cut down on discourse "why isn't this fast" questions) |
If you're willing to try out LocalScopeBenchmarks.jl (linked above), I think it might help |
Do you mean to try it out with the intention of it becoming a supported package in the medium-term, or to try out the patterns to see if it works (and could hence be integrated back into |
More the latter, I'm afraid. I'm going to keep playing with it myself to see if it's the right interface, but I'm not quite to the point of a long-term commitment. |
@jlperla If students are still having trouble understanding benchmark scope (which is indeed a complicated thing in Julia), perhaps the easiest way to avoid the problem is to just teach them to always use the normal |
Sorry for the delay. Thanks @jrevels I think you may misunderstand this usage. It is not about teaching people how to do real benchmarking (which requires sufficient care that I think variable interpolation is a reasonable thing to do). The main motivation for this is pure exploration for performance in code, with no intention to do fancy benchmarks. i.e. you have a bunch of functions and want to time one of them, so if you just throw a |
If anyone is curious, try flipping through https://discourse.julialang.org/search?q=btime to see 2 things: (1) how essential this package is for the Julia ecosystem; and (2) how many times |
Hi. We are almost a year (and countless mistakes of me and others forgetting To be precise, the proposal is to add simple, convenient macro to the package (i.e. not changing the behavior of any existing functions!) for a "local" version of btime. Lets call it f(x) = x^2
y = 1:5
@btime f.($y) Then the following would be equivalent @lbtime f.(y) Again, this is not in any way intended for serious benchmarking. It just makes it less error-prone to use BenchmarkTools for simple cases. The code could be based off of https://github.com/rdeits/LocalScopeBenchmarks.jl/blob/master/src/LocalScopeBenchmarks.jl or even simplified further. |
@ajozefiak I discussed this with @jrevels and here is the basic strategy. Right now, What is discussed above was to create a So do the following in a PR:
To be clear on finding places where it breaks, you may not have too many troubles if it doesn't use the @lbtime f(x, y) Should be equivalent to @btime f(x, y) setup=(x = $x, y = $y) Or probably even (though you should check that this doesn't cause any issues) @btime f(x, y) setup=(x = $x, y = $y, f = $f) Keep in mind that the @lbtime f(x, y) setup=(x = 10) Should be equivalent to @btime f(x, y) setup=(x = 10, y = $y, f = $f) Similarly, if someone has already escaped a variable, then we need to make sure it doesn't break - since a lot of users of @lbtime f(x, y, $z) is equivalent to @btime f(x, y, z) setup=(x = 10, y = $y, f = $f, z = $z) Or if it is easier, @btime f(x, y, $z) setup=(x = 10, y = $y, f = $f) Good luck with learning the julia AST! Soon it will be time to go one step further and get your hands dirty with the IR. |
A few other tests I thought of:
Should probably be the same as @btime f(x, $(y+1)) setup = (f = $f, x = $x) which might change the approach to the |
I know this has been brought up before (e.g. #65 ) and others, but let me make a slightly different suggestion. The current
@btime
and@benchmark
are great, but what about a simple @rdeits made a similar suggestion in #65 (comment)The issue is that the vast majority of benchmarking I do is of the form
@btime f(a,b)
and no matter how many times I have read the manual, I will always forget to do it as@btime f($a, $b)
. Not to mention that it is harder to read or flip benchmarks on/off easily for a function you are working on.So my suggestion is a macro
@bfunction
or whatever, which only accepts a function as head and automatically interpolates all arguments. i.e.Is that sort of thing possible to do with the macro expansion model in Julia? If I was able to get a PR would you consider merging it, or do you disagree with the feature?
The text was updated successfully, but these errors were encountered: