Views
No views yet

Transformerspip install transformers1from transformers import AutoModelForCausalLM, AutoTokenizer
2checkpoint = "prithivMLmods/Reasoning-SmolLM2-135M"
3
4device = "cuda" # for GPU usage or "cpu" for CPU usage
5tokenizer = AutoTokenizer.from_pretrained(checkpoint)
6# for multiple GPUs install accelerate and do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
7model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
8
9messages = [{"role": "user", "content": "What is the capital of France."}]
10input_text=tokenizer.apply_chat_template(messages, tokenize=False)
11inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
12outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
13print(tokenizer.decode(outputs[0]))pip. These include transformers, datasets, trl, torch, accelerate, bitsandbytes, and wandb.!pip install transformers datasets trl torch accelerate bitsandbytes wandbAutoModelForCausalLM, AutoTokenizer, TrainingArguments, pipeline, load_dataset, and SFTTrainer.1from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, pipeline
2from datasets import load_dataset
3from trl import SFTConfig, SFTTrainer, setup_chat_format
4import torch
5import os1device = (
2 "cuda"
3 if torch.cuda.is_available()
4 else "mps" if torch.backends.mps.is_available() else "cpu"
5)AutoModelForCausalLM and AutoTokenizer to load the SmolLM model and tokenizer from Hugging Face.1model_name = "HuggingFaceTB/SmolLM2-360M"
2model = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=model_name)
3tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name)setup_chat_format function to prepare the model and tokenizer for chat-based tasks.model, tokenizer = setup_chat_format(model=model, tokenizer=tokenizer)1prompt = "Explain AGI ?"
2pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
3print(pipe(prompt, max_new_tokens=200))1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
3model_name = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
4model = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=model_name)
5tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name)
6
7tokenizer.chat_template = None
8
9from trl.models.utils import setup_chat_format
10model, tokenizer = setup_chat_format(model=model, tokenizer=tokenizer)
11
12prompt = "Explain AGI?"
13pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
14print(pipe(prompt, max_new_tokens=200))Deepthink-Reasoning.
load_dataset function to load the dataset from Hugging Face.ds = load_dataset("prithivMLmods/Deepthink-Reasoning")1def tokenize_function(examples):
2 prompts = [p.strip() for p in examples["prompt"]]
3 responses = [r.strip() for r in examples["response"]]
4 texts = [
5 tokenizer.apply_chat_template(
6 [{"role": "user", "content": p}, {"role": "assistant", "content": r}],
7 tokenize=False
8 )
9 for p, r in zip(prompts, responses)
10 ]
11 return tokenizer(texts, truncation=True, padding="max_length", max_length=512)ds = ds.map(tokenize_function, batched=True)TrainingArguments to specify parameters like batch size, learning rate, number of steps, and optimization settings.1use_bf16 = torch.cuda.is_bf16_supported()
2training_args = TrainingArguments(
3 per_device_train_batch_size=2,
4 gradient_accumulation_steps=4,
5 warmup_steps=5,
6 max_steps=60,
7 learning_rate=2e-4,
8 fp16=not use_bf16,
9 bf16=use_bf16,
10 logging_steps=1,
11 optim="adamw_8bit",
12 weight_decay=0.01,
13 lr_scheduler_type="linear",
14 seed=3407,
15 output_dir="outputs",
16 report_to="wandb",
17)SFTTrainer with the model, tokenizer, dataset, and training arguments.1trainer = SFTTrainer(
2 model=model,
3 processing_class=tokenizer,
4 train_dataset=ds["train"],
5 args=training_args,
6)train method on the trainer.trainer.train()save_pretrained method to save the model and tokenizer.1save_directory = "/content/my_model"
2model.save_pretrained(save_directory)
3tokenizer.save_pretrained(save_directory)1import shutil
2shutil.make_archive(save_directory, 'zip', save_directory)
3
4from google.colab import files
5files.download(f"{save_directory}.zip")| Item | Link |
|---|---|
| Model | SmolLM2-CoT-360M |
| Quantized Version | SmolLM2-CoT-360M-GGUF |
| Notebook | Link |
|---|---|
| SmolLM-FT-360M | SmolLM-FT-360M.ipynb |