-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer-iteration.py
131 lines (110 loc) · 5.09 KB
/
layer-iteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import coremltools as ct
import os
import argparse
import json
def load_model(model_path, weights_dir):
try:
model = ct.models.MLModel(model_path)
spec = model._spec
model = ct.models.MLModel(spec, weights_dir=weights_dir)
return model, spec
except FileNotFoundError:
print(f"Error: Model file '{model_path}' not found.")
return None, None
def get_precision(weights):
if hasattr(weights, 'floatValue'):
return "float32"
elif hasattr(weights, 'float16Value'):
return "float16"
elif hasattr(weights, 'intValue'):
return "int32"
elif hasattr(weights, 'rawValue'):
return "raw byte array (custom precision)"
else:
return "unknown"
def print_model_description(spec):
print("Model Description:")
metadata = spec.description.metadata
if metadata:
print(f"Model Name: {metadata.shortDescription}")
print(f"Author: {metadata.author}")
print(f"License: {metadata.license}")
print(f"Version: {metadata.versionString}")
def print_model_io(spec):
print("\nModel Inputs:")
for input_desc in spec.description.input:
print(f"Name: {input_desc.name}, Type: {input_desc.type.WhichOneof('Type')}")
print("\nModel Outputs:")
for output_desc in spec.description.output:
print(f"Name: {output_desc.name}, Type: {output_desc.type.WhichOneof('Type')}")
def inspect_layers(spec):
layer_types = set()
print("\nLayers and their Precision:")
for layer in spec.neuralNetwork.layers:
layer_type = layer.WhichOneof('layer')
layer_types.add(layer_type)
print(f"Layer: {layer.name}")
if hasattr(layer, 'weights'):
precision = get_precision(layer.weights)
print(f"Type: {layer_type}, Precision: {precision}")
else:
print(f"Type: {layer_type}, Precision: unknown")
print("\nUnique Layer Types in the Model:")
for layer_type in layer_types:
print(layer_type)
def load_weights(weights_path):
if os.path.exists(weights_path):
with open(weights_path, "rb") as f:
weights_data = f.read()
print("\nweights.bin file loaded, size:", len(weights_data), "bytes")
else:
print("\nweights.bin file not found")
def get_weights_metadata(model):
try:
weight_metadata_dict = ct.optimize.coreml.get_weights_metadata(model)
except AttributeError:
print("Error: get_weights_metadata function not found in coremltools.utils")
weight_metadata_dict = {}
print("\nWeights Metadata:")
#print(weight_metadata_dict)
for weight_name, weight_metadata in weight_metadata_dict.items():
print(f"Weight Name: {weight_name}")
print(f" - val: {weight_metadata.val}")
print(f" - Sparsity: {weight_metadata.sparsity}")
print(f" - Unique Values: {weight_metadata.unique_values}")
print(f" - Child Ops: {weight_metadata.child_ops}")
def load_manifest(manifest_path):
with open(manifest_path, "r") as f:
manifest_data = json.load(f)
return manifest_data
def print_manifest_info(manifest_data):
print("\nManifest Information:")
#print(f"Model Name: {manifest_data['name']}")
#print(f"Model Version: {manifest_data['version']}")
#print(f"Author: {manifest_data['author']}")
#print(f"License: {manifest_data['license']}")
#print(f"Description: {manifest_data['description']}")
def main():
parser = argparse.ArgumentParser(description="Inspect a Core ML model.")
parser.add_argument("--mlmodel_path", help="Path to the .mlmodel file")
parser.add_argument("--weights_dir", help="Directory containing the weights file")
parser.add_argument("--manifest_path", help="Path to the Manifest.json file")
args = parser.parse_args()
if not args.mlmodel_path or not args.weights_dir or not args.manifest_path:
print("Error: Please provide --mlmodel_path, --weights_dir, and --manifest_path arguments.")
return
model_path = args.mlmodel_path
weights_dir = args.weights_dir
manifest_path = args.manifest_path
model, spec = load_model(model_path, weights_dir)
manifest_data = load_manifest(manifest_path)
if model and spec:
print_model_description(spec)
print_model_io(spec)
inspect_layers(spec)
load_weights(os.path.join(weights_dir, "weight.bin"))
get_weights_metadata(model)
print_manifest_info(manifest_data)
if __name__ == "__main__":
main()
#python layer-iteration.py --mlmodel_path "/Volumes/Macintosh HD/Users/anthonymikinka/corenet/mlx_examples/open_elm/OpenELM-270M-Instruct-128-FP16ComputePrecisionv2.mlpackage" --weights_dir "/Volumes/Macintosh HD/Users/anthonymikinka/corenet/mlx_examples/open_elm/OpenELM-270M-Instruct-128-FP16ComputePrecisionv2.mlpackage/Data/com.apple.CoreML/weights" --manifest_path "/Volumes/Macintosh HD/Users/anthonymikinka/corenet/mlx_examples/open_elm/OpenELM-270M-Instruct-128-FP16ComputePrecisionv2.mlpackage/Manifest.json"