Skip to content

softmax(x) and logsoftmax(x) update their arguments #592

@dfdx

Description

@dfdx

Describe the bug

Unlike NNlib versions, softmax(x) and logsoftmax(x) in CUDA.jl modify their arguments in-place.

To reproduce

The simplest way to confirm it is to look at the code, but for the sake of completeness here's the minimal reproducible example:

using CUDA
using NNlib

x = cu(rand(5, 10))
original_x = copy(x)
softmax(x)
@assert x == original_x

Possible fix

It should be easy to fix softmax(), logsoftmax(), ∇softmax() and ∇logsoftmax() by allocating a new array for the result, e.g.:

softmax(x::DenseCuArray{T}; dims=1) where T<:CUDNNFloat =
         softmax!(similar(x), x, dims=dims)

Right now I'm struggling with another issue stopping me from creating a PR, but I plan to come back to it later.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions