instmodel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
instmodel/model.py ADDED
@@ -0,0 +1,872 @@
1
+ #!/usr/bin/env python3
2
+ import numpy as np
3
+ from scipy.stats import kendalltau
4
+ import pandas as pd
5
+ from abc import ABC, abstractmethod
6
+ from typing import Optional
7
+ import json
8
+
9
+ import tensorflow as tf
10
+ from keras.models import Model
11
+ from keras import layers
12
+
13
+ from .instruction_model import (
14
+ instruction_model_inference,
15
+ )
16
+
17
+
18
+ def create_stair_structure(
19
+ features_len: int,
20
+ hidden_sizes: Optional[list[int]] = None,
21
+ use_batch_norm: bool = False,
22
+ ):
23
+ """
24
+ Creates a stair-structured model graph with an optional batch normalization layer.
25
+
26
+ Args:
27
+ features_len: The dimensionality of the input features.
28
+ hidden_sizes: A list of hidden layer sizes. Defaults to [14, 12, 10] if None.
29
+ use_batch_norm: Whether to apply batch normalization on the input.
30
+
31
+ Returns:
32
+ A tuple (model_graph, list_of_keras_layers).
33
+ """
34
+ hidden_sizes = hidden_sizes or [14, 12, 10]
35
+ input_layer = InputBuffer(features_len)
36
+ normalizer_layer = NormalizationComputation()
37
+ layers, current_buffer = (
38
+ ([normalizer_layer], normalizer_layer(input_layer))
39
+ if use_batch_norm
40
+ else ([], input_layer)
41
+ )
42
+
43
+ buffers = [current_buffer]
44
+
45
+ for size in hidden_sizes:
46
+ mid_layer = Dense(size, activation="relu")
47
+ mid_buffer = mid_layer(current_buffer)
48
+ layers.append(mid_layer)
49
+ buffers.append(mid_buffer)
50
+ current_buffer = mid_buffer
51
+
52
+ current_buffer = (
53
+ Concatenate()(buffers) if len(buffers) > 1 else current_buffer
54
+ )
55
+ model = ModelGraph(input_layer, current_buffer)
56
+
57
+ return model, [layer.keras_layer for layer in layers]
58
+
59
+
60
+ NO_BATCH_NORM = 0
61
+ INPLACE = 1
62
+ NOT_INPLACE = 2
63
+
64
+
65
+ def ff_model(sizes: list[int], use_batch_norm: int = 0, activations=None):
66
+ """
67
+ Builds a feed-forward model graph based on provided layer sizes and activations.
68
+
69
+ Args:
70
+ sizes: A list with structure [features_len, hidden_layer_sizes..., last_layer_size].
71
+ use_batch_norm: Batch normalization mode (NO_BATCH_NORM, INPLACE, or NOT_INPLACE).
72
+ activations: Activation functions for each layer. Can be a list or a shorthand string.
73
+
74
+ Returns:
75
+ A ModelGraph representing the feed-forward network.
76
+ """
77
+ features_len, *hidden_sizes, last_layer_size = sizes
78
+ if activations is None:
79
+ activations = ["relu"] * len(hidden_sizes) + ["sigmoid"]
80
+ elif len(activations) != len(hidden_sizes) + 1:
81
+ raise ValueError(
82
+ "The number of activations must match the number of hidden layers + 1."
83
+ )
84
+
85
+ if isinstance(activations, str):
86
+ activation_map = {
87
+ "r": "relu",
88
+ "s": "sigmoid",
89
+ "t": "tanh",
90
+ "l": None,
91
+ }
92
+ activations = [activation_map[activation] for activation in activations]
93
+
94
+ input_layer = InputBuffer(features_len)
95
+ current_buffer = (
96
+ NormalizationComputation(in_place=use_batch_norm == INPLACE)(input_layer)
97
+ if use_batch_norm != NO_BATCH_NORM
98
+ else input_layer
99
+ )
100
+ for size in hidden_sizes:
101
+ current_buffer = Dense(size, activation=activations.pop(0))(
102
+ current_buffer
103
+ )
104
+
105
+ dense = Dense(last_layer_size, activation=activations.pop(0))(
106
+ current_buffer
107
+ )
108
+ model = ModelGraph(input_layer, dense)
109
+
110
+ return model
111
+
112
+
113
+ def generate_validation_data(
114
+ features: list[str],
115
+ model: Model,
116
+ means=None,
117
+ stds=None,
118
+ ):
119
+ """
120
+ Generates validation data by creating random inputs and obtaining the model outputs.
121
+
122
+ Args:
123
+ features: List of feature names.
124
+ model: A Keras model instance used for inference.
125
+ means: Optional mean values to add to the inputs.
126
+ stds: Optional standard deviation values to scale the inputs.
127
+
128
+ Returns:
129
+ A dictionary with keys "inputs" and "expected_outputs".
130
+ """
131
+ input_data = np.random.randn(10, len(features)).astype(np.float32)
132
+
133
+ if stds is not None:
134
+ input_data = input_data * (np.array(stds) + 1e-6)
135
+ if means is not None:
136
+ input_data = input_data + np.array(means)
137
+
138
+ output_data = model.predict_on_batch(input_data)
139
+
140
+ return {
141
+ "inputs": input_data.tolist(),
142
+ "expected_outputs": output_data.tolist(),
143
+ }
144
+
145
+
146
+ def tau_compare(predictions, y_data):
147
+ """
148
+ Computes Kendall's Tau-b correlation for each output column.
149
+
150
+ Args:
151
+ predictions: The model predictions as a NumPy array.
152
+ y_data: The ground truth values as a NumPy array.
153
+
154
+ Returns:
155
+ A list of Tau-b scores if multiple columns are present; otherwise a single float.
156
+ """
157
+ n_samples, n_cols = y_data.shape
158
+ results = []
159
+
160
+ for col in range(n_cols):
161
+ # Extract the predictions and ground truth for the current column.
162
+ pred_col = predictions[:, col]
163
+ y_col = y_data[:, col]
164
+
165
+ tau, p_value = kendalltau(pred_col, y_col)
166
+ if np.isnan(tau):
167
+ tau = 0.0
168
+
169
+ results.append(tau)
170
+
171
+ return results if len(results) > 1 else results[0]
172
+
173
+
174
+ def score_selection(model, x_data, y_data):
175
+ """
176
+ Computes Kendall's Tau-b for each output column of y_data.
177
+
178
+ Args:
179
+ model: A model with a .predict() method or an object processed via instruction_model_inference.
180
+ x_data: Input feature data.
181
+ y_data: Ground truth labels (NumPy array, pd.Series, or pd.DataFrame).
182
+
183
+ Returns:
184
+ A list of Tau-b correlation scores, or a single float if y_data has one column.
185
+ """
186
+ if isinstance(y_data, (pd.DataFrame, pd.Series)):
187
+ y_data = y_data.to_numpy()
188
+
189
+ if y_data.ndim == 1:
190
+ y_data = y_data.reshape(-1, 1)
191
+
192
+ if hasattr(model, "predict"):
193
+ predictions = model.predict(x_data)
194
+ else:
195
+ predictions = instruction_model_inference(model, x_data)[-1]
196
+
197
+ if isinstance(predictions, (pd.DataFrame, pd.Series)):
198
+ predictions = predictions.to_numpy()
199
+
200
+ if predictions.ndim == 1:
201
+ predictions = predictions.reshape(-1, 1)
202
+
203
+ return tau_compare(predictions, y_data)
204
+
205
+
206
+ ###############################################################################
207
+ # 1. DataBuffer Classes
208
+ ###############################################################################
209
+ class DataBuffer:
210
+ """
211
+ A container for a Keras tensor that also tracks the operation (op) that produced it,
212
+ and maintains a list of input DataBuffers used for that operation.
213
+ """
214
+
215
+ def __init__(self, tensor, op=None, inputs=None):
216
+ self.tensor = tensor # The underlying Keras (symbolic) tensor.
217
+ self.op = op # The ComputationOp that produced this buffer (if any).
218
+ self.inputs = inputs if inputs is not None else [] # Always a list.
219
+
220
+ def __repr__(self):
221
+ return f"DataBuffer(shape={self.tensor.shape})"
222
+
223
+ @property
224
+ def os(self):
225
+ """
226
+ Shortcut for output size.
227
+ Assumes the tensor shape is (None, output_size) and returns that output_size.
228
+ """
229
+ return int(self.tensor.shape[-1])
230
+
231
+ def __getitem__(self, key):
232
+ """
233
+ Overloads indexing for DataBuffer. When a list, tuple, slice, or np.ndarray is
234
+ provided as key, it automatically creates a CopyMaskedComputation layer to select
235
+ the corresponding columns.
236
+
237
+ Example:
238
+ input_buffer = InputBuffer(3)
239
+ sliced = input_buffer[[2, 0]]
240
+ """
241
+ if isinstance(key, int):
242
+ indexes = [key]
243
+ elif isinstance(key, slice):
244
+ indexes = list(range(self.os))[key]
245
+ elif isinstance(key, (list, tuple, np.ndarray)):
246
+ indexes = list(key)
247
+ else:
248
+ raise TypeError("Unsupported index type for DataBuffer: " + str(type(key)))
249
+ return CopyMaskedComputation(indexes)([self])
250
+
251
+
252
+ class InputBuffer(DataBuffer):
253
+ """
254
+ Wraps a Keras Input. Can be instantiated with an integer (e.g. InputBuffer(15))
255
+ or a tuple (e.g. InputBuffer((15,))).
256
+ """
257
+
258
+ def __init__(self, shape_or_os, name=None):
259
+ if isinstance(shape_or_os, int):
260
+ shape = (shape_or_os,)
261
+ else:
262
+ shape = shape_or_os
263
+ inp = layers.Input(shape=shape, name=name)
264
+ super().__init__(inp, op=None, inputs=[])
265
+ self.shape = shape # MODIFIED: Store the input shape explicitly.
266
+
267
+ def __repr__(self):
268
+ return f"InputBuffer(shape={self.shape})"
269
+
270
+ def __call__(self, *args, **kwargs):
271
+ return self
272
+
273
+
274
+ ###############################################################################
275
+ # 2. ComputationOp Base Class and Existing Ops
276
+ ###############################################################################
277
+ class ComputationOp(ABC):
278
+ """
279
+ Base class for operations that wrap Keras layers.
280
+ Every op's __call__ expects a list of DataBuffer objects.
281
+ """
282
+
283
+ def __init__(self):
284
+ self.keras_layer = None
285
+
286
+ @abstractmethod # MODIFIED: Mark __call__ as an abstract method.
287
+ def __call__(self, inputs):
288
+ if not isinstance(inputs, list):
289
+ inputs = [inputs]
290
+ raise NotImplementedError("Subclasses must implement __call__.")
291
+
292
+ @abstractmethod # MODIFIED: Mark compile_instructions as an abstract method.
293
+ def compile_instructions(
294
+ self, input_indices, weights_visited, model_structure
295
+ ) -> int:
296
+ raise NotImplementedError("Subclasses must implement compile_instructions.")
297
+
298
+
299
+ class ActivationComputation(ComputationOp):
300
+ """
301
+ An activation operation that applies an activation function elementwise.
302
+ For "RELU", "SIGMOID", "TANH", "SOFTMAX", the native Keras Activation layer is used.
303
+ For custom activations (e.g., "SQRT", "LOG", "LOG10", "INVERSE"), a layers.Lambda layer is created.
304
+
305
+ The compile_instructions method creates an in-place activation instruction.
306
+ """
307
+
308
+ def __init__(self, activation, in_place=True, name: Optional[str] = None):
309
+ super().__init__()
310
+ self.in_place = in_place
311
+ self.activation = activation.upper()
312
+ self.name = name
313
+ if self.activation in {"RELU", "SIGMOID", "TANH", "SOFTMAX"}:
314
+ self.keras_layer = tf.keras.layers.Activation(
315
+ self.activation.lower(), name=name
316
+ )
317
+ elif self.activation == "SQRT":
318
+ self.keras_layer = layers.Lambda(
319
+ lambda x: tf.where(x > 0, tf.sqrt(x), tf.zeros_like(x)), name=name
320
+ )
321
+ elif self.activation == "LOG":
322
+ self.keras_layer = layers.Lambda(
323
+ lambda x: tf.math.log(tf.maximum(x, 0) + 1), name=name
324
+ )
325
+ elif self.activation == "LOG10":
326
+ self.keras_layer = layers.Lambda(
327
+ lambda x: tf.math.log(tf.maximum(x, 0) + 1) / tf.math.log(10.0),
328
+ name=name,
329
+ )
330
+ elif self.activation == "INVERSE":
331
+ self.keras_layer = layers.Lambda(lambda x: 1 - x, name=name)
332
+ else:
333
+ raise ValueError(f"Unexpected activation: {self.activation}")
334
+
335
+ def __call__(self, inputs):
336
+ if not isinstance(inputs, list):
337
+ inputs = [inputs]
338
+ y = self.keras_layer(inputs[0].tensor)
339
+ return DataBuffer(y, op=self, inputs=inputs)
340
+
341
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
342
+ if len(input_indices) != 1:
343
+ raise ValueError("ActivationComputation expects exactly one input.")
344
+ # The activation operation is in-place in the instruction model.
345
+
346
+ if self.in_place:
347
+ target_index = input_indices[0]
348
+ else:
349
+ output_index = len(model_structure["buffer_sizes"])
350
+ model_structure["buffer_sizes"].append(
351
+ model_structure["buffer_sizes"][input_indices[0]]
352
+ )
353
+ copy_instr = {
354
+ "type": "COPY",
355
+ "input": input_indices[0],
356
+ "output": output_index,
357
+ "internal_index": 0,
358
+ }
359
+ model_structure["instructions"].append(copy_instr)
360
+ target_index = output_index
361
+
362
+ instr = {
363
+ "type": "ACTIVATION",
364
+ "input": target_index,
365
+ "activation": self.activation,
366
+ }
367
+ model_structure["instructions"].append(instr)
368
+ return target_index
369
+
370
+
371
+ class Dense(ComputationOp):
372
+ """
373
+ A dense operation that expects its input as a one-element list.
374
+ Shared weights are stored so that repeated calls reuse the same weight index.
375
+ """
376
+
377
+ def __init__(self, output_size, activation=None, name=None):
378
+ super().__init__()
379
+ self.input_size = None
380
+ self.output_size = output_size
381
+ self.activation = activation
382
+ self.keras_layer = layers.Dense(output_size, activation=activation, name=name)
383
+
384
+ def __call__(self, inputs):
385
+ if not isinstance(inputs, list):
386
+ inputs = [inputs]
387
+ if len(inputs) != 1:
388
+ raise ValueError("Dense expects exactly one input in the list.")
389
+ input_tensor = inputs[0].tensor
390
+ output_tensor = self.keras_layer(input_tensor)
391
+ if self.input_size is None:
392
+ self.input_size = int(input_tensor.shape[-1])
393
+ return DataBuffer(output_tensor, op=self, inputs=inputs)
394
+
395
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
396
+ if len(input_indices) != 1:
397
+ raise ValueError(
398
+ "Dense.compile_instructions expects one input index."
399
+ )
400
+ wv = weights_visited["weights"]
401
+ if id(self) not in wv:
402
+ wv[id(self)] = len(model_structure["weights"])
403
+ weights, bias = self.keras_layer.get_weights()
404
+ model_structure["weights"].append(
405
+ weights.T.tolist()
406
+ ) # stored as (output, input)
407
+ model_structure["bias"].append(bias.tolist())
408
+
409
+ output_index = len(model_structure["buffer_sizes"])
410
+ model_structure["buffer_sizes"].append(self.output_size)
411
+ instr = {
412
+ "type": "DOT",
413
+ "input": input_indices[0],
414
+ "output": output_index,
415
+ "weights": wv[id(self)],
416
+ }
417
+ if self.activation is not None:
418
+ instr["activation"] = self.activation.upper()
419
+ model_structure["instructions"].append(instr)
420
+ return output_index
421
+
422
+
423
+ class CopyMaskedComputation(ComputationOp):
424
+ """
425
+ A copy-masked operation that selects specific columns from the input.
426
+ """
427
+
428
+ def __init__(self, indexes, name=None):
429
+ super().__init__()
430
+ self.indexes = indexes # List of column indices to select.
431
+ self.name = name
432
+
433
+ def __call__(self, inputs):
434
+ if not isinstance(inputs, list):
435
+ inputs = [inputs]
436
+ indices_tensor = tf.constant(self.indexes, dtype=tf.int32)
437
+ gather_layer = layers.Lambda(lambda x: tf.gather(x, indices=indices_tensor, axis=1))
438
+ output_tensor = gather_layer(inputs[0].tensor)
439
+ return DataBuffer(output_tensor, op=self, inputs=inputs)
440
+
441
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
442
+ output_index = len(model_structure["buffer_sizes"])
443
+ model_structure["buffer_sizes"].append(len(self.indexes))
444
+ instr = {
445
+ "type": "COPY_MASKED",
446
+ "input": input_indices[0],
447
+ "output": output_index,
448
+ "indexes": self.indexes,
449
+ }
450
+ model_structure["instructions"].append(instr)
451
+
452
+ return output_index
453
+
454
+
455
+ class Concatenate(ComputationOp):
456
+ """
457
+ A concatenation operation that takes a list of inputs and concatenates them along the last axis.
458
+ """
459
+
460
+ def __init__(self, axis=-1, name=None):
461
+ super().__init__()
462
+ self.axis = axis
463
+ self.keras_layer = layers.Concatenate(axis=axis, name=name)
464
+
465
+ def __call__(self, inputs):
466
+ if not isinstance(inputs, list):
467
+ inputs = [inputs]
468
+ tensors = [inp.tensor for inp in inputs]
469
+ output_tensor = self.keras_layer(tensors)
470
+ return DataBuffer(output_tensor, op=self, inputs=inputs)
471
+
472
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
473
+ offsets = []
474
+ total_size = 0
475
+ for idx in input_indices:
476
+ offsets.append(total_size)
477
+ total_size += model_structure["buffer_sizes"][idx]
478
+
479
+ output_index = len(model_structure["buffer_sizes"])
480
+ model_structure["buffer_sizes"].append(total_size)
481
+
482
+ for idx, offset in zip(input_indices, offsets):
483
+ instr = {
484
+ "type": "COPY",
485
+ "input": idx,
486
+ "output": output_index,
487
+ "internal_index": offset,
488
+ }
489
+ model_structure["instructions"].append(instr)
490
+ return output_index
491
+
492
+
493
+ class NormalizationComputation(ComputationOp):
494
+ """
495
+ A normalization operation that wraps a BatchNormalization layer.
496
+ """
497
+
498
+ def __init__(self, in_place=True, center=True, scale=True, epsilon=1e-3, name=None):
499
+ super().__init__()
500
+ self.in_place = in_place
501
+ self.epsilon = epsilon
502
+ self.keras_layer = tf.keras.layers.BatchNormalization(
503
+ epsilon=epsilon, center=center, scale=scale, axis=-1, name=name
504
+ )
505
+
506
+ def __call__(self, inputs):
507
+ if not isinstance(inputs, list):
508
+ inputs = [inputs]
509
+ new_tensor = self.keras_layer(inputs[0].tensor)
510
+ return DataBuffer(new_tensor, op=self, inputs=inputs)
511
+
512
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
513
+ weights = self.keras_layer.get_weights()
514
+
515
+ if len(weights) == 4:
516
+ gamma, beta, moving_mean, moving_variance = weights
517
+ elif len(weights) == 3:
518
+ if self.keras_layer.center and not self.keras_layer.scale:
519
+ beta, moving_mean, moving_variance = weights
520
+ gamma = np.ones_like(moving_mean)
521
+ else:
522
+ gamma, moving_mean, moving_variance = weights
523
+ beta = np.zeros_like(gamma)
524
+ elif len(weights) == 2:
525
+ moving_mean, moving_variance = weights
526
+ gamma = np.ones_like(moving_mean)
527
+ beta = np.zeros_like(moving_mean)
528
+ else:
529
+ raise ValueError(
530
+ f"Unexpected number of BN weights returned: {len(weights)}. "
531
+ "Check your 'center' and 'scale' arguments."
532
+ )
533
+
534
+ epsilon = self.epsilon
535
+ std = gamma / np.sqrt(moving_variance + epsilon)
536
+ center = -moving_mean
537
+
538
+ pw = weights_visited["parameters"]
539
+ if self.in_place:
540
+ target_index = input_indices[0]
541
+ else:
542
+ output_index = len(model_structure["buffer_sizes"])
543
+ model_structure["buffer_sizes"].append(
544
+ model_structure["buffer_sizes"][input_indices[0]]
545
+ )
546
+ copy_instr = {
547
+ "type": "COPY",
548
+ "input": input_indices[0],
549
+ "output": output_index,
550
+ "internal_index": 0,
551
+ }
552
+ model_structure["instructions"].append(copy_instr)
553
+ target_index = output_index
554
+
555
+ if id(self) not in pw:
556
+ pw[id(self)] = [len(model_structure["parameters"]) + i for i in range(3)]
557
+ model_structure["parameters"].append(center.tolist())
558
+ model_structure["parameters"].append(std.tolist())
559
+ model_structure["parameters"].append(beta.tolist())
560
+
561
+ instr_center = {
562
+ "type": "ADD_ELEMENTWISE",
563
+ "input": target_index,
564
+ "parameters": pw[id(self)][0],
565
+ }
566
+ instr_mul = {
567
+ "type": "MUL_ELEMENTWISE",
568
+ "input": target_index,
569
+ "parameters": pw[id(self)][1],
570
+ }
571
+ instr_add = {
572
+ "type": "ADD_ELEMENTWISE",
573
+ "input": target_index,
574
+ "parameters": pw[id(self)][2],
575
+ }
576
+
577
+ model_structure["instructions"].append(instr_center)
578
+ model_structure["instructions"].append(instr_mul)
579
+ model_structure["instructions"].append(instr_add)
580
+
581
+ return target_index
582
+
583
+
584
+ class Attention(ComputationOp):
585
+ """
586
+ An attention operation that computes softmax attention from a key buffer and applies it
587
+ elementwise to a target buffer.
588
+ """
589
+
590
+ def __init__(self, name=None):
591
+ super().__init__()
592
+ self.a = None # Target dimension (and output dimension)
593
+ self.b = None # Key dimension
594
+ self.keras_layer = None
595
+ self.name = name
596
+
597
+ def __call__(self, inputs):
598
+ if not isinstance(inputs, list) or len(inputs) != 2:
599
+ raise ValueError("Attention expects two inputs: [target, key]")
600
+ target, key = inputs
601
+
602
+ if self.keras_layer is None:
603
+ self.a = target.os
604
+ self.b = key.os
605
+ self.keras_layer = layers.Dense(self.a, name=self.name, activation="softmax")
606
+
607
+ softmaxed = self.keras_layer(key.tensor)
608
+ result_tensor = target.tensor * softmaxed
609
+ return DataBuffer(result_tensor, op=self, inputs=inputs)
610
+
611
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
612
+ wv = weights_visited["weights"]
613
+ if id(self) not in wv:
614
+ wv[id(self)] = len(model_structure["weights"])
615
+ weights, bias = self.keras_layer.get_weights()
616
+ model_structure["weights"].append(weights.T.tolist())
617
+ model_structure["bias"].append(bias.tolist())
618
+
619
+ output_index = len(model_structure["buffer_sizes"])
620
+ model_structure["buffer_sizes"].append(self.a)
621
+
622
+ instr = {
623
+ "type": "ATTENTION",
624
+ "input": input_indices[0],
625
+ "key": input_indices[1],
626
+ "output": output_index,
627
+ "weights": wv[id(self)],
628
+ }
629
+ model_structure["instructions"].append(instr)
630
+ return output_index
631
+
632
+
633
+ class Add(ComputationOp):
634
+ """
635
+ An elementwise addition operation that adds a list of input DataBuffers.
636
+ Internally it uses keras.layers.Add.
637
+ For instruction purposes, the instruction type is "ADD_ELEMENTWISE_BUFFERS"
638
+ and the input field is a list of buffer indices.
639
+ """
640
+
641
+ def __init__(self, name: Optional[str] = None):
642
+ super().__init__()
643
+ self.name = name
644
+ self.keras_layer = tf.keras.layers.Add(name=name)
645
+
646
+ def __call__(self, inputs):
647
+ if not isinstance(inputs, list):
648
+ inputs = [inputs]
649
+ # Gather the underlying tensors from each DataBuffer.
650
+ tensors = [inp.tensor for inp in inputs]
651
+ output_tensor = self.keras_layer(tensors)
652
+ return DataBuffer(output_tensor, op=self, inputs=inputs)
653
+
654
+ def compile_instructions(
655
+ self, input_indices, weights_visited, model_structure
656
+ ) -> int:
657
+ if not input_indices:
658
+ raise ValueError("Add expects at least one input.")
659
+ # Assume all input buffers have the same output size.
660
+ out_size = model_structure["buffer_sizes"][input_indices[0]]
661
+ for idx in input_indices:
662
+ if model_structure["buffer_sizes"][idx] != out_size:
663
+ raise ValueError("All inputs must have the same size for addition.")
664
+ output_index = len(model_structure["buffer_sizes"])
665
+ model_structure["buffer_sizes"].append(out_size)
666
+ instr = {
667
+ "type": "ADD_ELEMENTWISE_BUFFERS",
668
+ "input": input_indices, # Note: the entire list of input indices is provided.
669
+ "output": output_index,
670
+ }
671
+ model_structure["instructions"].append(instr)
672
+ return output_index
673
+
674
+
675
+ class Multiply(ComputationOp):
676
+ """
677
+ An elementwise multiplication operation that multiplies a list of input DataBuffers.
678
+ Internally it uses keras.layers.Multiply.
679
+ For instruction purposes, the instruction type is "MULTIPLY_ELEMENTWISE_BUFFERS"
680
+ and the input field is a list of buffer indices.
681
+ """
682
+
683
+ def __init__(self, name: Optional[str] = None):
684
+ super().__init__()
685
+ self.name = name
686
+ self.keras_layer = tf.keras.layers.Multiply(name=name)
687
+
688
+ def __call__(self, inputs):
689
+ if not isinstance(inputs, list) or len(inputs) < 2:
690
+ raise ValueError("Multiply expects at least two inputs.")
691
+ tensors = [inp.tensor for inp in inputs]
692
+ output_tensor = self.keras_layer(tensors)
693
+ return DataBuffer(output_tensor, op=self, inputs=inputs)
694
+
695
+ def compile_instructions(
696
+ self, input_indices, weights_visited, model_structure
697
+ ) -> int:
698
+ if not input_indices:
699
+ raise ValueError("Multiply expects at least one input.")
700
+ out_size = model_structure["buffer_sizes"][input_indices[0]]
701
+ for idx in input_indices:
702
+ if model_structure["buffer_sizes"][idx] != out_size:
703
+ raise ValueError(
704
+ "All inputs must have the same size for multiplication."
705
+ )
706
+ output_index = len(model_structure["buffer_sizes"])
707
+ model_structure["buffer_sizes"].append(out_size)
708
+ instr = {
709
+ "type": "MULTIPLY_ELEMENTWISE_BUFFERS",
710
+ "input": input_indices,
711
+ "output": output_index,
712
+ }
713
+ model_structure["instructions"].append(instr)
714
+ return output_index
715
+
716
+
717
+ ###############################################################################
718
+ # 3. ModelGraph and Instruction Model Compilation
719
+ ###############################################################################
720
+ class ModelGraph(ComputationOp):
721
+ """
722
+ Holds the connection between an array of input DataBuffers and an output DataBuffer,
723
+ along with references to all internal Keras layers (for training).
724
+ """
725
+
726
+ def __init__(self, input_buffers, output_buffer: DataBuffer, name=None):
727
+ super().__init__()
728
+ if not isinstance(input_buffers, list):
729
+ input_buffers = [input_buffers]
730
+ self.input_buffers = input_buffers
731
+ self.output_buffer = output_buffer
732
+ self._keras_model = Model(
733
+ inputs=[buf.tensor for buf in self.input_buffers],
734
+ outputs=self.output_buffer.tensor,
735
+ name=name,
736
+ )
737
+
738
+ @property
739
+ def os(self):
740
+ """
741
+ Shortcut for output size.
742
+ """
743
+ return self.output_buffer.os
744
+
745
+ def get_keras(self):
746
+ return self._keras_model
747
+
748
+ def compile(self, *args, **kwargs):
749
+ return self._keras_model.compile(*args, **kwargs)
750
+
751
+ def fit(self, *args, **kwargs):
752
+ return self._keras_model.fit(*args, **kwargs)
753
+
754
+ def predict(self, *args, **kwargs):
755
+ return self._keras_model.predict(*args, **kwargs)
756
+
757
+ def predict_on_batch(self, *args, **kwargs):
758
+ return self._keras_model.predict_on_batch(*args, **kwargs)
759
+
760
+ def summary(self):
761
+ return self._keras_model.summary()
762
+
763
+ def __call__(self, inputs):
764
+ if not isinstance(inputs, list):
765
+ inputs = [inputs]
766
+ out_tensor = self._keras_model([inp.tensor for inp in inputs])
767
+ return DataBuffer(out_tensor, op=self, inputs=inputs)
768
+
769
+ def compile_instructions(self, input_indices, weights_visited, model_structure):
770
+ visited = {}
771
+
772
+ def traverse(buffer: DataBuffer):
773
+ if id(buffer) in visited:
774
+ return visited[id(buffer)]
775
+ if isinstance(buffer, InputBuffer):
776
+ index = self.input_buffers.index(buffer)
777
+ idx = input_indices[index]
778
+ visited[id(buffer)] = idx
779
+ return idx
780
+ elif buffer.op is None:
781
+ idx = len(model_structure["buffer_sizes"])
782
+ model_structure["buffer_sizes"].append(buffer.os)
783
+ visited[id(buffer)] = idx
784
+ return idx
785
+ else:
786
+ input_idx = [traverse(inp) for inp in buffer.inputs]
787
+
788
+ idx = buffer.op.compile_instructions(
789
+ input_idx, weights_visited, model_structure
790
+ )
791
+ visited[id(buffer)] = idx
792
+ return idx
793
+
794
+ traverse(self.output_buffer)
795
+ return visited[id(self.output_buffer)]
796
+
797
+ def create_instruction_model(self, weights_visited=None, features=None):
798
+ model_structure = {
799
+ "features": features or [],
800
+ "buffer_sizes": [],
801
+ "instructions": [],
802
+ "maps": [],
803
+ "weights": [],
804
+ "bias": [],
805
+ "parameters": [],
806
+ }
807
+
808
+ if weights_visited is None:
809
+ weights_visited = {
810
+ "weights": {},
811
+ "parameters": {},
812
+ "maps": {},
813
+ }
814
+
815
+ visited = {}
816
+ for input_buffer in self.input_buffers:
817
+ if id(input_buffer) not in visited:
818
+ idx = len(model_structure["buffer_sizes"])
819
+ model_structure["buffer_sizes"].append(
820
+ int(input_buffer.tensor.shape[-1])
821
+ )
822
+ visited[id(input_buffer)] = idx
823
+
824
+ def traverse(buffer: DataBuffer):
825
+ if id(buffer) in visited:
826
+ return visited[id(buffer)]
827
+ if buffer.op is None:
828
+ idx = len(model_structure["buffer_sizes"])
829
+ model_structure["buffer_sizes"].append(int(buffer.tensor.shape[-1]))
830
+ visited[id(buffer)] = idx
831
+ return idx
832
+ else:
833
+ input_indices = [traverse(inp) for inp in buffer.inputs]
834
+ idx = buffer.op.compile_instructions(
835
+ input_indices, weights_visited, model_structure
836
+ )
837
+ visited[id(buffer)] = idx
838
+ return idx
839
+
840
+ traverse(self.output_buffer)
841
+
842
+ for input_buffer in self.input_buffers:
843
+ if id(input_buffer) not in visited:
844
+ raise ValueError(f"Input buffer {input_buffer} was not visited.")
845
+
846
+ return model_structure
847
+
848
+
849
+ def create_model_graph(inputs, output: DataBuffer) -> ModelGraph:
850
+ if not isinstance(inputs, list):
851
+ inputs = [inputs]
852
+ return ModelGraph(inputs, output)
853
+
854
+
855
+ def create_instruction_model(inputs, output: DataBuffer):
856
+ return create_model_graph(inputs, output).create_instruction_model()
857
+
858
+
859
+ def validate_keras_model(keras_model, validation_data):
860
+ """
861
+ Validates the Keras model using provided validation data.
862
+ """
863
+ x_val = np.array(validation_data["inputs"])
864
+ y_expected = np.array(validation_data["expected_outputs"])
865
+ y_pred = keras_model.predict(x_val)
866
+ if np.allclose(y_expected, y_pred, atol=1e-6):
867
+ print("Keras model validation successful: predictions match expected outputs.")
868
+ else:
869
+ print("Keras model validation failed.")
870
+ print("Expected outputs:", y_expected)
871
+ print("Predictions:", y_pred)
872
+ raise AssertionError("Keras model validation failed.")