diff --git a/docs/source/en/model_doc/granite.md b/docs/source/en/model_doc/granite.md index 0326bc5ad24..0f54db1bd2e 100644 --- a/docs/source/en/model_doc/granite.md +++ b/docs/source/en/model_doc/granite.md @@ -9,12 +9,11 @@ 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
PyTorch @@ -22,49 +21,94 @@ rendered properly in your Markdown viewer. SDPA
-## Overview +# Granite -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. +[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. -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. +You can find all the original Granite checkpoints under the [IBM-Granite](https://huggingface.co/ibm-granite) organization. -The abstract from the paper is the following: +> [!TIP] +> Click on the Granite models in the right sidebar for more examples of how to apply Granite to different language tasks. -*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.* +The example below demonstrates how to generate text with [`Pipeline`], [`AutoModel`, and from the command line. -Tips: + + + +```python +import torch +from transformers import pipeline + +pipe = pipeline( + 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) +``` + + + ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer -model_path = "ibm/PowerLM-3b" -tokenizer = AutoTokenizer.from_pretrained(model_path) +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" +) -# drop device_map if running on CPU -model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto") -model.eval() +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)) +``` + + -# change input text as desired -prompt = "Write a code to find the maximum value in a list of numbers." +```python +echo -e "Explain quantum computing simply." | transformers-cli run --task text-generation --model ibm-granite/granite-3.3-8b-instruct --device 0 +``` + + -# 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) +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 + +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)) ``` -This model was contributed by [mayank-mishra](https://huggingface.co/mayank-mishra). - - + ## GraniteConfig [[autodoc]] GraniteConfig