JuliaDiff / JuliaDiff/ChainRulesCore.jl

`ProjectTo{SparseMatrixCSC}` can incorrectly assume `eltype` of projected value

Open
#571 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ProjectTo
Dominant language
Julia
Stars
267
Forks
66
PR merge metrics
No merged PRs in 30d

Description

The example below attempts to project a Unitful.Quantity differential onto a sparse array of Float64

using SparseArrays
using ChainRulesCore

using Unitful
using UnitfulChainRules # defines projection of quantities onto Floats/Complex{<:Float}

A = sprandn(5,5, 0.5)
pA = ProjectTo(A)

dx = randn((5,5)) * u"m"

pA(dx)
# Unitful error:
# ERROR: DimensionError:  and m are not dimensionally compatible.

The error occurs here because we pre-allocate the nzvals vector using project_type(project.element). However, project_type doesn't take into account the value we are projecting, which, in the case of a Unitful.Quantity, can mean that project_type(ProjectTo(A)) != eltype(A).

In the case of SparseMatrixCSC, we can rewrite the projection without preallocating nzvals using mapreduce to get a type-stable pullback that doesn't use project_type. In general, would it be better to get away without project_type and write projections which infer the type based on the input types?

E- On closer inspection the mapreduce solution runs into issues if the differential isn't a homogeneous type, project_type may be a necessary solution here

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/projection.jl at lines 575-583 and reproduce the SparseMatrixCSC projection with a Unitful.Quantity differential and a Float64 sparse array. Investigate how project_type and the proposed mapreduce approach handle projected and heterogeneous values; done means the projection no longer raises the reported dimension error while preserving correct type behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.