Skip to content
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c2bf539
Update granite.md
tanuj-rai Apr 25, 2025
4e7bd11
Merge branch 'main' into update-granite-model-card
tanuj-rai Apr 25, 2025
b8919b0
Update docs/source/en/model_doc/granite.md
tanuj-rai Apr 26, 2025
9afbf99
Update docs/source/en/model_doc/granite.md
tanuj-rai Apr 26, 2025
6c53f2d
Update docs/source/en/model_doc/granite.md
tanuj-rai Apr 26, 2025
2f52e2a
Merge branch 'main' into update-granite-model-card
tanuj-rai Apr 26, 2025
1d5b2f8
update granite.md
tanuj-rai Apr 27, 2025
488a2cb
Merge branch 'main' into update-granite-model-card
tanuj-rai Apr 28, 2025
6d2b6da
Merge branch 'main' into update-granite-model-card
tanuj-rai May 2, 2025
d40ceb6
Merge branch 'main' into update-granite-model-card
tanuj-rai May 3, 2025
8162382
Merge branch 'main' into update-granite-model-card
tanuj-rai May 5, 2025
d50e8bf
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
ad573c1
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
059bf96
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
2a15c8d
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
3166d1c
Merge branch 'main' into update-granite-model-card
tanuj-rai May 22, 2025
413b74d
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
4045a50
Update docs/source/en/model_doc/granite.md
tanuj-rai May 22, 2025
5e9393d
Merge branch 'main' into update-granite-model-card
tanuj-rai May 22, 2025
190493e
Merge branch 'main' into update-granite-model-card
tanuj-rai May 23, 2025
900afb5
minor fixes
stevhliu May 23, 2025
f8a1d60
Merge branch 'main' into update-granite-model-card
tanuj-rai May 24, 2025
3bf48c3
Merge branch 'main' into update-granite-model-card
tanuj-rai May 27, 2025
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
106 changes: 75 additions & 31 deletions docs/source/en/model_doc/granite.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,106 @@ Unless required by applicable law or agreed to in writing, software distributed
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# Granite

<div class="flex flex-wrap space-x-1">
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
</div>

## Overview
# Granite

[Granite](https://huggingface.co/papers/2408.13359) is a 3B parameter language model trained with the Power scheduler. Discovering a good learning rate for pretraining large language models is difficult because it depends on so many variables (batch size, number of training tokens, etc.) and it is expensive to perform a hyperparameter search. The Power scheduler is based on a power-law relationship between the variables and their transferability to larger models. Combining the Power scheduler with Maximum Update Parameterization (MUP) allows a model to be pretrained with one set of hyperparameters regardless of all the variables.

You can find all the original Granite checkpoints under the [IBM-Granite](https://huggingface.co/ibm-granite) organization.

The Granite model was proposed in [Power Scheduler: A Batch Size and Token Number Agnostic Learning Rate Scheduler](https://arxiv.org/abs/2408.13359) by Yikang Shen, Matthew Stallone, Mayank Mishra, Gaoyuan Zhang, Shawn Tan, Aditya Prasad, Adriana Meza Soria, David D. Cox and Rameswar Panda.
> [!TIP]
> Click on the Granite models in the right sidebar for more examples of how to apply Granite to different language tasks.

PowerLM-3B is a 3B state-of-the-art small language model trained with the Power learning rate scheduler. It is trained on a wide range of open-source and synthetic datasets with permissive licenses. PowerLM-3B has shown promising results compared to other models in the size categories across various benchmarks, including natural language multi-choices, code generation, and math reasoning.
The example below demonstrates how to generate text with [`Pipeline`], [`AutoModel`, and from the command line.

The abstract from the paper is the following:
<hfoptions id="usage">
<hfoption id="Pipeline">

*Finding the optimal learning rate for language model pretraining is a challenging task.
This is not only because there is a complicated correlation between learning rate, batch size, number of training tokens, model size, and other hyperparameters but also because it is prohibitively expensive to perform a hyperparameter search for large language models with Billions or Trillions of parameters. Recent studies propose using small proxy models and small corpus to perform hyperparameter searches and transposing the optimal parameters to large models and large corpus. While the zero-shot transferability is theoretically and empirically proven for model size related hyperparameters, like depth and width, the zero-shot transfer from small corpus to large corpus is underexplored.
In this paper, we study the correlation between optimal learning rate, batch size, and number of training tokens for the recently proposed WSD scheduler. After thousands of small experiments, we found a power-law relationship between variables and demonstrated its transferability across model sizes. Based on the observation, we propose a new learning rate scheduler, Power scheduler, that is agnostic about the number of training tokens and batch size. The experiment shows that combining the Power scheduler with Maximum Update Parameterization (\mup) can consistently achieve impressive performance with one set of hyperparameters regardless of the number of training tokens, batch size, model size, and even model architecture. Our 3B dense and MoE models trained with the Power scheduler achieve comparable performance as state-of-the-art small language models.
We [open source](https://huggingface.co/collections/ibm/power-lm-66be64ae647ddf11b9808000) these pretrained models.*
```python
import torch
from transformers import pipeline
Comment thread
tanuj-rai marked this conversation as resolved.

pipe = pipeline(
Comment thread
tanuj-rai marked this conversation as resolved.
task="text-generation",
model="ibm-granite/granite-3.3-2b-base",
torch_dtype=torch.bfloat16,
device=0
)
pipe("Explain quantum computing in simple terms ", max_new_tokens=50)
```

Tips:
</hfoption>
<hfoption id="AutoModel">
Comment thread
tanuj-rai marked this conversation as resolved.

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_path = "ibm/PowerLM-3b"
tokenizer = AutoTokenizer.from_pretrained(model_path)

# drop device_map if running on CPU
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
model.eval()

# change input text as desired
prompt = "Write a code to find the maximum value in a list of numbers."

# tokenize the text
input_tokens = tokenizer(prompt, return_tensors="pt")
# generate output tokens
output = model.generate(**input_tokens, max_new_tokens=100)
# decode output tokens into text
output = tokenizer.batch_decode(output)
# loop over the batch to print, in this example the batch size is 1
for i in output:
print(i)
tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.3-2b-base")
model = AutoModelForCausalLM.from_pretrained(
"ibm-granite/granite-3.3-2b-base",
torch_dtype=torch.bfloat16,
device_map="auto",
attn_implementation="sdpa"
)

inputs = tokenizer("Explain quantum computing in simple terms", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_length=50, cache_implementation="static")
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
</hfoption>
<hfoption id="transformers CLI">

This model was contributed by [mayank-mishra](https://huggingface.co/mayank-mishra).
```python
echo -e "Explain quantum computing simply." | transformers-cli run --task text-generation --model ibm-granite/granite-3.3-8b-instruct --device 0
Comment thread
tanuj-rai marked this conversation as resolved.
```
</hfoption>
Comment thread
tanuj-rai marked this conversation as resolved.
</hfoptions>

Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends.

The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to int4.

```python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
Comment thread
tanuj-rai marked this conversation as resolved.

quantization_config = BitsAndBytesConfig(load_in_4bit=True)
tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.3-8b-base")
model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-3.3-8b-base", torch_dtype=torch.bfloat16, device_map="auto", attn_implementation="sdpa", quantization_config=quantization_config)

inputs = tokenizer("Explain quantum computing in simple terms", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_length=50, cache_implementation="static")
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

quantization_config = BitsAndBytesConfig(load_in_4bit=True)

tokenizer = AutoTokenizer.from_pretrained(""ibm-granite/granite-3.3-2b-base"")
model = AutoModelForCausalLM.from_pretrained(
"ibm-granite/granite-3.3-2b-base",
torch_dtype=torch.bfloat16,
device_map="auto",
attn_implementation="sdpa",
quantization_config=quantization_config,
)

input_ids = tokenizer("Explain artificial intelligence to a 10 year old", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_length=50, cache_implementation="static")
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```


## GraniteConfig

[[autodoc]] GraniteConfig
Expand Down