(device, embedding_class, input_shape, embedding_dim, quant_storage)
| 324 | ids=lambda x: x.__name__ if inspect.isclass(x) else str(x), |
| 325 | ) |
| 326 | def test_embedding_lossless(device, embedding_class, input_shape, embedding_dim, quant_storage): |
| 327 | if device == "hpu": |
| 328 | if embedding_class is bnb.nn.EmbeddingFP4: |
| 329 | pytest.skip("FP4 is not supported on HPU") |
| 330 | elif embedding_class is bnb.nn.EmbeddingNF4 and not is_supported_on_hpu("nf4", torch.float32, quant_storage): |
| 331 | pytest.skip("This configuration is not supported on HPU") |
| 332 | |
| 333 | num_embeddings = 128 |
| 334 | |
| 335 | src_weight = (torch.randn((num_embeddings, embedding_dim), dtype=torch.float32) > 0).to( |
| 336 | torch.float32 |
| 337 | ) * 2 - 1 # Embeddings filled with {-1, 1} values. It should compress losslessly |
| 338 | |
| 339 | emb_base = nn.Embedding( |
| 340 | num_embeddings=num_embeddings, |
| 341 | embedding_dim=embedding_dim, |
| 342 | _freeze=True, |
| 343 | _weight=src_weight, |
| 344 | ) |
| 345 | if embedding_class is bnb.nn.Embedding8bit: |
| 346 | e = embedding_class(num_embeddings=num_embeddings, embedding_dim=embedding_dim) |
| 347 | else: |
| 348 | e = embedding_class(num_embeddings=num_embeddings, embedding_dim=embedding_dim, quant_storage=quant_storage) |
| 349 | |
| 350 | e.load_state_dict(emb_base.state_dict()) |
| 351 | |
| 352 | emb_base.to(device) |
| 353 | e.to(device) |
| 354 | |
| 355 | input_tokens = torch.randint(low=0, high=num_embeddings, size=input_shape, device=device) |
| 356 | |
| 357 | torch.testing.assert_close( |
| 358 | actual=e(input_tokens), |
| 359 | expected=emb_base(input_tokens), |
| 360 | ) |
| 361 | |
| 362 | |
| 363 | @pytest.mark.parametrize("device", get_available_devices()) |
nothing calls this directly
no test coverage detected