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

WIP: GaLore Implementation #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/polaris/updates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,64 @@ defmodule Polaris.Updates do
Map.merge(params, state, fn _, s1, s2 -> merge_inner(s1, s2) end)
end

@doc """
Applies the GaLore algorithm to an optimizer for low-memory
training.
"""
def galore({parent_init_fn, parent_apply_fn}, galore_params, opts \\ []) do
opts = Keyword.validate!(opts, rank: 128, scale: 1.0)

init_fn = fn params ->
# on initialization, project down so we initialize parent
# state with low-rank version
{galore, regular} = Map.split(params, galore_params)
{projected, _ortho_matrix} = apply_galore_projection_down(galore, opts)
parent_init_fn.(Map.merge(projected, regular))
end

apply_fn = fn updates, state, params ->
{galore, regular} = Map.split(updates, galore_params)
{projected, ortho_matrix} = apply_galore_projection_down(galore, opts)
{scaled_updates, new_state} = parent_apply_fn.(Map.merge(projected, regular), state, params)
{galore, regular} = Map.split(scaled_updates, galore_params)
galore_updates = apply_galore_projection_up(galore, ortho_matrix, opts)
{Map.merge(galore_updates, regular), new_state}
end

{init_fn, apply_fn}
end

defnp apply_galore_projection_down(params, opts \\ []) do
opts = keyword!(opts, rank: 128, scale: 1.0)

ortho_matrix =
deep_new(params, fn g ->
get_orthogonal_matrix(g, rank: opts[:rank])
end)

projected =
deep_merge(params, ortho_matrix, fn g, ortho ->
Nx.dot(g, Nx.transpose(ortho))
end)

{projected, ortho_matrix}
end

defnp apply_galore_projection_up(params, ortho_matrix, opts \\ []) do
opts = keyword!(opts, rank: 128, scale: 1.0)

deep_merge(params, ortho_matrix, fn g, ortho ->
opts[:scale] * Nx.dot(g, ortho)
end)
end

defnp get_orthogonal_matrix(g, opts \\ []) do
opts = keyword!(opts, rank: 128)

{_u, _s, vh} = Nx.LinAlg.svd(g, full_matrices?: false)
vh[[0..opts[:rank], ..]]
end

## Helpers

defnp update_moment(x, moment, decay, order) do
Expand Down
Loading