Dot matrix of parameters of a neural network?

Hello, I am trying to do the dot product of the parameters of a Zygote neural network times some big matrix:

params = best_model |> Flux.params
dot( params[1], big_matrix)

but I am getting this error message:

Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations do not execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.

[1] error(s::String)
@ Base ./error.jl:33
[2] assertscalar(op::String)
@ GPUArraysCore ~/.julia/packages/GPUArraysCore/rSIl2/src/GPUArraysCore.jl:78
[3] getindex(xs::CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, I::Int64)
@ GPUArrays ~/.julia/packages/GPUArrays/EVTem/src/host/indexing.jl:9
[4] first
@ ./abstractarray.jl:398 [inlined]
[5] dot(x::CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, y::Matrix{Int64})
@ LinearAlgebra /opt/julia-1.7.3/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl:915
[6] top-level scope
@ In[388]:1
[7] eval
@ ./boot.jl:373 [inlined]
[8] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196

What can I do to work around this issue?
I didn’t really wanted to use the index, it seems that dot() is using it internally and it is causing issues

It looks like your params are on the GPU and big_matrix is not. Try moving them to be the same device:

gpu_big_matrix = gpu(matrix)
dot( params[1], gpu_big_matrix)


cpu_params = cpu(params[1])
dot(cpu_params, big_matrix)
1 Like