Skip to content

Granite modeling for training #830

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

Merged
merged 24 commits into from
Apr 11, 2025
Merged

Granite modeling for training #830

merged 24 commits into from
Apr 11, 2025

Conversation

tengomucho
Copy link
Collaborator

What does this PR do?

This PR introduces the modeling code for Granite, based on NxD core modeling example for Llama.
So far it is possible to instantiate ibm-granite/granite-3.2-2b-instruct and perform an inference on the sharded model.
The modeling code is based on transformers, and adapted to include sharding (only tested with TP parallelism).

  • MLP Linear layers are parallelized and fused.
  • Linear layers in Attention are parallelized too.

Flash attention is not used so far because it requires not-padded prompts to generate correct outputs. It was not possible to use fused layers, because in Granite self.num_heads != self.num_key_value_heads. The attention kernel implementation is then just the eager kernel.

This is NOT a complete implementation for training: parallel loss is not implemented, as well as a complete training test and/or example. These will follow up soon, but I thought it was better to submit this to get feedback sooner.

  • Did you write any new necessary tests?

The old functions are deprecated and produce a warning.
This commit is just a copy of Transformers' 4.48.1 implementation of
granite modeling. The code will be subsequentially modified to allow
parallelism during training.
The model parallelization is verified with a  simple inference.
Note that for now after each linear outputs are gathered, otherwise the
result in bfloat16 diverges too much compared to the one obtained when
using float32.
This allows for parallelization on the attention, using eager algorithm.
This allows to perform the attention calculation in a sharded way,
accelerating it.
Remove use_cache, cache_position and past_key_values arguments when
possible, since they are not used on training models and it is not
useful to keep them here.
After some tests, only the eager attention kernel has proven reliable,
so only this option is left in the code of the modeling for this model.
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@tengomucho tengomucho marked this pull request as ready for review April 9, 2025 13:49
from transformers.models.granite import GraniteConfig


class NeuronGraniteConfig(GraniteConfig):
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 rather create a completely different config instead of extending the GraniteConfig. This is what I do for LLM models.

Copy link
Member

Choose a reason for hiding this comment

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

In the PR I am working on to enable custom modeling, I just attach the mp_config which already existed before.
You can check it here and here.

from transformers.models.granite import GraniteConfig


class NeuronGraniteConfig(GraniteConfig):
Copy link
Member

Choose a reason for hiding this comment

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

In the PR I am working on to enable custom modeling, I just attach the mp_config which already existed before.
You can check it here and here.

Copy link
Member

Choose a reason for hiding this comment

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

For weight fusing / QGAQKV etc I have this API: https://github.com/huggingface/optimum-neuron/pull/801/files#diff-7fda1f4deb9a79e4201376095bd5eabc0650da1fb05672493d7949af0820be18R101-R107

WDYT?

Basically the idea is to have a common API so that we can share the code for splitting the loaded weights in from_pretrained and consolidating the checkpoints afterwards.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree it would be good to have a common API for these kind of things, and in general even for layers. I prefer having simpler functions though. I propose to isolate this, and then we can make the API uniform once your PR is ready.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

About the mp_config: I will separate the config as you had done, and we can merge the mp_config implementations once your PR is ready.

slice_tensor and fuse_weights can be reused in future modeling
implementations.
Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

LGTM!

Let's address the remaining points once #801 is merged as agreed offline.

Thanks @tengomucho !!

It is not used for now and it should not passed over to parent class.
The bidirectional dict just requires a key, that can be the class name
for simplicity.
Copy link
Collaborator

@dacorvo dacorvo left a comment

Choose a reason for hiding this comment

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

LGTM, thanks !

@tengomucho tengomucho merged commit 3ef0ff7 into main Apr 11, 2025
7 of 9 checks passed
@tengomucho tengomucho deleted the training-granite-modeling branch April 11, 2025 14:06
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.

4 participants