This is going to be the shortest blog post ever.
Industry observers note that question: Llama 3.2 landed two weeks ago on Hugging Face / Transformers. When will it be available in Keras? (Related: Industry News: ‘Minecraft Mommy’ goes viral on TikTok f...)
In a recent development, answer: It has been working from day 1 😀. There is nothing to wait for. (Related: Breaking: AI for Game Development: Creating a Farming G...)
Industry observers note that yes, Keras Llama3 can be loaded from any standard (i.e. safetensors) Hugging Face checkpoint, including the 3.2 checkpoints. If a conversion is required, it happens on the fly. Try this:.
OK, OK, I'm being told that if I want to publish a blog post, I have to fill the space. Here are a couple of additional things to know about Keras.
Industry observers note that keras is the time-tested modeling library for JAX, PyTorch and TensorFlow. You might have noticed this line in the demo Colab:.
It has to appear before import keras and controls if the model is running on JAX, PyTorch or TensorFlow. Very handy to try your favorite models on JAX with XLA compilation 🚀.
Keras is a modeling library and keras-hub is its collection of pre-trained models. It was previously called KerasNLP and KerasCV. The rename is in progress. It has all the popular pre-trained models (Llama3, Gemma, StableDiffusion, Segment Anything, ...) with their canonical implementation in Keras.
Industry observers note that I mean, "tokenizer included". model.generate() just works on strings:.
In a recent development, same thing for training. You can train on a set of strings directly:.
Industry observers note that instruction-tuned variants of popular LLMs can be used for turn-by-turn conversations. Here, Llama-3.2-1B-Instruct understands the following conversation tagging (see meta docs).
The conversation, once formatted in this way, can be fed directly to model.generate().
For convenience, the demo Colab implements a helper class called ChatState that does the necessary string concats automatically.
If you don't like "batteries included" and want to get to the underlying tokenizer and model, they are easily accessible:.
Recent findings suggest that the Tokenizer just transforms text into integer vectors. Here "Hello" translates into a single token:.
According to recent reports, the Preprocessor is a catch-all concept for doing all the data transformations a model requires. This could be, for example, image resizing or augmentation for tasks involving images, or text tokenization like here for a text model. For the CausalLM task, the preprocessor takes care of three additional details:.
Just call model.fit(ds) on your training dataset. This trainer is compatible with the various distributed training, mixed precision, quantization or LoRA/QLoRA options available in Keras. It's also totally optional. You can write a custom training loop if you want.
For a complete example, see the demo Colab where we fine-tune Llama 3.2 to speak like a pirate:.
Once you are happy with your fine-tuned model, upload it directly from Keras using:.
The uploaded model is visible here.
It has been revealed that some of you are wondering, why use Keras when one can already work with LLMs on Hugging Face using Transformers? Answer: even if you don't care about Keras' flexibility and usability as a modeling framework (you should!), Keras is your quickest path to advanced model parallelism thanks to JAX and its powerful XLA compiler.
Sources indicate that let's pick an 8B parameters model to demonstrate: meta-llama/Llama-3.1-8B-Instruct (demo Colab here). Without quantization, this model is too large for any single accelerator. With Keras, you can load it sharded across multiple accelerators, GPU or TPU. If you are uncertain about the "correct" weight shardings, most models provide sensible defaults. Here, call keras_hub.models.Llama3Backbone.get_layout_map(device_mesh):.
And if you don't trust the default layout map provided by the model, you can define your own. In this example running on a "small" TPU setup with only 8 cores, the following layout map is a bit faster than the default (54s/epoch rather than 62s/epoch):.
Recent findings suggest that check out the demo Colab here which fine-tunes the larger 8B Llama on pirate-speak, in just under 8 min on a Google TPU v5e (available in JupyterLab on Hugging Face Spaces. The fine-tuned model is here. And if you need a short model explainer about model parallelism and Keras, I have you covered here.