From 401621618b7ff548fd04c2ea5b2aca05afd936ac Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Thu, 2 Apr 2026 11:44:26 -0700 Subject: [PATCH] fix(studio): don't set trust_remote_code for Gemma 4 training (#4795) Gemma 4 is a native transformers 5.5 model and does not need trust_remote_code=True. The auto-enable logic (added for NemotronH) was catching all transformers 5.x models, including Gemma 4. When trust_remote_code=True, unsloth_compile_transformers() returns early without running the compiler. This disables the fused cross entropy patch, causing logged training loss to be inflated by the gradient_accumulation_steps factor. Exclude models matching "gemma-4" or "gemma4" from the auto-enable so the compiler runs and applies fused cross entropy correctly. --- studio/backend/core/training/worker.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.py index 699cfe74f7..0454eada89 100644 --- a/studio/backend/core/training/worker.py +++ b/studio/backend/core/training/worker.py @@ -390,11 +390,16 @@ def run_training_process( # Some newer architectures (e.g. NemotronH) have config parsing bugs in # transformers that require trust_remote_code=True as a workaround. # Only auto-enable for unsloth/* prefixed models (trusted source). + # Exclude Gemma 4 since it is a native transformers 5.5 model and + # trust_remote_code=True would bypass the compiler (disabling fused CE). from utils.transformers_version import needs_transformers_5 + _lowered = model_name.lower() + _is_native_t5 = any(x in _lowered for x in ("gemma-4", "gemma4")) if ( needs_transformers_5(model_name) - and model_name.lower().startswith("unsloth/") + and _lowered.startswith("unsloth/") + and not _is_native_t5 and not config.get("trust_remote_code", False) ): config["trust_remote_code"] = True