# CUDNN operators not supporting Views/SubArrays

**URL:** <https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463>\
**Category:** Machine Learning\
**Created:** [August 31, 2021, 10:50pm UTC](https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463 "2021-08-31T22:50:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremiedb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremiedb/32/29150_2.png) [@jeremiedb](https://discourse.julialang.org/u/jeremiedb)\
**Post date:** [August 31, 2021, 10:50pm UTC](https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463/1 "2021-08-31T22:50:23Z")

</div>

Is there a reason for Convolution (and other CUDNN related functions) to be restricted to `DenseCuArray` as found in the [NNlibCUDA.jl](https://github.com/FluxML/NNlibCUDA.jl/blob/ed0a9a6473c7d40e7361d14cbc71c338cc3960ea/src/cudnn/conv.jl#L25) repo?

For example, taking a view here will result in an error as CUDA has to resort to CPU as the `Conv` fucntion doesn’t support the `view` on the `CuArray`:

```julia
using CUDA
using Flux

m = Conv((1,), 3 => 4) |> gpu

x1 = CUDA.rand(1,3,8);
m(x1); # works fine

x2 = CUDA.rand(1,4,8);
x3 = view(x2, :, 1:3, :);
m(x3);
ERROR: TaskFailedException

    nested task error: Scalar indexing is disallowed.

```

Performing a Dense operator on a view is working fine however (as Dense function supports AbstractArray input and doesn’t rely on CUDNN).

The actual use case is that I’m using a custom dataloader in which the full dataset is stored as a CuArray and I’d like it to provide the data batches as views to avoid allocations. Are CUDNN operators inherently forbidding such optimization?

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [September 2, 2021, 8:14am UTC](https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463/2 "2021-09-02T08:14:26Z")

</div>

Does it work relaxing to AnyCuArray?

---

<div class="post-metadata">

**Author:** ![jeremiedb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremiedb/32/29150_2.png) [@jeremiedb](https://discourse.julialang.org/u/jeremiedb)\
**Post date:** [September 2, 2021, 9:53pm UTC](https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463/3 "2021-09-02T21:53:35Z")

</div>

Yes, by switching the `cudnn/conv.jl` functions to `AnyCuArray` instead of `DenseCuArray` in CUDNN.jl, it produces the same output.

I’m however quite unsafe whether there’s some CUDNN trap I’m overlooking by doing so.  
Would it makes sense that I open a PR with the conversion to `AnyCuArray`? I guess @denizyuret would be aware if there are contraindications to such relaxation?

---

<div class="post-metadata">

**Author:** ![denizyuret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denizyuret/32/568_2.png) [@denizyuret](https://discourse.julialang.org/u/denizyuret)\
**Post date:** [September 3, 2021, 8:05am UTC](https://discourse.julialang.org/t/cudnn-operators-not-supporting-views-subarrays/67463/4 "2021-09-03T08:05:58Z")

</div>

One potential problem could be views that are not contiguous: I am fairly sure the low level cudnn functions would not like these. If I remember correctly CUDNN tensors can have strides but not all functions support them. We could test with a PR that uses AnyCuArray and see the limits.
