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

Basic operations are type unstable #648

Open
yushoteke opened this issue Dec 22, 2020 · 2 comments
Open

Basic operations are type unstable #648

yushoteke opened this issue Dec 22, 2020 · 2 comments

Comments

@yushoteke
Copy link

Hi,

I am currently using Julia v1.5.3, Knet v1.4.5.
By doing @code_warntype a+b, where a,b has types KnetArray{Float32,2}, The return type is any. I found this to be true for some other basic operations as well, for example, @code_warntype a*b.

I tried to fix this myself, but I found out it uses the LibKnet8, so could you have a look at what's going on underneath?

@denizyuret
Copy link
Owner

I am not sure why Julia is not able to deduce the type or how to debug it. The same problem does not seem to exist with unary operators like sin, cos, which puzzles me.

On the other hand this may not be worth spending too much on as I am planning to retire KnetArrays as soon as I can fix all the performance issues with CuArrays. (see https://discourse.julialang.org/t/ann-knet-1-4-0-accelerating-cuarrays/45206)

@yushoteke
Copy link
Author

I looked into the issue by calling the following:
@code_warntype Base.broadcasted(Knet.KnetArrays.:+,a,b)
Where a,b are KnetArray.

Looks like the issue is due to a branching. The branch when a,b have similar sizes looks ok to me, but the other branch, when their sizes are different, if calls vbroadcast_shape, and this is where the type starts to mess up. vbroadcast_shape returns a n dimensional tuple, an indeterminate type, and without a concrete type, the _broadcasted function cannot infer the type.

I'm thinking, suppose a is type KnetArray{T,N1}, b is type KnetArray{T,N2}, as long as they are broadcastable, the result must be of type KnetArray{T,max(N1,N2)}. So maybe you could force the type at the end of computation. I'm not sure whether the above is truely the case, but if that is, then maybe something like the following could solve the type stability issue:

function broadcasted(::typeof($J),x::KnetArray{$T,N},y::KnetArray{$T,M}) where {N,M}
if size(x)==size(y)
z = similar(x)
@knet8($F11,(Cint,Ptr{$T},Ptr{$T},Ptr{$T}),length(z),x,y,z)
return z
else
bs = vbroadcast_shape(x,y)
z::KnetArray{$T,max(N,M)} = similar(x,bs[1])
_broadcasted($J,x,y,z,bs)
end
end

Please check if this contradicts anything else you have in mind.

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

No branches or pull requests

2 participants