PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py








PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py







PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py





PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py




PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py


PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py

PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py
PS C:\InfinityX\Automa\Test> & C:/Users/themy/AppData/Local/Programs/Python/Python312/python.exe c:/InfinityX/Automa/Test/test.py
Could not locate the tokenizer configuration file, will try to use the model config instead.
Model config LlamaConfig {
  "_name_or_path": "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8",
  "attention_bias": false,
  "attention_dropout": 0.0,
  "bos_token_id": 1,
  "eos_token_id": 2,
  "head_dim": 128,
  "hidden_act": "silu",
  "hidden_size": 4096,
  "initializer_range": 0.02,
  "intermediate_size": 11008,
  "max_position_embeddings": 2048,
  "mlp_bias": false,
  "model_type": "llama",
  "num_attention_heads": 32,
  "num_hidden_layers": 32,
  "num_key_value_heads": 32,
  "pretraining_tp": 1,
  "rms_norm_eps": 1e-06,
  "rope_scaling": null,
  "rope_theta": 10000.0,
  "tie_word_embeddings": false,
  "transformers_version": "4.46.2",
  "use_cache": true,
  "vocab_size": 32000
}

loading file tokenizer.model from cache at models/models--meta-llama--Llama-3.2-1B-Instruct-QLORA_INT4_EO8\snapshots\5a1360c72e95cb5e1a0a9c4a6b128605bf86a65c\tokenizer.model
loading file tokenizer.json from cache at None
loading file added_tokens.json from cache at None
loading file special_tokens_map.json from cache at None
loading file tokenizer_config.json from cache at None
You are using the default legacy behaviour of the <class 'transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thoroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565 - if you loaded a llama tokenizer from a GGUF file you can ignore this message.
Converting from Tiktoken
Traceback (most recent call last):
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1496, in extract_vocab_merges_from_model
    from tiktoken.load import load_tiktoken_bpe
