Can you edit this code to use tiktoken instead of the transformers tokenizer instead?…
- User
Can you edit this code to use tiktoken instead of the transformers tokenizer instead? And if you see any problems in this code alse can you fix them?
Fine-tuning EleutherAI/pythia-14m on Quardo/wikipedia-turkish-qa dataset
import os import torch import json from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments from datasets import load_dataset, Dataset
Check device (GPU or CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}")
model_name = "EleutherAI/pythia-14m" print(f"Loading tokenizer and model '{model_name}'...") tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
Load the dataset
data_file = "Quardo/wikipedia-turkish-qa/tokenized_dataset.json"
if os.path.exists(data_file): print(f"Loading tokenized dataset from {data_file}...") with open(data_file, 'r', encoding='utf-8') as f: tokenized_data = json.load(f) tokenized_dataset = Dataset.from_dict(tokenized_data)
else: print("Loading raw dataset...") raw_dataset = load_dataset('json', data_files="Quardo/wikipedia-turkish-qa/train.jsonl", split="train")
code# Tokenization function def tokenize_function(examples): tokenizer.pad_token = tokenizer.eos_token return tokenizer( examples["text"], truncation=True, max_length=2048, padding="max_length" ) # Tokenize the dataset print("Tokenizing dataset...") tokenized_dataset = raw_dataset.map(tokenize_function, batched=True, remove_columns=["text"]) # Save the tokenized dataset to a file print(f"Saving tokenized dataset to {data_file}...") with open(data_file, 'w', encoding='utf-8') as f: json.dump(tokenized_dataset.to_dict(), f)Data collator
from transformers import DataCollatorForLanguageModeling
data_collator = DataCollatorForLanguageModeling( tokenizer=tokenizer, mlm=False )
Training arguments
training_args = TrainingArguments( output_dir="./output/", overwrite_output_dir=True, num_train_epochs=1, per_device_train_batch_size=1, # Small batch size due to limited VRAM gradient_accumulation_steps=8, # Accumulate gradients (effective batch size) save_steps=500, save_total_limit=2, logging_steps=100, learning_rate=5e-5, fp16=False, # Set to True if using a GPU that supports FP16 )
- Assistant