instmodel 1.2.1__tar.gz → 1.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: instmodel
3
- Version: 1.2.1
3
+ Version: 1.2.2
4
4
  Summary: A package that provides advanced coding tools for training and deploying AI
5
5
  Home-page: https://github.com/Joaoprcf/instmodel
6
6
  Author: Joao Ferreira
@@ -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
 
@@ -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
 
@@ -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
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: instmodel
3
- Version: 1.2.1
3
+ Version: 1.2.2
4
4
  Summary: A package that provides advanced coding tools for training and deploying AI
5
5
  Home-page: https://github.com/Joaoprcf/instmodel
6
6
  Author: Joao Ferreira
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = instmodel
3
- version = 1.2.1
3
+ version = 1.2.2
4
4
  author = Joao Ferreira
5
5
  author_email = joaoprcf@gmail.com
6
6
  description = A package that provides advanced coding tools for training and deploying AI
@@ -1,4 +1,5 @@
1
1
  from instmodel.tf import (
2
+ ActivationComputation,
2
3
  Attention,
3
4
  Concatenate,
4
5
  Dense,
@@ -983,3 +984,59 @@ def test_multiply_heads_scalar():
983
984
  # Validate using instruction_model_inference directly (supports list inputs)
984
985
  *_, inst_output = instruction_model_inference(inst_model, [data, scalar])
985
986
  assert np.allclose(inst_output, keras_pred, atol=1e-6)
987
+
988
+
989
+ def test_exp_activation():
990
+ """Tests EXP activation: Keras Lambda, NumPy, and instruction model agree; clamped at [-88, 88]."""
991
+ x_np = np.array([-100.0, -88.0, -1.0, 0.0, 1.0, 88.0, 100.0], dtype=np.float32)
992
+ expected = np.exp(np.clip(x_np, -88, 88).astype(np.float64)).astype(np.float32)
993
+
994
+ # TF/Keras backend via ActivationComputation
995
+ inp = InputBuffer(len(x_np))
996
+ out = ActivationComputation("EXP", in_place=False)(inp)
997
+ model = ModelGraph(inp, out)
998
+ result_tf = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
999
+ assert np.allclose(result_tf, expected, atol=1e-6), (
1000
+ f"TF EXP mismatch: {result_tf} vs {expected}"
1001
+ )
1002
+
1003
+ # Instruction model round-trip
1004
+ inst_model = model.create_instruction_model()
1005
+ inst_model["validation_data"] = {
1006
+ "inputs": x_np.reshape(1, -1).tolist(),
1007
+ "expected_outputs": result_tf.reshape(1, -1).tolist(),
1008
+ }
1009
+ validate_instruction_model(inst_model)
1010
+
1011
+ # Clamping: exp(-100) == exp(-88), exp(100) == exp(88)
1012
+ assert np.isclose(result_tf[0], result_tf[1], atol=1e-6), "EXP(-100) should be clamped to exp(-88)"
1013
+ assert np.isclose(result_tf[-1], result_tf[-2], atol=1e-6), "EXP(100) should be clamped to exp(88)"
1014
+
1015
+
1016
+ def test_sign_activation():
1017
+ """Tests SIGN activation: Keras Lambda, NumPy, and instruction model agree; returns -1, 0, or 1."""
1018
+ x_np = np.array([-5.0, -0.01, 0.0, 0.01, 5.0], dtype=np.float32)
1019
+ expected = np.array([-1.0, -1.0, 0.0, 1.0, 1.0], dtype=np.float32)
1020
+
1021
+ # TF/Keras backend via ActivationComputation
1022
+ inp = InputBuffer(len(x_np))
1023
+ out = ActivationComputation("SIGN", in_place=False)(inp)
1024
+ model = ModelGraph(inp, out)
1025
+ result_tf = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
1026
+ assert np.array_equal(result_tf, expected), (
1027
+ f"TF SIGN mismatch: {result_tf} vs {expected}"
1028
+ )
1029
+
1030
+ # Instruction model round-trip
1031
+ inst_model = model.create_instruction_model()
1032
+ inst_model["validation_data"] = {
1033
+ "inputs": x_np.reshape(1, -1).tolist(),
1034
+ "expected_outputs": result_tf.reshape(1, -1).tolist(),
1035
+ }
1036
+ validate_instruction_model(inst_model)
1037
+
1038
+ # RELU(SIGN(x)) == STEP(x)
1039
+ step_expected = np.array([0.0, 0.0, 0.0, 1.0, 1.0], dtype=np.float32)
1040
+ assert np.array_equal(np.maximum(0, result_tf), step_expected), (
1041
+ "RELU(SIGN(x)) should produce step function"
1042
+ )
@@ -1,4 +1,5 @@
1
1
  from instmodel.torch import (
2
+ ActivationComputation,
2
3
  Attention,
3
4
  Concatenate,
4
5
  Dense,
@@ -917,3 +918,75 @@ def test_multiply_heads_scalar():
917
918
 
918
919
  *_, inst_output = instruction_model_inference(inst_model, [data, scalar])
919
920
  assert np.allclose(inst_output, torch_pred, atol=1e-6)
921
+
922
+
923
+ def test_exp_activation():
924
+ """Tests EXP activation: PyTorch, NumPy, and instruction model agree; clamped at [-88, 88]."""
925
+ from instmodel.torch import _apply_activation
926
+
927
+ x_np = np.array([-100.0, -88.0, -1.0, 0.0, 1.0, 88.0, 100.0], dtype=np.float32)
928
+ expected = np.exp(np.clip(x_np, -88, 88).astype(np.float64)).astype(np.float32)
929
+
930
+ # PyTorch backend
931
+ result_torch = _apply_activation(torch.tensor(x_np), "EXP").numpy()
932
+ assert np.allclose(result_torch, expected, atol=1e-6), (
933
+ f"PyTorch EXP mismatch: {result_torch} vs {expected}"
934
+ )
935
+
936
+ # ActivationComputation + ModelGraph round-trip
937
+ inp = InputBuffer(len(x_np))
938
+ out = ActivationComputation("EXP", in_place=False)(inp)
939
+ model = ModelGraph(inp, out)
940
+ model_pred = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
941
+ assert np.allclose(model_pred, expected, atol=1e-6), (
942
+ f"ModelGraph EXP mismatch: {model_pred} vs {expected}"
943
+ )
944
+
945
+ # Instruction model round-trip
946
+ inst_model = model.create_instruction_model()
947
+ inst_model["validation_data"] = {
948
+ "inputs": x_np.reshape(1, -1).tolist(),
949
+ "expected_outputs": model_pred.reshape(1, -1).tolist(),
950
+ }
951
+ validate_instruction_model(inst_model)
952
+
953
+ # Clamping: exp(-100) == exp(-88), exp(100) == exp(88)
954
+ assert np.isclose(result_torch[0], result_torch[1], atol=1e-6), "EXP(-100) should be clamped to exp(-88)"
955
+ assert np.isclose(result_torch[-1], result_torch[-2], atol=1e-6), "EXP(100) should be clamped to exp(88)"
956
+
957
+
958
+ def test_sign_activation():
959
+ """Tests SIGN activation: PyTorch, NumPy, and instruction model agree; returns -1, 0, or 1."""
960
+ from instmodel.torch import _apply_activation
961
+
962
+ x_np = np.array([-5.0, -0.01, 0.0, 0.01, 5.0], dtype=np.float32)
963
+ expected = np.array([-1.0, -1.0, 0.0, 1.0, 1.0], dtype=np.float32)
964
+
965
+ # PyTorch backend
966
+ result_torch = _apply_activation(torch.tensor(x_np), "SIGN").numpy()
967
+ assert np.array_equal(result_torch, expected), (
968
+ f"PyTorch SIGN mismatch: {result_torch} vs {expected}"
969
+ )
970
+
971
+ # ActivationComputation + ModelGraph round-trip
972
+ inp = InputBuffer(len(x_np))
973
+ out = ActivationComputation("SIGN", in_place=False)(inp)
974
+ model = ModelGraph(inp, out)
975
+ model_pred = model.predict(x_np.reshape(1, -1), verbose=0).flatten()
976
+ assert np.array_equal(model_pred, expected), (
977
+ f"ModelGraph SIGN mismatch: {model_pred} vs {expected}"
978
+ )
979
+
980
+ # Instruction model round-trip
981
+ inst_model = model.create_instruction_model()
982
+ inst_model["validation_data"] = {
983
+ "inputs": x_np.reshape(1, -1).tolist(),
984
+ "expected_outputs": model_pred.reshape(1, -1).tolist(),
985
+ }
986
+ validate_instruction_model(inst_model)
987
+
988
+ # RELU(SIGN(x)) == STEP(x)
989
+ step_expected = np.array([0.0, 0.0, 0.0, 1.0, 1.0], dtype=np.float32)
990
+ assert np.array_equal(np.maximum(0, result_torch), step_expected), (
991
+ "RELU(SIGN(x)) should produce step function"
992
+ )
File without changes
File without changes
File without changes
File without changes
File without changes