|
| 1 | +import finalfusion |
| 2 | +import pytest |
| 3 | +import numpy |
| 4 | + |
| 5 | +TEST_NORMS = [ |
| 6 | + 6.557438373565674, |
| 7 | + 8.83176040649414, |
| 8 | + 6.164413928985596, |
| 9 | + 9.165151596069336, |
| 10 | + 7.4833149909973145, |
| 11 | + 7.211102485656738, |
| 12 | + 7.4833149909973145 |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +def test_embeddings_with_norms(): |
| 17 | + embeds = finalfusion.Embeddings( |
| 18 | + "tests/embeddings.fifu") |
| 19 | + embeds_dict = dict() |
| 20 | + with open("tests/embeddings.txt", "r", encoding="utf8") as lines: |
| 21 | + for line in lines: |
| 22 | + line_list = line.split(' ') |
| 23 | + embeds_dict[line_list[0]] = [float(val) for val in line_list[1:]] |
| 24 | + |
| 25 | + for embedding_with_norm, norm in zip(embeds.iter_with_norm(), TEST_NORMS): |
| 26 | + unnormed_embed = embedding_with_norm[1] * norm |
| 27 | + test_embed = embeds_dict[embedding_with_norm[0]] |
| 28 | + assert numpy.allclose( |
| 29 | + unnormed_embed, test_embed), "Embedding from 'iter_with_norm()' fails to match!" |
| 30 | + assert len( |
| 31 | + embedding_with_norm) == 3, "The number of values returned by 'iter_with_norm()' does not match!" |
| 32 | + |
| 33 | + |
| 34 | +def test_embeddings(): |
| 35 | + embeds = finalfusion.Embeddings( |
| 36 | + "tests/embeddings.fifu") |
| 37 | + embeds_dict = dict() |
| 38 | + with open("tests/embeddings.txt", "r", encoding="utf8") as lines: |
| 39 | + for line in lines: |
| 40 | + line_list = line.split(' ') |
| 41 | + embeds_dict[line_list[0]] = [float(i) for i in line_list[1:]] |
| 42 | + |
| 43 | + for embedding_with_norm, norm in zip(embeds, TEST_NORMS): |
| 44 | + unnormed_embed = embedding_with_norm[1] * norm |
| 45 | + test_embed = embeds_dict[embedding_with_norm[0]] |
| 46 | + assert numpy.allclose( |
| 47 | + unnormed_embed, test_embed), "Embedding from normal iterator fails to match!" |
| 48 | + assert len( |
| 49 | + embedding_with_norm) == 2, "The number of values returned by normal iterator does not match!" |
| 50 | + |
| 51 | + |
| 52 | +def test_norms(): |
| 53 | + embeds = finalfusion.Embeddings( |
| 54 | + "tests/embeddings.fifu") |
| 55 | + embeds_dict = dict() |
| 56 | + with open("tests/embeddings.txt", "r", encoding="utf8") as lines: |
| 57 | + for line in lines: |
| 58 | + line_list = line.split(' ') |
| 59 | + embeds_dict[line_list[0]] = [float(val) for val in line_list[1:]] |
| 60 | + |
| 61 | + for embedding_with_norm, norm in zip(embeds.iter_with_norm(), TEST_NORMS): |
| 62 | + assert pytest.approx( |
| 63 | + embedding_with_norm[2] == norm), "Norm fails to match!" |
0 commit comments