keras-hub-nightly 0.21.0.dev202505260411__py3-none-any.whl → 0.21.0.dev202505280410__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.
@@ -3,6 +3,7 @@ from keras_hub.src.layers.preprocessing.preprocessing_layer import (
3
3
  PreprocessingLayer,
4
4
  )
5
5
  from keras_hub.src.utils.tensor_utils import convert_to_ragged_batch
6
+ from keras_hub.src.utils.tensor_utils import pad
6
7
  from keras_hub.src.utils.tensor_utils import preprocessing_function
7
8
 
8
9
  try:
@@ -66,6 +67,8 @@ class MultiSegmentPacker(PreprocessingLayer):
66
67
  "waterfall" algorithm that allocates quota in a
67
68
  left-to-right manner and fills up the buckets until we run
68
69
  out of budget. It support arbitrary number of segments.
70
+ padding_side: str. Whether to pad the input on the "left" or "right".
71
+ Defaults to "right".
69
72
 
70
73
  Returns:
71
74
  A tuple with two elements. The first is the dense, packed token
@@ -124,6 +127,7 @@ class MultiSegmentPacker(PreprocessingLayer):
124
127
  sep_value=None,
125
128
  pad_value=None,
126
129
  truncate="round_robin",
130
+ padding_side="right",
127
131
  **kwargs,
128
132
  ):
129
133
  super().__init__(**kwargs)
@@ -162,6 +166,7 @@ class MultiSegmentPacker(PreprocessingLayer):
162
166
  self.end_value = end_value
163
167
 
164
168
  self.pad_value = pad_value
169
+ self.padding_side = padding_side
165
170
 
166
171
  def get_config(self):
167
172
  config = super().get_config()
@@ -173,6 +178,7 @@ class MultiSegmentPacker(PreprocessingLayer):
173
178
  "sep_value": self._sep_value,
174
179
  "pad_value": self.pad_value,
175
180
  "truncate": self.truncate,
181
+ "padding_side": self.padding_side,
176
182
  }
177
183
  )
178
184
  return config
@@ -287,10 +293,18 @@ class MultiSegmentPacker(PreprocessingLayer):
287
293
  # Pad to dense tensor output.
288
294
  sequence_length = sequence_length or self.sequence_length
289
295
  shape = tf.cast([-1, sequence_length], "int64")
290
- token_ids = token_ids.to_tensor(
291
- shape=shape, default_value=self.pad_value
296
+ token_ids = pad(
297
+ token_ids,
298
+ shape=shape,
299
+ padding_side=self.padding_side,
300
+ pad_value=self.pad_value,
301
+ )
302
+ segment_ids = pad(
303
+ segment_ids,
304
+ shape=shape,
305
+ padding_side=self.padding_side,
306
+ pad_value=0,
292
307
  )
293
- segment_ids = segment_ids.to_tensor(shape=shape)
294
308
  # Remove the batch dim if added.
295
309
  if unbatched:
296
310
  token_ids = tf.squeeze(token_ids, 0)
@@ -3,6 +3,7 @@ from keras_hub.src.layers.preprocessing.preprocessing_layer import (
3
3
  PreprocessingLayer,
4
4
  )
5
5
  from keras_hub.src.utils.tensor_utils import convert_to_ragged_batch
6
+ from keras_hub.src.utils.tensor_utils import pad
6
7
  from keras_hub.src.utils.tensor_utils import preprocessing_function
7
8
 
8
9
  try:
@@ -39,6 +40,8 @@ class StartEndPacker(PreprocessingLayer):
39
40
  0 or "" will be added depending on the dtype of the input tensor.
40
41
  return_padding_mask: bool. Whether to return a boolean padding mask of
41
42
  all locations that are filled in with the `pad_value`.
43
+ padding_side: str. Whether to pad the input on the "left" or "right".
44
+ Defaults to "right".
42
45
 
43
46
  Call arguments:
44
47
  inputs: A `tf.Tensor`, `tf.RaggedTensor`, or list of python strings.
@@ -111,6 +114,7 @@ class StartEndPacker(PreprocessingLayer):
111
114
  pad_value=None,
112
115
  return_padding_mask=False,
113
116
  name=None,
117
+ padding_side="right",
114
118
  **kwargs,
115
119
  ):
