Skip to content

Missing trainer.py file in src/diffusers/trainer.py causing import error #14487

Description

@bobrathbone

Describe the bug

There is no file called trainer.py at /src/diffusers/ in the official diffusers repository.
I(myenv) C:\Temp\jan\scripts\diffusers>python
Python 3.13.14 (tags/v3.13.14:fd17997, Jun 10 2026, 13:03:48) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

from diffusers.trainer import Trainer as DiffusersTrainer
Traceback (most recent call last):
File "", line 1, in
from diffusers.trainer import Trainer as DiffusersTrainer
ModuleNotFoundError: No module named 'diffusers.trainer'

These are my imports

import torch
from diffusers import (
    StableDiffusionPipeline,
    UNet2DConditionModel,
)

from diffusers.loaders import LoraLoaderMixin
from peft import LoraConfig, get_peft_model
from datasets import Dataset
from diffusers.trainer import Trainer as DiffusersTrainer  

Below is my training subroutine

def train():
    # Load dataset
    print("Loading training data...")
    raw_data = load_dataset_from_jsonl(LABELS_FILE)

    if not raw_data:
        raise ValueError(f"No valid entries found in {LABELS_FILE}!")

    print(f"Loaded {len(raw_data)} image-prompt pairs.")

    # Create dataset object (for trainer compatibility)
    prompts = [item["prompt"] for item in raw_data]
    img_paths = [Path(item["image_path"]) for item in raw_data]

    train_dataset = ImagePromptDataset(prompts=prompts, image_paths=img_paths)

    # Setup model
    pipe = setup_model()

    # Replace UNet with LoRA-enabled version (if not already done)
    if hasattr(pipe.unet, "model"):
        print("✅ Successfully applied LoRA to SD pipeline.")

    # Training arguments — use LoRA-specific config
    training_args = {
        "output_dir": "./lora_finetuned_sd",
        "logging_steps": 10,
        "save_steps": 50,
        "learning_rate": 2e-4,
        "num_train_epochs": EPOCHS,
        "gradient_accumulation_steps": BATCH_SIZE,  # Adjust per GPU batch size
        "per_device_train_batch_size": BATCH_SIZE,
        "fp16": True,
        "warmup_ratio": 0.05,
        "weight_decay": 0.01,
        "report_to": None,
    }

    trainer = DiffusersTrainer(
        model=pipe.unet,
        args=training_args,
        train_dataset=train_dataset,
        tokenizer=None
    )

    print("Starting training...")
    trainer.train()

    # Save final LoRA weights
    pipe.save_lora_weights("./lora_finetuned_sd", safe=True)
    print("✅ Final LoRA model saved to ./lora_finetuned_sd")

Reproduction

python

from diffusers.trainer import Trainer as DiffusersTrainer

Logs

>python
Python 3.13.14 (tags/v3.13.14:fd17997, Jun 10 2026, 13:03:48) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> **from diffusers.trainer import Trainer as DiffusersTrainer**
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    from diffusers.trainer import Trainer as DiffusersTrainer
ModuleNotFoundError: No module named 'diffusers.trainer'
>>>

System Info

Python 3.13.14

import diffusers
print (diffusers.version)
0.40.0.dev0

Who can help?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions