Skip to content

Type stability of KernelSum #458

Closed
Closed
@simsurace

Description

@simsurace
using KernelFunctions
using Test
k = RBFKernel() + RBFKernel() * ExponentialKernel()
@inferred k(0.1, 0.2) # ERROR
@time k(0.1, 0.2)  # 0.000009 seconds (5 allocations: 176 bytes)

but e.g.

using KernelFunctions
using Test
k = RBFKernel() + RBFKernel() * LinearKernel()
@inferred k(0.1, 0.2) # works

Similar results hold for kernelmatrix and kernelmatrix_diag.
I haven't figured out why certain kernels show the problem while others don't.
This may be a variant of JuliaLang/julia#45748 and could be a Julia issue.
The similarity to the referenced Julia issue is that calling sum before affects type inference:

# restart Julia
using KernelFunctions
using Test
k = RBFKernel() + RBFKernel() * ExponentialKernel()
sum(f -> f(0.1, 0.2), k.kernels)
@inferred k(0.1, 0.2) # works
@time k(0.1, 0.2) # 0.000008 seconds (1 allocation: 16 bytes)

Despite this probably being a Julia issue, we may want to patch it anyways, as it can adversely impact AD for certain GP models.
I'll open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions