3434
3535
3636def merge_safetensors (
37- pretrained_model_name_or_path : str | os .PathLike [str ], ** kwargs
37+ pretrained_model_name_or_path : str | os .PathLike [str ], model_class : str , ** kwargs
3838) -> tuple [dict [str , torch .Tensor ], dict [str , str ]]:
3939 """
4040 Merge split safetensors model files into a single state dict and metadata.
@@ -47,6 +47,8 @@ def merge_safetensors(
4747 ----------
4848 pretrained_model_name_or_path : str or os.PathLike
4949 Path to the model directory or HuggingFace repo.
50+ model_class : str
51+ Specify model class. E.g. NunchakuFluxTransformer2dModel or NunchakuZImageTransformer2DModel
5052 **kwargs
5153 Additional keyword arguments for subfolder, comfy_config_path, and HuggingFace download options.
5254
@@ -108,6 +110,9 @@ def merge_safetensors(
108110 state_dict .update (transformer_block_sd )
109111
110112 rank = next ((v .shape [1 ] for k , v in transformer_block_sd .items () if ".lora_down" in k ), 32 )
113+ if "ZImage" in model_class :
114+ rank = next ((v .shape [1 ] for k , v in transformer_block_sd .items () if ".proj_down" in k ), 32 )
115+ skip_refiners = not any (("refiner" in k and "attention.to_qkv" in k ) for k in transformer_block_sd .keys ())
111116
112117 precision = "int4"
113118 for v in state_dict .values ():
@@ -134,10 +139,12 @@ def merge_safetensors(
134139 },
135140 "rank" : rank ,
136141 }
142+ if "ZImage" in model_class :
143+ quantization_config ["skip_refiners" ] = skip_refiners
137144 return state_dict , {
138145 "config" : Path (config_path ).read_text (),
139146 "comfy_config" : Path (comfy_config_path ).read_text (),
140- "model_class" : "NunchakuFluxTransformer2dModel" ,
147+ "model_class" : model_class ,
141148 "quantization_config" : json .dumps (quantization_config ),
142149 }
143150
@@ -151,10 +158,20 @@ def merge_safetensors(
151158 required = True ,
152159 help = "Path to model directory. It can also be a huggingface repo." ,
153160 )
161+ parser .add_argument (
162+ "-m" ,
163+ "--model-class" ,
164+ type = str ,
165+ required = True ,
166+ help = "Specify model class. E.g. NunchakuFluxTransformer2dModel or NunchakuZImageTransformer2DModel" ,
167+ )
154168 parser .add_argument ("-o" , "--output-path" , type = Path , required = True , help = "Path to output path" )
155169 args = parser .parse_args ()
156- state_dict , metadata = merge_safetensors (args .input_path )
170+ state_dict , metadata = merge_safetensors (args .input_path , args . model_class )
157171 output_path = Path (args .output_path )
172+ print (f" --input-path: { args .input_path } " )
173+ print (f" --model-class: { args .model_class } " )
174+ print (f" --output-path: { args .output_path } " )
158175 dirpath = output_path .parent
159176 dirpath .mkdir (parents = True , exist_ok = True )
160177 save_file (state_dict , output_path , metadata = metadata )
0 commit comments