116
120
  super().__init__(name=name, **kwargs)
@@ -139,6 +143,7 @@ class StartEndPacker(PreprocessingLayer):
139
143
 
140
144
  self.pad_value = pad_value
141
145
  self.return_padding_mask = return_padding_mask
146
+ self.padding_side = padding_side
142
147
 
143
148
  @preprocessing_function
144
149
  def call(
@@ -154,6 +159,13 @@ class StartEndPacker(PreprocessingLayer):
154
159
  batch_size = tf.shape(x)[0]
155
160
  sequence_length = sequence_length or self.sequence_length
156
161
  dtype = inputs.dtype
162
+ # Truncate.
163
+ truncation_length = sequence_length
164
+ if add_start_value and self.start_value is not None:
165
+ truncation_length -= len(self.start_value)
166
+ if add_end_value and self.end_value is not None:
167
+ truncation_length -= len(self.end_value)
168
+ x = x[..., :truncation_length]
157
169
 
158
170
  # Concatenate start and end tokens.
159
171
  if add_start_value and self.start_value is not None:
@@ -167,23 +179,28 @@ class StartEndPacker(PreprocessingLayer):
167
179
  end_token_id_tensor = tf.repeat(
168
180
  end_value[tf.newaxis, :], repeats=batch_size, axis=0
169
181
  )
170
- # Trim to leave room for end token.
171
- x = x[..., : sequence_length - len(self.end_value)]
172
182
  x = tf.concat([x, end_token_id_tensor], axis=-1)
173
183
 
174
184
  # Pad to desired length.
175
- outputs = x.to_tensor(
176
- default_value=self.pad_value,
185
+ outputs = pad(
186
+ x,
187
+ pad_value=self.pad_value,
188
+ padding_side=self.padding_side,
177
189
  shape=(batch_size, sequence_length),
178
190
  )
179
191
  outputs = tf.squeeze(outputs, axis=0) if unbatched else outputs
180
192
 
181
193
  if self.return_padding_mask:
182
194
  mask = tf.ones_like(x, dtype="bool")
183
- mask = mask.to_tensor(shape=(batch_size, sequence_length))
195
+
196
+ mask = pad(
197
+ mask,
198
+ pad_value=False,
199
+ padding_side=self.padding_side,
200
+ shape=(batch_size, sequence_length),
201
+ )
184
202
  mask = tf.squeeze(mask, axis=0) if unbatched else mask
185
203
  return outputs, mask
186
-
187
204
  return outputs
188
205
 
189
206
  def get_config(self):
@@ -195,6 +212,7 @@ class StartEndPacker(PreprocessingLayer):
195
212
  "end_value": self._end_value,
196
213
  "pad_value": self.pad_value,
197
214
  "return_padding_mask": self.return_padding_mask,
215
+ "padding_side": self.padding_side,
198
216
  }
199
217
  )
200
218
  return config
@@ -189,23 +189,26 @@ class Backbone(keras.Model):
189
189
  saver = get_preset_saver(preset_dir)
190
190
  saver.save_backbone(self, max_shard_size=max_shard_size)
191
191
 
192
- def get_lora_target_names(self):
193
- """Returns list of layer names which are to be LoRA-fied.
194
-
195
- Subclasses can override this method if the names of layers to be
196
- LoRa-fied are different.
197
- """
192
+ def default_lora_layer_names(self):
193
+ """Returns list of layer names which are to be LoRA-fied."""
198
194
  return ["query_dense", "value_dense", "query", "value"]
199
195
 
200
- def enable_lora(self, rank, target_names=None):
196
+ def enable_lora(self, rank, target_layer_names=None):
201
197
  """Enable Lora on the backbone.
202
198
 
203
199
  Calling this method will freeze all weights on the backbone,
204
200
  while enabling Lora on the query & value `EinsumDense` layers
205
201
  of the attention layers.
202
+
203
+ Args:
204
+ rank: The rank of the LoRA factorization.
205
+ target_layer_names: A list of strings, the names of the layers to
206
+ apply LoRA to. If `None`, this will be populated with the
207
+ default LoRA layer names as returned by
208
+ `backbone.default_lora_layer_names()`.
206
209
  """
207
- if target_names is None:
208
- target_names = self.get_lora_target_names()
210
+ if target_layer_names is None:
211
+ target_layer_names = self.default_lora_layer_names()
209
212
  self.trainable = True
210
213
  self._lora_enabled_layers = []
211
214
  self._lora_rank = rank
@@ -214,7 +217,7 @@ class Backbone(keras.Model):
214
217
  all_layers = self._flatten_layers(include_self=False)
215
218
  all_layers = [lyr for lyr in all_layers if lyr.weights]
216
219
  for i, layer in enumerate(all_layers):
217
- for name in target_names:
220
+ for name in target_layer_names:
218
221
  if layer.name == name:
219
222
  if hasattr(layer, "enable_lora"):
220
223
  layer.trainable = True
@@ -402,8 +402,8 @@ class Gemma3Backbone(Backbone):
402
402
  )
403
403
  return config
404
404
 
405
- def get_lora_target_names(self):
406
- target_names = super().get_lora_target_names()
405
+ def default_lora_layer_names(self):
406
+ target_names = super().default_lora_layer_names()
407
407
 
408
408
  # Add these for `Gemma3VITAttention`.
409
409
  if not self.text_only_model:
@@ -274,8 +274,8 @@ class PaliGemmaBackbone(Backbone):
274
274
  # Keep the image_sequence_length as a backbone property for easy access.
275
275
  self.image_sequence_length = self.vit_encoder.image_sequence_length
276
276
 
277
- def get_lora_target_names(self):
278
- target_names = super().get_lora_target_names()
277
+ def default_lora_layer_names(self):
278
+ target_names = super().default_lora_layer_names()
279
279
 
280
280
  # Add these for `PaliGemmaVITAttention`.
281
281
  target_names += ["query_proj", "value_proj"]
@@ -21,6 +21,20 @@ except ImportError:
21
21
  NO_CONVERT_COUNTER = threading.local()
22
22
 
23
23
 
24
+ def pad(x, shape, padding_side, pad_value):
25
+ if padding_side == "left":
26
+ x = x[..., ::-1]
27
+
28
+ outputs = x.to_tensor(
29
+ default_value=pad_value,
30
+ shape=shape,
31
+ )
32
+
33
+ if padding_side == "left":
34
+ outputs = outputs[..., ::-1]
35
+ return outputs
36
+
37
+
24
38
  @contextlib.contextmanager
25
39
  def no_convert_scope():
26
40
  try:
keras_hub/src/version.py CHANGED
@@ -1,7 +1,7 @@
1
1
  from keras_hub.src.api_export import keras_hub_export
2
2
 
3
3
  # Unique source of truth for the version number.
4
- __version__ = "0.21.0.dev202505260411"
4
+ __version__ = "0.21.0.dev202505280410"
5
5
 
6
6
 
7
7
  @keras_hub_export("keras_hub.version")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: keras-hub-nightly
3
- Version: 0.21.0.dev202505260411
3
+ Version: 0.21.0.dev202505280410
4
4
  Summary: Pretrained models for Keras.
5
5
  Author-email: Keras team <keras-users@googlegroups.com>
6
6
  License-Expression: Apache-2.0
@@ -5,7 +5,7 @@ keras_hub/models/__init__.py,sha256=itSzodVUeuX6HQnmsSXY0Wv-5Htbu397410R-SFW_4I,
5
5
  keras_hub/samplers/__init__.py,sha256=aFQIkiqbZpi8vjrPp2MVII4QUfE-eQjra5fMeHsoy7k,886
6
6
  keras_hub/src/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  keras_hub/src/api_export.py,sha256=9pQZK27JObxWZ96QPLBp1OBsjWigh1iuV6RglPGMRk0,1499
8
- keras_hub/src/version.py,sha256=FubkZAJAXcRoiCMdCqMfSHK7ez2rS1csTt9DB_iw1jk,222
8
+ keras_hub/src/version.py,sha256=LkNchDcdwovYSb5xEjf09V8MHim3X9wy6VrAyA_7afw,222
9
9
  keras_hub/src/layers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  keras_hub/src/layers/modeling/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  keras_hub/src/layers/modeling/alibi_bias.py,sha256=1XBTHI52L_iJDhN_w5ydu_iMhCuTgQAxEPwcLA6BPuk,4411
@@ -28,11 +28,11 @@ keras_hub/src/layers/preprocessing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQe
28
28
  keras_hub/src/layers/preprocessing/audio_converter.py,sha256=YGh_kQw65a1Z6S5zzSNVP-ChyLYHq3-eOYpOS53xIN8,4156
29
29
  keras_hub/src/layers/preprocessing/image_converter.py,sha256=p2CoSV_zfHIVZqLo1hQk2BdOL_RtBlr5wUtgpAmtwwY,15926
30
30
  keras_hub/src/layers/preprocessing/masked_lm_mask_generator.py,sha256=itxWq3FHYlR0I7jKarQlSKbSmRLl9ut_UTSP3ZDwP0A,8162
31
- keras_hub/src/layers/preprocessing/multi_segment_packer.py,sha256=ZNqnUFnc9Af122Q7T6YyUoXgIdU9AgIJfsvR1UrCjFU,12068
31
+ keras_hub/src/layers/preprocessing/multi_segment_packer.py,sha256=APP62tF9Tw4zah7oL5maSYRXMwcR4RwicZMhQq2wRxY,12509
32
32
  keras_hub/src/layers/preprocessing/preprocessing_layer.py,sha256=WyX41b9Ev_YJ5uVQVOAqD0PQasMOPDoyDjl_PkzkAkE,687
33
33
  keras_hub/src/layers/preprocessing/random_deletion.py,sha256=_EmBt4d8TTPLF3OQhA8HoBmej-BX_BocbjeW6jzi6Wo,9768
34
34
  keras_hub/src/layers/preprocessing/random_swap.py,sha256=cV7HqMwu_JHTbhe9UMVAsZdOTLsukyZDteEBYp0idiM,9509
35
- keras_hub/src/layers/preprocessing/start_end_packer.py,sha256=lY2K937z6JucxNe7VknynhhjrcUfFigU6mqIdv2gS-Y,7973
35
+ keras_hub/src/layers/preprocessing/start_end_packer.py,sha256=F_yCyI6yyxAfunb37C0AzFX3lKjaZg08HMjUXOpjgwc,8642
36
36
  keras_hub/src/metrics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
37
  keras_hub/src/metrics/bleu.py,sha256=pnid5azpAxO6vKEfUtAby3nH29OGbwYKgVGOGeoaA3I,13694
38
38
  keras_hub/src/metrics/edit_distance.py,sha256=kjhe8uNjvv8aN49RyrKAbNi7a8_OlB8fMza0J_CfNQg,6353
@@ -43,7 +43,7 @@ keras_hub/src/metrics/rouge_n.py,sha256=JoFtmgjF4Ic263ny6bfD6vMHKreH9le3HnOOxemu
43
43
  keras_hub/src/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
44
44
  keras_hub/src/models/audio_to_text.py,sha256=XoOjXtKBX6K1fz-zOXcdVo3FpjuxCMnJZh2LQcYXb_0,2726
45
45
  keras_hub/src/models/audio_to_text_preprocessor.py,sha256=GS-WWyJ6aSsPRxi_0bxvxA00h2mT2FEwSdAoQXAUYVI,3249
