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

feat: vectorize cumulative op #1163

Merged
merged 1 commit into from Mar 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 71 additions & 7 deletions nx/lib/nx.ex
Expand Up @@ -9917,6 +9917,22 @@ defmodule Nx do
[21, 15, 8]
]
>

## Vectorized axes

Works the same as if the accumulation was to happen over a list of tensors.
`:axis` refers to the non-vectorized shape.

iex> Nx.cumulative_sum(Nx.tensor([[2, 3, 1], [1, 3, 2], [2, 1, 3]]) |> Nx.vectorize(:x), axis: 0)
#Nx.Tensor<
vectorized[x: 3]
s64[3]
[
[2, 5, 6],
[1, 4, 6],
[2, 3, 6]
]
>
"""
@doc type: :cumulative
def cumulative_sum(tensor, opts \\ []),
Expand Down Expand Up @@ -9977,6 +9993,22 @@ defmodule Nx do
[336, 56, 8]
]
>

## Vectorized axes

Works the same as if the accumulation was to happen over a list of tensors.
`:axis` refers to the non-vectorized shape.

iex> Nx.cumulative_product(Nx.tensor([[2, 3, 0], [1, 3, 2], [2, 1, 3]]) |> Nx.vectorize(:x), axis: 0)
#Nx.Tensor<
vectorized[x: 3]
s64[3]
[
[2, 6, 0],
[1, 3, 6],
[2, 2, 6]
]
>
"""
@doc type: :cumulative
def cumulative_product(tensor, opts \\ []),
Expand Down Expand Up @@ -10037,6 +10069,22 @@ defmodule Nx do
[1, 1, 3]
]
>

## Vectorized axes

Works the same as if the accumulation was to happen over a list of tensors.
`:axis` refers to the non-vectorized shape.

iex> Nx.cumulative_min(Nx.tensor([[2, 3, 1], [1, 3, 2], [2, 1, 3]]) |> Nx.vectorize(:x), axis: 0)
#Nx.Tensor<
vectorized[x: 3]
s64[3]
[
[2, 2, 1],
[1, 1, 1],
[2, 1, 1]
]
>
"""
@doc type: :cumulative
def cumulative_min(tensor, opts \\ []),
Expand Down Expand Up @@ -10097,20 +10145,36 @@ defmodule Nx do
[3, 3, 3]
]
>

## Vectorized axes

Works the same as if the accumulation was to happen over a list of tensors.
`:axis` refers to the non-vectorized shape.

iex> Nx.cumulative_max(Nx.tensor([[2, 3, 1], [1, 3, 2], [2, 1, 3]]) |> Nx.vectorize(:x), axis: 0)
#Nx.Tensor<
vectorized[x: 3]
s64[3]
[
[2, 3, 3],
[1, 3, 3],
[2, 2, 3]
]
>
"""
@doc type: :cumulative
def cumulative_max(tensor, opts \\ []),
do: cumulative_op(tensor, opts, :cumulative_max, &Nx.max/2)

defp cumulative_op(tensor, opts, op, reduce_fun) do
opts = keyword!(opts, axis: 0, reverse: false)
reverse = opts[:reverse]
tensor = to_tensor(tensor)
Nx.Shared.raise_vectorized_not_implemented_yet(tensor, __ENV__.function)
axis = Nx.Shape.normalize_axis(tensor.shape, opts[:axis], tensor.names)
apply_vectorized(tensor, fn tensor, offset ->
opts = keyword!(opts, axis: 0, reverse: false)
reverse = opts[:reverse]
axis = Nx.Shape.normalize_axis(tensor.shape, opts[:axis], tensor.names, offset)

Nx.Shared.optional(op, [tensor, [axis: axis, reverse: reverse]], tensor, fn tensor, opts ->
associative_scan(tensor, reduce_fun, opts)
Nx.Shared.optional(op, [tensor, [axis: axis, reverse: reverse]], tensor, fn tensor, opts ->
associative_scan(tensor, reduce_fun, opts)
end)
end)
end

Expand Down