CuYao compiles depending on value, not depending on type
jlbosse opened this issue · comments
It seems like CuYao recompiles apply!
every time the parameters of a circuit are changed, even though the circuit is still the same. This is in contrast to running a circuit on the CPU and and not on the GPU where compilation happens only once when running a circuit for the first time and then all future runs are fast when changing the parameters. See the following MWE:
using Yao, Yao.AD, Yao.EasyBuild
using CuYao
import CUDA
n = 10
d = 5
circuit = variational_circuit(n, d)
cureg = zero_state(n) |> cu
reg = zero_state(n)
# every time this block gets run the first `apply!()` takes much longer than the second `appply!()`
begin
cureg.state .= 0.
@CUDA.allowscalar cureg.state[1] = 1.
dispatch!(circuit, :random)
@time apply!(cureg, circuit)
@time apply!(cureg, circuit)
end
# only the very first time this block gets run the first `apply!()` invokes a compilation pass, after
# that the first `apply!()` and the second apply always take roughly the same time.
begin
reg.state .= 0.
reg.state[1] = 1.
dispatch!(circuit, :random)
@time apply!(reg, circuit)
@time apply!(reg, circuit)
end
Just confirmed this issue, it was due to the use of closure when writing kernel functions, will fix it later today. Thanks for reporting the bug!
You issue will be fixed by this PR: QuantumBFS/CuYao.jl#74
Thanks again for the bug report!
Great, and thank you for providing a fix that quickly!
The PR in CuYao has been merged and is being tagged, I will close this issue for now. Feel free to reopen it if the patch does not fix your issue.
Worked perfectly, thanks