|
20 | 20 |
|
21 | 21 | import json |
22 | 22 | import os |
| 23 | +import tempfile |
23 | 24 |
|
24 | 25 | os.environ["KERAS_BACKEND"] = "torch" |
25 | 26 | os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
@@ -257,69 +258,77 @@ def main(_): |
257 | 258 | print(f"Converting: {hf_model_id} -> {preset}") |
258 | 259 | print(f"{'=' * 60}\n") |
259 | 260 |
|
260 | | - # Download config to get parameters |
261 | | - print("Downloading config...") |
262 | | - config_path = hf_hub_download(hf_model_id, "config.json") |
263 | | - with open(config_path, "r") as f: |
264 | | - transformers_config = json.load(f) |
265 | | - |
266 | | - # Convert config |
267 | | - keras_config = convert_backbone_config(transformers_config) |
268 | | - |
269 | | - # Create KerasHub model |
270 | | - print("\nCreating KerasHub Gemma3Backbone...") |
271 | | - # Use float32 to match HF model default often |
272 | | - keras_model = keras_hub.models.Gemma3Backbone( |
273 | | - **keras_config, dtype="float32" |
274 | | - ) |
| 261 | + # Use a temporary directory for downloading HuggingFace artifacts |
| 262 | + # This keeps the output preset directory clean |
| 263 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 264 | + print(f"Using temporary directory for downloads: {temp_dir}") |
| 265 | + |
| 266 | + # Download config to get parameters |
| 267 | + print("Downloading config...") |
| 268 | + config_path = hf_hub_download(hf_model_id, "config.json") |
| 269 | + with open(config_path, "r") as f: |
| 270 | + transformers_config = json.load(f) |
| 271 | + |
| 272 | + # Convert config |
| 273 | + keras_config = convert_backbone_config(transformers_config) |
| 274 | + |
| 275 | + # Create KerasHub model |
| 276 | + print("\nCreating KerasHub Gemma3Backbone...") |
| 277 | + # Use float32 to match HF model default often |
| 278 | + keras_model = keras_hub.models.Gemma3Backbone( |
| 279 | + **keras_config, dtype="float32" |
| 280 | + ) |
275 | 281 |
|
276 | | - # Build model by calling it once |
277 | | - dummy_input = { |
278 | | - "token_ids": np.ones((1, 16), dtype="int32"), |
279 | | - "padding_mask": np.ones((1, 16), dtype="int32"), |
280 | | - } |
281 | | - _ = keras_model(dummy_input) |
| 282 | + # Build model by calling it once |
| 283 | + dummy_input = { |
| 284 | + "token_ids": np.ones((1, 16), dtype="int32"), |
| 285 | + "padding_mask": np.ones((1, 16), dtype="int32"), |
| 286 | + } |
| 287 | + _ = keras_model(dummy_input) |
282 | 288 |
|
283 | | - print(f"KerasHub model parameters: {keras_model.count_params():,}") |
| 289 | + print(f"KerasHub model parameters: {keras_model.count_params():,}") |
284 | 290 |
|
285 | | - # Transfer weights |
286 | | - print("\nAttaching simple loader...") |
287 | | - print(f"Downloading model.safetensors into {preset}...") |
288 | | - os.makedirs(preset, exist_ok=True) |
| 291 | + # Transfer weights |
| 292 | + print("\nAttaching simple loader...") |
| 293 | + print("Downloading model.safetensors into temporary directory...") |
289 | 294 |
|
290 | | - hf_hub_download(hf_model_id, "model.safetensors", local_dir=preset) |
| 295 | + hf_hub_download(hf_model_id, "model.safetensors", local_dir=temp_dir) |
291 | 296 |
|
292 | | - # We also need to download the dense layer weights for our new logic |
293 | | - print("Downloading dense layer weights...") |
294 | | - hf_hub_download(hf_model_id, "2_Dense/model.safetensors", local_dir=preset) |
295 | | - hf_hub_download(hf_model_id, "3_Dense/model.safetensors", local_dir=preset) |
| 297 | + # We also need to download the dense layer weights for our new logic |
| 298 | + print("Downloading dense layer weights...") |
| 299 | + hf_hub_download( |
| 300 | + hf_model_id, "2_Dense/model.safetensors", local_dir=temp_dir |
| 301 | + ) |
| 302 | + hf_hub_download( |
| 303 | + hf_model_id, "3_Dense/model.safetensors", local_dir=temp_dir |
| 304 | + ) |
296 | 305 |
|
297 | | - print("Converting weights...") |
| 306 | + print("Converting weights...") |
298 | 307 |
|
299 | | - with SafetensorLoader(preset) as loader: |
300 | | - convert_weights(keras_model, loader, transformers_config) |
| 308 | + with SafetensorLoader(temp_dir) as loader: |
| 309 | + convert_weights(keras_model, loader, transformers_config) |
301 | 310 |
|
302 | | - # Convert tokenizer |
303 | | - print("\nConverting tokenizer...") |
304 | | - # Helper to clean up tokenizer download if needed |
305 | | - hf_hub_download(hf_model_id, "tokenizer.model", local_dir=preset) |
306 | | - keras_tokenizer = convert_tokenizer( |
307 | | - keras_hub.models.Gemma3Tokenizer, preset |
308 | | - ) |
| 311 | + # Convert tokenizer |
| 312 | + print("\nConverting tokenizer...") |
| 313 | + # Helper to clean up tokenizer download if needed |
| 314 | + hf_hub_download(hf_model_id, "tokenizer.model", local_dir=temp_dir) |
| 315 | + keras_tokenizer = convert_tokenizer( |
| 316 | + keras_hub.models.Gemma3Tokenizer, temp_dir |
| 317 | + ) |
309 | 318 |
|
310 | | - # Validate |
311 | | - passed = validate_output(keras_model, keras_tokenizer, hf_model_id) |
| 319 | + # Validate |
| 320 | + passed = validate_output(keras_model, keras_tokenizer, hf_model_id) |
312 | 321 |
|
313 | | - if not passed: |
314 | | - print("\n⚠️ Verification failed. Check weight mapping.") |
315 | | - return |
| 322 | + if not passed: |
| 323 | + print("\n⚠️ Verification failed. Check weight mapping.") |
| 324 | + return |
316 | 325 |
|
317 | | - # Save preset |
318 | | - print(f"\nSaving to preset: ./{preset}") |
319 | | - keras_model.save_to_preset(preset) |
320 | | - keras_tokenizer.save_to_preset(preset) |
| 326 | + # Save preset |
| 327 | + print(f"\nSaving to preset: ./{preset}") |
| 328 | + keras_model.save_to_preset(preset) |
| 329 | + keras_tokenizer.save_to_preset(preset) |
321 | 330 |
|
322 | | - print(f"\n✅ Successfully converted and saved to: ./{preset}") |
| 331 | + print(f"\n✅ Successfully converted and saved to: ./{preset}") |
323 | 332 |
|
324 | 333 |
|
325 | 334 | if __name__ == "__main__": |
|
0 commit comments