46
- keras_hub/src/models/backbone.py,sha256=KS2x3HFWKhEYhroUFT3uZgSkeW_48zPGqUNvxCDDIQQ,11534
46
+ keras_hub/src/models/backbone.py,sha256=utZP09_u5FpMGiq8jl3W98TCW8CysndwLw2VCs3BHz8,11780
47
47
  keras_hub/src/models/causal_lm.py,sha256=ReaF-i3SHsCkHh4c28jM72QjMQ8x7yiCwG39FRb-7KE,16786
48
48
  keras_hub/src/models/causal_lm_preprocessor.py,sha256=YY7VJZicdmnjDSWi9g4_pEpd5bdJK166GlWcapvokF0,6663
49
49
  keras_hub/src/models/feature_pyramid_backbone.py,sha256=clEW-TTQSVJ_5qFNdDF0iABkin1p_xlBUFjJrC7T0IA,2247
@@ -196,7 +196,7 @@ keras_hub/src/models/gemma/gemma_tokenizer.py,sha256=FhcyNL4lo63MqOhTQPFr07-u3Bd
196
196
  keras_hub/src/models/gemma/rms_normalization.py,sha256=fku-JEo2sNy-ytX7ySD1sRzdhRAPmYex_z8oFk1NiG8,833
197
197
  keras_hub/src/models/gemma3/__init__.py,sha256=oPFadkdK5DRLD6sYx83iTetY5daWuSzmJilLjokHcbU,257
198
198
  keras_hub/src/models/gemma3/gemma3_attention.py,sha256=VstFCTVsplcDNSgnyBcSpLgKn-pktJ39D5Ri-Bb7BQA,13628
199
- keras_hub/src/models/gemma3/gemma3_backbone.py,sha256=xw6gbFZWZuREcN1iyPj-1Hm-3EmRglgFD5fQSzDp3zA,16439
199
+ keras_hub/src/models/gemma3/gemma3_backbone.py,sha256=CaVUQAKrBd1b_7gF7dyTWLjJebzzMd24_3oUipVu5gE,16445
200
200
  keras_hub/src/models/gemma3/gemma3_causal_lm.py,sha256=U3C9TWlIz8VefAxQ0wJ6bDz18wqHBie8B26Ub_nFZs4,13843
201
201
  keras_hub/src/models/gemma3/gemma3_causal_lm_preprocessor.py,sha256=vjt4N-zr0Eb5kvkOR-WUgskDTNe64L_6tYnhyNb6xaE,29601
202
202
  keras_hub/src/models/gemma3/gemma3_decoder_block.py,sha256=6PLlpDxxF67stDv74fw9nNgUHBWmTLx6qGygJwyu5FY,10819
@@ -286,7 +286,7 @@ keras_hub/src/models/opt/opt_causal_lm_preprocessor.py,sha256=xHfslVMOZlAIj2V2jI
286
286
  keras_hub/src/models/opt/opt_presets.py,sha256=LrjgI5gbq4Cvfl_pmeCnKn4hS_V_0GYTeJaDc9tbeZM,1745
287
287
  keras_hub/src/models/opt/opt_tokenizer.py,sha256=oDHeed4xf07tm14hj_C78BkzMuuRwRP2cRHmqYnObrs,2557
288
288
  keras_hub/src/models/pali_gemma/__init__.py,sha256=uODWTlttOOchcTLpiYHCEWMXnDxIz8ZVIeYFQN2bd8o,288
289
- keras_hub/src/models/pali_gemma/pali_gemma_backbone.py,sha256=_Sa22j4jk_7400h33S22w0S8Dh8Lzzl6A5WeEp55zSk,13637
289
+ keras_hub/src/models/pali_gemma/pali_gemma_backbone.py,sha256=e1KAg4bmK1PrmYW-Ewx3vD7S2DlX9K8LmbRwv30VEkA,13643
290
290
  keras_hub/src/models/pali_gemma/pali_gemma_causal_lm.py,sha256=AViEs6YltUqWnIVo7J02JkXcanBgLSdwZwF56TVr8gc,11345
