mttf 1.3.6__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.
Files changed (47) hide show
  1. mt/keras/__init__.py +8 -0
  2. mt/keras_src/__init__.py +16 -0
  3. mt/keras_src/applications_src/__init__.py +33 -0
  4. mt/keras_src/applications_src/classifier.py +497 -0
  5. mt/keras_src/applications_src/mobilenet_v3_split.py +544 -0
  6. mt/keras_src/applications_src/mobilevit.py +292 -0
  7. mt/keras_src/base.py +28 -0
  8. mt/keras_src/constraints_src/__init__.py +14 -0
  9. mt/keras_src/constraints_src/center_around.py +19 -0
  10. mt/keras_src/layers_src/__init__.py +43 -0
  11. mt/keras_src/layers_src/counter.py +27 -0
  12. mt/keras_src/layers_src/floor.py +24 -0
  13. mt/keras_src/layers_src/identical.py +15 -0
  14. mt/keras_src/layers_src/image_sizing.py +1605 -0
  15. mt/keras_src/layers_src/normed_conv2d.py +239 -0
  16. mt/keras_src/layers_src/simple_mha.py +472 -0
  17. mt/keras_src/layers_src/soft_bend.py +36 -0
  18. mt/keras_src/layers_src/transformer_encoder.py +246 -0
  19. mt/keras_src/layers_src/utils.py +88 -0
  20. mt/keras_src/layers_src/var_regularizer.py +38 -0
  21. mt/tf/__init__.py +10 -0
  22. mt/tf/init.py +25 -0
  23. mt/tf/keras_applications/__init__.py +5 -0
  24. mt/tf/keras_layers/__init__.py +5 -0
  25. mt/tf/mttf_version.py +5 -0
  26. mt/tf/utils.py +44 -0
  27. mt/tf/version.py +5 -0
  28. mt/tfc/__init__.py +291 -0
  29. mt/tfg/__init__.py +8 -0
  30. mt/tfp/__init__.py +11 -0
  31. mt/tfp/real_nvp.py +116 -0
  32. mttf-1.3.6.data/scripts/dmt_build_package_and_upload_to_nexus.sh +25 -0
  33. mttf-1.3.6.data/scripts/dmt_pipi.sh +7 -0
  34. mttf-1.3.6.data/scripts/dmt_twineu.sh +2 -0
  35. mttf-1.3.6.data/scripts/pipi.sh +7 -0
  36. mttf-1.3.6.data/scripts/user_build_package_and_upload_to_nexus.sh +25 -0
  37. mttf-1.3.6.data/scripts/user_pipi.sh +8 -0
  38. mttf-1.3.6.data/scripts/user_twineu.sh +3 -0
  39. mttf-1.3.6.data/scripts/wml_build_package_and_upload_to_nexus.sh +25 -0
  40. mttf-1.3.6.data/scripts/wml_nexus.py +50 -0
  41. mttf-1.3.6.data/scripts/wml_pipi.sh +7 -0
  42. mttf-1.3.6.data/scripts/wml_twineu.sh +2 -0
  43. mttf-1.3.6.dist-info/METADATA +18 -0
  44. mttf-1.3.6.dist-info/RECORD +47 -0
  45. mttf-1.3.6.dist-info/WHEEL +5 -0
  46. mttf-1.3.6.dist-info/licenses/LICENSE +21 -0
  47. mttf-1.3.6.dist-info/top_level.txt +1 -0
@@ -0,0 +1,246 @@
1
+ from .. import layers, activations, initializers
2
+
3
+
4
+ class MTTransformerEncoder(layers.Layer):
5
+ """Transformer encoder.
6
+
7
+ This layer was ripped from [KerasHub](https://github.com/keras-team/keras-hub/blob/master/keras_hub/src/layers/modeling/transformer_encoder.py)
8
+ by MT. It has been adapted and simplified to work with Keras 2. It does not
9
+ take as input attention mask or padding mask, but instead computes them
10
+ internally.
11
+
12
+ This class follows the architecture of the transformer encoder layer in the
13
+ paper [Attention is All You Need](https://arxiv.org/abs/1706.03762). Users
14
+ can instantiate multiple instances of this class to stack up an encoder.
15
+
16
+ Args:
17
+ intermediate_dim: int, the hidden size of feedforward network.
18
+ num_heads: int, the number of heads in the
19
+ `keras.layers.MultiHeadAttention` layer.
20
+ dropout: float. the dropout value, shared by
21
+ `keras.layers.MultiHeadAttention` and feedforward network.
22
+ Defaults to `0.`.
23
+ activation: string or `keras.activations`. the
24
+ activation function of feedforward network.
25
+ Defaults to `"relu"`.
26
+ layer_norm_epsilon: float. The epsilon value in layer
27
+ normalization components. Defaults to `1e-5`.
28
+ kernel_initializer: string or `keras.initializers` initializer.
29
+ The kernel initializer for the dense and multiheaded
30
+ attention layers. Defaults to `"glorot_uniform"`.
31
+ bias_initializer: string or `keras.initializers` initializer.
32
+ The bias initializer for the dense and multiheaded
33
+ attention layers. Defaults to `"zeros"`.
34
+ normalize_first: bool. If True, the inputs to the
35
+ attention layer and the intermediate dense layer are normalized
36
+ (similar to GPT-2). If set to False, outputs of attention layer and
37
+ intermediate dense layer are normalized (similar to BERT).
38
+ Defaults to `False`.
39
+ **kwargs: other keyword arguments passed to `keras.layers.Layer`,
40
+ including `name`, `trainable`, `dtype` etc.
41
+
42
+ Example:
43
+
44
+ ```python
45
+ # Create a single transformer encoder layer.
46
+ encoder = keras_hub.layers.MTTransformerEncoder(
47
+ intermediate_dim=64, num_heads=8)
48
+
49
+ # Create a simple model containing the encoder.
50
+ input = keras.Input(shape=(10, 64))
51
+ output = encoder(input)
52
+ model = keras.Model(inputs=input, outputs=output)
53
+
54
+ # Call encoder on the inputs.
55
+ input_data = np.random.uniform(size=(2, 10, 64))
56
+ output = model(input_data)
57
+ ```
58
+
59
+ References:
60
+ - [Vaswani et al., 2017](https://arxiv.org/abs/1706.03762)
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ intermediate_dim,
66
+ num_heads,
67
+ dropout=0,
68
+ activation="relu",
69
+ layer_norm_epsilon=1e-05,
70
+ kernel_initializer="glorot_uniform",
71
+ bias_initializer="zeros",
72
+ normalize_first=False,
73
+ **kwargs,
74
+ ):
75
+ super().__init__(**kwargs)
76
+ self.intermediate_dim = intermediate_dim
77
+ self.num_heads = num_heads
78
+ self.dropout = dropout
79
+ self.activation = activations.get(activation)
80
+ self.layer_norm_epsilon = layer_norm_epsilon
81
+ self.kernel_initializer = initializers.get(kernel_initializer)
82
+ self.bias_initializer = initializers.get(bias_initializer)
83
+ self.normalize_first = normalize_first
84
+ self.supports_masking = True
85
+
86
+ def build(self, inputs_shape):
87
+ # Infer the dimension of our hidden feature size from the build shape.
88
+ hidden_dim = inputs_shape[-1]
89
+ # Attention head size is `hidden_dim` over the number of heads.
90
+ key_dim = int(hidden_dim // self.num_heads)
91
+ if key_dim == 0:
92
+ raise ValueError(
93
+ "Attention `key_dim` computed cannot be zero. "
94
+ f"The `hidden_dim` value of {hidden_dim} has to be equal to "
95
+ f"or greater than `num_heads` value of {self.num_heads}."
96
+ )
97
+
98
+ # Self attention layers.
99
+ self._self_attention_layer = layers.MultiHeadAttention(
100
+ num_heads=self.num_heads,
101
+ key_dim=key_dim,
102
+ dropout=self.dropout,
103
+ kernel_initializer=self.kernel_initializer,
104
+ bias_initializer=self.bias_initializer,
105
+ dtype=self.dtype_policy,
106
+ name="self_attention_layer",
107
+ )
108
+ if hasattr(self._self_attention_layer, "_build_from_signature"):
109
+ self._self_attention_layer._build_from_signature(
110
+ query=inputs_shape,
111
+ value=inputs_shape,
112
+ )
113
+ else:
114
+ self._self_attention_layer.build(
115
+ query_shape=inputs_shape,
116
+ value_shape=inputs_shape,
117
+ )
118
+ self._self_attention_layer_norm = layers.LayerNormalization(
119
+ epsilon=self.layer_norm_epsilon,
120
+ dtype=self.dtype_policy,
121
+ name="self_attention_layer_norm",
122
+ )
123
+ self._self_attention_layer_norm.build(inputs_shape)
124
+ self._self_attention_dropout = layers.Dropout(
125
+ rate=self.dropout,
126
+ dtype=self.dtype_policy,
127
+ name="self_attention_dropout",
128
+ )
129
+
130
+ # Feedforward layers.
131
+ self._feedforward_layer_norm = layers.LayerNormalization(
132
+ epsilon=self.layer_norm_epsilon,
133
+ dtype=self.dtype_policy,
134
+ name="feedforward_layer_norm",
135
+ )
136
+ self._feedforward_layer_norm.build(inputs_shape)
137
+ self._feedforward_intermediate_dense = layers.Dense(
138
+ self.intermediate_dim,
139
+ activation=self.activation,
140
+ kernel_initializer=self.kernel_initializer,
141
+ bias_initializer=self.bias_initializer,
142
+ dtype=self.dtype_policy,
143
+ name="feedforward_intermediate_dense",
144
+ )
145
+ self._feedforward_intermediate_dense.build(inputs_shape)
146
+ self._feedforward_output_dense = layers.Dense(
147
+ hidden_dim,
148
+ kernel_initializer=self.kernel_initializer,
149
+ bias_initializer=self.bias_initializer,
150
+ dtype=self.dtype_policy,
151
+ name="feedforward_output_dense",
152
+ )
153
+ intermediate_shape = list(inputs_shape)
154
+ intermediate_shape[-1] = self.intermediate_dim
155
+ self._feedforward_output_dense.build(tuple(intermediate_shape))
156
+ self._feedforward_dropout = layers.Dropout(
157
+ rate=self.dropout,
158
+ dtype=self.dtype_policy,
159
+ name="feedforward_dropout",
160
+ )
161
+ self.built = True
162
+
163
+ def call(
164
+ self,
165
+ inputs,
166
+ training=None,
167
+ return_attention_scores=False,
168
+ ):
169
+ """Forward pass of the MTTransformerEncoder.
170
+
171
+ Args:
172
+ inputs: a Tensor. The input data to MTTransformerEncoder, should be
173
+ of shape [batch_size, sequence_length, hidden_dim].
174
+ training: a boolean indicating whether the layer should behave in
175
+ training mode or in inference mode.
176
+ return_attention_scores: a boolean indicating whether the output
177
+ should be `(attention_output, attention_scores)` if `True` or
178
+ `attention_output` if `False`. Defaults to `False`.
179
+
180
+ Returns:
181
+ A Tensor of the same shape as the `inputs`.
182
+ """
183
+ x = inputs # Intermediate result.
184
+
185
+ # Self attention block.
186
+ residual = x
187
+ if self.normalize_first:
188
+ x = self._self_attention_layer_norm(x)
189
+
190
+ if return_attention_scores:
191
+ x, attention_scores = self._self_attention_layer(
192
+ query=x,
193
+ value=x,
194
+ return_attention_scores=return_attention_scores,
195
+ training=training,
196
+ )
197
+ else:
198
+ x = self._self_attention_layer(
199
+ query=x,
200
+ value=x,
201
+ training=training,
202
+ )
203
+
204
+ x = self._self_attention_dropout(x, training=training)
205
+ x = x + residual
206
+ if not self.normalize_first:
207
+ x = self._self_attention_layer_norm(x)
208
+
209
+ # Feedforward block.
210
+ residual = x
211
+ if self.normalize_first:
212
+ x = self._feedforward_layer_norm(x)
213
+ x = self._feedforward_intermediate_dense(x)
214
+ x = self._feedforward_output_dense(x)
215
+ x = self._feedforward_dropout(x, training=training)
216
+ x = x + residual
217
+ if not self.normalize_first:
218
+ x = self._feedforward_layer_norm(x)
219
+
220
+ if return_attention_scores:
221
+ return x, attention_scores
222
+
223
+ return x
224
+
225
+ def get_config(self):
226
+ config = super().get_config()
227
+ config.update(
228
+ {
229
+ "intermediate_dim": self.intermediate_dim,
230
+ "num_heads": self.num_heads,
231
+ "dropout": self.dropout,
232
+ "activation": activations.serialize(self.activation),
233
+ "layer_norm_epsilon": self.layer_norm_epsilon,
234
+ "kernel_initializer": initializers.serialize(
235
+ self.kernel_initializer
236
+ ),
237
+ "bias_initializer": initializers.serialize(
238
+ self.bias_initializer
239
+ ),
240
+ "normalize_first": self.normalize_first,
241
+ }
242
+ )
243
+ return config
244
+
245
+ def compute_output_shape(self, inputs_shape):
246
+ return inputs_shape
@@ -0,0 +1,88 @@
1
+ """Useful subroutines dealing with GPU devices."""
2
+
3
+ from mt import tp, tfc
4
+
5
+
6
+ def conv2d(name_scope: tfc.NameScope, x, filters, kernel_size, **kwargs):
7
+ """Wrapper of Keras Conv2D layer with a LayerNormalization layer.
8
+
9
+ Parameters
10
+ ----------
11
+ name_scope : mt.tfc.NameScope
12
+ the name scope. For every conv2d invocation, the name scope is iterated.
13
+ x : tensor-like
14
+ Keras tensor or TF tensor as input
15
+ filters : int
16
+ The dimensionality of the output space (i.e. the number of output filters in the
17
+ convolution).
18
+ kernel_size : int or tuple or list
19
+ An integer or tuple/list of 2 integers, specifying the height and width of the 2D
20
+ convolution window. Can be a single integer to specify the same value for all spatial
21
+ dimensions.
22
+ **kwargs : dict
23
+ all other keyword arguments to be passed as-is to Conv2D layer construction
24
+
25
+ Returns
26
+ -------
27
+ tensor-like
28
+ TF tensor as output
29
+ """
30
+
31
+ from .. import layers
32
+
33
+ next(name_scope)
34
+ x = layers.LayerNormalization(name=name_scope("prenorm"))(x)
35
+ x = layers.Conv2D(filters, kernel_size, name=name_scope("conv"), **kwargs)(x)
36
+
37
+ return x
38
+
39
+
40
+ def dense2d(
41
+ name_scope: tfc.NameScope, x, filters, kernel_size, activation="tanh", **kwargs
42
+ ):
43
+ """Wrapper of Keras Conv2D layer with a LayerNormalization layer.
44
+
45
+ TBD. But basically, prenorm, then expand to twice the dim, then prenorm, then project to target
46
+ dim with target kernel size
47
+
48
+ Parameters
49
+ ----------
50
+ name_scope : mt.tfc.NameScope
51
+ the name scope. For every conv2d invocation, the name scope is iterated.
52
+ x : tensor-like
53
+ Keras tensor or TF tensor as input
54
+ filters : int
55
+ The dimensionality of the output space (i.e. the number of output filters in the
56
+ convolution).
57
+ kernel_size : int or tuple or list
58
+ An integer or tuple/list of 2 integers, specifying the height and width of the 2D
59
+ convolution window. Can be a single integer to specify the same value for all spatial
60
+ dimensions.
61
+ activation : object
62
+ the activation of the last conv layer
63
+ **kwargs : dict
64
+ all other keyword arguments to be passed as-is to Conv2D layer construction
65
+
66
+ Returns
67
+ -------
68
+ tensor-like
69
+ TF tensor as output
70
+ """
71
+
72
+ from .. import layers
73
+
74
+ next(name_scope)
75
+ x = layers.LayerNormalization(name=name_scope("expand_prenorm"))(x)
76
+ x = layers.Conv2D(
77
+ x.shape[3] * 2, 1, name=name_scope("expand"), activation="relu", **kwargs
78
+ )(x)
79
+ x = layers.LayerNormalization(name=name_scope("project_prenorm"))(x)
80
+ x = layers.Conv2D(
81
+ filters,
82
+ kernel_size,
83
+ name=name_scope("project"),
84
+ activation=activation,
85
+ **kwargs
86
+ )(x)
87
+
88
+ return x
@@ -0,0 +1,38 @@
1
+ import tensorflow as tf
2
+
3
+ from mt import tp
4
+
5
+ from .. import layers
6
+
7
+
8
+ class VarianceRegularizer(layers.Layer):
9
+ """A regularizer on the variance of the input tensor.
10
+
11
+ Negative rate for making the variance larger. Positive rate for making the variance smaller.
12
+ """
13
+
14
+ def __init__(self, rate=1e-2, l_axes: list = [-1], **kwargs):
15
+ super(VarianceRegularizer, self).__init__(**kwargs)
16
+ self.rate = rate
17
+ self.l_axes = l_axes
18
+
19
+ def call(self, x):
20
+ mean = tf.reduce_mean(x, axis=self.l_axes, keepdims=True)
21
+ err = x - mean
22
+ esq = err * err
23
+ var = tf.reduce_mean(esq, axis=self.l_axes)
24
+ sum_var = tf.reduce_sum(var)
25
+ self.add_loss(self.rate * sum_var)
26
+ return x
27
+
28
+ call.__doc__ = layers.Layer.call.__doc__
29
+
30
+ def get_config(self):
31
+ config = {
32
+ "rate": self.rate,
33
+ "l_axes": self.l_axes,
34
+ }
35
+ base_config = super(VarianceRegularizer, self).get_config()
36
+ return dict(list(base_config.items()) + list(config.items()))
37
+
38
+ get_config.__doc__ = layers.Layer.get_config.__doc__
mt/tf/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ from .mttf_version import version as __mttf_version__
2
+ from .init import init as mttf_init
3
+ from .utils import *
4
+ from tensorflow import *
5
+ import tensorflow.compiler as compiler
6
+ import tensorflow.core as core
7
+ import tensorflow.keras as keras
8
+ import tensorflow.lite as lite
9
+ import tensorflow.python as python
10
+ import tensorflow.tools as tools
mt/tf/init.py ADDED
@@ -0,0 +1,25 @@
1
+ """Initialises TensorFlow, monkey-patching if necessary."""
2
+
3
+ from packaging.version import Version
4
+
5
+ __all__ = ["init"]
6
+
7
+
8
+ def init():
9
+ """Initialises tensorflow, monkey-patching if necessary."""
10
+
11
+ import tensorflow
12
+ import sys
13
+
14
+ tf_ver = Version(tensorflow.__version__)
15
+
16
+ if tf_ver < Version("2.10"):
17
+ raise ImportError(
18
+ f"The minimum TF version that mttf supports is 2.10. Your TF is {tf_ver}. "
19
+ "Please upgrade."
20
+ )
21
+
22
+ return tensorflow
23
+
24
+
25
+ init()
@@ -0,0 +1,5 @@
1
+ from mt.logg import logger
2
+
3
+ logger.warn_module_move("mt.tf.keras_applications", "mt.keras.applications")
4
+
5
+ from mt.keras.applications import *
@@ -0,0 +1,5 @@
1
+ from mt.logg import logger
2
+
3
+ logger.warn_module_move("mt.tf.keras_layers", "mt.keras.layers")
4
+
5
+ from mt.keras.layers import *
mt/tf/mttf_version.py ADDED
@@ -0,0 +1,5 @@
1
+ MAJOR_VERSION = 1
2
+ MINOR_VERSION = 0
3
+ PATCH_VERSION = 0
4
+ version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
+ __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']
mt/tf/utils.py ADDED
@@ -0,0 +1,44 @@
1
+ """Useful subroutines dealing with GPU devices."""
2
+
3
+ from mt import tp, np, tfc
4
+
5
+ __api__ = ["gpus_in_tf_format", "as_floatx", "sigmoid", "asigmoid"]
6
+
7
+
8
+ NameScope = tfc.NameScope # for backward compatibility
9
+
10
+
11
+ def gpus_in_tf_format(gpus):
12
+ """Converts a gpu list or a gpu count into a list of GPUs in TF format."""
13
+
14
+ if isinstance(gpus, int):
15
+ gpus = range(gpus)
16
+ return ["/GPU:{}".format(x) for x in gpus]
17
+
18
+
19
+ def as_floatx(x):
20
+ """Ensures that a tensor is of dtype floatx."""
21
+
22
+ import tensorflow as tf
23
+
24
+ if not np.issubdtype(x.dtype.as_numpy_dtype, np.floating):
25
+ x = tf.cast(x, tf.keras.backend.floatx())
26
+ return x
27
+
28
+
29
+ def sigmoid(x):
30
+ """Stable sigmoid, taken from tfp."""
31
+
32
+ import tensorflow as tf
33
+
34
+ x = tf.convert_to_tensor(x)
35
+ cutoff = -20 if x.dtype == tf.float64 else -9
36
+ return tf.where(x < cutoff, tf.exp(x), tf.math.sigmoid(x))
37
+
38
+
39
+ def asigmoid(y):
40
+ """Inverse of sigmoid, taken from tfp."""
41
+
42
+ import tensorflow as tf
43
+
44
+ return tf.math.log(y) - tf.math.log1p(-y)
mt/tf/version.py ADDED
@@ -0,0 +1,5 @@
1
+ MAJOR_VERSION = 1
2
+ MINOR_VERSION = 3
3
+ PATCH_VERSION = 6
4
+ version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
+ __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']