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

fix: broadcast vectors for grad calculation #1535

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

polvalente
Copy link
Contributor

closes #1533

@polvalente polvalente self-assigned this Sep 15, 2024
nx/lib/nx/defn/grad.ex Outdated Show resolved Hide resolved
Expr.constant(%T{shape: shape, type: {:f, 32}, names: names}, float, [])
case shape do
%T{vectorized_axes: [_ | _]} = t ->
Expr.tensor(Nx.fill(t, float, type: :f32))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably get rid of the names here too.

I also wonder if should move the check for vectorized_axes to constant. Today if someone passes vectorized_axes, Expr.constant is broken. So maybe we should create a tensor if a vectorized axes is given to tensor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -338,6 +333,8 @@ defmodule Nx.Defn.Grad do
@verify_grad Application.compile_env(:nx, :verify_grad, false)

defp update_grads(op, args, ans, g, _to_grad_ids, grads) do
args = revectorize_args(args, ans)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not revectorized everything on every operation. Is there any chance we could do in broadcast only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[unbroadcast(x, Nx.multiply(g, y), ans), unbroadcast(y, Nx.multiply(g, x), ans)]

Lines like this one make it so that g is vectorized and y is unvectorized but has axes with the same name, so things break there.

@@ -1394,6 +1394,11 @@ defmodule Nx.Defn.Expr do

## Constant helpers and related optimizations

defp constant(%{vectorized_axes: [_ | _]} = out, number) do
out = %{out | names: Enum.map(out.names, fn _ -> nil end)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this part should be done here, we should preserve the names. Sorry for the confusion.

@@ -1343,9 +1334,77 @@ defmodule Nx.Defn.Grad do

## General helpers

defp unbroadcast(%{shape: shape} = x, res, %{shape: shape}), do: {x, res}
defp revectorize_args(args, ans) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only apply this if args has more than one element and there are vectorized axes.

Also please test x * sin(y) where y is vectorized.

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

Successfully merging this pull request may close these issues.

Nx.Defn.Grad returns sum of gradient over vectorized axis?
2 participants