Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

SimpleKernel performance with Zygote

Aperta
#270 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
julia

Direzione di ricerca

Inizia in src/matrix/kernelmatrix.jl alla riga collegata usando Base.Fix1 e riproduci i benchmark Julia per kernelmatrix e Zygote.pullback. Confronta il comportamento del forward pass con il benchmark pairwise per isolare la regressione. Il lavoro è completato quando viene documentato o implementato un approccio che avvicini sostanzialmente il forward pass di Zygote al primal senza introdurre regressioni nel percorso kernel esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

It looks like we've got a performance bug in the above. Specifically, the primal looks to be fine:

julia> @benchmark kernelmatrix(SEKernel(), $(randn(100)))
BenchmarkTools.Trial:
  memory estimate:  158.25 KiB
  allocs estimate:  8
  --------------
  minimum time:     92.684 μs (0.00% GC)
  median time:      155.135 μs (0.00% GC)
  mean time:        181.207 μs (12.32% GC)
  maximum time:     15.995 ms (98.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

while Zygotes forwards-pass is about 100x worse:

julia> @benchmark Zygote.pullback(kernelmatrix, SEKernel(), $(randn(100)))
BenchmarkTools.Trial:
  memory estimate:  7.25 MiB
  allocs estimate:  290068
  --------------
  minimum time:     34.106 ms (0.00% GC)
  median time:      35.180 ms (0.00% GC)
  mean time:        36.456 ms (2.37% GC)
  maximum time:     60.521 ms (25.00% GC)
  --------------
  samples:          138
  evals/sample:     1

Given that the bar for a reasonably performant forwards-pass is 1-2x the primal, this is definitely sub-optimal.

I'm reasonably sure that the culprit is the use of Base.Fix1 here: https://github.com/JuliaGaussianProcesses/KernelFunctions.jl/blob/13985cc6cb8903004f33e6fe9d2540571594a3c8/src/matrix/kernelmatrix.jl#L95

maping interesting types in is generally a bad idea when used in conjunction with Zygote unfortunately.

@theogf @devmotion I'm not sure what our best options are here. I think there are two questions here:

  1. could we tweak the current implementation a bit into something that Zygote likes, and get sane performance?
  2. what escape hatches do we have / could we have to make it straightforward to hand-improve performance where necessary?

For example, the use of Base.Fix1 here might actually be a real win, because we could hand-optimise the implementation of map(::Base.Fix1{typeof(kappa), SEKernel}, ::Array{<:Real}) quite straightforwardly. Unfortunately, this would essentially mean that any kernel that we care about having good performance with would require us to hand-implement stuff. If we did this using ForwardDiff, it might be fine though 🤷

n.b. it's definitely not the pairwise computations:

julia> @benchmark Zygote.pullback(KernelFunctions.pairwise, KernelFunctions.SqEuclidean(), randn(100))
BenchmarkTools.Trial:
  memory estimate:  82.25 KiB
  allocs estimate:  45
  --------------
  minimum time:     28.473 μs (0.00% GC)
  median time:      59.303 μs (0.00% GC)
  mean time:        74.604 μs (15.96% GC)
  maximum time:     15.654 ms (99.13% GC)
  --------------
  samples:          10000
  evals/sample:     1

they've been hand-optimised for ages, so it would have been really surprising if there were a problem there.

Lingua principale
Julia
Stelle
276
Fork
42
Merge medio
2g 20h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaGaussianProcesses/KernelFunctions.jl

Tutte le issue di JuliaGaussianProcesses/KernelFunctions.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.