GeminiFan207 commited on
Commit
e122497
·
verified ·
1 Parent(s): 4d8a04d

Create model_3_of_10.safetensors

Browse files
Files changed (1) hide show
  1. model_3_of_10.safetensors +69 -0
model_3_of_10.safetensors ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import load_file, save_file
3
+ import logging
4
+ import asyncio
5
+
6
+ # Logging setup
7
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
8
+
9
+ # Define model checkpoint path
10
+ MODEL_CHECKPOINT = "model-3-of-10.safetensors"
11
+
12
+ # Detect GPU availability
13
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
+ # Load model with efficient memory management
16
+ async def load_model(filepath: str) -> dict:
17
+ """Asynchronously loads a model from a safetensors file."""
18
+ try:
19
+ logging.info(f"Loading model from {filepath} on {DEVICE}...")
20
+ model_data = load_file(filepath, device=DEVICE)
21
+ logging.info(f"Model {filepath} successfully loaded.")
22
+ return model_data
23
+ except Exception as e:
24
+ logging.error(f"Error loading model: {str(e)}")
25
+ raise RuntimeError(f"Error loading model: {str(e)}")
26
+
27
+ # Save model with optimized storage format
28
+ async def save_model(filepath: str, model_tensors: dict):
29
+ """Asynchronously saves a model to a safetensors file."""
30
+ try:
31
+ logging.info(f"Saving model to {filepath}...")
32
+ save_file(model_tensors, filepath)
33
+ logging.info(f"Model saved at {filepath}")
34
+ except Exception as e:
35
+ logging.error(f"Error saving model: {str(e)}")
36
+ raise RuntimeError(f"Error saving model: {str(e)}")
37
+
38
+ # Dynamically generate layers for efficient scaling
39
+ def initialize_model(layers: list = [4096, 8192, 16384], dtype: torch.dtype = torch.float16) -> dict:
40
+ """Initializes a model with random tensors for each layer."""
41
+ model_tensors = {}
42
+ for i, size in enumerate(layers):
43
+ layer_name = f"layer_{i+1}"
44
+ logging.info(f"Initializing {layer_name} with size {size}x{size} on {DEVICE}...")
45
+ model_tensors[layer_name] = torch.randn(size, size, dtype=dtype, device=DEVICE)
46
+
47
+ torch.cuda.empty_cache() # Free unused memory
48
+ logging.info("Model initialization completed.")
49
+ return model_tensors
50
+
51
+ # Main execution
52
+ async def main():
53
+ model_data = initialize_model()
54
+
55
+ # Save the model for deployment
56
+ await save_model(MODEL_CHECKPOINT, model_data)
57
+
58
+ # Load the model for verification
59
+ loaded_model_data = await load_model(MODEL_CHECKPOINT)
60
+
61
+ # Verify loaded tensors match saved tensors
62
+ for key in model_data:
63
+ if not torch.allclose(model_data[key], loaded_model_data[key], atol=1e-5):
64
+ logging.warning(f"Tensor mismatch in {key}!")
65
+ else:
66
+ logging.info(f"Tensor {key} verified successfully.")
67
+
68
+ # Run asynchronously
69
+ asyncio.run(main())