# Gradient for Cholesky decomposition with CuArrays with Zygote.jl

**URL:** https://discourse.julialang.org/t/gradient-for-cholesky-decomposition-with-cuarrays-with-zygote-jl/98900
**Category:** General Usage
**Tags:** question, linearalgebra, zygote, autodiff
**Created:** [May 15, 2023, 9:47pm UTC](https://discourse.julialang.org/t/gradient-for-cholesky-decomposition-with-cuarrays-with-zygote-jl/98900 "2023-05-15T21:47:48Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![guixien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guixien/32/34399_2.png) [@guixien](https://discourse.julialang.org/u/guixien)
#### Post date: [May 15, 2023, 9:47pm UTC](https://discourse.julialang.org/t/gradient-for-cholesky-decomposition-with-cuarrays-with-zygote-jl/98900/1 "2023-05-15T21:47:48Z")

</div>

Here’s a minimal example:

> using Zygote, CUDA, LinearAlgebra  
> CUDA.allowscalar(false)  
> C = cu(rand(5,5))  
> f(x) = sum(cholesky(x’ \* x).L)  
> Zygote.gradient(f, C)

While the CPU version works fine, this will throw the scalar indexing error. It seems like this is a recurring issue:

- [Need a GPU compatible `rrule` for Cholesky · Issue #629 · JuliaDiff/ChainRules.jl · GitHub](https://github.com/JuliaDiff/ChainRules.jl/issues/629)
- [Adjoint of `cholesky` is hard-coded for the CPU · Issue #1210 · FluxML/Zygote.jl · GitHub](https://github.com/FluxML/Zygote.jl/issues/1210)

and there’s a purposed change with a customized adjoint:

> <https://github.com/FluxML/Zygote.jl/pull/1238/files/5d0626c456bcef86e0b46ece6c5810e6cca58468>
>
> This addresses #1210

But that doesn’t work for the above case.

Does anyone know how to get around this?
