Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KnetArray linear indexing does not work #32

Closed
kirnap opened this issue Oct 28, 2016 · 3 comments
Closed

KnetArray linear indexing does not work #32

kirnap opened this issue Oct 28, 2016 · 3 comments

Comments

@kirnap
Copy link
Collaborator

kirnap commented Oct 28, 2016

a = randn(256,10000); x = convert(Knet.KnetArray, a); x[[1,2,3]] creates the following error:

ERROR: MethodError: no method matching getindex(::Knet.KnetArray{Float64,2}, ::Array{Int64,1})

@denizyuret denizyuret self-assigned this Nov 3, 2016
@denizyuret
Copy link
Owner

You can implement this as x[1:3]. Do you need non-contiguous indices?

@denizyuret
Copy link
Owner

Currently only Integer, Range, or Colon indices are supported for KnetArray. To get the rest to work, we need to complete the missing getindex methods.

In Julia, a[[1,3,5]] calls:

getindex(A::AbstractArray, I...) at abstractarray.jl:751
_getindex(l::Base.LinearIndexing, A::AbstractArray, i::Union{AbstractArray,Colon,Real}) at multidimensional.jl:275
_unsafe_getindex(::Base.LinearIndexing, A::AbstractArray, I::Union{AbstractArray,Colon,Real}...) at multidimensional.jl:291
_unsafe_getindex!(dest::AbstractArray, src::AbstractArray, I::Union{AbstractArray,Colon,Real}...) at multidimensional.jl:340

and a[[true,false,true]] calls:

_unsafe_getindex(::Base.LinearFast, src::AbstractArray, I::AbstractArray{Bool,N<:Any}) at multidimensional.jl:321

These need to be ported to KnetArrays. We get them for free if we inherit from AbstractArray, but that has other problems such as inefficient operations getting called inadvertently on KnetArrays. However as a temporary workaround you can try modifying the type declaration of KnetArray in karray.jl to inherit from AbstractArray:

type KnetArray{T,N} <: AbstractArray

@denizyuret
Copy link
Owner

Implemented the following indexing options:

  • 1-D: Int, Colon, UnitRange, StepRange, Array{Int}, BitArray, CartesianIndex, Array{CartesianIndex} (1-D includes linear indexing of multidimensional arrays)
  • 2-D: (Colon,Union{Int,Colon,UnitRange,StepRange,Array{Int}}), (Union{Int,UnitRange,Colon}...) (in any order)
  • N-D: (Int...)
    The 1-D support is complete, 2-D and N-D are still missing a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants