MasonProtter / Bumper.jl

Bring Your Own Stack
MIT License
152 stars 6 forks source link

ldiv! does not accept PtrArray #37

Open RobertGregg opened 5 months ago

RobertGregg commented 5 months ago

First off, interesting package! I think my issue is more with StrideArrays and LinearAlgebra not meshing well and Bumper is caught in the middle.

The error I'm getting comes from trying to use ldiv! which requires a factorized matrix, but StridedArrays always tries to produce a PtrArray regardless of the function applied:

X = rand(100,100)
y = rand(100)

function f(X,y)

    numObs, numFeatures = size(X)
    T = eltype(X)

    @no_escape begin
        Xfact = @alloc(T, numObs, numFeatures)
        b = @alloc(T, numFeatures)
        ŷ = @alloc(T, numObs)

        Xfact .= X
        qr!(Xfact)
        ldiv!(b,Xfact,y) # <-- ERROR: MethodError: no method matching ldiv!(::PtrArray{…}, ::PtrArray{…})
        mul!(ŷ,X,b)

        err = sum((yᵢ - ŷᵢ)^2 for (yᵢ, ŷᵢ) in zip(y,ŷ)) / numObs
    end

    return err
end

I'm guessing there's no easy way to avoid using PtrArrays. I can use X\y but this of course allocates which kind of defeats the purpose.