Comments (6)
Thanks for doing the test. Yes, I do think this can give incorrect gradients, sadly. Somehow I didn't think of this case before.
The obvious fix would be that, when i
appears with two different shifts, it should be marked "unsafe" for the gradient calculation. (There are already some situations where the macro notices this.) However, in this example, marking both i
and j
unsafe would mean the entire gradient calculation has to run serially, which will be awful on the GPU.
A possible work-around would be to pretend that the different terms are separate arrays. Then it will accumulate the gradients into 𝛥x
, 𝛥x2
, 𝛥x3
and Zygote will combine them afterwards:
reg2(x) = begin
x2 = x
x3 = x
sum(@tullio res[i, j] := abs2(x[i, j] - x2[i, j+1]) + abs2(x[i, j] - x3[i+1, j]))
end
Perhaps it could solve for the index needed for the gradient, replacing
𝛥x[i + 1, j] = 𝛥x[i + 1, j] + 𝛥ℛ[1] * conj(-(((x[i, j] - x[i + 1, j]) + …
with
𝛥x[iplus1, j] = 𝛥x[iplus1, j] + 𝛥ℛ[1] * conj(-(((x[iplus1 - 1, j] - x[iplus1, j]) + …
But this would be pretty complicated, and the range of values allowed for iplus1
and i
differ, so it wouldn’t be simple to fill 𝛥x
in one safe loop like this.
from tullio.jl.
Yes. But I think it's much harder to get a wrong answer on the CPU, as there are fewer threads, which is probably why I didn't notice. For res
above, if this testimage is divided 4 ways, and they each start at the top left of their blocks, they are very unlikely to fight each other at the overlapping edges.
There might be other functions which behave much worse, maybe something like x[end-i]
instead of x[i+1]
? But this doesn't seem to:
julia> f2(x) = sum(@tullio out[i] := x[i] + sqrt(x[20-i]) threads=2); # 2 is the threshold to divide among threads
julia> Zygote.gradient(f2, 1:20)[1] ≈ ForwardDiff.gradient(f2, 1:20) # all 4 threads work on these 20 elements
true
from tullio.jl.
So you mean the gradients could be wrong?
Because I'm observing certain artifacts in my deconvolution using the same regularizer on GPU vs on CPU. So something with the GPU might be wrong.
Here a MWE:
using Tullio, CUDA, KernelAbstractions, Zygote, TestImages
reg(x) = sum(@tullio res[i, j] := abs2(x[i, j] - x[i, j+1]) + abs2(x[i, j] - x[i+1, j]))
x = Float32.(testimage("resolution_test_512"))
x_c = CuArray(x);
y1 = Zygote.gradient(reg, x)[1]
y2 = Zygote.gradient(reg, x_c)[1]
.≈(y1, Array(y2), rtol=0.01)[100:110, 100:110]
Returns
11×11 BitMatrix:
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
1 0 1 1 1 0 0 1 1 0 1
So the gradient might be broken, showing some column patterns.
from tullio.jl.
Ok, the workaround seems to work. I'll probably stick with it for now!
Thanks, your support is exceptional great! :)
from tullio.jl.
So this affects both CPU and GPU, correct?
from tullio.jl.
Yeah, I agree. Could be wrong but unlikely to happen and if, only affecting a minor amount of pixels.
from tullio.jl.
Related Issues (20)
- HybridArrays HOT 9
- Runtime dispatch when reducing to scalar
- Alternative to Tullio for Chained Multiplication HOT 4
- @views macro causes module compilation failure HOT 3
- Reporting a bug when Tullio being included with LoopVectorization HOT 1
- [Question] Is it possible to create a vector of SVectors from a Matrix using Tullio? HOT 2
- [Question] How to change summation order? HOT 5
- Use package extensions HOT 1
- How finalizers `|>` work HOT 5
- Method error when broadcast and sum of matrices HOT 1
- GPU Kernel Compilation Failed with Interpolations HOT 2
- Upgrade to CUDA.CUDAKernels HOT 9
- Bug when using Tullio + LoopVectorization HOT 5
- Add Finch.jl backend HOT 4
- CUDA v4 support HOT 2
- Using threads, vs setting threads=false gives different result HOT 3
- Issue with vectorized functions on GPU HOT 3
- Error when specifying the range of an index with a UnitRange HOT 4
- Scalar indexing with CUDA HOT 10
- Please update dep of FillArrays to v1.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tullio.jl.