kto trainer invalid configuration error
Reproduction
from datasets import load_dataset, Dataset
from trl import KTOConfig, KTOTrainer
from transformers import AutoModelForCausalLM, AutoTokenizer
import pandas as pd
import torch
df = pd.read_csv('data/kto_data.csv')
df = df.sample(frac = 1, random_state = 42) #set random seed for reproducibility
from sklearn.model_selection import train_test_split
# Split into train/test
train, val = train_test_split(df, test_size=0.15, random_state=42)
train = Dataset.from_pandas(train)
val = Dataset.from_pandas(val)
tokenizer = AutoTokenizer.from_pretrained('google/gemma-3-12b-it')
model = AutoModelForCausalLM.from_pretrained('sft_model/best_model', device_map="auto", torch_dtype=torch.bfloat16, attn_implementation='eager')
training_args = KTOConfig(output_dir=output_directory,
undesirable_weight=1.8,
num_train_epochs=5,
logging_steps=250,
eval_strategy="steps", # Perform evaluation at regular intervals
eval_steps=250, # Evaluate every `eval_steps` steps
save_steps=500,
load_best_model_at_end=True,
report_to = "wandb",
do_eval = True,
warmup_ratio = .1,
learning_rate = .000005,
)
trainer = KTOTrainer(model=model, args=training_args, processing_class=tokenizer, train_dataset=train, eval_dataset = val)
trainer.train()
outputs:
Traceback (most recent call last):
File "/sfs/gpfs/tardis/project/bryan_research/MATHWELL2_dev/kto_27b_lora.py", line 91, in <module>
trainer.train()
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/transformers/trainer.py", line 2238, in train
return inner_training_loop(
^^^^^^^^^^^^^^^^^^^^
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/transformers/trainer.py", line 2582, in _inner_training_loop
tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/transformers/trainer.py", line 3796, in training_step
loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/trl/trainer/kto_trainer.py", line 1466, in compute_loss
loss, metrics = self.get_batch_loss_metrics(model, inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/trl/trainer/kto_trainer.py", line 1368, in get_batch_loss_metrics
forward_output = self.forward(model, batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch/brc4cb/llm_course/lib/python3.11/site-packages/trl/trainer/kto_trainer.py", line 1090, in forward
chosen_logits = completion_logits[chosen_idx, ...]
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
torch.AcceleratorError: CUDA error: invalid configuration argument
System Info
I am using Python 3.11, PyTorch 2.8.0+cu128, Transformers 4.55.4, TRL 0.21.0, and Accelerate 1.10.1. I get this error when using both a single GPU and multi-GPUs. I also get it across GPU types (A6000, H200, and A100 80 GB GPUs). I have tried downgrading TRL and that did not resolve the issue. Any help anyone could offer would be greatly appreciated.
Checklist
- [x] I have checked that my issue isn't already filed (see open issues)
- [x] I have included my system information
- [x] Any code provided is minimal, complete, and reproducible (more on MREs)
- [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
- [x] Any traceback provided is complete
Hey @bryanchrist, I tried to reproduce the issue but couldn't. Could it be linked to your data (feel free share it if you want me to have another look) ? For instance if you have only label = True or label = False, I think this may create this type of issues.
Got the same error on a H100 running the BCO example script from 0.22.2
Hey @bryanchrist, I tried to reproduce the issue but couldn't. Could it be linked to your data (feel free share it if you want me to have another look) ? For instance if you have only label = True or label = False, I think this may create this type of issues.
Thank you for looking into this. The data has a roughly even balance of True and False labels, so I do not think this is the issue. I formatted the dataset according to the recommendations on the example script on the KTO Trainer.
@bryanchrist happens if you try just for debugging, running on cpu?
Hi,
I’m running into the same error. The code is the sample from the KTOTrainer page. From what I’ve checked, this error occurs when the PyTorch version is 2.8.0. I confirmed that downgrading PyTorch to 2.7.1 prevents this bug. As of today (2025/10/15), my accelerate, transformers, and trl are the latest releases:
accelerate==1.10.1
transformers==4.57.1
trl==0.23.1
When I lower the torch version in my environment, the following libraries are downgraded as well:
- nvidia-cublas-cu12==12.8.4.1
+ nvidia-cublas-cu12==12.6.4.1
- nvidia-cuda-cupti-cu12==12.8.90
+ nvidia-cuda-cupti-cu12==12.6.80
- nvidia-cuda-nvrtc-cu12==12.8.93
+ nvidia-cuda-nvrtc-cu12==12.6.77
- nvidia-cuda-runtime-cu12==12.8.90
+ nvidia-cuda-runtime-cu12==12.6.77
- nvidia-cudnn-cu12==9.10.2.21
+ nvidia-cudnn-cu12==9.5.1.17
- nvidia-cufft-cu12==11.3.3.83
+ nvidia-cufft-cu12==11.3.0.4
- nvidia-cufile-cu12==1.13.1.3
+ nvidia-cufile-cu12==1.11.1.6
- nvidia-curand-cu12==10.3.9.90
+ nvidia-curand-cu12==10.3.7.77
- nvidia-cusolver-cu12==11.7.3.90
+ nvidia-cusolver-cu12==11.7.1.2
- nvidia-cusparse-cu12==12.5.8.93
+ nvidia-cusparse-cu12==12.5.4.2
- nvidia-cusparselt-cu12==0.7.1
+ nvidia-cusparselt-cu12==0.6.3
- nvidia-nccl-cu12==2.27.3
+ nvidia-nccl-cu12==2.26.2
- nvidia-nvjitlink-cu12==12.8.93
+ nvidia-nvjitlink-cu12==12.6.85
- nvidia-nvtx-cu12==12.8.90
+ nvidia-nvtx-cu12==12.6.77
- torch==2.8.0
+ torch==2.7.1
- triton==3.4.0
+ triton==3.3.1
I believe the root cause lies in one of these.
The error does not appear to be caused by the environment. The problem is that KTOTrainer cannot work with unbalanced batches (e.g., [False, False] or [True, True]). Converting the dataset to [True, False, True, False, True...] helps avoid the CUDA error, but does not solve the problem.
Hi,
I’m running into the same error. The code is the sample from the KTOTrainer page. From what I’ve checked, this error occurs when the PyTorch version is
2.8.0. I confirmed that downgrading PyTorch to2.7.1prevents this bug. As of today (2025/10/15), my accelerate, transformers, and trl are the latest releases:accelerate==1.10.1 transformers==4.57.1 trl==0.23.1When I lower the torch version in my environment, the following libraries are downgraded as well:
- nvidia-cublas-cu12==12.8.4.1 + nvidia-cublas-cu12==12.6.4.1 - nvidia-cuda-cupti-cu12==12.8.90 + nvidia-cuda-cupti-cu12==12.6.80 - nvidia-cuda-nvrtc-cu12==12.8.93 + nvidia-cuda-nvrtc-cu12==12.6.77 - nvidia-cuda-runtime-cu12==12.8.90 + nvidia-cuda-runtime-cu12==12.6.77 - nvidia-cudnn-cu12==9.10.2.21 + nvidia-cudnn-cu12==9.5.1.17 - nvidia-cufft-cu12==11.3.3.83 + nvidia-cufft-cu12==11.3.0.4 - nvidia-cufile-cu12==1.13.1.3 + nvidia-cufile-cu12==1.11.1.6 - nvidia-curand-cu12==10.3.9.90 + nvidia-curand-cu12==10.3.7.77 - nvidia-cusolver-cu12==11.7.3.90 + nvidia-cusolver-cu12==11.7.1.2 - nvidia-cusparse-cu12==12.5.8.93 + nvidia-cusparse-cu12==12.5.4.2 - nvidia-cusparselt-cu12==0.7.1 + nvidia-cusparselt-cu12==0.6.3 - nvidia-nccl-cu12==2.27.3 + nvidia-nccl-cu12==2.26.2 - nvidia-nvjitlink-cu12==12.8.93 + nvidia-nvjitlink-cu12==12.6.85 - nvidia-nvtx-cu12==12.8.90 + nvidia-nvtx-cu12==12.6.77 - torch==2.8.0 + torch==2.7.1 - triton==3.4.0 + triton==3.3.1I believe the root cause lies in one of these.
Hello,
When we downgrade torch like this, do we need to downgrade torchvision and torchtext as well?
I’m not using torchvision and torchtext in my project, so I can’t say for sure, but since trl depends on torch, it will probably work just by downgrading torch.
(That said, there is a possibility that torchvision and torchtext will also be downgraded when you downgrade torch.)
Hi, I’m running into the same error. The code is the sample from the KTOTrainer page. From what I’ve checked, this error occurs when the PyTorch version is
2.8.0. I confirmed that downgrading PyTorch to2.7.1prevents this bug. As of today (2025/10/15), my accelerate, transformers, and trl are the latest releases:accelerate==1.10.1 transformers==4.57.1 trl==0.23.1When I lower the torch version in my environment, the following libraries are downgraded as well:
- nvidia-cublas-cu12==12.8.4.1 + nvidia-cublas-cu12==12.6.4.1 - nvidia-cuda-cupti-cu12==12.8.90 + nvidia-cuda-cupti-cu12==12.6.80 - nvidia-cuda-nvrtc-cu12==12.8.93 + nvidia-cuda-nvrtc-cu12==12.6.77 - nvidia-cuda-runtime-cu12==12.8.90 + nvidia-cuda-runtime-cu12==12.6.77 - nvidia-cudnn-cu12==9.10.2.21 + nvidia-cudnn-cu12==9.5.1.17 - nvidia-cufft-cu12==11.3.3.83 + nvidia-cufft-cu12==11.3.0.4 - nvidia-cufile-cu12==1.13.1.3 + nvidia-cufile-cu12==1.11.1.6 - nvidia-curand-cu12==10.3.9.90 + nvidia-curand-cu12==10.3.7.77 - nvidia-cusolver-cu12==11.7.3.90 + nvidia-cusolver-cu12==11.7.1.2 - nvidia-cusparse-cu12==12.5.8.93 + nvidia-cusparse-cu12==12.5.4.2 - nvidia-cusparselt-cu12==0.7.1 + nvidia-cusparselt-cu12==0.6.3 - nvidia-nccl-cu12==2.27.3 + nvidia-nccl-cu12==2.26.2 - nvidia-nvjitlink-cu12==12.8.93 + nvidia-nvjitlink-cu12==12.6.85 - nvidia-nvtx-cu12==12.8.90 + nvidia-nvtx-cu12==12.6.77 - torch==2.8.0 + torch==2.7.1 - triton==3.4.0 + triton==3.3.1I believe the root cause lies in one of these.
Hello,
When we downgrade torch like this, do we need to downgrade torchvision and torchtext as well?
Hi, I'm having the same issue. Did you resolve this issue by downgrading torch?
Hi, I’m running into the same error. The code is the sample from the KTOTrainer page. From what I’ve checked, this error occurs when the PyTorch version is
2.8.0. I confirmed that downgrading PyTorch to2.7.1prevents this bug. As of today (2025/10/15), my accelerate, transformers, and trl are the latest releases:accelerate==1.10.1 transformers==4.57.1 trl==0.23.1When I lower the torch version in my environment, the following libraries are downgraded as well:
- nvidia-cublas-cu12==12.8.4.1 + nvidia-cublas-cu12==12.6.4.1 - nvidia-cuda-cupti-cu12==12.8.90 + nvidia-cuda-cupti-cu12==12.6.80 - nvidia-cuda-nvrtc-cu12==12.8.93 + nvidia-cuda-nvrtc-cu12==12.6.77 - nvidia-cuda-runtime-cu12==12.8.90 + nvidia-cuda-runtime-cu12==12.6.77 - nvidia-cudnn-cu12==9.10.2.21 + nvidia-cudnn-cu12==9.5.1.17 - nvidia-cufft-cu12==11.3.3.83 + nvidia-cufft-cu12==11.3.0.4 - nvidia-cufile-cu12==1.13.1.3 + nvidia-cufile-cu12==1.11.1.6 - nvidia-curand-cu12==10.3.9.90 + nvidia-curand-cu12==10.3.7.77 - nvidia-cusolver-cu12==11.7.3.90 + nvidia-cusolver-cu12==11.7.1.2 - nvidia-cusparse-cu12==12.5.8.93 + nvidia-cusparse-cu12==12.5.4.2 - nvidia-cusparselt-cu12==0.7.1 + nvidia-cusparselt-cu12==0.6.3 - nvidia-nccl-cu12==2.27.3 + nvidia-nccl-cu12==2.26.2 - nvidia-nvjitlink-cu12==12.8.93 + nvidia-nvjitlink-cu12==12.6.85 - nvidia-nvtx-cu12==12.8.90 + nvidia-nvtx-cu12==12.6.77 - torch==2.8.0 + torch==2.7.1 - triton==3.4.0 + triton==3.3.1I believe the root cause lies in one of these.
Hello, When we downgrade torch like this, do we need to downgrade torchvision and torchtext as well?
Hi, I'm having the same issue. Did you resolve this issue by downgrading torch?
Hi, I fixed the issue.
-
Downgrading torch works. But, we need to downgrade torchvision and torchtext as well. Thankfully, You can change the runtime version in GPU selection section of colab. It works in the last runtime version. Aditionally, no changes needed are needed in kaggle for KTOtrainer to work.
-
KTOTrainer takes a lot of space . Refer-https://discord.com/channels/879548962464493619/879548962464493622/1440675714419523585
-
To fix the above problem, refer -https://huggingface.co/datasets/John6666/forum2/blob/main/trl_kto_blow_up_memory_1.md
Hi, I’m running into the same error. The code is the sample from the KTOTrainer page. From what I’ve checked, this error occurs when the PyTorch version is
2.8.0. I confirmed that downgrading PyTorch to2.7.1prevents this bug. As of today (2025/10/15), my accelerate, transformers, and trl are the latest releases:accelerate==1.10.1 transformers==4.57.1 trl==0.23.1When I lower the torch version in my environment, the following libraries are downgraded as well:
- nvidia-cublas-cu12==12.8.4.1 + nvidia-cublas-cu12==12.6.4.1 - nvidia-cuda-cupti-cu12==12.8.90 + nvidia-cuda-cupti-cu12==12.6.80 - nvidia-cuda-nvrtc-cu12==12.8.93 + nvidia-cuda-nvrtc-cu12==12.6.77 - nvidia-cuda-runtime-cu12==12.8.90 + nvidia-cuda-runtime-cu12==12.6.77 - nvidia-cudnn-cu12==9.10.2.21 + nvidia-cudnn-cu12==9.5.1.17 - nvidia-cufft-cu12==11.3.3.83 + nvidia-cufft-cu12==11.3.0.4 - nvidia-cufile-cu12==1.13.1.3 + nvidia-cufile-cu12==1.11.1.6 - nvidia-curand-cu12==10.3.9.90 + nvidia-curand-cu12==10.3.7.77 - nvidia-cusolver-cu12==11.7.3.90 + nvidia-cusolver-cu12==11.7.1.2 - nvidia-cusparse-cu12==12.5.8.93 + nvidia-cusparse-cu12==12.5.4.2 - nvidia-cusparselt-cu12==0.7.1 + nvidia-cusparselt-cu12==0.6.3 - nvidia-nccl-cu12==2.27.3 + nvidia-nccl-cu12==2.26.2 - nvidia-nvjitlink-cu12==12.8.93 + nvidia-nvjitlink-cu12==12.6.85 - nvidia-nvtx-cu12==12.8.90 + nvidia-nvtx-cu12==12.6.77 - torch==2.8.0 + torch==2.7.1 - triton==3.4.0 + triton==3.3.1I believe the root cause lies in one of these.
Hello, When we downgrade torch like this, do we need to downgrade torchvision and torchtext as well?
Hi, I'm having the same issue. Did you resolve this issue by downgrading torch?
Hi, I fixed the issue.
- Downgrading torch works. But, we need to downgrade torchvision and torchtext as well. Thankfully, You can change the runtime version in GPU selection section of colab. It works in the last runtime version. Aditionally, no changes needed are needed in kaggle for KTOtrainer to work.
- KTOTrainer takes a lot of space . Refer-https://discord.com/channels/879548962464493619/879548962464493622/1440675714419523585
- To fix the above problem, refer -https://huggingface.co/datasets/John6666/forum2/blob/main/trl_kto_blow_up_memory_1.md
Thank you! I have solved this problem by following your advice.
Hey @bryanchrist, sorry for the very slow reply — we're catching up on the backlog.
This was a genuine bug: KTO built chosen_idx/rejected_idx as Python lists and used advanced indexing on the full-vocab logits (completion_logits[chosen_idx, ...], the exact line in your traceback). On PyTorch 2.8 with large-vocab models like gemma-3 (~256k vocab), that launches a CUDA kernel with an invalid grid configuration → invalid configuration argument. It's fixed in #4635 (switched to index_select, see https://github.com/huggingface/trl/blob/b6c5ae16c693c38860bab33d1034610aa5babfa9/trl/experimental/kto/kto_trainer.py#L1334-L1340), released in TRL v0.26.0 — so the fix is to upgrade, not downgrade (downgrading kept the old code path, which is why that didn't help). pip install -U trl should resolve it; note KTO now lives at from trl.experimental.kto import KTOTrainer.
Closing as fixed, but happy to re-open if you still hit this on >=0.26.0.