instmodel 1.2.2__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.2/instmodel.egg-info → instmodel-1.3.0}/PKG-INFO +1 -1
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel/instruction_model.py +12 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel/tf.py +126 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel/torch.py +114 -0
- {instmodel-1.2.2 → instmodel-1.3.0/instmodel.egg-info}/PKG-INFO +1 -1
- {instmodel-1.2.2 → instmodel-1.3.0}/setup.cfg +1 -1
- {instmodel-1.2.2 → instmodel-1.3.0}/tests/test_tf.py +174 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/tests/test_torch.py +174 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/LICENSE +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/MANIFEST.in +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/README.md +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel/__init__.py +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel/training_utils.py +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel.egg-info/SOURCES.txt +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel.egg-info/dependency_links.txt +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel.egg-info/requires.txt +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/instmodel.egg-info/top_level.txt +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/pyproject.toml +0 -0
- {instmodel-1.2.2 → instmodel-1.3.0}/setup.py +0 -0
|
@@ -326,6 +326,14 @@ def instruction_model_inference(model, input_data):
|
|
|
326
326
|
param = parameters[instruction["parameters"]]
|
|
327
327
|
buffers[input_index] = input_data_buffer * param
|
|
328
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
|
+
|
|
329
337
|
# Ensure the source buffer (still unassigned to an output) is float32.
|
|
330
338
|
buffers[input_index] = np.array(buffers[input_index], dtype=np.float32)
|
|
331
339
|
|
|
@@ -382,6 +390,10 @@ def concatenate_instruction_models(models, start_from_first=False):
|
|
|
382
390
|
new_instruction["weights"] += w_offset
|
|
383
391
|
if "parameters" in new_instruction:
|
|
384
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
|
|
385
397
|
if "map" in new_instruction:
|
|
386
398
|
new_instruction["map"] += m_offset
|
|
387
399
|
if "input" in new_instruction:
|
|
@@ -1213,6 +1213,132 @@ class ShiftVectorized(ComputationOp):
|
|
|
1213
1213
|
return target_index
|
|
1214
1214
|
|
|
1215
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
|
+
|
|
1216
1342
|
class Attention(ComputationOp):
|
|
1217
1343
|
"""
|
|
1218
1344
|
An attention operation that computes softmax attention from a key buffer and applies it
|
|
@@ -897,6 +897,120 @@ class ShiftVectorized(ComputationOp):
|
|
|
897
897
|
return target_index
|
|
898
898
|
|
|
899
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
|
+
|
|
900
1014
|
class Attention(ComputationOp):
|
|
901
1015
|
def __init__(self, name=None):
|
|
902
1016
|
super().__init__()
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from instmodel.tf import (
|
|
2
2
|
ActivationComputation,
|
|
3
3
|
Attention,
|
|
4
|
+
ClipVectorized,
|
|
4
5
|
Concatenate,
|
|
5
6
|
Dense,
|
|
6
7
|
InputBuffer,
|
|
@@ -1040,3 +1041,176 @@ def test_sign_activation():
|
|
|
1040
1041
|
assert np.array_equal(np.maximum(0, result_tf), step_expected), (
|
|
1041
1042
|
"RELU(SIGN(x)) should produce step function"
|
|
1042
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,6 +1,7 @@
|
|
|
1
1
|
from instmodel.torch import (
|
|
2
2
|
ActivationComputation,
|
|
3
3
|
Attention,
|
|
4
|
+
ClipVectorized,
|
|
4
5
|
Concatenate,
|
|
5
6
|
Dense,
|
|
6
7
|
InputBuffer,
|
|
@@ -990,3 +991,176 @@ def test_sign_activation():
|
|
|
990
991
|
assert np.array_equal(np.maximum(0, result_torch), step_expected), (
|
|
991
992
|
"RELU(SIGN(x)) should produce step function"
|
|
992
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
|