# !nvidia-smi # Install Pytorch & other libraries get_ipython().run_line_magic("pip", " install torch tensorboard") # Install Hugging Face libraries get_ipython().run_line_magic("pip", " install transformers datasets accelerate evaluate trl protobuf sentencepiece") # COMMENT IN: if you are running on a GPU that supports BF16 data type and flash attn, such as NVIDIA L4 or NVIDIA A100 #% pip install flash-attn from kaggle_secrets import UserSecretsClient user_secrets = UserSecretsClient() hf_token = user_secrets.get_secret("HF_TOKEN") from huggingface_hub import login # Login into Hugging Face Hub login(hf_token) # base_model = "HuggingFaceTB/SmolLM2-1.7B-Instruct" checkpoint_dir = "/kaggle/working" #@param {type:"string"} base_model = "google/gemma-3-270m-it" # @param ["google/gemma-3-270m-it","google/gemma-3-1b-it","google/gemma-3-4b-it","google/gemma-3-12b-it","google/gemma-3-27b-it"] {"allow-input":true} # checkpoint_dir = "/content/drive/MyDrive/MyGemmaNPC" #@param {type:"string"} learning_rate = 5e-5 #@param {type:"number"} from datasets import load_dataset def create_conversation(sample): return { "messages": [ {"role": "user", "content": sample["player"]}, {"role": "assistant", "content": sample["alien"]} ] } npc_type = "martian" #@param ["martian", "venusian"] # Load dataset from the Hub dataset = load_dataset("bebechien/MobileGameNPC", npc_type, split="train") # Convert dataset to conversational format dataset = dataset.map(create_conversation, remove_columns=dataset.features, batched=False) # Split dataset into 80% training samples and 20% test samples dataset = dataset.train_test_split(test_size=0.2, shuffle=False) # Print formatted user prompt print(dataset["train"][0]["messages"]) import torch from transformers import AutoTokenizer, AutoModelForCausalLM # Load model and tokenizer model = AutoModelForCausalLM.from_pretrained( base_model, torch_dtype="auto", device_map="auto", attn_implementation="eager" ) tokenizer = AutoTokenizer.from_pretrained(base_model) print(f"Device: {model.device}") print(f"DType: {model.dtype}") from transformers import pipeline from random import randint import re # Load the model and tokenizer into the pipeline pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) # Load a random sample from the test dataset rand_idx = randint(0, len(dataset["test"])-1) test_sample = dataset["test"][rand_idx] # Convert as test example into a prompt with the Gemma template prompt = pipe.tokenizer.apply_chat_template(test_sample["messages"][:1], tokenize=False, add_generation_prompt=True) outputs = pipe(prompt, max_new_tokens=256, disable_compile=True) # Extract the user query and original answer print(f"Question:\n{test_sample['messages'][0]['content']}\n") print(f"Original Answer:\n{test_sample['messages'][1]['content']}\n") print(f"Generated Answer (base model):\n{outputs[0]['generated_text'][len(prompt):].strip()}") outputs = pipe([{"role": "user", "content": "Sorry, you are a game NPC."}], max_new_tokens=256, disable_compile=True) print(outputs[0]['generated_text'][1]['content']) dataset['train'] message = [ # give persona {"role": "system", "content": "You are a Martian NPC with a unique speaking style. Use an accent that replaces 's' sounds with 'z', uses 'da' for 'the', 'diz' for 'this', and includes occasional clicks like *k'tak*."}, ] # few shot prompt for item in dataset['test']: message.append( {"role": "user", "content": item["messages"][0]["content"]} ) message.append( {"role": "assistant", "content": item["messages"][1]["content"]} ) # actual question message.append( {"role": "user", "content": "What is this place?"} ) outputs = pipe(message, max_new_tokens=256, disable_compile=True) print(outputs[0]['generated_text']) print("-"*80) print(outputs[0]['generated_text'][-1]['content']) from trl import SFTConfig torch_dtype = model.dtype args = SFTConfig( output_dir=checkpoint_dir, # directory to save and repository id max_length=512, # max sequence length for model and packing of the dataset packing=False, # Groups multiple samples in the dataset into a single sequence num_train_epochs=5, # number of training epochs per_device_train_batch_size=4, # batch size per device during training gradient_checkpointing=False, # Caching is incompatible with gradient checkpointing optim="adamw_torch_fused", # use fused adamw optimizer logging_steps=1, # log every step save_strategy="epoch", # save checkpoint every epoch eval_strategy="epoch", # evaluate checkpoint every epoch learning_rate=learning_rate, # learning rate fp16=True if torch_dtype == torch.float16 else False, # use float16 precision bf16=True if torch_dtype == torch.bfloat16 else False, # use bfloat16 precision lr_scheduler_type="constant", # use constant learning rate scheduler push_to_hub=True, # push model to hub report_to="tensorboard", # report metrics to tensorboard dataset_kwargs={ "add_special_tokens": False, # Template with special tokens "append_concat_token": True, # Add EOS token as separator token between examples }, loss_type="nll", # I added this for version compatibility. Kaggle probably has a differenet TRL version than colab and th ) from trl import SFTTrainer # Create Trainer object trainer = SFTTrainer( model=model, args=args, train_dataset=dataset['train'], eval_dataset=dataset['test'], processing_class=tokenizer, ) # Start training, the model will be automatically saved to the Hub and the output directory trainer.train() # Save the final model again to the Hugging Face Hub trainer.save_model()