ModuleNotFoundError: No module named 'tiktoken'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1636, in convert_slow_tokenizer
    ).converted()
      ^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1533, in converted
    tokenizer = self.tokenizer()
                ^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1526, in tokenizer
    vocab_scores, merges = self.extract_vocab_merges_from_model(self.vocab_file)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1498, in extract_vocab_merges_from_model
    raise ValueError(
ValueError: `tiktoken` is required to read a `tiktoken` file. Install it with `pip install tiktoken`.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\InfinityX\Automa\Test\test.py", line 201, in <module>
    main()
  File "c:\InfinityX\Automa\Test\test.py", line 91, in main
    tokenizer = AutoTokenizer.from_pretrained(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\models\auto\tokenization_auto.py", line 939, in from_pretrained
    return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\tokenization_utils_base.py", line 2213, in from_pretrained
    return cls._from_pretrained(
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\tokenization_utils_base.py", line 2447, in _from_pretrained
    tokenizer = cls(*init_inputs, **init_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\models\llama\tokenization_llama_fast.py", line 157, in __init__
    super().__init__(
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\tokenization_utils_fast.py", line 138, in __init__
    fast_tokenizer = convert_slow_tokenizer(self, from_tiktoken=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\themy\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\convert_slow_tokenizer.py", line 1638, in convert_slow_tokenizer
    raise ValueError(
ValueError: Converting from Tiktoken failed, if a converter for SentencePiece is available, provide a model path with a SentencePiece tokenizer.model file.Currently available slow->fast convertors: ['AlbertTokenizer', 'BartTokenizer', 'BarthezTokenizer', 'BertTokenizer', 'BigBirdTokenizer', 'BlenderbotTokenizer', 'CamembertTokenizer', 'CLIPTokenizer', 'CodeGenTokenizer', 'ConvBertTokenizer', 'DebertaTokenizer', 'DebertaV2Tokenizer', 'DistilBertTokenizer', 'DPRReaderTokenizer', 'DPRQuestionEncoderTokenizer', 'DPRContextEncoderTokenizer', 'ElectraTokenizer', 'FNetTokenizer', 'FunnelTokenizer', 'GPT2Tokenizer', 'HerbertTokenizer', 'LayoutLMTokenizer', 'LayoutLMv2Tokenizer', 'LayoutLMv3Tokenizer', 'LayoutXLMTokenizer', 'LongformerTokenizer', 'LEDTokenizer', 'LxmertTokenizer', 'MarkupLMTokenizer', 'MBartTokenizer', 'MBart50Tokenizer', 'MPNetTokenizer', 'MobileBertTokenizer', 'MvpTokenizer', 'NllbTokenizer', 'OpenAIGPTTokenizer', 'PegasusTokenizer', 'Qwen2Tokenizer', 'RealmTokenizer', 'ReformerTokenizer', 'RemBertTokenizer', 'RetriBertTokenizer', 'RobertaTokenizer', 'RoFormerTokenizer', 'SeamlessM4TTokenizer', 'SqueezeBertTokenizer', 'T5Tokenizer', 'UdopTokenizer', 'WhisperTokenizer', 'XLMRobertaTokenizer', 'XLNetTokenizer', 'SplinterTokenizer', 'XGLMTokenizer', 'LlamaTokenizer', 'CodeLlamaTokenizer', 'GemmaTokenizer', 'Phi3Tokenizer']
PS C:\InfinityX\Automa\Test> 
import torch
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification,
    AutoConfig,
    Trainer,
    TrainingArguments,
    DataCollatorWithPadding,
)
from torch.utils.data import Dataset
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from peft import LoraConfig, get_peft_model
import numpy as np
from transformers.utils import logging

# Set up logging
logging.set_verbosity_info()

# Define the dataset class
class SentimentDataset(Dataset):
    def __init__(self, texts, labels, tokenizer):
        self.encodings = tokenizer(
            texts,
            truncation=True,
            padding=True,
            max_length=512,
            return_tensors="pt",
        )
        self.labels = torch.tensor(labels)

    def __getitem__(self, idx):
        item = {key: val[idx] for key, val in self.encodings.items()}
        item['labels'] = self.labels[idx]
        return item

    def __len__(self):
        return len(self.labels)

# Define the metrics function
def compute_metrics(pred):
    labels = pred.label_ids
    preds = np.argmax(pred.predictions, axis=-1)
    accuracy = accuracy_score(labels, preds)
    precision, recall, f1, _ = precision_recall_fscore_support(
        labels, preds, average='weighted', zero_division=0
    )
    return {
        'accuracy': accuracy,
        'precision': precision,
        'recall': recall,
        'f1': f1
    }

# Define the Custom Trainer to handle device placement of labels
from transformers import Trainer

class CustomTrainer(Trainer):
    def compute_loss(self, model, inputs, return_outputs=False, **kwargs):
        labels = inputs.pop("labels")
        # Move inputs to the correct device
        inputs = {k: v.to(model.device) for k, v in inputs.items()}
        # Forward pass
        outputs = model(**inputs)
        logits = outputs.get("logits")
        # Move labels to the same device as logits
        labels = labels.to(logits.device)
        # Compute loss
        loss_fct = torch.nn.CrossEntropyLoss()
        loss = loss_fct(logits.view(-1, model.config.num_labels), labels.view(-1))
        return (loss, outputs) if return_outputs else loss

# Function to move LoRA modules to the correct device
def move_lora_to_device(model):
    for name, module in model.named_modules():
        if hasattr(module, 'lora_A') or hasattr(module, 'lora_B'):
            device = next(module.parameters()).device
            if hasattr(module, 'lora_A'):
                module.lora_A.to(device)
            if hasattr(module, 'lora_B'):
                module.lora_B.to(device)

def main():
    # Model and Tokenizer loading
    model_name = "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8"
    cache_dir = "models/"

    # Load the tokenizer
    tokenizer = AutoTokenizer.from_pretrained(
        model_name,
        cache_dir=cache_dir,
        padding_side="right",
    )
    tokenizer.pad_token = tokenizer.eos_token

    # Load the model configuration
    config = AutoConfig.from_pretrained(
        model_name,
        cache_dir=cache_dir,
    )
    config.pad_token_id = tokenizer.pad_token_id
    config.num_labels = 3

    # Check if GPUs are available
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if torch.cuda.is_available():
        n_gpus = torch.cuda.device_count()
        print(f"Number of GPUs available: {n_gpus}")
    else:
        n_gpus = 0
        print("No GPUs available.")

    # Load the model
    model = AutoModelForSequenceClassification.from_pretrained(
        model_name,
        config=config,
        cache_dir=cache_dir,
        local_files_only=True,
        device_map={"": device} if device.type == "cuda" else None,  # Load on the specified device
    )

    # LoRA configuration
    lora_config = LoraConfig(
        r=8,
        lora_alpha=16,
        target_modules=["q_proj", "v_proj"],
        lora_dropout=0.05,
        bias="none",
        task_type="SEQ_CLS",
    )

    # Apply LoRA to the model
    model = get_peft_model(model, lora_config)
    model.print_trainable_parameters()

    # Ensure LoRA modules are on the correct devices
    move_lora_to_device(model)

    # Data Loading and Preprocessing
    df = pd.read_csv("limited_reviews.csv")

    train_texts, val_texts, train_labels, val_labels = train_test_split(
        df['text'].astype(str).tolist(),
        df['sentiment'].tolist(),
        test_size=0.2,
        random_state=42
    )

    label_map = {'positive': 2, 'negative': 0, 'neutral': 1}
    train_labels = [label_map.get(label, -1) for label in train_labels]
    val_labels = [label_map.get(label, -1) for label in val_labels]

    train_dataset = SentimentDataset(train_texts, train_labels, tokenizer)
    val_dataset = SentimentDataset(val_texts, val_labels, tokenizer)

    # Data collator
    data_collator = DataCollatorWithPadding(tokenizer, padding='longest', return_tensors='pt')

    # Training arguments
    training_args = TrainingArguments(
        output_dir='./results',
        num_train_epochs=1,
        per_device_train_batch_size=2,
        per_device_eval_batch_size=2,
        gradient_accumulation_steps=16,
        eval_strategy="epoch",  # Updated from 'evaluation_strategy'
        save_strategy="epoch",
        learning_rate=5e-5,
        save_total_limit=2,
        load_best_model_at_end=True,
        logging_steps=50,
        dataloader_num_workers=0,  # Set to 0 to prevent multiple workers on Windows
        report_to="none",
    )

    # Initialize the Custom Trainer
    trainer = CustomTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=val_dataset,
        tokenizer=tokenizer,
        data_collator=data_collator,
        compute_metrics=compute_metrics,
    )

    # Train the model
    trainer.train()

    # Evaluate the model
    eval_results = trainer.evaluate()
    print(f"Evaluation results: {eval_results}")

    # Save the model and tokenizer
    trainer.save_model("fine_tuned_llama_sentiment_model")
    tokenizer.save_pretrained("fine_tuned_llama_sentiment_model")

if __name__ == "__main__":
    main()


fix and return full code.