|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
| 17 | +import numpy as np |
17 | 18 | import pytest |
| 19 | +from merlin.dataloader.ops.embeddings import EmbeddingOperator |
| 20 | +from merlin.io import Dataset |
| 21 | +from merlin.schema import ColumnSchema |
18 | 22 | from merlin.schema import Schema as CoreSchema |
19 | 23 | from merlin.schema import Tags |
20 | 24 |
|
21 | 25 | import transformers4rec.torch as tr |
22 | 26 | from tests.conftest import parametrize_schemas |
| 27 | +from transformers4rec.torch.utils.data_utils import MerlinDataLoader |
23 | 28 |
|
24 | 29 |
|
25 | 30 | @parametrize_schemas("yoochoose") |
@@ -217,3 +222,173 @@ def test_sequential_and_non_sequential_tabular_features(schema, torch_yoochoose_ |
217 | 222 | outputs = tab_module(torch_yoochoose_like) |
218 | 223 |
|
219 | 224 | assert list(outputs.shape) == [100, 20, 203] |
| 225 | + |
| 226 | + |
| 227 | +@pytest.mark.parametrize( |
| 228 | + "pretrained_dim", |
| 229 | + [None, 128, {"pretrained_item_id_embeddings": 128, "pretrained_user_id_embeddings": 128}], |
| 230 | +) |
| 231 | +def test_sequential_input_block_with_pretrained_embeddings(pretrained_dim): |
| 232 | + data = tr.data.music_streaming_testing_data |
| 233 | + seq_schema = data.merlin_schema.select_by_name(["item_id"]) |
| 234 | + # Set the property `dims` for the non-sequential feature: "user_id" |
| 235 | + user_cardinality = data.merlin_schema["user_id"].int_domain.max + 1 |
| 236 | + seq_schema = seq_schema + CoreSchema( |
| 237 | + [ |
| 238 | + ColumnSchema( |
| 239 | + "user_id", |
| 240 | + dtype=np.int32, |
| 241 | + tags=[Tags.USER, Tags.CATEGORICAL], |
| 242 | + properties={ |
| 243 | + "domain": {"name": "user_id", "min": 0, "max": user_cardinality}, |
| 244 | + }, |
| 245 | + dims=(None,), |
| 246 | + ) |
| 247 | + ] |
| 248 | + ) |
| 249 | + batch_size, max_length = 128, 20 |
| 250 | + embedding_dim_default, item_dim, user_dim = 8, 32, 16 |
| 251 | + |
| 252 | + # generate pre-trained embeddings tables |
| 253 | + item_cardinality = seq_schema["item_id"].int_domain.max + 1 |
| 254 | + np_emb_item_id = np.random.rand(item_cardinality, item_dim) |
| 255 | + np_emb_user_id = np.random.rand(user_cardinality, user_dim) |
| 256 | + embeddings_op_item = EmbeddingOperator( |
| 257 | + np_emb_item_id, lookup_key="item_id", embedding_name="pretrained_item_id_embeddings" |
| 258 | + ) |
| 259 | + embeddings_op_user = EmbeddingOperator( |
| 260 | + np_emb_user_id, lookup_key="user_id", embedding_name="pretrained_user_id_embeddings" |
| 261 | + ) |
| 262 | + |
| 263 | + # set dataloader with pre-trained embeddings |
| 264 | + data_loader = MerlinDataLoader.from_schema( |
| 265 | + seq_schema, |
| 266 | + data.path, |
| 267 | + batch_size=batch_size, |
| 268 | + max_sequence_length=max_length, |
| 269 | + transforms=[embeddings_op_item, embeddings_op_user], |
| 270 | + shuffle=False, |
| 271 | + ) |
| 272 | + |
| 273 | + batch, _ = next(iter(data_loader)) |
| 274 | + |
| 275 | + # Sequential input block with pre-trained features |
| 276 | + inputs = tr.TabularSequenceFeatures.from_schema( |
| 277 | + data_loader.output_schema, |
| 278 | + max_sequence_length=20, |
| 279 | + pretrained_output_dims=pretrained_dim, |
| 280 | + aggregation=None, |
| 281 | + ) |
| 282 | + |
| 283 | + # Sequential input + concat aggregation, which inherently performs broadcasting of 2-D features. |
| 284 | + inputs_with_concat = tr.TabularSequenceFeatures.from_schema( |
| 285 | + data_loader.output_schema, |
| 286 | + embedding_dim_default=embedding_dim_default, |
| 287 | + max_sequence_length=20, |
| 288 | + aggregation="concat", |
| 289 | + ) |
| 290 | + |
| 291 | + output = inputs.to(batch["item_id"].device).double()(batch) |
| 292 | + concat_output = inputs_with_concat.to(batch["item_id"].device).double()(batch) |
| 293 | + |
| 294 | + assert concat_output.shape[-1] == embedding_dim_default * 2 + item_dim + user_dim |
| 295 | + |
| 296 | + assert "pretrained_item_id_embeddings" in output |
| 297 | + if pretrained_dim is not None: |
| 298 | + assert list(output["pretrained_item_id_embeddings"].shape) == [ |
| 299 | + batch_size, |
| 300 | + max_length, |
| 301 | + 128, |
| 302 | + ] |
| 303 | + assert list(output["pretrained_user_id_embeddings"].shape) == [ |
| 304 | + batch_size, |
| 305 | + 128, |
| 306 | + ] |
| 307 | + else: |
| 308 | + assert list(output["pretrained_item_id_embeddings"].shape) == [ |
| 309 | + batch_size, |
| 310 | + max_length, |
| 311 | + item_dim, |
| 312 | + ] |
| 313 | + assert list(output["pretrained_user_id_embeddings"].shape) == [ |
| 314 | + batch_size, |
| 315 | + user_dim, |
| 316 | + ] |
| 317 | + |
| 318 | + |
| 319 | +@pytest.mark.parametrize( |
| 320 | + "pretrained_dim", |
| 321 | + [None, 128, {"pretrained_item_id_embeddings": 128, "pretrained_user_id_embeddings": 128}], |
| 322 | +) |
| 323 | +def test_non_sequential_input_block_with_pretrained_embeddings(pretrained_dim): |
| 324 | + data = tr.data.music_streaming_testing_data |
| 325 | + seq_schema = data.merlin_schema.select_by_name(["item_id"]) |
| 326 | + # Set the property `dims` for the non-sequential feature: "user_id" |
| 327 | + user_cardinality = data.merlin_schema["user_id"].int_domain.max + 1 |
| 328 | + seq_schema = seq_schema + CoreSchema( |
| 329 | + [ |
| 330 | + ColumnSchema( |
| 331 | + "user_id", |
| 332 | + dtype=np.int32, |
| 333 | + tags=[Tags.USER, Tags.CATEGORICAL], |
| 334 | + properties={ |
| 335 | + "domain": {"name": "user_id", "min": 0, "max": user_cardinality}, |
| 336 | + }, |
| 337 | + dims=(None,), |
| 338 | + ) |
| 339 | + ] |
| 340 | + ) |
| 341 | + batch_size, max_length = 128, 20 |
| 342 | + item_dim, user_dim = 32, 16 |
| 343 | + |
| 344 | + # generate pre-trained embeddings tables |
| 345 | + item_cardinality = seq_schema["item_id"].int_domain.max + 1 |
| 346 | + np_emb_item_id = np.random.rand(item_cardinality, item_dim) |
| 347 | + np_emb_user_id = np.random.rand(user_cardinality, user_dim) |
| 348 | + embeddings_op_item = EmbeddingOperator( |
| 349 | + np_emb_item_id, lookup_key="item_id", embedding_name="pretrained_item_id_embeddings" |
| 350 | + ) |
| 351 | + embeddings_op_user = EmbeddingOperator( |
| 352 | + np_emb_user_id, lookup_key="user_id", embedding_name="pretrained_user_id_embeddings" |
| 353 | + ) |
| 354 | + |
| 355 | + # set dataloader with pre-trained embeddings |
| 356 | + data_loader = MerlinDataLoader.from_schema( |
| 357 | + seq_schema, |
| 358 | + Dataset(data.path, schema=seq_schema), |
| 359 | + batch_size=batch_size, |
| 360 | + max_sequence_length=max_length, |
| 361 | + transforms=[embeddings_op_item, embeddings_op_user], |
| 362 | + shuffle=False, |
| 363 | + ) |
| 364 | + |
| 365 | + batch, _ = next(iter(data_loader)) |
| 366 | + |
| 367 | + # Non-Sequential input block with a 3-D pre-trained feature |
| 368 | + inputs = tr.TabularFeatures.from_schema( |
| 369 | + data_loader.output_schema, |
| 370 | + pretrained_output_dims=pretrained_dim, |
| 371 | + sequence_combiner="mean", |
| 372 | + aggregation=None, |
| 373 | + ) |
| 374 | + output = inputs.to(batch["item_id"].device).double()(batch) |
| 375 | + |
| 376 | + assert "pretrained_item_id_embeddings" in output |
| 377 | + if pretrained_dim is not None: |
| 378 | + assert list(output["pretrained_item_id_embeddings"].shape) == [ |
| 379 | + batch_size, |
| 380 | + 128, |
| 381 | + ] |
| 382 | + assert list(output["pretrained_user_id_embeddings"].shape) == [ |
| 383 | + batch_size, |
| 384 | + 128, |
| 385 | + ] |
| 386 | + else: |
| 387 | + assert list(output["pretrained_item_id_embeddings"].shape) == [ |
| 388 | + batch_size, |
| 389 | + item_dim, |
| 390 | + ] |
| 391 | + assert list(output["pretrained_user_id_embeddings"].shape) == [ |
| 392 | + batch_size, |
| 393 | + user_dim, |
| 394 | + ] |
0 commit comments