291
291
  keras_hub/src/models/pali_gemma/pali_gemma_causal_lm_preprocessor.py,sha256=F57y0fZ0wYYxfGIjfrJc1W9uQpViYFx5bvFjj5CqUbI,4814
292
292
  keras_hub/src/models/pali_gemma/pali_gemma_decoder_block.py,sha256=24ABQ1vGlppV-KfWh0YqJjzM_Lu2GIwvyJ4X2XXie_A,5616
@@ -471,7 +471,7 @@ keras_hub/src/utils/keras_utils.py,sha256=2qrh4F-rqceVFSx0-cbsFBfWae5hBXFb_sEtPP
471
471
  keras_hub/src/utils/pipeline_model.py,sha256=jgzB6NQPSl0KOu08N-TazfOnXnUJbZjH2EXXhx25Ftg,9084
472
472
  keras_hub/src/utils/preset_utils.py,sha256=fx0gNqOTdvW-ZdP0Y3ZaCGE7frYBhwi3lG_GO0swG4w,34602
473
473
  keras_hub/src/utils/python_utils.py,sha256=N8nWeO3san4YnGkffRXG3Ix7VEIMTKSN21FX5TuL7G8,202
474
- keras_hub/src/utils/tensor_utils.py,sha256=vRbvvnFwA6FutJ7InC1w60HDTVNi87CniDGOLQ3hKPA,15855
474
+ keras_hub/src/utils/tensor_utils.py,sha256=WrohV6-hvxtLE6rRRhtN4hy8GkHikV-NrRnVEYUwJQo,16133
475
475
  keras_hub/src/utils/coco/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
476
476
  keras_hub/src/utils/coco/coco_utils.py,sha256=x_QnUUvZ92zoFzMJugiInHORc4NrMdWVBkpp8BAYF6s,2586
477
477
  keras_hub/src/utils/imagenet/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -502,7 +502,7 @@ keras_hub/src/utils/transformers/preset_loader.py,sha256=1nfS5xVsl-JROGXJXltTqV1
502
502
  keras_hub/src/utils/transformers/safetensor_utils.py,sha256=CYUHyA4y-B61r7NDnCsFb4t_UmSwZ1k9L-8gzEd6KRg,3339
503
503
  keras_hub/tokenizers/__init__.py,sha256=uMjjm0mzUkRb0e4Ac_JK8aJ9cKGUi5UqmzWoWAFJprE,4164
504
504
  keras_hub/utils/__init__.py,sha256=jXPqVGBpJr_PpYmqD8aDG-fRMlxH-ulqCR2SZMn288Y,646
505
- keras_hub_nightly-0.21.0.dev202505260411.dist-info/METADATA,sha256=CcO9QJzVc-KyhmcZzlRCJJ1j7KSAICdeh18YIXG6mtw,7393
506
- keras_hub_nightly-0.21.0.dev202505260411.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
507
- keras_hub_nightly-0.21.0.dev202505260411.dist-info/top_level.txt,sha256=N4J6piIWBKa38A4uV-CnIopnOEf8mHAbkNXafXm_CuA,10
508
- keras_hub_nightly-0.21.0.dev202505260411.dist-info/RECORD,,
505
+ keras_hub_nightly-0.21.0.dev202505280410.dist-info/METADATA,sha256=DW6jOe7Tbk32rdB5bnZHYlyBZYuzTYIui1EoKkhPMpY,7393
506
+ keras_hub_nightly-0.21.0.dev202505280410.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
507
+ keras_hub_nightly-0.21.0.dev202505280410.dist-info/top_level.txt,sha256=N4J6piIWBKa38A4uV-CnIopnOEf8mHAbkNXafXm_CuA,10
508
+ keras_hub_nightly-0.21.0.dev202505280410.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5