From f7840da2852c4df4690ef1fd8419efee2eec853d Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sat, 20 Jun 2026 08:44:33 +0000 Subject: [PATCH] Build UNSLOTH_MODEL_NAME fresh per load (fixes gpt-oss merged reload) UNSLOTH_MODEL_NAME was built by prepending its previous os.environ value, so it accumulated load flags across loads and across processes (it is inherited by a save->reload subprocess). After loading a gpt-oss bnb-4bit model, a leftover _load_in_4bit_ stayed in the string, and a subsequent non-4bit load (e.g. reloading a merged_16bit checkpoint) still saw _load_in_4bit_. gpt-oss then kept its BnB router patch (router.linear.weight) while the merged checkpoint stores the stock router.weight, raising 'Unsloth: Critical error since some weights are not initialized'. Rebuild the string from THIS load's model name and flags so each load is self-contained. The model-type tag and load flags are still present, so all UNSLOTH_MODEL_NAME consumers keep working; this only drops stale flags carried over from an unrelated earlier load. --- unsloth/models/loader.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index 459b6ed32d..09dd5364d4 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -1234,7 +1234,14 @@ class FastModel(FastBaseModel): # Save model types and loading method lowered_model_name = model_name.lower() - string = os.environ.get("UNSLOTH_MODEL_NAME", "") + model_types_all + # Build UNSLOTH_MODEL_NAME freshly from THIS load's model name + flags. Do not + # prepend the previous os.environ value: it persists in-process and is inherited + # across processes (e.g. a save->reload subprocess), so it accumulates stale load + # flags. A leftover "_load_in_4bit_" from an earlier bnb-4bit load would make + # gpt-oss wrongly take the BnB router patch (router.linear.weight) when later + # reloading a merged 16bit checkpoint (router.weight), raising + # "Unsloth: Critical error since some weights are not initialized". + string = lowered_model_name + "," + model_types_all if load_in_4bit: string += "_load_in_4bit_" if load_in_8bit: