Fix CPU/CUDA device mismatch when training Klein edit with control_path#742
Merged
jaretburkett merged 1 commit intoostris:mainfrom Mar 25, 2026
Conversation
When training Klein models with a `control_path` (edit/kontext-style
paired datasets), `encode_image_refs()` returns tensors that reside on
the VAE's device (CPU, since the VAE weights are loaded via
`load_file(..., device="cpu")` and are never explicitly moved to the
training device). Concatenating those CPU tensors with the training
latents (`packed_latents`) that live on CUDA raises:
RuntimeError: Expected all tensors to be on the same device
Fix: move `img_cond_seq` and `img_cond_seq_ids` to the same device
(and dtype) as `img_input` / `img_input_ids` before concatenation.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
nice job! When set |
Contributor
|
Thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When training Klein models (e.g.
flux2_klein_9b) with a paired dataset usingcontrol_path, the training crashes with:Fixes #652
Root Cause
In
load_model()(flux2_model.py), the VAE weights are loaded via:The VAE is never explicitly moved to the training device (unlike the transformer and text encoder which receive
.to(self.device_torch)calls). As a result,self.vaelives on CPU throughout training.When
get_noise_prediction()processes a batch with control images, it calls:Inside
encode_image_refs(src/sampling.py), the input is moved toae.device(CPU) for encoding and the returned tensors remain on CPU. These are then concatenated withpacked_latents/img_idswhich are on CUDA — causing the device mismatch crash.Fix
Move
img_cond_seqandimg_cond_seq_idsto the same device and dtype asimg_inputbefore concatenation:This is a minimal, targeted fix at the concatenation point. It handles both the device mismatch (CPU→CUDA) and the dtype cast in one step, without changing VAE loading behaviour or affecting other code paths.
Reproduction
Config: any Klein training config (
arch: flux2_klein_9borflux2_klein_4b) with acontrol_pathset in the dataset section (edit / paired-image training). Training will crash at the first step whenimg_cond_seqis notNone.