---
title: "Hugging Face's TensorFlow Philosophy"
description: "Hugging Face's transformers library treats TensorFlow models as first-class Keras citizens: every model is a Keras Model and every layer a Keras Layer, so fit(), compile() and predict() work directly, and models compose into custom hybrids via subclassing."
url: "https://swe.quest/articles/hugging-face-s-tensorflow-philosophy-1d3751167c605c69"
canonical: "https://huggingface.co/blog/tensorflow-philosophy"
source: "Hugging Face"
published: "2022-08-12"
difficulty: "medium"
kind: "Opinion"
reading_minutes: 14
tags: ["machine-learning","python","model-training","compilers"]
---

# Hugging Face's TensorFlow Philosophy

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-08-12, 14 min read, Opinion. Original: [https://huggingface.co/blog/tensorflow-philosophy](https://huggingface.co/blog/tensorflow-philosophy)

## Summary

Hugging Face's transformers library treats TensorFlow models as first-class Keras citizens: every model is a Keras Model and every layer a Keras Layer, so fit(), compile() and predict() work directly, and models compose into custom hybrids via subclassing. If compile() is called without a loss argument, a sensible default loss is supplied (e.g. masked LM loss that matches BERT's original training), but a user-specified loss overrides it. Labels can now be passed the standard Keras way, and prepare\_tf\_dataset() streams disk-backed datasets with per-batch padding. XLA via jit\_compile=True brings generation and training to roughly JAX-level speed, with the caveat that variable input shapes force recompilation.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)

## Prerequisites

- Keras basics
- transfer learning
- TensorFlow

## Keywords

TensorFlow, Keras, Hugging Face Transformers, XLA, tf.data, Hugging Face Datasets, TF Serving

## Related posts

- [Faster Text Generation with TensorFlow and XLA](https://swe.quest/articles/faster-text-generation-with-tensorflow-and-xla-627c823778b1885e.md): Hugging Face · 2022-07-27 · Medium · 11 min read · Inference & serving, Compilers, LLMs, Performance engineering, Python
  TensorFlow text generation in Hugging Face transformers can be compiled with XLA by wrapping \`model.generate\` in \`tf.function(jit\_compile=True)\`, yielding up to 100x speedups and outperforming PyTorch.
- [Announcing New Hugging Face and KerasHub integration](https://swe.quest/articles/announcing-new-hugging-face-and-kerashub-integration-4c5abe3fdc47bd7b.md): Hugging Face · 2024-07-10 · Medium · 5 min read · Machine learning, LLMs, Python
  KerasHub can now load Transformers-format checkpoints from the Hugging Face Hub directly, through a shared model save format.
- [“Llama 3.2 in Keras”](https://swe.quest/articles/llama-3-2-in-keras-c28d5e4494723a08.md): Hugging Face · 2024-10-21 · Medium · 5 min read · Model training, LLMs, Python, Inference & serving
  Keras (via keras\_hub) loads Llama 3.2 directly from standard safetensors Hugging Face checkpoints with on-the-fly conversion, so it has worked since day one.
- [Fine-Tuning Gemma Models in Hugging Face](https://swe.quest/articles/fine-tuning-gemma-models-in-hugging-face-d432d00965e71590.md): Hugging Face · 2024-02-23 · Medium · 5 min read · Model training, LLMs, Python
  Gemma 2B and 7B can be fine-tuned cheaply with LoRA via the Hugging Face PEFT library, loading the base model in 4-bit NF4 with bitsandbytes (QLoRA) so it fits on free Colab GPUs.
- [Training a language model with 🤗 Transformers using TensorFlow and TPUs](https://swe.quest/articles/training-a-language-model-with-transformers-using-tensorflow-and-tpus-7d72fd7dce30644b.md): Hugging Face · 2023-04-27 · Medium · 8 min read · Model training, Hardware, Python, LLMs
  Training a RoBERTa-base masked language model from scratch on WikiText with TensorFlow on TPUs, end to end: training a Unigram tokenizer with 🤗 tokenizers, tokenizing and chunking text into fixed 128-token sequences, sharding into TFRecords stored in Google Cloud Storage, and training under TPUStrategy with XLA-compatible Transformers models.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
