Gemma4-E4B-Function-Calling-xLAM
google/gemma-4-E4B-it fine-tuned for function calling on
Salesforce/xlam-function-calling-60k with QLoRA
(Transformers + PEFT + TRL SFTTrainer), then merged into a bf16 copy of the base model.
What this repository contains
The full multimodal checkpoint (Gemma4ForConditionalGeneration). LoRA adapted only the language model; the vision and audio encoders are the base model's weights, unchanged, and the base model's processor is included. Training and the usage below are text-only.
Prompt format
Raw tagged text, no chat template. The prompt is the query and tools blocks; the model writes the answers block
and then <eos>. Tools and answers are JSON. The prompt starts with <bos>, which this tokenizer does not add by itself.
<bos><query>...</query>
<tools>[...tool definitions as JSON...]</tools>
<answers>[...function calls as JSON...]</answers><eos>
Only the answers block and the EOS carried loss in training.
Usage
import json
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = 'ermiaazarkhalili/Gemma4-E4B-Function-Calling-xLAM'
tokenizer = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, dtype="auto", device_map="auto")
query = 'What information can be obtained about the Maine Coon cat breed?'
tools = json.loads('[{"name": "get_breed_information", "description": "Fetch information about a specific cat breed from the Cat Breeds API.", "parameters": {"breed": {"description": "The name of the cat breed to fetch information for.", "type": "str", "default": "aegean"}}}, {"name": "country_region_cities", "description": "Fetches a list of cities within a specified region of a given country from the GeoDB API.", "parameters": {"countryid": {"description": "An ISO-3166 country code or WikiData ID.", "type": "str", "default": "US"}, "regioncode": {"description": "An ISO-3166 or FIPS region code.", "type": "str", "default": "CA"}, "limit": {"description": "The maximum number of results to retrieve. Defaults to None.", "type": "int, optional", "default": ""}, "hateoasmode": {"description": "Include HATEOAS-style links in results. Defaults to None.", "type": "bool, optional", "default": ""}, "asciimode": {"description": "Display results using ASCII characters. Defaults to None.", "type": "bool, optional", "default": ""}, "nameprefixdefaultlangresults": {"description": "Match on names in the default language if a non-default language is requested when prefix-matching. Defaults to None.", "type": "bool, optional", "default": ""}, "timezoneids": {"description": "Only include cities in these time zones. Comma-separated values. Defaults to None.", "type": "str, optional", "default": ""}, "nameprefix": {"description": "Only include cities whose names start with this prefix. If languagecode is set, the prefix will be matched on the name as it appears in that language. Defaults to None.", "type": "str, optional", "default": ""}, "types": {"description": "Only include cities of these types (comma-separated): CITY, ADM2. Defaults to None.", "type": "str, optional", "default": ""}, "minpopulation": {"description": "Only include cities with at least this population. Defaults to None.", "type": "int, optional", "default": ""}, "languagecode": {"description": "Display results in this language. Defaults to None.", "type": "str, optional", "default": ""}, "offset": {"description": "The zero-based offset into the results. Defaults to None.", "type": "int, optional", "default": ""}, "maxpopulation": {"description": "Only include cities with no more than this population. Defaults to None.", "type": "int, optional", "default": ""}, "includedeleted": {"description": "Whether to include any cities marked deleted. Options are: ALL, SINCE_YESTERDAY, SINCE_LAST_WEEK, NONE. Defaults to None.", "type": "str, optional", "default": ""}, "sort": {"description": "How to sort the results. Format: ±SORT_FIELD,±SORT_FIELD where SORT_FIELD = elevation, name, population. Defaults to None.", "type": "str, optional", "default": ""}}}, {"name": "company_details", "description": "Fetch details of a company from Indeed\'s API.", "parameters": {"company_id": {"description": "The unique identifier of the company to fetch details for.", "type": "str", "default": "Microsoft"}, "locality": {"description": "The locality or country code for Indeed\'s subdomain. Default is \'us\' if not provided.", "type": "str, optional", "default": ""}}}]')
prompt = (
'<bos>'
+ '<query>' + query + '</query>' + '\n\n'
+ '<tools>' + json.dumps(tools, ensure_ascii=False) + '</tools>' + '\n\n'
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
out = model.generate(**inputs, max_new_tokens=995, do_sample=False, eos_token_id=tokenizer.eos_token_id)
text = tokenizer.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
calls = json.loads(text.split('<answers>', 1)[1].split('</answers>', 1)[0])
print(calls)
Training
Smoke test: False.
| Setting | Value |
|---|---|
model_name | google/gemma-4-E4B-it |
hub_model_id | ermiaazarkhalili/Gemma4-E4B-Function-Calling-xLAM |
max_length | 2048 |
load_in_4bit | True |
bnb_4bit_quant_type | nf4 |
bnb_4bit_compute_dtype | bfloat16 |
use_double_quant | True |
attn_implementation | sdpa |
r | 16 |
lora_alpha | 32 |
lora_dropout | 0.05 |
target_modules | all-linear |
bias | none |
task_type | CAUSAL_LM |
output_dir | ./gemma4-e4b-function-calling-xlam |
num_train_epochs | 1 |
per_device_train_batch_size | 4 |
per_device_eval_batch_size | 2 |
gradient_accumulation_steps | 2 |
learning_rate | 0.0001 |
lr_scheduler_type | cosine |
warmup_steps | 0.05 |
weight_decay | 0.01 |
bf16 | True |
logging_steps | 10 |
eval_strategy | steps |
eval_steps | 0.05 |
save_strategy | steps |
save_steps | 0.05 |
save_total_limit | 2 |
report_to | none |
push_to_hub | False |
seed | 42 |
dataset_name | Salesforce/xlam-function-calling-60k |
dataset_num_proc | 4 |
eval_fraction | 0.05 |
eval_max_samples | 1000 |
label_check_rows | 8 |
smoke_num_samples | 100 |
smoke_max_steps | 5 |
Results
| Setting | Value |
|---|---|
rows_loaded | 60000 |
train_rows | 56979 |
eval_rows | 1000 |
train_dropped_over_max_length | 21 |
eval_dropped_over_max_length | 0 |
global_step | 7123 |
train_loss | 0.024887873865815674 |
train_runtime_s | 17820.447 |
evaluations | 20 |
last_eval | {'eval_loss': 0.015246130526065826, 'eval_runtime': 66.1699, 'eval_samples_per_second': 15.113, 'eval_steps_per_second': 7.556, 'eval_entropy': 0.01657911641136161, 'eval_mean_token_accuracy': 0.9956827517747879, 'eval_num_tokens': 23235017.0, 'step': 7123} |
trainable_params | 38879232 |
lora_layers | {'model.language_model': 343} |
resumed_from | None |
These are training and held-out loss figures only. No function-calling accuracy benchmark was run.