(device, embedding_class, input_shape, embedding_dim, quant_storage)
| 375 | ids=lambda x: x.__name__ if inspect.isclass(x) else str(x), |
| 376 | ) |
| 377 | def test_embedding_error(device, embedding_class, input_shape, embedding_dim, quant_storage): |
| 378 | if device == "hpu": |
| 379 | if embedding_class is bnb.nn.EmbeddingFP4: |
| 380 | pytest.skip("FP4 is not supported on HPU") |
| 381 | elif embedding_class is bnb.nn.EmbeddingNF4 and not is_supported_on_hpu("nf4", torch.float32, quant_storage): |
| 382 | pytest.skip("This configuration is not supported on HPU") |
| 383 | |
| 384 | is_8bit = embedding_class is bnb.nn.Embedding8bit |
| 385 | |
| 386 | num_embeddings = 128 |
| 387 | |
| 388 | src_weight = torch.rand((num_embeddings, embedding_dim), dtype=torch.float32) |
| 389 | |
| 390 | emb_base = nn.Embedding( |
| 391 | num_embeddings=num_embeddings, |
| 392 | embedding_dim=embedding_dim, |
| 393 | _freeze=True, |
| 394 | _weight=src_weight, |
| 395 | ) |
| 396 | if is_8bit: |
| 397 | e = embedding_class(num_embeddings=num_embeddings, embedding_dim=embedding_dim) |
| 398 | else: |
| 399 | e = embedding_class(num_embeddings=num_embeddings, embedding_dim=embedding_dim, quant_storage=quant_storage) |
| 400 | |
| 401 | e.load_state_dict(emb_base.state_dict()) |
| 402 | |
| 403 | emb_base.to(device) |
| 404 | e.to(device) |
| 405 | |
| 406 | input_tokens = torch.randint(low=0, high=num_embeddings, size=input_shape, device=device) |
| 407 | |
| 408 | torch.testing.assert_close( |
| 409 | actual=e(input_tokens), |
| 410 | expected=emb_base(input_tokens), |
| 411 | atol=0.05 if is_8bit else 0.20, |
| 412 | rtol=0.0, |
| 413 | ) |
| 414 | |
| 415 | |
| 416 | @pytest.mark.parametrize("device", get_available_devices()) |
nothing calls this directly
no test coverage detected