-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpreprocess_CMSSW.py
228 lines (189 loc) · 7.58 KB
/
preprocess_CMSSW.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import pickle
import tensorflow as tf
import sys
import time
import math
import yaml
import inspect
import numpy as np
import pandas as pd
from argparse import SUPPRESS, ArgumentParser as _AP
import keras.backend as K
from qkeras.utils import model_save_quantized_weights
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import RandomUnderSampler
from collections import Counter
from uuid import uuid4
from datetime import datetime
# Keras / QKeras imports
from tensorflow import keras
from keras.layers import (Input, Flatten, Dense, ReLU, Reshape, Conv2DTranspose)
from keras.models import Model
from qkeras import QActivation, QConv2D, QDense, quantized_bits
# Custom modules
from utils.telescope import telescopeMSE8x8 # If you need to reference the loss
from utils.utils import ArgumentParser, save_CMSSW_models
import utils.graph as graph # For writing frozen graphs
##############################################################################
# Argument Parsing
##############################################################################
p = ArgumentParser()
p.add_args(
('--mname', p.STR),
('--mpath', p.STR),
('--model_per_eLink', p.STORE_TRUE),
('--model_per_bit_config', p.STORE_TRUE),
('--alloc_geom', p.STR),
('--load_from_scan', p.STORE_TRUE),
('--specific_m', p.INT),
)
args = p.parse_args()
##############################################################################
# Global Configuration
##############################################################################
# Destination directory for the CMSSW-friendly models
loading_dir = os.path.join(args.mpath, 'training_models')
saving_dir = os.path.join(args.mpath,'CMSSW_models')
if not os.path.exists(args.mpath):
os.system("mkdir -p " + loading_dir)
os.system("mkdir -p " + saving_dir)
os.system("mkdir -p " + args.mpath)
# If you map eLinks to bits, set them up here
bitsPerOutputLink = [
0, 1, 3, 5, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
]
# Decide which model(s) to process
if args.specific_m is not None:
all_models = [args.specific_m]
elif args.model_per_eLink:
if args.alloc_geom == 'old':
all_models = [2, 3, 4, 5]
elif args.alloc_geom == 'new':
all_models = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
elif args.model_per_bit_config:
if args.alloc_geom == 'old':
all_models = [3, 5, 7, 9]
elif args.alloc_geom == 'new':
all_models = [1, 3, 5, 7, 9]
else:
all_models = []
##############################################################################
# Process Each Model (Depending on eLinks or Bits)
##############################################################################
for m in all_models:
if args.model_per_eLink:
eLinks = m
bitsPerOutput = bitsPerOutputLink[eLinks]
print(f"Preparing Model with {eLinks} eLinks")
out_subdir = os.path.join(saving_dir, f"model_{eLinks}_eLinks")
elif args.model_per_bit_config:
bitsPerOutput = m
print(f"Preparing Model with {bitsPerOutput} output bits")
out_subdir = os.path.join(saving_dir, f"model_{bitsPerOutput}_bits")
else:
# Should not happen if you always specify one or the other
continue
if not os.path.exists(out_subdir):
os.system("mkdir -p " + out_subdir)
# Setup fixed parameters
batch = 1
nIntegerBits = 1
nDecimalBits = bitsPerOutput - nIntegerBits
outputSaturationValue = (1 << nIntegerBits) - 1. / (1 << nDecimalBits) if bitsPerOutput > 0 else 1
maxBitsPerOutput = 9
outputMaxIntSize = 1
if bitsPerOutput > 0:
outputMaxIntSize = (1 << nDecimalBits)
outputMaxIntSizeGlobal = 1
if maxBitsPerOutput > 0:
outputMaxIntSizeGlobal = (1 << (maxBitsPerOutput - nIntegerBits))
# Model layout hyperparameters
n_kernels = 8
n_encoded = 16
conv_weightBits = 6
conv_biasBits = 6
dense_weightBits = 6
dense_biasBits = 6
encodedBits = 9
CNN_kernel_size = 3
padding = tf.constant([[0, 0], [0, 1], [0, 1], [0, 0]])
##########################################################################
# Build Encoder
##########################################################################
input_enc = keras.Input(batch_shape=(batch, 8, 8, 1), name='Wafer')
# Quantize input: 8 bits, 1 integer bit
x = QActivation(quantized_bits(bits=8, integer=1), name='input_quantization')(input_enc)
# Pad to (9x9)
x = tf.pad(x, padding, mode='CONSTANT', constant_values=0, name=None)
# Convolution
x = QConv2D(
n_kernels,
CNN_kernel_size,
strides=2,
padding='valid',
kernel_quantizer=quantized_bits(bits=conv_weightBits, integer=0, keep_negative=1, alpha=1),
bias_quantizer=quantized_bits(bits=conv_biasBits, integer=0, keep_negative=1, alpha=1),
name="conv2d"
)(x)
# Activation
x = QActivation(
quantized_bits(bits=8, integer=1),
name='act'
)(x)
# Flatten and Dense
x = Flatten()(x)
x = QDense(
n_encoded,
kernel_quantizer=quantized_bits(bits=dense_weightBits, integer=0, keep_negative=1, alpha=1),
bias_quantizer=quantized_bits(bits=dense_biasBits, integer=0, keep_negative=1, alpha=1),
name="dense"
)(x)
# Quantize latent (9 bits, 1 integer bit)
x = QActivation(
quantized_bits(bits=encodedBits, integer=1),
name='latent_quantization'
)(x)
# Apply floor+min for bit saturation if needed
if bitsPerOutput > 0 and maxBitsPerOutput > 0:
x_floor = tf.math.floor(x * outputMaxIntSize)
x = tf.minimum(x_floor / outputMaxIntSize, outputSaturationValue)
encoder = keras.Model([input_enc], x, name="encoder")
##########################################################################
# Build Decoder
##########################################################################
input_dec = keras.Input(batch_shape=(batch, 24))
y = Dense(24)(input_dec)
y = ReLU()(y)
y = Dense(64)(y)
y = ReLU()(y)
y = Dense(128)(y)
y = ReLU()(y)
y = Reshape((4, 4, 8))(y)
y = Conv2DTranspose(1, (3, 3), strides=(2, 2), padding='valid')(y)
# Slice to (8x8)
y = y[:, 0:8, 0:8]
y = ReLU()(y)
decoder = keras.Model([input_dec], y, name="decoder")
##########################################################################
# Load Weights
##########################################################################
# If loading from a scanning checkpoint
if args.model_per_eLink:
encoder_path = os.path.join(loading_dir, f"model_{m}_eLinks", "best-encoder-epoch.tf")
decoder_path = os.path.join(loading_dir, f"model_{m}_eLinks", "best-decoder-epoch.tf")
elif args.model_per_bit_config:
encoder_path = os.path.join(loading_dir, f"model_{m}_bits", "best-encoder-epoch.tf")
decoder_path = os.path.join(loading_dir, f"model_{m}_bits", "best-decoder-epoch.tf")
encoder.load_weights(encoder_path)
decoder.load_weights(decoder_path)
print(f"Loaded model weights for model_{m}")
# Compile dummy model to finalize graph (optional for QKeras usage)
loss = telescopeMSE8x8 # if you need the same telescope loss
opt = tf.keras.optimizers.Adam(learning_rate=0.1, weight_decay=0.000025)
encoder.compile(optimizer=opt, loss=loss)
decoder.compile(optimizer=opt, loss=loss)
##########################################################################
# Save Frozen Graphs for CMSSW
##########################################################################
save_CMSSW_models(encoder, decoder, out_subdir, args.mname, isQK=True)