Notebooks
G
Google Gemini
[FunctionGemma]Finetune FunctionGemma 270M For Mobile Actions With Hugging Face

[FunctionGemma]Finetune FunctionGemma 270M For Mobile Actions With Hugging Face

recurrentgemmagemmapaligemmaFunctionGemmagemma-cookbookcodegemma

Copyright 2025 Google LLC.

[ ]

Fine-tune FunctionGemma 270M for Mobile Actions

This notebook fine-tunes FunctionGemma for the task of taking user request to perform mobile actions through the Hugging Face Transformer Reinforcement Learning (TRL) library.

When training FunctionGemma 270M on a Google Colab A100 GPU accelerator, this process can take 60 minutes end-to-end. Run each code snippet to:

  1. Set up the Colab environment
  2. Prepare a dataset for fine-tuning
  3. Load and test the base FunctionGemma 270M model
  4. Fine-tune the model
  5. Test, evaluate, and save the model for further use
  6. Convert the checkpoint to .litertlm for deployment

Prerequisites

This colab needs the A100 GPU. You will need a Colab Pro subscription or Colab Pay to Go with credits.

Set up development environment

The first step is to install the necessary libraries using the pip package installer.

[1]
Requirement already satisfied: torch in /usr/local/lib/python3.12/dist-packages (2.9.0+cu126)
Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from torch) (3.20.0)
Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.12/dist-packages (from torch) (4.15.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from torch) (75.2.0)
Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch) (1.14.0)
Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch) (3.6.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch) (3.1.6)
Requirement already satisfied: fsspec>=0.8.5 in /usr/local/lib/python3.12/dist-packages (from torch) (2025.3.0)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.77)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.77)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.80)
Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch) (9.10.2.21)
Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.4.1)
Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch) (11.3.0.4)
Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch) (10.3.7.77)
Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch) (11.7.1.2)
Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch) (12.5.4.2)
Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch) (0.7.1)
Requirement already satisfied: nvidia-nccl-cu12==2.27.5 in /usr/local/lib/python3.12/dist-packages (from torch) (2.27.5)
Requirement already satisfied: nvidia-nvshmem-cu12==3.3.20 in /usr/local/lib/python3.12/dist-packages (from torch) (3.3.20)
Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.77)
Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch) (12.6.85)
Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch) (1.11.1.6)
Requirement already satisfied: triton==3.5.0 in /usr/local/lib/python3.12/dist-packages (from torch) (3.5.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch) (1.3.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch) (3.0.3)
Collecting transformers==4.57.1
  Downloading transformers-4.57.1-py3-none-any.whl.metadata (43 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.0/44.0 kB 1.8 MB/s eta 0:00:00
Collecting trl==0.25.1
  Downloading trl-0.25.1-py3-none-any.whl.metadata (11 kB)
Collecting datasets==4.4.1
  Downloading datasets-4.4.1-py3-none-any.whl.metadata (19 kB)
Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (3.20.0)
Requirement already satisfied: huggingface-hub<1.0,>=0.34.0 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (0.36.0)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (2.0.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (25.0)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (6.0.3)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (2025.11.3)
Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (2.32.4)
Requirement already satisfied: tokenizers<=0.23.0,>=0.22.0 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (0.22.1)
Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (0.7.0)
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.12/dist-packages (from transformers==4.57.1) (4.67.1)
Requirement already satisfied: accelerate>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from trl==0.25.1) (1.12.0)
Collecting pyarrow>=21.0.0 (from datasets==4.4.1)
  Downloading pyarrow-22.0.0-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (3.2 kB)
Requirement already satisfied: dill<0.4.1,>=0.3.0 in /usr/local/lib/python3.12/dist-packages (from datasets==4.4.1) (0.3.8)
Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from datasets==4.4.1) (2.2.2)
Requirement already satisfied: httpx<1.0.0 in /usr/local/lib/python3.12/dist-packages (from datasets==4.4.1) (0.28.1)
Requirement already satisfied: xxhash in /usr/local/lib/python3.12/dist-packages (from datasets==4.4.1) (3.6.0)
Requirement already satisfied: multiprocess<0.70.19 in /usr/local/lib/python3.12/dist-packages (from datasets==4.4.1) (0.70.16)
Requirement already satisfied: fsspec<=2025.10.0,>=2023.1.0 in /usr/local/lib/python3.12/dist-packages (from fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (2025.3.0)
Requirement already satisfied: psutil in /usr/local/lib/python3.12/dist-packages (from accelerate>=1.4.0->trl==0.25.1) (5.9.5)
Requirement already satisfied: torch>=2.0.0 in /usr/local/lib/python3.12/dist-packages (from accelerate>=1.4.0->trl==0.25.1) (2.9.0+cu126)
Requirement already satisfied: aiohttp!=4.0.0a0,!=4.0.0a1 in /usr/local/lib/python3.12/dist-packages (from fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (3.13.2)
Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx<1.0.0->datasets==4.4.1) (4.12.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx<1.0.0->datasets==4.4.1) (2025.11.12)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1.0.0->datasets==4.4.1) (1.0.9)
Requirement already satisfied: idna in /usr/local/lib/python3.12/dist-packages (from httpx<1.0.0->datasets==4.4.1) (3.11)
Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1.0.0->datasets==4.4.1) (0.16.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.34.0->transformers==4.57.1) (4.15.0)
Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.34.0->transformers==4.57.1) (1.2.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->transformers==4.57.1) (3.4.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests->transformers==4.57.1) (2.5.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets==4.4.1) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets==4.4.1) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas->datasets==4.4.1) (2025.3)
Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (2.6.1)
Requirement already satisfied: aiosignal>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (1.4.0)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (25.4.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (1.8.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (6.7.0)
Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (0.4.1)
Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.12/dist-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2025.10.0,>=2023.1.0->datasets==4.4.1) (1.22.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas->datasets==4.4.1) (1.17.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (75.2.0)
Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (1.14.0)
Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (3.6.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (3.1.6)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.77)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.77)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.80)
Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (9.10.2.21)
Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.4.1)
Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (11.3.0.4)
Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (10.3.7.77)
Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (11.7.1.2)
Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.5.4.2)
Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (0.7.1)
Requirement already satisfied: nvidia-nccl-cu12==2.27.5 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (2.27.5)
Requirement already satisfied: nvidia-nvshmem-cu12==3.3.20 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (3.3.20)
Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.77)
Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (12.6.85)
Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (1.11.1.6)
Requirement already satisfied: triton==3.5.0 in /usr/local/lib/python3.12/dist-packages (from torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (3.5.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (1.3.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch>=2.0.0->accelerate>=1.4.0->trl==0.25.1) (3.0.3)
Downloading transformers-4.57.1-py3-none-any.whl (12.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.0/12.0 MB 127.7 MB/s eta 0:00:00
Downloading trl-0.25.1-py3-none-any.whl (465 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 465.5/465.5 kB 34.5 MB/s eta 0:00:00
Downloading datasets-4.4.1-py3-none-any.whl (511 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 511.6/511.6 kB 49.0 MB/s eta 0:00:00
Downloading pyarrow-22.0.0-cp312-cp312-manylinux_2_28_x86_64.whl (47.7 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.7/47.7 MB 49.7 MB/s eta 0:00:00
Installing collected packages: pyarrow, transformers, datasets, trl
  Attempting uninstall: pyarrow
    Found existing installation: pyarrow 18.1.0
    Uninstalling pyarrow-18.1.0:
      Successfully uninstalled pyarrow-18.1.0
  Attempting uninstall: transformers
    Found existing installation: transformers 4.57.3
    Uninstalling transformers-4.57.3:
      Successfully uninstalled transformers-4.57.3
  Attempting uninstall: datasets
    Found existing installation: datasets 4.0.0
    Uninstalling datasets-4.0.0:
      Successfully uninstalled datasets-4.0.0
Successfully installed datasets-4.4.1 pyarrow-22.0.0 transformers-4.57.1 trl-0.25.1

You may have to restart your session (runtime) to use newly installed libraries.

##Enable Hugging Face permissions

To use Gemma models, you'll need to accept the model usage license and create an Access Token:

  1. Accept license on the model page.

  2. Get a valid Access Token with 'Write' access (very important!)

  3. Create a new Colab secret in the left toolbar. Specify HF_TOKEN as the 'Name', add your unique token as the 'Value', and toggle 'Notebook access' on.

[ ]

Load the model

You can access FunctionGemma 270M from Hugging Face Hub by accepting the license terms. The instruction-tuned version of the model has already been trained on how to follow directions and with fine-tuning, you'll now adapt it to a new task.

[2]
config.json:   0%|          | 0.00/1.37k [00:00<?, ?B/s]
model.safetensors:   0%|          | 0.00/536M [00:00<?, ?B/s]
generation_config.json:   0%|          | 0.00/176 [00:00<?, ?B/s]
tokenizer_config.json:   0%|          | 0.00/1.16M [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.69M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/33.4M [00:00<?, ?B/s]
added_tokens.json:   0%|          | 0.00/63.0 [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/706 [00:00<?, ?B/s]
chat_template.jinja:   0%|          | 0.00/13.8k [00:00<?, ?B/s]
Device: cuda:0
DType:  torch.bfloat16

Device should print as cuda if you're using a GPU runtime.

Load the dataset

To fine-tune FunctionGemma, we utilize the Mobile Actions dataset, which is publicly available on Hugging Face. Each entry in this dataset provides:

  • The set of tools (functions) the model can use:
    1. Turn the flashlight on
    2. Turn the flashlight off
    3. Create a contact in the phone's contact list
    4. Send an email
    5. Show a location on the map
    6. Open the WiFi settings
    7. Create a new calendar event
  • The system prompt providing the context like current date and time
  • The user prompt, like turn on the flashlight.
  • The expected model response, including the appropriate function calls.
[7]

Here's an example from your dataset: 
{
  "metadata": "eval",
  "tools": [
    {
      "function": {
        "name": "send_email",
        "description": "Sends an email.",
        "parameters": {
          "type": "OBJECT",
          "properties": {
            "body": {
              "type": "STRING",
              "description": "The body of the email."
            },
            "to": {
              "type": "STRING",
              "description": "The email address of the recipient."
            },
            "subject": {
              "type": "STRING",
              "description": "The subject of the email."
            }
          },
          "required": [
            "to",
            "subject"
          ]
        }
      }
    },
    {
      "function": {
        "name": "turn_on_flashlight",
        "description": "Turns the flashlight on.",
        "parameters": {
          "type": "OBJECT",
          "properties": {}
        }
      }
    },
    {
      "function": {
        "name": "open_wifi_settings",
        "description": "Opens the Wi-Fi settings.",
        "parameters": {
          "type": "OBJECT",
          "properties": {}
        }
      }
    },
    {
      "function": {
        "name": "create_calendar_event",
        "description": "Creates a new calendar event.",
        "parameters": {
          "type": "OBJECT",
          "properties": {
            "title": {
              "type": "STRING",
              "description": "The title of the event."
            },
            "datetime": {
              "type": "STRING",
              "description": "The date and time of the event in the format YYYY-MM-DDTHH:MM:SS."
            }
          },
          "required": [
            "title",
            "datetime"
          ]
        }
      }
    },
    {
      "function": {
        "name": "show_map",
        "description": "Shows a location on the map.",
        "parameters": {
          "type": "OBJECT",
          "properties": {
            "query": {
              "type": "STRING",
              "description": "The location to search for. May be the name of a place, a business, or an address."
            }
          },
          "required": [
            "query"
          ]
        }
      }
    },
    {
      "function": {
        "name": "create_contact",
        "description": "Creates a contact in the phone's contact list.",
        "parameters": {
          "type": "OBJECT",
          "properties": {
            "email": {
              "type": "STRING",
              "description": "The email address of the contact."
            },
            "last_name": {
              "type": "STRING",
              "description": "The last name of the contact."
            },
            "phone_number": {
              "type": "STRING",
              "description": "The phone number of the contact."
            },
            "first_name": {
              "type": "STRING",
              "description": "The first name of the contact."
            }
          },
          "required": [
            "first_name",
            "last_name"
          ]
        }
      }
    },
    {
      "function": {
        "name": "turn_off_flashlight",
        "description": "Turns the flashlight off.",
        "parameters": {
          "type": "OBJECT",
          "properties": {}
        }
      }
    }
  ],
  "messages": [
    {
      "role": "developer",
      "content": "Current date and time given in YYYY-MM-DDTHH:MM:SS format: 2024-10-20T14:27:48\nDay of week is Sunday\nYou are a model that can do function calling with the following functions\n"
    },
    {
      "role": "user",
      "content": "Please create a calendar event titled \"Q4 Planning Meeting\" for October 24, 2024, at 2:00 PM."
    },
    {
      "role": "assistant",
      "tool_calls": [
        {
          "function": {
            "name": "create_calendar_event",
            "arguments": {
              "title": "Q4 Planning Meeting",
              "datetime": "2024-10-24T14:00:00"
            }
          }
        }
      ]
    }
  ]
}

Process the dataset for training and evaluation

Now that you've loaded your data, format the training dataset into Prompt-completion format for more efficient training later (completion_only_loss=True). This means the model will only learn from the completion instead of the prompt.

  • prompt for the non-trainable parts
  • completion for the trainable parts
[8]
Map:   0%|          | 0/9654 [00:00<?, ? examples/s]
[9]
Here's an example from the formatted dataset:
{
  "text": "{\"metadata\": \"train\", \"tools\": [{\"function\": {\"name\": \"send_email\", \"description\": \"Sends an email.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"subject\": {\"type\": \"STRING\", \"description\": \"The subject of the email.\"}, \"body\": {\"type\": \"STRING\", \"description\": \"The body of the email.\"}, \"to\": {\"type\": \"STRING\", \"description\": \"The email address of the recipient.\"}}, \"required\": [\"to\", \"subject\"]}}}, {\"function\": {\"name\": \"show_map\", \"description\": \"Shows a location on the map.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"query\": {\"type\": \"STRING\", \"description\": \"The location to search for. May be the name of a place, a business, or an address.\"}}, \"required\": [\"query\"]}}}, {\"function\": {\"name\": \"turn_off_flashlight\", \"description\": \"Turns the flashlight off.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}, {\"function\": {\"name\": \"open_wifi_settings\", \"description\": \"Opens the Wi-Fi settings.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}, {\"function\": {\"name\": \"create_calendar_event\", \"description\": \"Creates a new calendar event.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"title\": {\"type\": \"STRING\", \"description\": \"The title of the event.\"}, \"datetime\": {\"type\": \"STRING\", \"description\": \"The date and time of the event in the format YYYY-MM-DDTHH:MM:SS.\"}}, \"required\": [\"title\", \"datetime\"]}}}, {\"function\": {\"name\": \"create_contact\", \"description\": \"Creates a contact in the phone's contact list.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"last_name\": {\"type\": \"STRING\", \"description\": \"The last name of the contact.\"}, \"phone_number\": {\"type\": \"STRING\", \"description\": \"The phone number of the contact.\"}, \"email\": {\"type\": \"STRING\", \"description\": \"The email address of the contact.\"}, \"first_name\": {\"type\": \"STRING\", \"description\": \"The first name of the contact.\"}}, \"required\": [\"first_name\", \"last_name\"]}}}, {\"function\": {\"name\": \"turn_on_flashlight\", \"description\": \"Turns the flashlight on.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}], \"messages\": [{\"role\": \"developer\", \"content\": \"Current date and time given in YYYY-MM-DDTHH:MM:SS format: 2026-01-16T00:50:30\\nDay of week is Friday\\nYou are a model that can do function calling with the following functions\\n\"}, {\"role\": \"user\", \"content\": \"Please schedule a calendar event titled 'Meeting with Mr. Dubois' for January 20th, 2026 at 3:00 PM.\"}, {\"role\": \"assistant\", \"tool_calls\": [{\"function\": {\"name\": \"create_calendar_event\", \"arguments\": {\"title\": \"Meeting with Mr. Dubois\", \"datetime\": \"2026-01-20T15:00:00\"}}}]}]}",
  "prompt": "<bos><start_of_turn>developer\nCurrent date and time given in YYYY-MM-DDTHH:MM:SS format: 2026-01-16T00:50:30\nDay of week is Friday\nYou are a model that can do function calling with the following functions<start_function_declaration>declaration:send_email{description:<escape>Sends an email.<escape>,parameters:{properties:{body:{description:<escape>The body of the email.<escape>,type:<escape>STRING<escape>},subject:{description:<escape>The subject of the email.<escape>,type:<escape>STRING<escape>},to:{description:<escape>The email address of the recipient.<escape>,type:<escape>STRING<escape>}},required:[<escape>to<escape>,<escape>subject<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:show_map{description:<escape>Shows a location on the map.<escape>,parameters:{properties:{query:{description:<escape>The location to search for. May be the name of a place, a business, or an address.<escape>,type:<escape>STRING<escape>}},required:[<escape>query<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:turn_off_flashlight{description:<escape>Turns the flashlight off.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:open_wifi_settings{description:<escape>Opens the Wi-Fi settings.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_calendar_event{description:<escape>Creates a new calendar event.<escape>,parameters:{properties:{datetime:{description:<escape>The date and time of the event in the format YYYY-MM-DDTHH:MM:SS.<escape>,type:<escape>STRING<escape>},title:{description:<escape>The title of the event.<escape>,type:<escape>STRING<escape>}},required:[<escape>title<escape>,<escape>datetime<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_contact{description:<escape>Creates a contact in the phone's contact list.<escape>,parameters:{properties:{email:{description:<escape>The email address of the contact.<escape>,type:<escape>STRING<escape>},first_name:{description:<escape>The first name of the contact.<escape>,type:<escape>STRING<escape>},last_name:{description:<escape>The last name of the contact.<escape>,type:<escape>STRING<escape>},phone_number:{description:<escape>The phone number of the contact.<escape>,type:<escape>STRING<escape>}},required:[<escape>first_name<escape>,<escape>last_name<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:turn_on_flashlight{description:<escape>Turns the flashlight on.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nPlease schedule a calendar event titled 'Meeting with Mr. Dubois' for January 20th, 2026 at 3:00 PM.<end_of_turn>\n<start_of_turn>model\n",
  "completion": "<start_function_call>call:create_calendar_event{datetime:<escape>2026-01-20T15:00:00<escape>,title:<escape>Meeting with Mr. Dubois<escape>}<end_function_call><start_function_response>",
  "split": "train"
}

The longest example length is 4157 with 897 tokens. We need to set the max_length larger than the token count in SFTConfig below.
{
  "text": "{\"metadata\": \"train\", \"tools\": [{\"function\": {\"name\": \"open_wifi_settings\", \"description\": \"Opens the Wi-Fi settings.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}, {\"function\": {\"name\": \"turn_off_flashlight\", \"description\": \"Turns the flashlight off.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}, {\"function\": {\"name\": \"create_calendar_event\", \"description\": \"Creates a new calendar event.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"datetime\": {\"type\": \"STRING\", \"description\": \"The date and time of the event in the format YYYY-MM-DDTHH:MM:SS.\"}, \"title\": {\"type\": \"STRING\", \"description\": \"The title of the event.\"}}, \"required\": [\"title\", \"datetime\"]}}}, {\"function\": {\"name\": \"create_contact\", \"description\": \"Creates a contact in the phone's contact list.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"last_name\": {\"type\": \"STRING\", \"description\": \"The last name of the contact.\"}, \"email\": {\"type\": \"STRING\", \"description\": \"The email address of the contact.\"}, \"first_name\": {\"type\": \"STRING\", \"description\": \"The first name of the contact.\"}, \"phone_number\": {\"type\": \"STRING\", \"description\": \"The phone number of the contact.\"}}, \"required\": [\"first_name\", \"last_name\"]}}}, {\"function\": {\"name\": \"turn_on_flashlight\", \"description\": \"Turns the flashlight on.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {}}}}, {\"function\": {\"name\": \"show_map\", \"description\": \"Shows a location on the map.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"query\": {\"type\": \"STRING\", \"description\": \"The location to search for. May be the name of a place, a business, or an address.\"}}, \"required\": [\"query\"]}}}, {\"function\": {\"name\": \"send_email\", \"description\": \"Sends an email.\", \"parameters\": {\"type\": \"OBJECT\", \"properties\": {\"to\": {\"type\": \"STRING\", \"description\": \"The email address of the recipient.\"}, \"body\": {\"type\": \"STRING\", \"description\": \"The body of the email.\"}, \"subject\": {\"type\": \"STRING\", \"description\": \"The subject of the email.\"}}, \"required\": [\"to\", \"subject\"]}}}], \"messages\": [{\"role\": \"developer\", \"content\": \"Current date and time given in YYYY-MM-DDTHH:MM:SS format: 2025-11-24T12:55:01\\nDay of week is Monday\\nYou are a model that can do function calling with the following functions\\n\"}, {\"role\": \"user\", \"content\": \"I need to create a calendar event titled \\\"Q4 Review Strategy Session\\\" for 2025-11-26T15:00:00, and simultaneously send an email to elena.vasiliev@corpmail.com with the subject \\\"Follow-up on Q4 Review Prep\\\" and the body \\\"Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.\\\"\\n\\nPlease schedule a calendar event titled \\\"Q4 Review Strategy Session\\\" for 2025-11-26 at 3:00 PM, and immediately send an email to elena.vasiliev@corpmail.com with the subject \\\"Follow-up on Q4 Review Prep\\\" and the body \\\"Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.\\\"\"}, {\"role\": \"assistant\", \"tool_calls\": [{\"function\": {\"name\": \"create_calendar_event\", \"arguments\": {\"title\": \"Q4 Review Strategy Session\", \"datetime\": \"2025-11-26T15:00:00\"}}}, {\"function\": {\"name\": \"send_email\", \"arguments\": {\"body\": \"Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.\", \"subject\": \"Follow-up on Q4 Review Prep\", \"to\": \"elena.vasiliev@corpmail.com\"}}}]}]}",
  "prompt": "<bos><start_of_turn>developer\nCurrent date and time given in YYYY-MM-DDTHH:MM:SS format: 2025-11-24T12:55:01\nDay of week is Monday\nYou are a model that can do function calling with the following functions<start_function_declaration>declaration:open_wifi_settings{description:<escape>Opens the Wi-Fi settings.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:turn_off_flashlight{description:<escape>Turns the flashlight off.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_calendar_event{description:<escape>Creates a new calendar event.<escape>,parameters:{properties:{datetime:{description:<escape>The date and time of the event in the format YYYY-MM-DDTHH:MM:SS.<escape>,type:<escape>STRING<escape>},title:{description:<escape>The title of the event.<escape>,type:<escape>STRING<escape>}},required:[<escape>title<escape>,<escape>datetime<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_contact{description:<escape>Creates a contact in the phone's contact list.<escape>,parameters:{properties:{email:{description:<escape>The email address of the contact.<escape>,type:<escape>STRING<escape>},first_name:{description:<escape>The first name of the contact.<escape>,type:<escape>STRING<escape>},last_name:{description:<escape>The last name of the contact.<escape>,type:<escape>STRING<escape>},phone_number:{description:<escape>The phone number of the contact.<escape>,type:<escape>STRING<escape>}},required:[<escape>first_name<escape>,<escape>last_name<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:turn_on_flashlight{description:<escape>Turns the flashlight on.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:show_map{description:<escape>Shows a location on the map.<escape>,parameters:{properties:{query:{description:<escape>The location to search for. May be the name of a place, a business, or an address.<escape>,type:<escape>STRING<escape>}},required:[<escape>query<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:send_email{description:<escape>Sends an email.<escape>,parameters:{properties:{body:{description:<escape>The body of the email.<escape>,type:<escape>STRING<escape>},subject:{description:<escape>The subject of the email.<escape>,type:<escape>STRING<escape>},to:{description:<escape>The email address of the recipient.<escape>,type:<escape>STRING<escape>}},required:[<escape>to<escape>,<escape>subject<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nI need to create a calendar event titled \"Q4 Review Strategy Session\" for 2025-11-26T15:00:00, and simultaneously send an email to elena.vasiliev@corpmail.com with the subject \"Follow-up on Q4 Review Prep\" and the body \"Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.\"\n\nPlease schedule a calendar event titled \"Q4 Review Strategy Session\" for 2025-11-26 at 3:00 PM, and immediately send an email to elena.vasiliev@corpmail.com with the subject \"Follow-up on Q4 Review Prep\" and the body \"Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.\"<end_of_turn>\n<start_of_turn>model\n",
  "completion": "<start_function_call>call:create_calendar_event{datetime:<escape>2025-11-26T15:00:00<escape>,title:<escape>Q4 Review Strategy Session<escape>}<end_function_call><start_function_call>call:send_email{body:<escape>Hi Elena, I've scheduled our follow-up meeting to discuss the Q4 review strategy. Please review the attached calendar invite. Let me know if you have any conflicts. Thanks.<escape>,subject:<escape>Follow-up on Q4 Review Prep<escape>,to:<escape>elena.vasiliev@corpmail.com<escape>}<end_function_call><start_function_response>",
  "split": "train"
}

Using max_token_count of 997 (= 897 + 100) for training below.
[10]
Filter:   0%|          | 0/9654 [00:00<?, ? examples/s]
Filter:   0%|          | 0/9654 [00:00<?, ? examples/s]

Recommended: Test the base model

Now, we have loaded both the base model and the dataset. Let's first check how the base model's ability to respond to
a random sample.

Try testing it a few times.

[ ]
user_prompt
Device set to use cuda:0

Prompt: Schedule a "team meeting" tomorrow at 4pm.

Base model output: I apologize, but I cannot assist with scheduling meetings. My current capabilities are limited to managing calendar events and contact management tools. I cannot generate or update meeting schedules.

Note that how the base model is unable to successfully call the create_calendar_event function for this prompt.

Now, we will pick a sample from the training dataset and see how it performs.

Test with training dataset

[ ]
Device set to use cuda:0

Input prompt   : <bos><start_of_turn>developer
Current date and time given in YYYY-MM-DDTHH:MM:SS format: 2026-09-16T18:59:36
Day of week is Wednesday
You are a model that can do function calling with the following functions<start_function_declaration>declaration:turn_on_flashlight{description:<escape>Turns the flashlight on.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_contact{description:<escape>Creates a contact in the phone's contact list.<escape>,parameters:{properties:{email:{description:<escape>The email address of the contact.<escape>,type:<escape>STRING<escape>},first_name:{description:<escape>The first name of the contact.<escape>,type:<escape>STRING<escape>},last_name:{description:<escape>The last name of the contact.<escape>,type:<escape>STRING<escape>},phone_number:{description:<escape>The phone number of the contact.<escape>,type:<escape>STRING<escape>}},required:[<escape>first_name<escape>,<escape>last_name<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:send_email{description:<escape>Sends an email.<escape>,parameters:{properties:{body:{description:<escape>The body of the email.<escape>,type:<escape>STRING<escape>},subject:{description:<escape>The subject of the email.<escape>,type:<escape>STRING<escape>},to:{description:<escape>The email address of the recipient.<escape>,type:<escape>STRING<escape>}},required:[<escape>to<escape>,<escape>subject<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:create_calendar_event{description:<escape>Creates a new calendar event.<escape>,parameters:{properties:{datetime:{description:<escape>The date and time of the event in the format YYYY-MM-DDTHH:MM:SS.<escape>,type:<escape>STRING<escape>},title:{description:<escape>The title of the event.<escape>,type:<escape>STRING<escape>}},required:[<escape>title<escape>,<escape>datetime<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:show_map{description:<escape>Shows a location on the map.<escape>,parameters:{properties:{query:{description:<escape>The location to search for. May be the name of a place, a business, or an address.<escape>,type:<escape>STRING<escape>}},required:[<escape>query<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:open_wifi_settings{description:<escape>Opens the Wi-Fi settings.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:turn_off_flashlight{description:<escape>Turns the flashlight off.<escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>
<start_of_turn>user
Please save a new contact named Kenji Tanaka with the phone number 090-1234-5678 and email address kenji.tanaka@corpmail.jp. At the same time, schedule a calendar event titled 'Q3 Review Prep' for the following Friday at 2:00 PM.<end_of_turn>
<start_of_turn>model


Expected output: <start_function_call>call:create_contact{email:<escape>kenji.tanaka@corpmail.jp<escape>,first_name:<escape>Kenji<escape>,last_name:<escape>Tanaka<escape>,phone_number:<escape>090-1234-5678<escape>}<end_function_call><start_function_call>call:create_calendar_event{datetime:<escape>2026-09-18T14:00:00<escape>,title:<escape>Q3 Review Prep<escape>}<end_function_call><start_function_response>

Actual output  : <start_function_call>call:create_contact{email:<escape>kenji.tanaka@corpmail.jp<escape>,first_name:<escape>Kenji<escape>,last_name:<escape>Tanaka<escape>,phone_number:<escape>090-1234-5678<escape>}<end_function_call><start_function_call>call:send_email{to:<escape>kenji.tanaka@corpmail.jp<escape>,subject:<escape>Q3 Review Prep<escape>,message:<escape>Save the new contact Kenji Tanaka with the phone number 090-1234-5678.</h1><end_of_turn>

The base model output may not meet your expectations—and that's okay!

FunctionGemma 270M was designed for task specialization, which means it can improve performance for specific tasks when trained with representative examples. Let's fine-tune the model for more reliable outputs.

Fine-tune the model

Hugging Face TRL provides tools for training and fine-tuning LLMs.

Configure the tuning job

Define the training configuration for the FunctionGemma base model.

[ ]
Training configured

Start training

SFTTrainer tokenizes the datasets and trains the base model using the hyperparameters from the previous step.

The training time varies based on a range of factors, such as the size of your dataset or number of epochs. Using a A100 GPU, this takes about 8 minutes for 1 epoch.

[ ]
Adding EOS to train dataset:   0%|          | 0/8693 [00:00<?, ? examples/s]
Tokenizing train dataset:   0%|          | 0/8693 [00:00<?, ? examples/s]
Truncating train dataset:   0%|          | 0/8693 [00:00<?, ? examples/s]
Adding EOS to eval dataset:   0%|          | 0/961 [00:00<?, ? examples/s]
Tokenizing eval dataset:   0%|          | 0/961 [00:00<?, ? examples/s]
Truncating eval dataset:   0%|          | 0/961 [00:00<?, ? examples/s]
The model is already on multiple devices. Skipping the move to device specified in `args`.
The tokenizer has new PAD/BOS/EOS tokens that differ from the model config and generation config. The model config and generation config were aligned accordingly, being updated with the tokenizer's values. Updated tokens: {'bos_token_id': 2, 'pad_token_id': 0}.
Fine-tuned model saved to /content/mobile-actions-functiongemma

The weights for each training checkpoint (epoch) will be saved in your temporary Colab session storage. Now, you can evaluate the training and validation loss metrics to choose which checkpoint to for the model.

Plot training results

To evaluate the model, you can plot the training and validation losses using Matplotlib to visualize these metrics over training steps or epochs. This helps monitor the training process and make informed decisions about which hyperparameters to adjust.

[ ]

Test the fine-tuned model

Let's compare your fine-tuned model performance against the base model! Test a few inputs by updating user_prompt.

[ ]
user_prompt

Evaluate the fine-tuned model

Evaluating a fine-tuned model is essential to ensure that the process actually improved the model's performance without introducing new issues.

[ ]
[ ]
[ ]
[ ]

Review what are not matching

The score about tell how much eval data get exactly correct function calls. It gives the lower bound of correctness. Some output might not match the eval date but still acceptable. For example, the show_map function call below:

  • show_map:{'query': 'Maison Marulaz, Besançon, France'}
  • show_map:{'query': 'Maison Marulaz in Besançon, France'}

Let's take a look at the eval date without exact match. Many of them could also be acceptable.

[ ]

Does the model call the function you'd expect?

If you're not getting the results you want, you can try using different hyperparameters to train the model, or updating your training dataset to contain more representative examples.

Once you're happy with the results, you can save your model to Hugging Face Hub.

Save your model and upload to Hugging Face Hub

You now have a customized FunctionGemma 270M model! 🎉

Upload it to a repository on Hugging Face Hub so you easily share your model or access it later.

[ ]
model_name

Conversion to .litertlm for on-device deployment

The first step is to install the necessary libraries using the pip package installer.

[ ]

Build the .litertlm from the fine-tuned model

After running the script below, you can find the converted model in located in /content/litertlm/mobile-actions_q8_ekv1024.litertlm in the colab environment. Copy it to a persistent storage like Google Drive.

[ ]

Save the .litertlm on Google Drive

To deploy the converted model on Google AI Edge Gallery, we can first store the model on Google Drive. On the Gallery app, import the model from Google Drive later.

[ ]
[ ]

Summary and next steps

Congratulations! You have completed the first part of Fine-tune FunctionGemma 270M for Mobile Actions.

You have successfully finetuned the FunctionGemma 270M with the Mobile Actions dataset and converted it to .litertlm format.