Skip to content

[Bug] NameError: name 'psutil' is not defined in UnslothSFTTrainer._prepare_dataset (Windows environment) #3777

Closed

Description

@midorin-Linux

Environment

  • OS: Windows 11 (Confirmed in Ubuntu 24.04.3 LTS on WSL2)
  • GPU: Single RTX 5060 Ti 16GB
  • CUDA: 13.0
  • Python: 3.13.9
  • torch: 2.9.1+cu130
  • unsloth: git commit 06daf28c8b79782375bb7e17a830b11266407bc9 (latest at time of testing)
  • transformers: 4.57.3
  • trl: 0.24.0
  • bitsandbytes: 0.49.0
  • psutil: 7.2.0

Minimal Code

!pip install "unsloth[cu130-torch291] @ git+https://github.com/unslothai/unsloth.git"
!pip uninstall torch torchvision
!pip install torch torchvision --index-url https://download.pytorch.org/whl/cu130


from unsloth import FastLanguageModel
from trl import SFTTrainer
from transformers import TrainingArguments
from datasets import load_dataset
import torch

max_seq_length = 2048
dtype = None
load_in_4bit = True

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="unsloth/gpt-oss-20b",
    max_seq_length=max_seq_length,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
)

model = FastLanguageModel.get_peft_model(
    model,
    r=16,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
    lora_alpha=32,
    lora_dropout=0.05,
    bias="none",
    use_gradient_checkpointing="unsloth",
    random_state=3407,
    use_rslora=True,
)

dataset = load_dataset("AlicanKiraz0/Agentic-Chain-of-Thought-Coding-SFT-Dataset", split="train")

def formatting_prompts_func(examples):
    output_texts = []
    for i in range(len(examples['user'])):
        text = (
            f"### System:\n{examples['system'][i]}\n\n"
            f"### User:\n{examples['user'][i]}\n\n"
            f"### Assistant:\n{examples['assistant'][i]}{tokenizer.eos_token}"
        )
        output_texts.append(text)
    return output_texts

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=dataset,
    formatting_func=formatting_prompts_func,
    max_seq_length=max_seq_length,
    packing=False,
    args=TrainingArguments(
        per_device_train_batch_size=1,
        gradient_accumulation_steps=8,
        num_train_epochs=3,
        learning_rate=2e-5,
        bf16=torch.cuda.is_bf16_supported(),
        fp16=not torch.cuda.is_bf16_supported(),
        optim="adamw_8bit",
        output_dir="gpt-oss-20b-finetuned",
        report_to="none",
    ),
)

trainer.train()

Error Message

Traceback (most recent call last):
  ...
  File "...\unsloth_compiled_cache\UnslothSFTTrainer.py", line 1114, in _prepare_dataset
    dataset_num_proc = max(psutil.cpu_count()+4, 2)
NameError: name 'psutil' is not defined

Activity

  1. Sneakr commented on Dec 25, 2025

    @Sneakr
    Collaborator

    Hi, there's been users with similar error recently that noticed it was environment issues. Can you please test this and verify if it's running or if there's something wrong with your environment/versions. https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-(20B)-Fine-tuning.ipynb

  2. midorin-Linux commented on Dec 26, 2025

    @midorin-Linux
    Author

    Thank you for your assistance. When I followed your advice and tested it in Google Colab, I didn't encounter the NameError: name 'psutil' is not defined error. Based on this, I suspect the issue was related to environment configuration.

  3. chenzhongxiang commented on Dec 27, 2025

    @chenzhongxiang

    I found that using the SFTConfig provided by trl as the args parameter for SFTTrainer, which works without issues.

  4. added a commit that references this issue on Dec 29, 2025
    3455f02
  5. added a commit that references this issue on May 14, 2026
    6180add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions