instmodel 1.2.1__tar.gz → 1.3.0__tar.gz
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-1.2.1/instmodel.egg-info → instmodel-1.3.0}/PKG-INFO +1 -1
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel/instruction_model.py +20 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel/tf.py +132 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel/torch.py +118 -0
- {instmodel-1.2.1 → instmodel-1.3.0/instmodel.egg-info}/PKG-INFO +1 -1
- {instmodel-1.2.1 → instmodel-1.3.0}/setup.cfg +1 -1
- {instmodel-1.2.1 → instmodel-1.3.0}/tests/test_tf.py +231 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/tests/test_torch.py +247 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/LICENSE +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/MANIFEST.in +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/README.md +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel/__init__.py +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel/training_utils.py +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel.egg-info/SOURCES.txt +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel.egg-info/dependency_links.txt +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel.egg-info/requires.txt +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/instmodel.egg-info/top_level.txt +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/pyproject.toml +0 -0
- {instmodel-1.2.1 → instmodel-1.3.0}/setup.py +0 -0
|
@@ -119,6 +119,14 @@ def apply_activation(instruction, buffers, output_index):
|
|
|
119
119
|
)
|
|
120
120
|
buffers[output_index] = result
|
|
121
121
|
|
|
122
|
+
elif act == "EXP":
|
|
123
|
+
# Clamp to [-88, 88] to avoid float32 overflow/underflow
|
|
124
|
+
buffers[output_index] = np.exp(np.clip(x, -88, 88))
|
|
125
|
+
|
|
126
|
+
elif act == "SIGN":
|
|
127
|
+
# sign(x): -1 for x < 0, 0 for x == 0, 1 for x > 0
|
|
128
|
+
buffers[output_index] = np.sign(x)
|
|
129
|
+
|
|
122
130
|
else:
|
|
123
131
|
raise ValueError(f"Unexpected activation: {act}")
|
|
124
132
|
|
|
@@ -318,6 +326,14 @@ def instruction_model_inference(model, input_data):
|
|
|
318
326
|
param = parameters[instruction["parameters"]]
|
|
319
327
|
buffers[input_index] = input_data_buffer * param
|
|
320
328
|
|
|
329
|
+
elif instruction["type"] == "CLIP_ELEMENTWISE":
|
|
330
|
+
result = input_data_buffer
|
|
331
|
+
if "parameters_min" in instruction:
|
|
332
|
+
result = np.maximum(result, parameters[instruction["parameters_min"]])
|
|
333
|
+
if "parameters_max" in instruction:
|
|
334
|
+
result = np.minimum(result, parameters[instruction["parameters_max"]])
|
|
335
|
+
buffers[input_index] = result
|
|
336
|
+
|
|
321
337
|
# Ensure the source buffer (still unassigned to an output) is float32.
|
|
322
338
|
buffers[input_index] = np.array(buffers[input_index], dtype=np.float32)
|
|
323
339
|
|
|
@@ -374,6 +390,10 @@ def concatenate_instruction_models(models, start_from_first=False):
|
|
|
374
390
|
new_instruction["weights"] += w_offset
|
|
375
391
|
if "parameters" in new_instruction:
|
|
376
392
|
new_instruction["parameters"] += p_offset
|
|
393
|
+
if "parameters_min" in new_instruction:
|
|
394
|
+
new_instruction["parameters_min"] += p_offset
|
|
395
|
+
if "parameters_max" in new_instruction:
|
|
396
|
+
new_instruction["parameters_max"] += p_offset
|
|
377
397
|
if "map" in new_instruction:
|
|
378
398
|
new_instruction["map"] += m_offset
|
|
379
399
|
if "input" in new_instruction:
|
|
@@ -512,6 +512,12 @@ class ActivationComputation(ComputationOp):
|
|
|
512
512
|
)
|
|
513
513
|
elif self.activation == "INVERSE":
|
|
514
514
|
self.keras_layer = layers.Lambda(lambda x: 1 - x, name=name)
|
|
515
|
+
elif self.activation == "EXP":
|
|
516
|
+
self.keras_layer = layers.Lambda(
|
|
517
|
+
lambda x: tf.exp(tf.clip_by_value(x, -88, 88)), name=name
|
|
518
|
+
)
|
|
519
|
+
elif self.activation == "SIGN":
|
|
520
|
+
self.keras_layer = layers.Lambda(lambda x: tf.sign(x), name=name)
|
|
515
521
|
else:
|
|
516
522
|
raise ValueError(f"Unexpected activation: {self.activation}")
|
|
517
523
|
|
|
@@ -1207,6 +1213,132 @@ class ShiftVectorized(ComputationOp):
|
|
|
1207
1213
|
return target_index
|
|
1208
1214
|
|
|
1209
1215
|
|
|
1216
|
+
class ClipVectorized(ComputationOp):
|
|
1217
|
+
"""
|
|
1218
|
+
Clips a buffer elementwise by fixed min/max vectors.
|
|
1219
|
+
Compiles to a CLIP_ELEMENTWISE instruction.
|
|
1220
|
+
"""
|
|
1221
|
+
|
|
1222
|
+
def __init__(self, clip_min=None, clip_max=None, in_place=False, name=None):
|
|
1223
|
+
super().__init__()
|
|
1224
|
+
if clip_min is None and clip_max is None:
|
|
1225
|
+
raise ValueError("ClipVectorized requires at least one of clip_min or clip_max.")
|
|
1226
|
+
|
|
1227
|
+
self.in_place = in_place
|
|
1228
|
+
self.name = name
|
|
1229
|
+
self._has_min = clip_min is not None
|
|
1230
|
+
self._has_max = clip_max is not None
|
|
1231
|
+
|
|
1232
|
+
if self._has_min:
|
|
1233
|
+
arr = np.asarray(clip_min, dtype=np.float32)
|
|
1234
|
+
self._min_is_scalar = arr.ndim == 0 or (arr.ndim == 1 and arr.size == 1)
|
|
1235
|
+
if self._min_is_scalar:
|
|
1236
|
+
self._min_scalar = float(arr.flat[0])
|
|
1237
|
+
self.clip_min_vector = None
|
|
1238
|
+
self._min_tensor = tf.constant(self._min_scalar, dtype=tf.float32)
|
|
1239
|
+
else:
|
|
1240
|
+
self._min_scalar = None
|
|
1241
|
+
self.clip_min_vector = arr
|
|
1242
|
+
self._min_tensor = tf.constant(self.clip_min_vector, dtype=tf.float32)
|
|
1243
|
+
else:
|
|
1244
|
+
self._min_is_scalar = None
|
|
1245
|
+
self._min_scalar = None
|
|
1246
|
+
self.clip_min_vector = None
|
|
1247
|
+
self._min_tensor = None
|
|
1248
|
+
|
|
1249
|
+
if self._has_max:
|
|
1250
|
+
arr = np.asarray(clip_max, dtype=np.float32)
|
|
1251
|
+
self._max_is_scalar = arr.ndim == 0 or (arr.ndim == 1 and arr.size == 1)
|
|
1252
|
+
if self._max_is_scalar:
|
|
1253
|
+
self._max_scalar = float(arr.flat[0])
|
|
1254
|
+
self.clip_max_vector = None
|
|
1255
|
+
self._max_tensor = tf.constant(self._max_scalar, dtype=tf.float32)
|
|
1256
|
+
else:
|
|
1257
|
+
self._max_scalar = None
|
|
1258
|
+
self.clip_max_vector = arr
|
|
1259
|
+
self._max_tensor = tf.constant(self.clip_max_vector, dtype=tf.float32)
|
|
1260
|
+
else:
|
|
1261
|
+
self._max_is_scalar = None
|
|
1262
|
+
self._max_scalar = None
|
|
1263
|
+
self.clip_max_vector = None
|
|
1264
|
+
self._max_tensor = None
|
|
1265
|
+
|
|
1266
|
+
min_t = self._min_tensor
|
|
1267
|
+
max_t = self._max_tensor
|
|
1268
|
+
has_min = self._has_min
|
|
1269
|
+
has_max = self._has_max
|
|
1270
|
+
|
|
1271
|
+
def clip_fn(x):
|
|
1272
|
+
result = x
|
|
1273
|
+
if has_min:
|
|
1274
|
+
result = tf.maximum(result, min_t)
|
|
1275
|
+
if has_max:
|
|
1276
|
+
result = tf.minimum(result, max_t)
|
|
1277
|
+
return result
|
|
1278
|
+
|
|
1279
|
+
self.keras_layer = layers.Lambda(clip_fn, name=name)
|
|
1280
|
+
|
|
1281
|
+
def __call__(self, inputs):
|
|
1282
|
+
if not isinstance(inputs, list):
|
|
1283
|
+
inputs = [inputs]
|
|
1284
|
+
if len(inputs) != 1:
|
|
1285
|
+
raise ValueError("ClipVectorized expects exactly one input.")
|
|
1286
|
+
output_tensor = self.keras_layer(inputs[0].tensor)
|
|
1287
|
+
return DataBuffer(output_tensor, op=self, inputs=inputs)
|
|
1288
|
+
|
|
1289
|
+
def compile_instructions(self, input_indices, weights_visited, model_structure):
|
|
1290
|
+
if len(input_indices) != 1:
|
|
1291
|
+
raise ValueError("ClipVectorized expects exactly one input.")
|
|
1292
|
+
|
|
1293
|
+
pw = weights_visited["parameters"]
|
|
1294
|
+
|
|
1295
|
+
if self.in_place:
|
|
1296
|
+
target_index = input_indices[0]
|
|
1297
|
+
else:
|
|
1298
|
+
target_index = len(model_structure["buffer_sizes"])
|
|
1299
|
+
model_structure["buffer_sizes"].append(
|
|
1300
|
+
model_structure["buffer_sizes"][input_indices[0]]
|
|
1301
|
+
)
|
|
1302
|
+
copy_instr = {
|
|
1303
|
+
"type": "COPY",
|
|
1304
|
+
"input": input_indices[0],
|
|
1305
|
+
"output": target_index,
|
|
1306
|
+
"internal_index": 0,
|
|
1307
|
+
}
|
|
1308
|
+
model_structure["instructions"].append(copy_instr)
|
|
1309
|
+
|
|
1310
|
+
if id(self) not in pw:
|
|
1311
|
+
indices = {}
|
|
1312
|
+
if self._has_min:
|
|
1313
|
+
indices["min"] = len(model_structure["parameters"])
|
|
1314
|
+
if self._min_is_scalar:
|
|
1315
|
+
buffer_size = model_structure["buffer_sizes"][input_indices[0]]
|
|
1316
|
+
model_structure["parameters"].append([self._min_scalar] * buffer_size)
|
|
1317
|
+
else:
|
|
1318
|
+
model_structure["parameters"].append(self.clip_min_vector.tolist())
|
|
1319
|
+
if self._has_max:
|
|
1320
|
+
indices["max"] = len(model_structure["parameters"])
|
|
1321
|
+
if self._max_is_scalar:
|
|
1322
|
+
buffer_size = model_structure["buffer_sizes"][input_indices[0]]
|
|
1323
|
+
model_structure["parameters"].append([self._max_scalar] * buffer_size)
|
|
1324
|
+
else:
|
|
1325
|
+
model_structure["parameters"].append(self.clip_max_vector.tolist())
|
|
1326
|
+
pw[id(self)] = indices
|
|
1327
|
+
|
|
1328
|
+
cached = pw[id(self)]
|
|
1329
|
+
instr = {
|
|
1330
|
+
"type": "CLIP_ELEMENTWISE",
|
|
1331
|
+
"input": target_index,
|
|
1332
|
+
}
|
|
1333
|
+
if "min" in cached:
|
|
1334
|
+
instr["parameters_min"] = cached["min"]
|
|
1335
|
+
if "max" in cached:
|
|
1336
|
+
instr["parameters_max"] = cached["max"]
|
|
1337
|
+
|
|
1338
|
+
model_structure["instructions"].append(instr)
|
|
1339
|
+
return target_index
|
|
1340
|
+
|
|
1341
|
+
|
|
1210
1342
|
class Attention(ComputationOp):
|
|
1211
1343
|
"""
|
|
1212
1344
|
An attention operation that computes softmax attention from a key buffer and applies it
|
|
@@ -48,6 +48,10 @@ def _apply_activation(x: torch.Tensor, activation: str) -> torch.Tensor:
|
|
|
48
48
|
)
|
|
49
49
|
elif act == "INVERSE":
|
|
50
50
|
return 1 - x
|
|
51
|
+
elif act == "EXP":
|
|
52
|
+
return torch.exp(torch.clamp(x, -88, 88))
|
|
53
|
+
elif act == "SIGN":
|
|
54
|
+
return torch.sign(x)
|
|
51
55
|
else:
|
|
52
56
|
raise ValueError(f"Unexpected activation: {activation}")
|
|
53
57
|
|
|
@@ -893,6 +897,120 @@ class ShiftVectorized(ComputationOp):
|
|
|
893
897
|
return target_index
|
|
894
898
|
|
|
895
899
|
|
|
900
|
+
class ClipVectorized(ComputationOp):
|
|
901
|
+
def __init__(self, clip_min=None, clip_max=None, in_place=False, name=None):
|
|
902
|
+
super().__init__()
|
|
903
|
+
if clip_min is None and clip_max is None:
|
|
904
|
+
raise ValueError("ClipVectorized requires at least one of clip_min or clip_max.")
|
|
905
|
+
|
|
906
|
+
self.in_place = in_place
|
|
907
|
+
self.name = name
|
|
908
|
+
self._has_min = clip_min is not None
|
|
909
|
+
self._has_max = clip_max is not None
|
|
910
|
+
|
|
911
|
+
if self._has_min:
|
|
912
|
+
arr = np.asarray(clip_min, dtype=np.float32)
|
|
913
|
+
self._min_is_scalar = arr.ndim == 0 or (arr.ndim == 1 and arr.size == 1)
|
|
914
|
+
if self._min_is_scalar:
|
|
915
|
+
self._min_scalar = float(arr.flat[0])
|
|
916
|
+
self.clip_min_vector = None
|
|
917
|
+
else:
|
|
918
|
+
self._min_scalar = None
|
|
919
|
+
self.clip_min_vector = arr
|
|
920
|
+
else:
|
|
921
|
+
self._min_is_scalar = None
|
|
922
|
+
self._min_scalar = None
|
|
923
|
+
self.clip_min_vector = None
|
|
924
|
+
|
|
925
|
+
if self._has_max:
|
|
926
|
+
arr = np.asarray(clip_max, dtype=np.float32)
|
|
927
|
+
self._max_is_scalar = arr.ndim == 0 or (arr.ndim == 1 and arr.size == 1)
|
|
928
|
+
if self._max_is_scalar:
|
|
929
|
+
self._max_scalar = float(arr.flat[0])
|
|
930
|
+
self.clip_max_vector = None
|
|
931
|
+
else:
|
|
932
|
+
self._max_scalar = None
|
|
933
|
+
self.clip_max_vector = arr
|
|
934
|
+
else:
|
|
935
|
+
self._max_is_scalar = None
|
|
936
|
+
self._max_scalar = None
|
|
937
|
+
self.clip_max_vector = None
|
|
938
|
+
|
|
939
|
+
def __call__(self, inputs):
|
|
940
|
+
if not isinstance(inputs, list):
|
|
941
|
+
inputs = [inputs]
|
|
942
|
+
if len(inputs) != 1:
|
|
943
|
+
raise ValueError("ClipVectorized expects exactly one input.")
|
|
944
|
+
return DataBuffer(inputs[0]._shape, op=self, inputs=inputs)
|
|
945
|
+
|
|
946
|
+
def forward_op(self, x: torch.Tensor) -> torch.Tensor:
|
|
947
|
+
if self._has_min:
|
|
948
|
+
if self._min_is_scalar:
|
|
949
|
+
x = torch.clamp(x, min=self._min_scalar)
|
|
950
|
+
else:
|
|
951
|
+
t_min = torch.tensor(self.clip_min_vector, dtype=x.dtype, device=x.device)
|
|
952
|
+
x = torch.maximum(x, t_min)
|
|
953
|
+
if self._has_max:
|
|
954
|
+
if self._max_is_scalar:
|
|
955
|
+
x = torch.clamp(x, max=self._max_scalar)
|
|
956
|
+
else:
|
|
957
|
+
t_max = torch.tensor(self.clip_max_vector, dtype=x.dtype, device=x.device)
|
|
958
|
+
x = torch.minimum(x, t_max)
|
|
959
|
+
return x
|
|
960
|
+
|
|
961
|
+
def compile_instructions(self, input_indices, weights_visited, model_structure):
|
|
962
|
+
if len(input_indices) != 1:
|
|
963
|
+
raise ValueError("ClipVectorized expects exactly one input.")
|
|
964
|
+
|
|
965
|
+
pw = weights_visited["parameters"]
|
|
966
|
+
|
|
967
|
+
if self.in_place:
|
|
968
|
+
target_index = input_indices[0]
|
|
969
|
+
else:
|
|
970
|
+
target_index = len(model_structure["buffer_sizes"])
|
|
971
|
+
model_structure["buffer_sizes"].append(
|
|
972
|
+
model_structure["buffer_sizes"][input_indices[0]]
|
|
973
|
+
)
|
|
974
|
+
copy_instr = {
|
|
975
|
+
"type": "COPY",
|
|
976
|
+
"input": input_indices[0],
|
|
977
|
+
"output": target_index,
|
|
978
|
+
"internal_index": 0,
|
|
979
|
+
}
|
|
980
|
+
model_structure["instructions"].append(copy_instr)
|
|
981
|
+
|
|
982
|
+
if id(self) not in pw:
|
|
983
|
+
indices = {}
|
|
984
|
+
if self._has_min:
|
|
985
|
+
indices["min"] = len(model_structure["parameters"])
|
|
986
|
+
if self._min_is_scalar:
|
|
987
|
+
buffer_size = model_structure["buffer_sizes"][input_indices[0]]
|
|
988
|
+
model_structure["parameters"].append([self._min_scalar] * buffer_size)
|
|
989
|
+
else:
|
|
990
|
+
model_structure["parameters"].append(self.clip_min_vector.tolist())
|
|
991
|
+
if self._has_max:
|
|
992
|
+
indices["max"] = len(model_structure["parameters"])
|
|
993
|
+
if self._max_is_scalar:
|
|
994
|
+
buffer_size = model_structure["buffer_sizes"][input_indices[0]]
|
|
995
|
+
model_structure["parameters"].append([self._max_scalar] * buffer_size)
|
|
996
|
+
else:
|
|
997
|
+
model_structure["parameters"].append(self.clip_max_vector.tolist())
|
|
998
|
+
pw[id(self)] = indices
|
|
999
|
+
|
|
1000
|
+
cached = pw[id(self)]
|
|
1001
|
+
instr = {
|
|
1002
|
+
"type": "CLIP_ELEMENTWISE",
|
|
1003
|
+
"input": target_index,
|
|
1004
|
+
}
|
|
1005
|
+
if "min" in cached:
|
|
1006
|
+
instr["parameters_min"] = cached["min"]
|
|
1007
|
+
if "max" in cached:
|
|
1008
|
+
instr["parameters_max"] = cached["max"]
|
|
1009
|
+
|
|
1010
|
+
model_structure["instructions"].append(instr)
|
|
1011
|
+
return target_index
|
|
1012
|
+
|
|
1013
|
+
|
|
896
1014
|
class Attention(ComputationOp):
|
|
897
1015
|
def __init__(self, name=None):
|
|
898
1016
|
super().__init__()
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from instmodel.tf import (
|
|
2
|
+
ActivationComputation,
|
|
2
3
|
Attention,
|
|
4
|
+
ClipVectorized,
|
|
3
5
|
Concatenate,
|
|
4
6
|
Dense,
|
|
5
7
|
InputBuffer,
|
|
@@ -983,3 +985,232 @@ def test_multiply_heads_scalar():
|
|
|
983
985
|
# Validate using instruction_model_inference directly (supports list inputs)
|
|
984
986
|
*_, inst_output = instruction_model_inference(inst_model, [data, scalar])
|
|
985
987
|
assert np.allclose(inst_output, keras_pred, atol=1e-6)
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
def test_exp_activation():
|
|
991
|
+
"""Tests EXP activation: Keras Lambda, NumPy, and instruction model agree; clamped at [-88, 88]."""
|
|
992
|
+
x_np = np.array([-100.0, -88.0, -1.0, 0.0, 1.0, 88.0, 100.0], dtype=np.float32)
|
|
993
|
+
expected = np.exp(np.clip(x_np, -88, 88).astype(np.float64)).astype(np.float32)
|
|
994
|
+
|
|
995
|
+
# TF/Keras backend via ActivationComputation
|
|
996
|
+
inp = InputBuffer(len(x_np))
|
|
997
|
+
out = ActivationComputation("EXP", in_place=False)(inp)
|
|
998
|
+
model = ModelGraph(inp, out)
|
|
999
|
+
result_tf = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
|
|
1000
|
+
assert np.allclose(result_tf, expected, atol=1e-6), (
|
|
1001
|
+
f"TF EXP mismatch: {result_tf} vs {expected}"
|
|
1002
|
+
)
|
|
1003
|
+
|
|
1004
|
+
# Instruction model round-trip
|
|
1005
|
+
inst_model = model.create_instruction_model()
|
|
1006
|
+
inst_model["validation_data"] = {
|
|
1007
|
+
"inputs": x_np.reshape(1, -1).tolist(),
|
|
1008
|
+
"expected_outputs": result_tf.reshape(1, -1).tolist(),
|
|
1009
|
+
}
|
|
1010
|
+
validate_instruction_model(inst_model)
|
|
1011
|
+
|
|
1012
|
+
# Clamping: exp(-100) == exp(-88), exp(100) == exp(88)
|
|
1013
|
+
assert np.isclose(result_tf[0], result_tf[1], atol=1e-6), "EXP(-100) should be clamped to exp(-88)"
|
|
1014
|
+
assert np.isclose(result_tf[-1], result_tf[-2], atol=1e-6), "EXP(100) should be clamped to exp(88)"
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
def test_sign_activation():
|
|
1018
|
+
"""Tests SIGN activation: Keras Lambda, NumPy, and instruction model agree; returns -1, 0, or 1."""
|
|
1019
|
+
x_np = np.array([-5.0, -0.01, 0.0, 0.01, 5.0], dtype=np.float32)
|
|
1020
|
+
expected = np.array([-1.0, -1.0, 0.0, 1.0, 1.0], dtype=np.float32)
|
|
1021
|
+
|
|
1022
|
+
# TF/Keras backend via ActivationComputation
|
|
1023
|
+
inp = InputBuffer(len(x_np))
|
|
1024
|
+
out = ActivationComputation("SIGN", in_place=False)(inp)
|
|
1025
|
+
model = ModelGraph(inp, out)
|
|
1026
|
+
result_tf = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
|
|
1027
|
+
assert np.array_equal(result_tf, expected), (
|
|
1028
|
+
f"TF SIGN mismatch: {result_tf} vs {expected}"
|
|
1029
|
+
)
|
|
1030
|
+
|
|
1031
|
+
# Instruction model round-trip
|
|
1032
|
+
inst_model = model.create_instruction_model()
|
|
1033
|
+
inst_model["validation_data"] = {
|
|
1034
|
+
"inputs": x_np.reshape(1, -1).tolist(),
|
|
1035
|
+
"expected_outputs": result_tf.reshape(1, -1).tolist(),
|
|
1036
|
+
}
|
|
1037
|
+
validate_instruction_model(inst_model)
|
|
1038
|
+
|
|
1039
|
+
# RELU(SIGN(x)) == STEP(x)
|
|
1040
|
+
step_expected = np.array([0.0, 0.0, 0.0, 1.0, 1.0], dtype=np.float32)
|
|
1041
|
+
assert np.array_equal(np.maximum(0, result_tf), step_expected), (
|
|
1042
|
+
"RELU(SIGN(x)) should produce step function"
|
|
1043
|
+
)
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
def test_clip_vectorized_both_bounds():
|
|
1047
|
+
"""Tests Clip with both lower and upper vector bounds (in-place)."""
|
|
1048
|
+
input_buffer = InputBuffer(3)
|
|
1049
|
+
clipped = ClipVectorized(
|
|
1050
|
+
clip_min=[0.0, -1.0, 0.5],
|
|
1051
|
+
clip_max=[1.0, 2.0, 3.0],
|
|
1052
|
+
in_place=True,
|
|
1053
|
+
)(input_buffer)
|
|
1054
|
+
|
|
1055
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1056
|
+
result = model.create_instruction_model()
|
|
1057
|
+
|
|
1058
|
+
assert len(result["parameters"]) == 2
|
|
1059
|
+
assert result["parameters"][0] == [0.0, -1.0, 0.5]
|
|
1060
|
+
assert result["parameters"][1] == [1.0, 2.0, 3.0]
|
|
1061
|
+
|
|
1062
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1063
|
+
assert result == {
|
|
1064
|
+
"features": [],
|
|
1065
|
+
"buffer_sizes": [3],
|
|
1066
|
+
"instructions": [
|
|
1067
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_min": 0, "parameters_max": 1},
|
|
1068
|
+
],
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
x_data = np.array([[-2.0, 5.0, 1.0], [0.5, -3.0, 4.0]])
|
|
1072
|
+
keras_pred = model.predict(x_data, verbose=0)
|
|
1073
|
+
expected = np.clip(x_data, [0.0, -1.0, 0.5], [1.0, 2.0, 3.0])
|
|
1074
|
+
assert np.allclose(keras_pred, expected, atol=1e-6)
|
|
1075
|
+
|
|
1076
|
+
inst_model = model.create_instruction_model()
|
|
1077
|
+
inst_model["validation_data"] = {
|
|
1078
|
+
"inputs": x_data.tolist(),
|
|
1079
|
+
"expected_outputs": keras_pred.tolist(),
|
|
1080
|
+
}
|
|
1081
|
+
validate_instruction_model(inst_model)
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
def test_clip_vectorized_lower_only():
|
|
1085
|
+
"""Tests Clip with only a lower bound."""
|
|
1086
|
+
input_buffer = InputBuffer(2)
|
|
1087
|
+
clipped = ClipVectorized(clip_min=[0.0, -1.0], in_place=True)(input_buffer)
|
|
1088
|
+
|
|
1089
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1090
|
+
result = model.create_instruction_model()
|
|
1091
|
+
|
|
1092
|
+
assert len(result["parameters"]) == 1
|
|
1093
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1094
|
+
assert result == {
|
|
1095
|
+
"features": [],
|
|
1096
|
+
"buffer_sizes": [2],
|
|
1097
|
+
"instructions": [
|
|
1098
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_min": 0},
|
|
1099
|
+
],
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
x_data = np.array([[-5.0, 0.0], [3.0, -2.0]])
|
|
1103
|
+
keras_pred = model.predict(x_data, verbose=0)
|
|
1104
|
+
expected = np.maximum(x_data, [0.0, -1.0])
|
|
1105
|
+
assert np.allclose(keras_pred, expected, atol=1e-6)
|
|
1106
|
+
|
|
1107
|
+
inst_model = model.create_instruction_model()
|
|
1108
|
+
inst_model["validation_data"] = {
|
|
1109
|
+
"inputs": x_data.tolist(),
|
|
1110
|
+
"expected_outputs": keras_pred.tolist(),
|
|
1111
|
+
}
|
|
1112
|
+
validate_instruction_model(inst_model)
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
def test_clip_vectorized_upper_only():
|
|
1116
|
+
"""Tests Clip with only an upper bound."""
|
|
1117
|
+
input_buffer = InputBuffer(2)
|
|
1118
|
+
clipped = ClipVectorized(clip_max=[1.0, 2.0], in_place=True)(input_buffer)
|
|
1119
|
+
|
|
1120
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1121
|
+
result = model.create_instruction_model()
|
|
1122
|
+
|
|
1123
|
+
assert len(result["parameters"]) == 1
|
|
1124
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1125
|
+
assert result == {
|
|
1126
|
+
"features": [],
|
|
1127
|
+
"buffer_sizes": [2],
|
|
1128
|
+
"instructions": [
|
|
1129
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_max": 0},
|
|
1130
|
+
],
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
x_data = np.array([[5.0, 0.0], [-3.0, 3.0]])
|
|
1134
|
+
keras_pred = model.predict(x_data, verbose=0)
|
|
1135
|
+
expected = np.minimum(x_data, [1.0, 2.0])
|
|
1136
|
+
assert np.allclose(keras_pred, expected, atol=1e-6)
|
|
1137
|
+
|
|
1138
|
+
inst_model = model.create_instruction_model()
|
|
1139
|
+
inst_model["validation_data"] = {
|
|
1140
|
+
"inputs": x_data.tolist(),
|
|
1141
|
+
"expected_outputs": keras_pred.tolist(),
|
|
1142
|
+
}
|
|
1143
|
+
validate_instruction_model(inst_model)
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
def test_clip_vectorized_scalar():
|
|
1147
|
+
"""Tests Clip with scalar bounds broadcast to all elements."""
|
|
1148
|
+
input_buffer = InputBuffer(4)
|
|
1149
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=1.0, in_place=True)(input_buffer)
|
|
1150
|
+
|
|
1151
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1152
|
+
result = model.create_instruction_model()
|
|
1153
|
+
|
|
1154
|
+
assert result["parameters"][0] == [0.0, 0.0, 0.0, 0.0]
|
|
1155
|
+
assert result["parameters"][1] == [1.0, 1.0, 1.0, 1.0]
|
|
1156
|
+
|
|
1157
|
+
x_data = np.array([[-1.0, 0.5, 1.5, 0.0], [2.0, -0.5, 0.3, 0.8]])
|
|
1158
|
+
keras_pred = model.predict(x_data, verbose=0)
|
|
1159
|
+
expected = np.clip(x_data, 0.0, 1.0)
|
|
1160
|
+
assert np.allclose(keras_pred, expected, atol=1e-6)
|
|
1161
|
+
|
|
1162
|
+
inst_model = model.create_instruction_model()
|
|
1163
|
+
inst_model["validation_data"] = {
|
|
1164
|
+
"inputs": x_data.tolist(),
|
|
1165
|
+
"expected_outputs": keras_pred.tolist(),
|
|
1166
|
+
}
|
|
1167
|
+
validate_instruction_model(inst_model)
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
def test_clip_vectorized_not_inplace():
|
|
1171
|
+
"""Tests Clip with in_place=False creates a COPY + new buffer."""
|
|
1172
|
+
input_buffer = InputBuffer(2)
|
|
1173
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=1.0, in_place=False)(input_buffer)
|
|
1174
|
+
|
|
1175
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1176
|
+
result = model.create_instruction_model()
|
|
1177
|
+
|
|
1178
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1179
|
+
assert result == {
|
|
1180
|
+
"features": [],
|
|
1181
|
+
"buffer_sizes": [2, 2],
|
|
1182
|
+
"instructions": [
|
|
1183
|
+
{"type": "COPY", "input": 0, "output": 1, "internal_index": 0},
|
|
1184
|
+
{"type": "CLIP_ELEMENTWISE", "input": 1, "parameters_min": 0, "parameters_max": 1},
|
|
1185
|
+
],
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
def test_clip_vectorized_no_bounds_raises():
|
|
1190
|
+
"""Tests that Clip raises ValueError when neither bound is provided."""
|
|
1191
|
+
import pytest
|
|
1192
|
+
with pytest.raises(ValueError, match="at least one"):
|
|
1193
|
+
ClipVectorized()
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
def test_clip_vectorized_in_pipeline():
|
|
1197
|
+
"""Tests Clip works end-to-end in a model with Dense layers."""
|
|
1198
|
+
input_buffer = InputBuffer(4)
|
|
1199
|
+
hidden = Dense(8, activation="relu", name="h1")(input_buffer)
|
|
1200
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=5.0, in_place=True)(hidden)
|
|
1201
|
+
output = Dense(1, activation="sigmoid", name="out")(clipped)
|
|
1202
|
+
|
|
1203
|
+
model = ModelGraph(input_buffer, output)
|
|
1204
|
+
model.compile(optimizer="adam", loss="binary_crossentropy")
|
|
1205
|
+
|
|
1206
|
+
x_data = np.random.random((10, 4)).astype(np.float32)
|
|
1207
|
+
y_data = np.random.randint(0, 2, size=(10, 1)).astype(np.float32)
|
|
1208
|
+
model.fit(x_data, y_data, epochs=1, verbose=0)
|
|
1209
|
+
|
|
1210
|
+
keras_pred = model.predict(x_data, verbose=0)
|
|
1211
|
+
inst_model = model.create_instruction_model()
|
|
1212
|
+
inst_model["validation_data"] = {
|
|
1213
|
+
"inputs": x_data.tolist(),
|
|
1214
|
+
"expected_outputs": keras_pred.tolist(),
|
|
1215
|
+
}
|
|
1216
|
+
validate_instruction_model(inst_model)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from instmodel.torch import (
|
|
2
|
+
ActivationComputation,
|
|
2
3
|
Attention,
|
|
4
|
+
ClipVectorized,
|
|
3
5
|
Concatenate,
|
|
4
6
|
Dense,
|
|
5
7
|
InputBuffer,
|
|
@@ -917,3 +919,248 @@ def test_multiply_heads_scalar():
|
|
|
917
919
|
|
|
918
920
|
*_, inst_output = instruction_model_inference(inst_model, [data, scalar])
|
|
919
921
|
assert np.allclose(inst_output, torch_pred, atol=1e-6)
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
def test_exp_activation():
|
|
925
|
+
"""Tests EXP activation: PyTorch, NumPy, and instruction model agree; clamped at [-88, 88]."""
|
|
926
|
+
from instmodel.torch import _apply_activation
|
|
927
|
+
|
|
928
|
+
x_np = np.array([-100.0, -88.0, -1.0, 0.0, 1.0, 88.0, 100.0], dtype=np.float32)
|
|
929
|
+
expected = np.exp(np.clip(x_np, -88, 88).astype(np.float64)).astype(np.float32)
|
|
930
|
+
|
|
931
|
+
# PyTorch backend
|
|
932
|
+
result_torch = _apply_activation(torch.tensor(x_np), "EXP").numpy()
|
|
933
|
+
assert np.allclose(result_torch, expected, atol=1e-6), (
|
|
934
|
+
f"PyTorch EXP mismatch: {result_torch} vs {expected}"
|
|
935
|
+
)
|
|
936
|
+
|
|
937
|
+
# ActivationComputation + ModelGraph round-trip
|
|
938
|
+
inp = InputBuffer(len(x_np))
|
|
939
|
+
out = ActivationComputation("EXP", in_place=False)(inp)
|
|
940
|
+
model = ModelGraph(inp, out)
|
|
941
|
+
model_pred = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
|
|
942
|
+
assert np.allclose(model_pred, expected, atol=1e-6), (
|
|
943
|
+
f"ModelGraph EXP mismatch: {model_pred} vs {expected}"
|
|
944
|
+
)
|
|
945
|
+
|
|
946
|
+
# Instruction model round-trip
|
|
947
|
+
inst_model = model.create_instruction_model()
|
|
948
|
+
inst_model["validation_data"] = {
|
|
949
|
+
"inputs": x_np.reshape(1, -1).tolist(),
|
|
950
|
+
"expected_outputs": model_pred.reshape(1, -1).tolist(),
|
|
951
|
+
}
|
|
952
|
+
validate_instruction_model(inst_model)
|
|
953
|
+
|
|
954
|
+
# Clamping: exp(-100) == exp(-88), exp(100) == exp(88)
|
|
955
|
+
assert np.isclose(result_torch[0], result_torch[1], atol=1e-6), "EXP(-100) should be clamped to exp(-88)"
|
|
956
|
+
assert np.isclose(result_torch[-1], result_torch[-2], atol=1e-6), "EXP(100) should be clamped to exp(88)"
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
def test_sign_activation():
|
|
960
|
+
"""Tests SIGN activation: PyTorch, NumPy, and instruction model agree; returns -1, 0, or 1."""
|
|
961
|
+
from instmodel.torch import _apply_activation
|
|
962
|
+
|
|
963
|
+
x_np = np.array([-5.0, -0.01, 0.0, 0.01, 5.0], dtype=np.float32)
|
|
964
|
+
expected = np.array([-1.0, -1.0, 0.0, 1.0, 1.0], dtype=np.float32)
|
|
965
|
+
|
|
966
|
+
# PyTorch backend
|
|
967
|
+
result_torch = _apply_activation(torch.tensor(x_np), "SIGN").numpy()
|
|
968
|
+
assert np.array_equal(result_torch, expected), (
|
|
969
|
+
f"PyTorch SIGN mismatch: {result_torch} vs {expected}"
|
|
970
|
+
)
|
|
971
|
+
|
|
972
|
+
# ActivationComputation + ModelGraph round-trip
|
|
973
|
+
inp = InputBuffer(len(x_np))
|
|
974
|
+
out = ActivationComputation("SIGN", in_place=False)(inp)
|
|
975
|
+
model = ModelGraph(inp, out)
|
|
976
|
+
model_pred = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
|
|
977
|
+
assert np.array_equal(model_pred, expected), (
|
|
978
|
+
f"ModelGraph SIGN mismatch: {model_pred} vs {expected}"
|
|
979
|
+
)
|
|
980
|
+
|
|
981
|
+
# Instruction model round-trip
|
|
982
|
+
inst_model = model.create_instruction_model()
|
|
983
|
+
inst_model["validation_data"] = {
|
|
984
|
+
"inputs": x_np.reshape(1, -1).tolist(),
|
|
985
|
+
"expected_outputs": model_pred.reshape(1, -1).tolist(),
|
|
986
|
+
}
|
|
987
|
+
validate_instruction_model(inst_model)
|
|
988
|
+
|
|
989
|
+
# RELU(SIGN(x)) == STEP(x)
|
|
990
|
+
step_expected = np.array([0.0, 0.0, 0.0, 1.0, 1.0], dtype=np.float32)
|
|
991
|
+
assert np.array_equal(np.maximum(0, result_torch), step_expected), (
|
|
992
|
+
"RELU(SIGN(x)) should produce step function"
|
|
993
|
+
)
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
def test_clip_vectorized_both_bounds():
|
|
997
|
+
"""Tests Clip with both lower and upper vector bounds (in-place)."""
|
|
998
|
+
input_buffer = InputBuffer(3)
|
|
999
|
+
clipped = ClipVectorized(
|
|
1000
|
+
clip_min=[0.0, -1.0, 0.5],
|
|
1001
|
+
clip_max=[1.0, 2.0, 3.0],
|
|
1002
|
+
in_place=True,
|
|
1003
|
+
)(input_buffer)
|
|
1004
|
+
|
|
1005
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1006
|
+
result = model.create_instruction_model()
|
|
1007
|
+
|
|
1008
|
+
assert len(result["parameters"]) == 2
|
|
1009
|
+
assert result["parameters"][0] == [0.0, -1.0, 0.5]
|
|
1010
|
+
assert result["parameters"][1] == [1.0, 2.0, 3.0]
|
|
1011
|
+
|
|
1012
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1013
|
+
assert result == {
|
|
1014
|
+
"features": [],
|
|
1015
|
+
"buffer_sizes": [3],
|
|
1016
|
+
"instructions": [
|
|
1017
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_min": 0, "parameters_max": 1},
|
|
1018
|
+
],
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
x_data = np.array([[-2.0, 5.0, 1.0], [0.5, -3.0, 4.0]])
|
|
1022
|
+
torch_pred = model.predict(x_data, verbose=0)
|
|
1023
|
+
expected = np.clip(x_data, [0.0, -1.0, 0.5], [1.0, 2.0, 3.0])
|
|
1024
|
+
assert np.allclose(torch_pred, expected, atol=1e-6)
|
|
1025
|
+
|
|
1026
|
+
inst_model = model.create_instruction_model()
|
|
1027
|
+
inst_model["validation_data"] = {
|
|
1028
|
+
"inputs": x_data.tolist(),
|
|
1029
|
+
"expected_outputs": torch_pred.tolist(),
|
|
1030
|
+
}
|
|
1031
|
+
validate_instruction_model(inst_model)
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
def test_clip_vectorized_lower_only():
|
|
1035
|
+
"""Tests Clip with only a lower bound."""
|
|
1036
|
+
input_buffer = InputBuffer(2)
|
|
1037
|
+
clipped = ClipVectorized(clip_min=[0.0, -1.0], in_place=True)(input_buffer)
|
|
1038
|
+
|
|
1039
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1040
|
+
result = model.create_instruction_model()
|
|
1041
|
+
|
|
1042
|
+
assert len(result["parameters"]) == 1
|
|
1043
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1044
|
+
assert result == {
|
|
1045
|
+
"features": [],
|
|
1046
|
+
"buffer_sizes": [2],
|
|
1047
|
+
"instructions": [
|
|
1048
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_min": 0},
|
|
1049
|
+
],
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
x_data = np.array([[-5.0, 0.0], [3.0, -2.0]])
|
|
1053
|
+
torch_pred = model.predict(x_data, verbose=0)
|
|
1054
|
+
expected = np.maximum(x_data, [0.0, -1.0])
|
|
1055
|
+
assert np.allclose(torch_pred, expected, atol=1e-6)
|
|
1056
|
+
|
|
1057
|
+
inst_model = model.create_instruction_model()
|
|
1058
|
+
inst_model["validation_data"] = {
|
|
1059
|
+
"inputs": x_data.tolist(),
|
|
1060
|
+
"expected_outputs": torch_pred.tolist(),
|
|
1061
|
+
}
|
|
1062
|
+
validate_instruction_model(inst_model)
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
def test_clip_vectorized_upper_only():
|
|
1066
|
+
"""Tests Clip with only an upper bound."""
|
|
1067
|
+
input_buffer = InputBuffer(2)
|
|
1068
|
+
clipped = ClipVectorized(clip_max=[1.0, 2.0], in_place=True)(input_buffer)
|
|
1069
|
+
|
|
1070
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1071
|
+
result = model.create_instruction_model()
|
|
1072
|
+
|
|
1073
|
+
assert len(result["parameters"]) == 1
|
|
1074
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1075
|
+
assert result == {
|
|
1076
|
+
"features": [],
|
|
1077
|
+
"buffer_sizes": [2],
|
|
1078
|
+
"instructions": [
|
|
1079
|
+
{"type": "CLIP_ELEMENTWISE", "input": 0, "parameters_max": 0},
|
|
1080
|
+
],
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
x_data = np.array([[5.0, 0.0], [-3.0, 3.0]])
|
|
1084
|
+
torch_pred = model.predict(x_data, verbose=0)
|
|
1085
|
+
expected = np.minimum(x_data, [1.0, 2.0])
|
|
1086
|
+
assert np.allclose(torch_pred, expected, atol=1e-6)
|
|
1087
|
+
|
|
1088
|
+
inst_model = model.create_instruction_model()
|
|
1089
|
+
inst_model["validation_data"] = {
|
|
1090
|
+
"inputs": x_data.tolist(),
|
|
1091
|
+
"expected_outputs": torch_pred.tolist(),
|
|
1092
|
+
}
|
|
1093
|
+
validate_instruction_model(inst_model)
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
def test_clip_vectorized_scalar():
|
|
1097
|
+
"""Tests Clip with scalar bounds broadcast to all elements."""
|
|
1098
|
+
input_buffer = InputBuffer(4)
|
|
1099
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=1.0, in_place=True)(input_buffer)
|
|
1100
|
+
|
|
1101
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1102
|
+
result = model.create_instruction_model()
|
|
1103
|
+
|
|
1104
|
+
assert result["parameters"][0] == [0.0, 0.0, 0.0, 0.0]
|
|
1105
|
+
assert result["parameters"][1] == [1.0, 1.0, 1.0, 1.0]
|
|
1106
|
+
|
|
1107
|
+
x_data = np.array([[-1.0, 0.5, 1.5, 0.0], [2.0, -0.5, 0.3, 0.8]])
|
|
1108
|
+
torch_pred = model.predict(x_data, verbose=0)
|
|
1109
|
+
expected = np.clip(x_data, 0.0, 1.0)
|
|
1110
|
+
assert np.allclose(torch_pred, expected, atol=1e-6)
|
|
1111
|
+
|
|
1112
|
+
inst_model = model.create_instruction_model()
|
|
1113
|
+
inst_model["validation_data"] = {
|
|
1114
|
+
"inputs": x_data.tolist(),
|
|
1115
|
+
"expected_outputs": torch_pred.tolist(),
|
|
1116
|
+
}
|
|
1117
|
+
validate_instruction_model(inst_model)
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
def test_clip_vectorized_not_inplace():
|
|
1121
|
+
"""Tests Clip with in_place=False creates a COPY + new buffer."""
|
|
1122
|
+
input_buffer = InputBuffer(2)
|
|
1123
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=1.0, in_place=False)(input_buffer)
|
|
1124
|
+
|
|
1125
|
+
model = ModelGraph(input_buffer, clipped)
|
|
1126
|
+
result = model.create_instruction_model()
|
|
1127
|
+
|
|
1128
|
+
del result["weights"], result["bias"], result["maps"], result["parameters"]
|
|
1129
|
+
assert result == {
|
|
1130
|
+
"features": [],
|
|
1131
|
+
"buffer_sizes": [2, 2],
|
|
1132
|
+
"instructions": [
|
|
1133
|
+
{"type": "COPY", "input": 0, "output": 1, "internal_index": 0},
|
|
1134
|
+
{"type": "CLIP_ELEMENTWISE", "input": 1, "parameters_min": 0, "parameters_max": 1},
|
|
1135
|
+
],
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
def test_clip_vectorized_no_bounds_raises():
|
|
1140
|
+
"""Tests that Clip raises ValueError when neither bound is provided."""
|
|
1141
|
+
import pytest
|
|
1142
|
+
with pytest.raises(ValueError, match="at least one"):
|
|
1143
|
+
ClipVectorized()
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
def test_clip_vectorized_in_pipeline():
|
|
1147
|
+
"""Tests Clip works end-to-end in a model with Dense layers."""
|
|
1148
|
+
input_buffer = InputBuffer(4)
|
|
1149
|
+
hidden = Dense(8, activation="relu", name="h1")(input_buffer)
|
|
1150
|
+
clipped = ClipVectorized(clip_min=0.0, clip_max=5.0, in_place=True)(hidden)
|
|
1151
|
+
output = Dense(1, activation="sigmoid", name="out")(clipped)
|
|
1152
|
+
|
|
1153
|
+
model = ModelGraph(input_buffer, output)
|
|
1154
|
+
model.compile(optimizer="adam", loss="binary_crossentropy")
|
|
1155
|
+
|
|
1156
|
+
x_data = np.random.random((10, 4)).astype(np.float32)
|
|
1157
|
+
y_data = np.random.randint(0, 2, size=(10, 1)).astype(np.float32)
|
|
1158
|
+
model.fit(x_data, y_data, epochs=1, verbose=0)
|
|
1159
|
+
|
|
1160
|
+
torch_pred = model.predict(x_data, verbose=0)
|
|
1161
|
+
inst_model = model.create_instruction_model()
|
|
1162
|
+
inst_model["validation_data"] = {
|
|
1163
|
+
"inputs": x_data.tolist(),
|
|
1164
|
+
"expected_outputs": torch_pred.tolist(),
|
|
1165
|
+
}
|
|
1166
|
+
validate_instruction_model(inst_model)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|