Adapt.jl
Adapt.jl copied to clipboard
default to convert
why we have this
adapt_(T, x) = x
instead of
adapt_(T, x) = convert(T, x)
so that adapt
could be used as an opt-out replacement to convert?
Bye C
@CarloLucibello @MikeInnes Any thoughts on this? I think that convert
is the most appropriate fallback.
The problem is that adapt can be called with anything, e.g. adapt(CuArray, 2.0)
. In cases like that you don't want to do anything, since 2.0
is already GPU compatible (and conversion is nonsensical anyway). At least in the GPU case, there's essentially only one type you want to convert (Array
) and everything else should be left alone, so it makes a lot more sense for this to be opt-in.
It might be good to see motivating use cases where opt-out would be better, but the answer might be that you just want a different construct than adapt
.
Ahhh good point. What if we did something like this:
function adapt_(T, x)
try
convert(T, x)
catch
x
end
end
It’s certainly not the prettiest code, but it has the desired effect - for example, in the CuArrays example, arrays will be converted to CuArrays, and other types will stay the same.
Sent with GitHawk