keras-rs-nightly 0.0.1.dev2025050103__py3-none-any.whl → 0.2.2.dev202506100336__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.
Potentially problematic release.
This version of keras-rs-nightly might be problematic. Click here for more details.
- keras_rs/layers/__init__.py +12 -0
- keras_rs/src/layers/embedding/__init__.py +0 -0
- keras_rs/src/layers/embedding/base_distributed_embedding.py +1124 -0
- keras_rs/src/layers/embedding/distributed_embedding.py +33 -0
- keras_rs/src/layers/embedding/distributed_embedding_config.py +129 -0
- keras_rs/src/layers/embedding/embed_reduce.py +309 -0
- keras_rs/src/layers/embedding/jax/__init__.py +0 -0
- keras_rs/src/layers/embedding/jax/config_conversion.py +398 -0
- keras_rs/src/layers/embedding/jax/distributed_embedding.py +892 -0
- keras_rs/src/layers/embedding/jax/embedding_lookup.py +255 -0
- keras_rs/src/layers/embedding/jax/embedding_utils.py +596 -0
- keras_rs/src/layers/embedding/tensorflow/__init__.py +0 -0
- keras_rs/src/layers/embedding/tensorflow/config_conversion.py +323 -0
- keras_rs/src/layers/embedding/tensorflow/distributed_embedding.py +424 -0
- keras_rs/src/layers/feature_interaction/dot_interaction.py +2 -2
- keras_rs/src/layers/feature_interaction/feature_cross.py +14 -16
- keras_rs/src/layers/retrieval/brute_force_retrieval.py +5 -5
- keras_rs/src/layers/retrieval/retrieval.py +4 -4
- keras_rs/src/losses/pairwise_loss.py +2 -2
- keras_rs/src/losses/pairwise_mean_squared_error.py +1 -3
- keras_rs/src/metrics/dcg.py +2 -2
- keras_rs/src/metrics/ndcg.py +2 -2
- keras_rs/src/metrics/ranking_metric.py +4 -4
- keras_rs/src/metrics/ranking_metrics_utils.py +8 -8
- keras_rs/src/metrics/utils.py +2 -4
- keras_rs/src/types.py +43 -14
- keras_rs/src/utils/keras_utils.py +26 -6
- keras_rs/src/version.py +1 -1
- {keras_rs_nightly-0.0.1.dev2025050103.dist-info → keras_rs_nightly-0.2.2.dev202506100336.dist-info}/METADATA +6 -3
- keras_rs_nightly-0.2.2.dev202506100336.dist-info/RECORD +55 -0
- {keras_rs_nightly-0.0.1.dev2025050103.dist-info → keras_rs_nightly-0.2.2.dev202506100336.dist-info}/WHEEL +1 -1
- keras_rs_nightly-0.0.1.dev2025050103.dist-info/RECORD +0 -42
- {keras_rs_nightly-0.0.1.dev2025050103.dist-info → keras_rs_nightly-0.2.2.dev202506100336.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
"""Utility functions for manipulating JAX embedding tables and inputs."""
|
|
2
|
+
|
|
3
|
+
import collections
|
|
4
|
+
import dataclasses
|
|
5
|
+
import typing
|
|
6
|
+
from typing import Any, Mapping, NamedTuple, Sequence, TypeAlias, TypeVar
|
|
7
|
+
|
|
8
|
+
import jax
|
|
9
|
+
import numpy as np
|
|
10
|
+
from jax import numpy as jnp
|
|
11
|
+
from jax_tpu_embedding.sparsecore.lib.nn import embedding
|
|
12
|
+
from jax_tpu_embedding.sparsecore.lib.nn.embedding_spec import FeatureSpec
|
|
13
|
+
from jax_tpu_embedding.sparsecore.lib.nn.embedding_spec import StackedTableSpec
|
|
14
|
+
from jax_tpu_embedding.sparsecore.lib.nn.embedding_spec import TableSpec
|
|
15
|
+
|
|
16
|
+
from keras_rs.src.types import Nested
|
|
17
|
+
|
|
18
|
+
T = TypeVar("T")
|
|
19
|
+
|
|
20
|
+
# Any to support tf.Ragged without needing an explicit TF dependency.
|
|
21
|
+
ArrayLike: TypeAlias = jax.Array | np.ndarray | Any # type: ignore
|
|
22
|
+
Shape: TypeAlias = tuple[int, ...]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FeatureSamples(NamedTuple):
|
|
26
|
+
tokens: ArrayLike
|
|
27
|
+
weights: ArrayLike
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ShardedCooMatrix(NamedTuple):
|
|
31
|
+
shard_starts: ArrayLike
|
|
32
|
+
shard_ends: ArrayLike
|
|
33
|
+
col_ids: ArrayLike
|
|
34
|
+
row_ids: ArrayLike
|
|
35
|
+
values: ArrayLike
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _round_up_to_multiple(value: int, multiple: int) -> int:
|
|
39
|
+
return ((value + multiple - 1) // multiple) * multiple
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _default_stacked_table_spec(
|
|
43
|
+
table_spec: TableSpec, num_shards: int, batch_size: int
|
|
44
|
+
) -> StackedTableSpec:
|
|
45
|
+
return StackedTableSpec(
|
|
46
|
+
stack_name=table_spec.name,
|
|
47
|
+
stack_vocab_size=_round_up_to_multiple(
|
|
48
|
+
table_spec.vocabulary_size, 8 * num_shards
|
|
49
|
+
),
|
|
50
|
+
stack_embedding_dim=_round_up_to_multiple(table_spec.embedding_dim, 8),
|
|
51
|
+
optimizer=table_spec.optimizer,
|
|
52
|
+
combiner=table_spec.combiner,
|
|
53
|
+
total_sample_count=batch_size,
|
|
54
|
+
max_ids_per_partition=table_spec.max_ids_per_partition,
|
|
55
|
+
max_unique_ids_per_partition=table_spec.max_unique_ids_per_partition,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _get_stacked_table_spec(
|
|
60
|
+
table_spec: TableSpec, num_shards: int, batch_size: int = 0
|
|
61
|
+
) -> StackedTableSpec:
|
|
62
|
+
return table_spec.stacked_table_spec or _default_stacked_table_spec(
|
|
63
|
+
table_spec, num_shards, batch_size
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def pad_table(
|
|
68
|
+
table_spec: TableSpec,
|
|
69
|
+
table_values: jax.Array,
|
|
70
|
+
num_shards: int,
|
|
71
|
+
pad_value: jnp.float32 = jnp.nan,
|
|
72
|
+
) -> jax.Array:
|
|
73
|
+
"""Adds appropriate padding to a table to prepare for stacking.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
table_spec: Table specification describing the table to pad.
|
|
77
|
+
table_values: Table values array to pad.
|
|
78
|
+
num_shards: Number of shards in the table (typically
|
|
79
|
+
`global_device_count * num_sc_per_device`).
|
|
80
|
+
pad_value: Value to use for padding.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
Padded table values.
|
|
84
|
+
"""
|
|
85
|
+
vocabulary_size = table_spec.vocabulary_size
|
|
86
|
+
embedding_dim = table_spec.embedding_dim
|
|
87
|
+
padded_vocabulary_size = _round_up_to_multiple(
|
|
88
|
+
vocabulary_size, 8 * num_shards
|
|
89
|
+
)
|
|
90
|
+
stack_embedding_dim = _get_stacked_table_spec(
|
|
91
|
+
table_spec, num_shards
|
|
92
|
+
).stack_embedding_dim
|
|
93
|
+
return jnp.pad(
|
|
94
|
+
table_values,
|
|
95
|
+
(
|
|
96
|
+
(0, padded_vocabulary_size - vocabulary_size),
|
|
97
|
+
(0, stack_embedding_dim - embedding_dim),
|
|
98
|
+
),
|
|
99
|
+
constant_values=pad_value,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _stack_and_shard_table(
|
|
104
|
+
stacked_table: jax.Array,
|
|
105
|
+
table_spec: TableSpec,
|
|
106
|
+
table: jax.Array,
|
|
107
|
+
num_shards: int,
|
|
108
|
+
pad_value: jnp.float32,
|
|
109
|
+
) -> jax.Array:
|
|
110
|
+
"""Stacks and shards a single table for use in sparsecore lookups."""
|
|
111
|
+
padded_values = pad_table(table_spec, table, num_shards, pad_value)
|
|
112
|
+
sharded_padded_vocabulary_size = padded_values.shape[0] // num_shards
|
|
113
|
+
stack_embedding_dim = stacked_table.shape[-1]
|
|
114
|
+
|
|
115
|
+
# Mod-shard vocabulary across devices.
|
|
116
|
+
sharded_values = jnp.swapaxes(
|
|
117
|
+
padded_values.reshape(-1, num_shards, stack_embedding_dim),
|
|
118
|
+
0,
|
|
119
|
+
1,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Rotate shards.
|
|
123
|
+
setting_in_stack = table_spec.setting_in_stack
|
|
124
|
+
rotated_values = jnp.roll(
|
|
125
|
+
sharded_values, setting_in_stack.shard_rotation, axis=0
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# Insert table into the stack.
|
|
129
|
+
table_row = setting_in_stack.row_offset_in_shard
|
|
130
|
+
stacked_table = stacked_table.at[
|
|
131
|
+
:, table_row : (table_row + sharded_padded_vocabulary_size), :
|
|
132
|
+
].set(rotated_values)
|
|
133
|
+
|
|
134
|
+
return stacked_table
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def stack_and_shard_tables(
|
|
138
|
+
table_specs: Nested[TableSpec],
|
|
139
|
+
tables: Nested[ArrayLike],
|
|
140
|
+
num_shards: int,
|
|
141
|
+
pad_value: jnp.float32 = jnp.nan,
|
|
142
|
+
) -> dict[str, Nested[jax.Array]]:
|
|
143
|
+
"""Stacks and shards tables for use in sparsecore lookups.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
table_specs: Nested collection of unstacked table specifications.
|
|
147
|
+
tables: Table values corresponding to the table_specs.
|
|
148
|
+
num_shards: Number of shards in the table (typically
|
|
149
|
+
`global_device_count * num_sc_per_device`).
|
|
150
|
+
pad_value: Value to use for padding.
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
A mapping of stacked table names to stacked table values.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
# Gather stacked table information.
|
|
157
|
+
stacked_table_map: dict[
|
|
158
|
+
str,
|
|
159
|
+
tuple[StackedTableSpec, list[TableSpec]],
|
|
160
|
+
] = {}
|
|
161
|
+
|
|
162
|
+
def collect_stacked_tables(table_spec: TableSpec) -> None:
|
|
163
|
+
stacked_table_spec = _get_stacked_table_spec(table_spec, num_shards)
|
|
164
|
+
stacked_table_name = stacked_table_spec.stack_name
|
|
165
|
+
if stacked_table_name not in stacked_table_map:
|
|
166
|
+
stacked_table_map[stacked_table_name] = (stacked_table_spec, [])
|
|
167
|
+
stacked_table_map[stacked_table_name][1].append(table_spec)
|
|
168
|
+
|
|
169
|
+
_ = jax.tree.map(collect_stacked_tables, table_specs)
|
|
170
|
+
|
|
171
|
+
table_map: dict[str, Nested[jax.Array]] = {}
|
|
172
|
+
|
|
173
|
+
def collect_tables(table_spec: TableSpec, table: Nested[jax.Array]) -> None:
|
|
174
|
+
table_map[table_spec.name] = table
|
|
175
|
+
|
|
176
|
+
_ = jax.tree.map(collect_tables, table_specs, tables)
|
|
177
|
+
|
|
178
|
+
stacked_tables: dict[str, Nested[jax.Array]] = {}
|
|
179
|
+
for (
|
|
180
|
+
stacked_table_spec,
|
|
181
|
+
table_specs,
|
|
182
|
+
) in stacked_table_map.values():
|
|
183
|
+
stack_vocab_size = stacked_table_spec.stack_vocab_size
|
|
184
|
+
sharded_vocab_size = stack_vocab_size // num_shards
|
|
185
|
+
stack_embedding_dim = stacked_table_spec.stack_embedding_dim
|
|
186
|
+
|
|
187
|
+
# Allocate initial buffer. The stacked table will be divided among
|
|
188
|
+
# shards by splitting the vocabulary dimension:
|
|
189
|
+
# [ v, e ] -> [s, v/s, e]
|
|
190
|
+
stacked_table_tree = jax.tree.map(
|
|
191
|
+
lambda _: jnp.zeros(
|
|
192
|
+
# pylint: disable-next=cell-var-from-loop, used only in loop body.
|
|
193
|
+
shape=(num_shards, sharded_vocab_size, stack_embedding_dim),
|
|
194
|
+
dtype=jnp.float32,
|
|
195
|
+
),
|
|
196
|
+
table_map[table_specs[0].name],
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
for table_spec in table_specs:
|
|
200
|
+
table_tree = table_map[table_spec.name]
|
|
201
|
+
stacked_table_tree = jax.tree.map(
|
|
202
|
+
lambda stacked_table, table: _stack_and_shard_table(
|
|
203
|
+
# pylint: disable-next=cell-var-from-loop, used only in loop body.
|
|
204
|
+
stacked_table,
|
|
205
|
+
# pylint: disable-next=cell-var-from-loop, used only in loop body.
|
|
206
|
+
table_spec,
|
|
207
|
+
table,
|
|
208
|
+
num_shards,
|
|
209
|
+
pad_value,
|
|
210
|
+
),
|
|
211
|
+
stacked_table_tree,
|
|
212
|
+
table_tree,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
stacked_tables[stacked_table_spec.stack_name] = stacked_table_tree
|
|
216
|
+
|
|
217
|
+
return stacked_tables
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _unshard_and_unstack_table(
|
|
221
|
+
table_spec: TableSpec,
|
|
222
|
+
stacked_table_tree: Nested[jax.Array],
|
|
223
|
+
num_shards: int,
|
|
224
|
+
) -> Nested[jax.Array]:
|
|
225
|
+
"""Unshards and unstacks a single table."""
|
|
226
|
+
vocabulary_size = table_spec.vocabulary_size
|
|
227
|
+
embedding_dim = table_spec.embedding_dim
|
|
228
|
+
|
|
229
|
+
def _unshard_and_unstack_single_table(
|
|
230
|
+
table_spec: TableSpec, stacked_table: jax.Array
|
|
231
|
+
) -> jax.Array:
|
|
232
|
+
stack_embedding_dim = stacked_table.shape[-1]
|
|
233
|
+
|
|
234
|
+
# Maybe re-shape in case it was flattened.
|
|
235
|
+
stacked_table = stacked_table.reshape(
|
|
236
|
+
num_shards, -1, stack_embedding_dim
|
|
237
|
+
)
|
|
238
|
+
sharded_vocabulary_size = (
|
|
239
|
+
_round_up_to_multiple(vocabulary_size, 8 * num_shards) // num_shards
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# Extract padded values from the stacked table.
|
|
243
|
+
setting_in_stack = table_spec.setting_in_stack
|
|
244
|
+
row = setting_in_stack.row_offset_in_shard
|
|
245
|
+
padded_values = stacked_table[
|
|
246
|
+
:, row : (row + sharded_vocabulary_size), :
|
|
247
|
+
]
|
|
248
|
+
|
|
249
|
+
# Un-rotate shards.
|
|
250
|
+
padded_values = jnp.roll(
|
|
251
|
+
padded_values, -setting_in_stack.shard_rotation, axis=0
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
# Un-mod-shard.
|
|
255
|
+
padded_values = jnp.swapaxes(padded_values, 0, 1).reshape(
|
|
256
|
+
-1, stack_embedding_dim
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
# Un-pad.
|
|
260
|
+
return padded_values[:vocabulary_size, :embedding_dim]
|
|
261
|
+
|
|
262
|
+
output: Nested[jax.Array] = jax.tree.map(
|
|
263
|
+
lambda stacked_table: _unshard_and_unstack_single_table(
|
|
264
|
+
table_spec, stacked_table
|
|
265
|
+
),
|
|
266
|
+
stacked_table_tree,
|
|
267
|
+
)
|
|
268
|
+
return output
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def unshard_and_unstack_tables(
|
|
272
|
+
table_specs: Nested[TableSpec],
|
|
273
|
+
stacked_tables: Mapping[str, Nested[jax.Array]],
|
|
274
|
+
num_shards: int,
|
|
275
|
+
) -> Nested[jax.Array]:
|
|
276
|
+
"""Unshards and unstacks a collection of tables.
|
|
277
|
+
|
|
278
|
+
Args:
|
|
279
|
+
table_specs: Nested collection of unstacked table specifications.
|
|
280
|
+
stacked_tables: Mapping of stacked table names to stacked table values.
|
|
281
|
+
num_shards: Number of shards in the table (typically
|
|
282
|
+
`global_device_count * num_sc_per_device`).
|
|
283
|
+
|
|
284
|
+
Returns:
|
|
285
|
+
A mapping of table names to unstacked table values.
|
|
286
|
+
"""
|
|
287
|
+
output: Nested[jax.Array] = jax.tree.map(
|
|
288
|
+
lambda table_spec: _unshard_and_unstack_table(
|
|
289
|
+
table_spec,
|
|
290
|
+
stacked_tables[
|
|
291
|
+
_get_stacked_table_spec(table_spec, num_shards=1).stack_name
|
|
292
|
+
],
|
|
293
|
+
num_shards,
|
|
294
|
+
),
|
|
295
|
+
table_specs,
|
|
296
|
+
)
|
|
297
|
+
return output
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def get_table_specs(feature_specs: Nested[FeatureSpec]) -> dict[str, TableSpec]:
|
|
301
|
+
table_spec_map: dict[str, TableSpec] = {}
|
|
302
|
+
flat_feature_specs, _ = jax.tree.flatten(feature_specs)
|
|
303
|
+
for feature_spec in flat_feature_specs:
|
|
304
|
+
table_spec = feature_spec.table_spec
|
|
305
|
+
table_spec_map[table_spec.name] = table_spec
|
|
306
|
+
return table_spec_map
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def get_table_stacks(
|
|
310
|
+
table_specs: Nested[TableSpec],
|
|
311
|
+
) -> dict[str, list[TableSpec]]:
|
|
312
|
+
"""Extracts lists of tables that are stacked together.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
table_specs: Nested collection of table specifications.
|
|
316
|
+
|
|
317
|
+
Returns:
|
|
318
|
+
A mapping of stacked table names to lists of table specifications for
|
|
319
|
+
each stack.
|
|
320
|
+
"""
|
|
321
|
+
stacked_table_specs: dict[str, list[TableSpec]] = collections.defaultdict(
|
|
322
|
+
list
|
|
323
|
+
)
|
|
324
|
+
flat_table_specs, _ = jax.tree.flatten(table_specs)
|
|
325
|
+
for table_spec in flat_table_specs:
|
|
326
|
+
table_spec = typing.cast(TableSpec, table_spec)
|
|
327
|
+
stacked_table_spec = table_spec.stacked_table_spec
|
|
328
|
+
if stacked_table_spec is not None:
|
|
329
|
+
stacked_table_specs[stacked_table_spec.stack_name].append(
|
|
330
|
+
table_spec
|
|
331
|
+
)
|
|
332
|
+
else:
|
|
333
|
+
stacked_table_specs[table_spec.name].append(table_spec)
|
|
334
|
+
|
|
335
|
+
return stacked_table_specs
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def update_stacked_table_specs(
|
|
339
|
+
feature_specs: Nested[FeatureSpec],
|
|
340
|
+
max_ids_per_partition: Mapping[str, int],
|
|
341
|
+
max_unique_ids_per_partition: Mapping[str, int],
|
|
342
|
+
) -> None:
|
|
343
|
+
"""Updates properties in the supplied feature specs.
|
|
344
|
+
|
|
345
|
+
Args:
|
|
346
|
+
feature_specs: Feature specs to update in-place.
|
|
347
|
+
max_ids_per_partition: Mapping of table stack name to
|
|
348
|
+
new `max_ids_per_partition` for the stack.
|
|
349
|
+
max_unique_ids_per_partition: Mapping of table stack name to
|
|
350
|
+
new `max_unique_ids_per_partition` for the stack.
|
|
351
|
+
"""
|
|
352
|
+
# Collect table specs and stacked table specs.
|
|
353
|
+
table_specs: dict[str, TableSpec] = {}
|
|
354
|
+
for feature_spec in jax.tree.flatten(feature_specs)[0]:
|
|
355
|
+
feature_spec = typing.cast(FeatureSpec, feature_spec)
|
|
356
|
+
table_specs[feature_spec.table_spec.name] = feature_spec.table_spec
|
|
357
|
+
|
|
358
|
+
stacked_table_specs: dict[str, StackedTableSpec] = {}
|
|
359
|
+
for table_spec in table_specs.values():
|
|
360
|
+
stacked_table_spec = typing.cast(
|
|
361
|
+
StackedTableSpec, table_spec.stacked_table_spec
|
|
362
|
+
)
|
|
363
|
+
stacked_table_specs[stacked_table_spec.stack_name] = stacked_table_spec
|
|
364
|
+
|
|
365
|
+
# Replace fields in the stacked_table_specs.
|
|
366
|
+
stacked_table_specs = {
|
|
367
|
+
stack_name: dataclasses.replace(
|
|
368
|
+
stacked_table_spec,
|
|
369
|
+
max_ids_per_partition=max_ids_per_partition[
|
|
370
|
+
stacked_table_spec.stack_name
|
|
371
|
+
],
|
|
372
|
+
max_unique_ids_per_partition=max_unique_ids_per_partition[
|
|
373
|
+
stacked_table_spec.stack_name
|
|
374
|
+
],
|
|
375
|
+
)
|
|
376
|
+
for stack_name, stacked_table_spec in stacked_table_specs.items()
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
# Insert new stacked tables into tables.
|
|
380
|
+
for table_spec in table_specs.values():
|
|
381
|
+
stacked_table_spec = typing.cast(
|
|
382
|
+
StackedTableSpec, table_spec.stacked_table_spec
|
|
383
|
+
)
|
|
384
|
+
table_spec.stacked_table_spec = stacked_table_specs[
|
|
385
|
+
stacked_table_spec.stack_name
|
|
386
|
+
]
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def convert_to_numpy(
|
|
390
|
+
ragged_or_dense: np.ndarray[Any, Any] | Sequence[Sequence[Any]] | Any,
|
|
391
|
+
dtype: Any,
|
|
392
|
+
) -> np.ndarray[Any, Any]:
|
|
393
|
+
"""Converts a ragged or dense list of inputs to a ragged/dense numpy array.
|
|
394
|
+
|
|
395
|
+
The output is adjusted to be 2D.
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
ragged_or_dense: Input that is either already a numpy array, or nested
|
|
399
|
+
sequence.
|
|
400
|
+
dtype: Numpy dtype of output array.
|
|
401
|
+
|
|
402
|
+
Returns:
|
|
403
|
+
Corresponding numpy array.
|
|
404
|
+
"""
|
|
405
|
+
if hasattr(ragged_or_dense, "numpy"):
|
|
406
|
+
# Support tf.RaggedTensor and other TF input dtypes.
|
|
407
|
+
if callable(getattr(ragged_or_dense, "numpy")):
|
|
408
|
+
ragged_or_dense = ragged_or_dense.numpy()
|
|
409
|
+
|
|
410
|
+
if isinstance(ragged_or_dense, jax.Array):
|
|
411
|
+
ragged_or_dense = np.asarray(ragged_or_dense)
|
|
412
|
+
|
|
413
|
+
if isinstance(ragged_or_dense, np.ndarray):
|
|
414
|
+
# Convert 1D to 2D.
|
|
415
|
+
if ragged_or_dense.dtype != np.ndarray and ragged_or_dense.ndim == 1:
|
|
416
|
+
return ragged_or_dense.reshape(-1, 1).astype(dtype)
|
|
417
|
+
|
|
418
|
+
# If dense, return converted dense type.
|
|
419
|
+
if ragged_or_dense.dtype != np.ndarray:
|
|
420
|
+
return ragged_or_dense.astype(dtype)
|
|
421
|
+
|
|
422
|
+
# Ragged numpy array.
|
|
423
|
+
return ragged_or_dense
|
|
424
|
+
|
|
425
|
+
# Handle 1D sequence input.
|
|
426
|
+
if not isinstance(ragged_or_dense[0], collections.abc.Sequence):
|
|
427
|
+
return np.asarray(ragged_or_dense, dtype=dtype).reshape(-1, 1)
|
|
428
|
+
|
|
429
|
+
# Assemble elements into an nd-array.
|
|
430
|
+
counts = [len(vals) for vals in ragged_or_dense]
|
|
431
|
+
if all([count == counts[0] for count in counts]):
|
|
432
|
+
# Dense input.
|
|
433
|
+
return np.asarray(ragged_or_dense, dtype=dtype)
|
|
434
|
+
else:
|
|
435
|
+
# Ragged input, convert to ragged numpy arrays.
|
|
436
|
+
return np.array(
|
|
437
|
+
[np.array(row, dtype=dtype) for row in ragged_or_dense],
|
|
438
|
+
dtype=np.ndarray,
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def ones_like(
|
|
443
|
+
ragged_or_dense: np.ndarray[Any, Any], dtype: Any = None
|
|
444
|
+
) -> np.ndarray[Any, Any]:
|
|
445
|
+
"""Creates an array of ones the same as as the input.
|
|
446
|
+
|
|
447
|
+
This differs from traditional numpy in that a ragged input will lead to
|
|
448
|
+
a resulting ragged array of ones, whereas np.ones_like(...) will instead
|
|
449
|
+
only consider the outer array and return a 1D dense array of ones.
|
|
450
|
+
|
|
451
|
+
Args:
|
|
452
|
+
ragged_or_dense: The ragged or dense input whose shape and data-type
|
|
453
|
+
define these same attributes of the returned array.
|
|
454
|
+
dtype: The data-type of the returned array.
|
|
455
|
+
|
|
456
|
+
Returns:
|
|
457
|
+
An array of ones with the same shape as the input, and specified data
|
|
458
|
+
type.
|
|
459
|
+
"""
|
|
460
|
+
dtype = dtype or ragged_or_dense.dtype
|
|
461
|
+
if ragged_or_dense.dtype == np.ndarray:
|
|
462
|
+
# Ragged.
|
|
463
|
+
return np.array(
|
|
464
|
+
[np.ones_like(row, dtype=dtype) for row in ragged_or_dense],
|
|
465
|
+
dtype=np.ndarray,
|
|
466
|
+
)
|
|
467
|
+
else:
|
|
468
|
+
# Dense.
|
|
469
|
+
return np.ones_like(ragged_or_dense, dtype=dtype)
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def create_feature_samples(
|
|
473
|
+
feature_structure: Nested[T],
|
|
474
|
+
feature_ids: Nested[ArrayLike | Sequence[int] | Sequence[Sequence[int]]],
|
|
475
|
+
feature_weights: None
|
|
476
|
+
| (Nested[ArrayLike | Sequence[float] | Sequence[Sequence[float]]]),
|
|
477
|
+
) -> Nested[FeatureSamples]:
|
|
478
|
+
"""Constructs a collection of sample tuples from provided IDs and weights.
|
|
479
|
+
|
|
480
|
+
Args:
|
|
481
|
+
feature_structure: The nested structure of the inputs (typically
|
|
482
|
+
`FeatureSpec`s).
|
|
483
|
+
feature_ids: The feature IDs to use for the samples.
|
|
484
|
+
feature_weights: The feature weights to use for the samples. Defaults
|
|
485
|
+
to ones if not provided.
|
|
486
|
+
|
|
487
|
+
Returns:
|
|
488
|
+
A nested collection of `FeatureSamples` corresponding to the input IDs
|
|
489
|
+
and weights, for use in embedding lookups.
|
|
490
|
+
"""
|
|
491
|
+
# Create numpy arrays from inputs.
|
|
492
|
+
feature_ids = jax.tree.map(
|
|
493
|
+
lambda _, ids: convert_to_numpy(ids, np.int32),
|
|
494
|
+
feature_structure,
|
|
495
|
+
feature_ids,
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
if feature_weights is None:
|
|
499
|
+
# Make ragged or dense ones_like.
|
|
500
|
+
feature_weights = jax.tree.map(
|
|
501
|
+
lambda _, ids: ones_like(ids, np.float32),
|
|
502
|
+
feature_structure,
|
|
503
|
+
feature_ids,
|
|
504
|
+
)
|
|
505
|
+
else:
|
|
506
|
+
feature_weights = jax.tree.map(
|
|
507
|
+
lambda _, wgts: convert_to_numpy(wgts, np.float32),
|
|
508
|
+
feature_structure,
|
|
509
|
+
feature_weights,
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
# Assemble.
|
|
513
|
+
def _create_feature_samples(
|
|
514
|
+
sample_ids: np.ndarray[Any, Any],
|
|
515
|
+
sample_weights: np.ndarray[Any, Any],
|
|
516
|
+
) -> FeatureSamples:
|
|
517
|
+
return FeatureSamples(sample_ids, sample_weights)
|
|
518
|
+
|
|
519
|
+
output: Nested[FeatureSamples] = jax.tree.map(
|
|
520
|
+
lambda _, sample_ids, sample_weights: _create_feature_samples(
|
|
521
|
+
sample_ids, sample_weights
|
|
522
|
+
),
|
|
523
|
+
feature_structure,
|
|
524
|
+
feature_ids,
|
|
525
|
+
feature_weights,
|
|
526
|
+
)
|
|
527
|
+
return output
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def stack_and_shard_samples(
|
|
531
|
+
feature_specs: Nested[FeatureSpec],
|
|
532
|
+
feature_samples: Nested[FeatureSamples],
|
|
533
|
+
local_device_count: int,
|
|
534
|
+
global_device_count: int,
|
|
535
|
+
num_sc_per_device: int,
|
|
536
|
+
static_buffer_size: int | Mapping[str, int] | None = None,
|
|
537
|
+
) -> tuple[dict[str, ShardedCooMatrix], embedding.SparseDenseMatmulInputStats]:
|
|
538
|
+
"""Prepares input samples for use in embedding lookups.
|
|
539
|
+
|
|
540
|
+
Args:
|
|
541
|
+
feature_specs: Nested collection of feature specifications.
|
|
542
|
+
feature_samples: Nested collection of feature samples.
|
|
543
|
+
local_device_count: Number of local JAX devices.
|
|
544
|
+
global_device_count: Number of global JAX devices.
|
|
545
|
+
num_sc_per_device: Number of sparsecores per device.
|
|
546
|
+
static_buffer_size: The static buffer size to use for the samples.
|
|
547
|
+
Defaults to None, in which case an upper-bound for the buffer size
|
|
548
|
+
will be automatically determined.
|
|
549
|
+
|
|
550
|
+
Returns:
|
|
551
|
+
The preprocessed inputs, and statistics useful for updating FeatureSpecs
|
|
552
|
+
based on the provided input data.
|
|
553
|
+
"""
|
|
554
|
+
del static_buffer_size # Currently ignored.
|
|
555
|
+
flat_feature_specs, _ = jax.tree.flatten(feature_specs)
|
|
556
|
+
|
|
557
|
+
feature_tokens = []
|
|
558
|
+
feature_weights = []
|
|
559
|
+
|
|
560
|
+
def collect_tokens_and_weights(
|
|
561
|
+
feature_spec: FeatureSpec, samples: FeatureSamples
|
|
562
|
+
) -> None:
|
|
563
|
+
del feature_spec
|
|
564
|
+
feature_tokens.append(samples.tokens)
|
|
565
|
+
feature_weights.append(samples.weights)
|
|
566
|
+
|
|
567
|
+
jax.tree.map(collect_tokens_and_weights, feature_specs, feature_samples)
|
|
568
|
+
|
|
569
|
+
preprocessed_inputs, stats = embedding.preprocess_sparse_dense_matmul_input(
|
|
570
|
+
feature_tokens,
|
|
571
|
+
feature_weights,
|
|
572
|
+
flat_feature_specs,
|
|
573
|
+
local_device_count=local_device_count,
|
|
574
|
+
global_device_count=global_device_count,
|
|
575
|
+
num_sc_per_device=num_sc_per_device,
|
|
576
|
+
sharding_strategy="MOD",
|
|
577
|
+
has_leading_dimension=False,
|
|
578
|
+
allow_id_dropping=True,
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
out: dict[str, ShardedCooMatrix] = {}
|
|
582
|
+
tables_names = preprocessed_inputs.lhs_row_pointers.keys()
|
|
583
|
+
for table_name in tables_names:
|
|
584
|
+
shard_ends = preprocessed_inputs.lhs_row_pointers[table_name]
|
|
585
|
+
shard_starts = np.concatenate(
|
|
586
|
+
[np.asarray([0]), _round_up_to_multiple(shard_ends[:-1], 8)]
|
|
587
|
+
)
|
|
588
|
+
out[table_name] = ShardedCooMatrix(
|
|
589
|
+
shard_starts=shard_starts,
|
|
590
|
+
shard_ends=shard_ends,
|
|
591
|
+
col_ids=preprocessed_inputs.lhs_embedding_ids[table_name],
|
|
592
|
+
row_ids=preprocessed_inputs.lhs_sample_ids[table_name],
|
|
593
|
+
values=preprocessed_inputs.lhs_gains[table_name],
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
return out, stats
|
|
File without changes
|