Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

rrule for casting LinearAlgebra.QRCompactWYQ into a Matrix

Đang mở
#516 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
julia
Lĩnh vực
devtools

Hướng nghiên cứu

Bắt đầu bằng việc tái hiện với Matrix(Q) và Q từ một phép phân rã qr không đầy đủ, sau đó kiểm tra src/rulesets/Base/array.jl và rrule hiện có của Matrix. Theo dõi cách ProjectTo và Array_pullback xử lý gradient đầu vào có kích thước (6,4) so với kích thước (6,6) của QRCompactWYQ. Được xem là hoàn tất khi backward pass cho Matrix(Q) hoàn thành mà không có DimensionMismatch và các lệnh gọi gradient hiện có vẫn hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I'm trying to work with the Q matrix from a qr-factorization within Zygote. In an incomplete QR factorization for m>=n,
the QRCompactWYQMatrix Q has size=(m,m) but only the first n columns are relevant: https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.qr

Using this in Zygote behaves as follows:

using LinearAlgebra
using Zygote
using Random

Random.seed!(1234)

V = rand(Float32, (6,4))
Q, _ = qr(V)

Zygote.gradient(A -> sum(A), Matrix(Q))
Zygote.gradient(A -> sum(A), Q)
Zygote.gradient(A -> sum(Matrix(A)), Q)

ERROR: LoadError: DimensionMismatch("variable with size(x) == (6, 6) cannot have a gradient with size(dx) == (6, 4)")
Stacktrace:
 [1] (::ChainRulesCore.ProjectTo{AbstractArray, NamedTuple{(:element, :axes), Tuple{ChainRulesCore.ProjectTo{Float32, NamedTuple{(), Tuple{}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}})(dx::FillArrays.Fill{Float32, 2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}})
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/Voykb/src/projection.jl:209
 [2] Array_pullback
   @ ~/.julia/packages/ChainRules/qd40H/src/rulesets/Base/array.jl:9 [inlined]
 [3] ZBack
   @ ~/.julia/packages/Zygote/nsu1Y/src/compiler/chainrules.jl:140 [inlined]
 [4] Pullback
   @ ~/source/gpuplayground/src/qr_project_mwe.jl:16 [inlined]
 [5] (::Zygote.var"#50#51"{typeof(∂(#5))})(Δ::Float32)
   @ Zygote ~/.julia/packages/Zygote/nsu1Y/src/compiler/interface.jl:41
 [6] gradient(f::Function, args::LinearAlgebra.QRCompactWYQ{Float32, Matrix{Float32}})
   @ Zygote ~/.julia/packages/Zygote/nsu1Y/src/compiler/interface.jl:76
 [7] top-level scope

The first two calls to gradient work fine. The last call fails because the incoming gradient is size=(6,4) but the matrix is of size=(6,6).

I tried modifying the rrule from here, https://github.com/JuliaDiff/ChainRules.jl/blob/master/src/rulesets/Base/array.jl line 7 like this:

function ChainRules.rrule(::typeof(Matrix), x::LinearAlgebra.QRCompactWYQ) 
  project_x = ProjectTo(x)
  Array_pullback(ȳ) = (NoTangent(), project_x(ȳ))
  return T(x), Array_pullback
end

but that didn't work. Is there a good way of making calls to Matrix(Q) in a backwards pass?
@mcabbott

Ngôn ngữ chính
Julia
Star
475
Fork
98
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JuliaDiff/ChainRules.jl

Tất cả issue của JuliaDiff/ChainRules.jl

Issue tương tự

Thêm issue về Julia

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.