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

Introduce explicit parameter mapping #148

Merged
merged 4 commits into from
Jan 26, 2023
Merged

Introduce explicit parameter mapping #148

merged 4 commits into from
Jan 26, 2023

Conversation

jonatanklosko
Copy link
Member

Closes #71.

Advantages:

  • This removes a lot of duplication between models. For example, now all of the models use the same attention implementation, and even the whole transformer block.
  • Certain models looked very differently, even though they were for the most part the same, that's because their reference implementations applied the same concepts, but implemented them on their own (naming, structuring and nesting the layers). Now the models are much more consistent, both in terms of layer nesting and naming, which makes it much easier to see the similarities and differences.
  • We no longer need to follow an artificial layers nesting, that follows from the class composition in the Python implementations. For example, when building a model with head, we don't need to prefix the base model with something like bert., we just add the head layers.
  • We have full control over the parameters, which makes us much less coupled to the original implementation details. For example the original GPT2 implementation uses conv1d layers (in terms of implementation those are essentially transposed dense layers) for attention. The parameter mapping has enough flexibility that we actually slice the conv1d kernels into dense layer kernels and use the same attention implementation as all other models.

Disadvantages:

  • A single file doesn't contain all of the model. This already wasn't the case and I don't consider this a major issue. Also, we could still be doing that and use consistent naming/implementation, but again I don't think we should.
  • Each model needs explicit mapping to hf/transformers parameters. This also implies we use different parameter names than hf/transformers.

Copy link
Contributor

@seanmor5 seanmor5 left a comment

Choose a reason for hiding this comment

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

@jonatanklosko I've just skimmed a bit, but my initial thoughts are that this is fantastic! It should also make adding new models significantly easier, and in general is much more manageable for us to maintain.

I will give a more in-depth review later

@seanmor5
Copy link
Contributor

Also, on requiring mappings, I don't think that's a big deal, HF actually requires mappings for non-PT models anyway, and we mostly end up hiding that fact from the user anyway. It's a consequence of depending on an external community for models, but I don't think it's a bad one

@josevalim
Copy link
Contributor

Fantastic work!


Both param names and values for corresponding layers may not match
exactly, so they require further transformations. For example, the
convolution `"kernel"` in Axon corresponds to a transposed `"weight"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if Axon should just make the switch to use weight everywhere like PyTorch. It would make the conversions a bit easier, though you'd still have to pay attention to doing the transpose in some cases

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we should be changing just because of PyTorch in this case, flax does use kernel. Parameter naming in particular is not much hassle, especially that in case we don't need to transpose, we just always look for weight instead of kernel by default.

lib/bumblebee/layers.ex Outdated Show resolved Hide resolved
@@ -148,6 +146,15 @@ defmodule Bumblebee.Layers.Decoder do
end
end

defnp append_attention_cache(key, value, attention_cache, offset, _opts \\ []) do
%{key: cached_key, value: cached_value} = attention_cache
indices = [0, Nx.as_type(offset, {:s, 64}), 0, 0]
Copy link
Contributor

Choose a reason for hiding this comment

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

When is offset not s64?

Copy link
Member Author

Choose a reason for hiding this comment

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

Offset comes from the cache, which we pass as model input and get back as model output, so Axon would automatically convert it to float either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh makes sense, I have opened this issue in Axon to fix this: elixir-nx/axon#464

@jonatanklosko jonatanklosko merged commit b4bada2 into main Jan 26, 2023
@jonatanklosko jonatanklosko deleted the jk-params-mapping branch January 26, 2023 12:29
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.

Remap layer names
3 participants