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

Feature request: gramma to enforce type stability proactively #30007

Closed
GiggleLiu opened this issue Nov 12, 2018 · 4 comments
Closed

Feature request: gramma to enforce type stability proactively #30007

GiggleLiu opened this issue Nov 12, 2018 · 4 comments

Comments

@GiggleLiu
Copy link
Contributor

GiggleLiu commented Nov 12, 2018

There should be gramma/macro in Julia to declare a function as type stable (or no box). Like stable function(x) = ..., so that the program can break down/warn immediately when unexpected allocations occur.

1). Speed and type stability are defining features of Julia, it should allow enforced type stable functions. I’d like to cite Tim’s comment in his GPU paper

The most remarkable aspect of the language and its main implementation is speed: carefully written Julia code performs exceptionally well on traditional microprocessors, approaching the speed of code written in statically-compiled languages like C or FORTRAN.
Julia’s competitive performance originates from clever language design that avoids the typical compilation and execution uncertainties associated with dynamic languages.

In many cases, I prefer my program to error automatically if some of my functions (e.g. designed to be used inside a loop) are type unstable (or have to use a Box).

2). Julia is a powerful language that can support GPU and TPU programming easily. However, kernel functions on these devices can not allocate memories, meanwhile hard to debug. Half of my debugging experiences are related to type instability. In this case, proactive type stability assurance is extremely important for noticing the bug immediately.

One may argue @code_warntype is enough, but it is not true. I think David Buchana gives a perfect example: rand([0,1],10) vs rand([0.,1.],10) make the code like 30 times slower due to not calling the BLAS. This kind of debugging is often time consuming: you should first notice your program is running slow, benchmark to verify it is slower than before, and profile to locate where it slows down, then @code_warntype can be helpful. So @code_warntype can only help in the final stage.

@davidbp
Copy link

davidbp commented Nov 12, 2018

I just want to leave a reproducible example

julia> B_64 = rand(Float64,1000,1000);

julia> A_32 = rand(Float32,1000,1000);

julia> B_32 = rand(Float32,1000,1000);

julia> @btime A_32 * B_32;
  8.949 ms (2 allocations: 3.81 MiB)

julia> @btime A_32 * B_64;
  836.115 ms (8 allocations: 7.63 MiB)

I do not want functions to crash if they are type unstable but it would be nice to have an optional mechanism to force them to crush when type conversions are done inside a function.

@mbauman
Copy link
Member

mbauman commented Nov 12, 2018

You're mixing two different terminologies here. The non-BLAS multiplication example is still type-stable, but it's hitting a generic fallback.

We have an issue for a proactively declared type-stable block — and if I remember right Jeff promised to add this during a JuliaCon talk. #10980

On the other front — the fallback generic algorithms that are correct but slow — is a harder one.

@GiggleLiu
Copy link
Contributor Author

To be concrete, I will give two more examples where this functionality is needed.

1). see this issue comment
ahwillia/Einsum.jl#1 (comment)

When Einsum is type unstable, error is better.

2). In my project, in order to implement high performance quantum circuit simulator, I have to keep every elementals function type stable

However, people can ruin the performance easily with all tests passed.

@KristofferC
Copy link
Member

Duplicate of #10980.

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

No branches or pull requests

4 participants