| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Unit tests for Cohere embed protocol: build_typed_embeddings and its |
| 4 | underlying packing helpers, plus Cohere-specific serving helpers.""" |
| 5 | |
| 6 | import struct |
| 7 | |
| 8 | import numpy as np |
| 9 | import pybase64 as base64 |
| 10 | import pytest |
| 11 | |
| 12 | from vllm.entrypoints.pooling.embed.protocol import ( |
| 13 | build_typed_embeddings, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | @pytest.fixture |
| 18 | def sample_embeddings() -> list[list[float]]: |
| 19 | return [ |
| 20 | [0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8], |
| 21 | [-0.05, 0.15, -0.25, 0.35, -0.45, 0.55, -0.65, 0.75], |
| 22 | ] |
| 23 | |
| 24 | |
| 25 | class TestBuildTypedEmbeddingsFloat: |
| 26 | def test_float_passthrough(self, sample_embeddings: list[list[float]]): |
| 27 | result = build_typed_embeddings(sample_embeddings, ["float"]) |
| 28 | assert result.float == sample_embeddings |
| 29 | assert result.binary is None |
| 30 | |
| 31 | def test_empty_input(self): |
| 32 | result = build_typed_embeddings([], ["float"]) |
| 33 | assert result.float == [] |
| 34 | |
| 35 | |
| 36 | class TestBuildTypedEmbeddingsBinary: |
| 37 | def test_binary_packing(self): |
| 38 | # 8 values: positive->1, negative->0 => bits: 10101010 = 0xAA = 170 |
| 39 | # signed: 170 - 128 = 42 |
| 40 | embs = [[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]] |
| 41 | result = build_typed_embeddings(embs, ["binary"]) |
| 42 | assert result.binary is not None |
| 43 | assert result.binary[0] == [42] |
| 44 | |
| 45 | def test_ubinary_packing(self): |
| 46 | embs = [[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]] |
| 47 | result = build_typed_embeddings(embs, ["ubinary"]) |
| 48 | assert result.ubinary is not None |
| 49 | assert result.ubinary[0] == [170] # 0b10101010 |
| 50 | |
| 51 | def test_binary_all_positive(self): |
| 52 | embs = [[0.1] * 8] |
| 53 | result = build_typed_embeddings(embs, ["binary"]) |
| 54 | assert result.binary is not None |
| 55 | # all bits = 1 => 0xFF = 255, signed: 255 - 128 = 127 |
| 56 | assert result.binary[0] == [127] |
| 57 | |
| 58 | def test_binary_all_negative(self): |
| 59 | embs = [[-0.1] * 8] |
| 60 | result = build_typed_embeddings(embs, ["binary"]) |
| 61 | assert result.binary is not None |
| 62 | # all bits = 0, signed: 0 - 128 = -128 |
| 63 | assert result.binary[0] == [-128] |
| 64 | |
| 65 | def test_binary_dimension_is_eighth(self, sample_embeddings: list[list[float]]): |
| 66 | result = build_typed_embeddings(sample_embeddings, ["binary"]) |
| 67 | assert result.binary is not None |
| 68 | for orig, packed in zip(sample_embeddings, result.binary): |
| 69 | assert len(packed) == len(orig) // 8 |
| 70 | |
| 71 | def test_zero_treated_as_positive(self): |
| 72 | embs = [[0.0] * 8] |
| 73 | result = build_typed_embeddings(embs, ["binary"]) |
| 74 | assert result.binary is not None |
| 75 | # 0.0 >= 0 is True, so bit=1 for all => 127 (signed) |
| 76 | assert result.binary[0] == [127] |
| 77 | |
| 78 | def test_negative_zero_treated_as_positive(self): |
| 79 | """Packing follows ``value >= 0``, not the IEEE-754 sign bit. |
| 80 | |
| 81 | ``-0.0 >= 0`` is True, so these bits must be set. An implementation |
| 82 | reading the sign bit instead would pack -128 here. |
| 83 | """ |
| 84 | result = build_typed_embeddings([[-0.0] * 8], ["binary"]) |
| 85 | assert result.binary is not None |
| 86 | assert result.binary[0] == [127] |
| 87 | |
| 88 | def test_negative_denormal_treated_as_negative(self): |
| 89 | """Sign is decided at float64 precision. |
| 90 | |
| 91 | The smallest float64 denormal underflows to -0.0 in float32, where |
| 92 | ``>= 0`` is True; narrowing the comparison would wrongly pack 127. |
| 93 | """ |
| 94 | result = build_typed_embeddings([[-5e-324] * 8], ["binary"]) |
| 95 | assert result.binary is not None |
| 96 | assert result.binary[0] == [-128] |
| 97 | |
| 98 | def test_empty_input(self): |
| 99 | result = build_typed_embeddings([], ["binary", "ubinary"]) |
| 100 | assert result.binary == [] |
| 101 | assert result.ubinary == [] |
| 102 | |
| 103 | @pytest.mark.parametrize("embs", [[0.1] * 8, [[[0.1] * 8] * 2]], ids=["1d", "3d"]) |
| 104 | def test_non_2d_input_raises(self, embs): |
| 105 | """A malformed batch must raise rather than pack along the last axis. |
| 106 | |
| 107 | Packing a 1D or 3D input would silently emit wrongly shaped |
| 108 | ``binary``/``ubinary`` values instead of reporting an error. |
| 109 | """ |
| 110 | with pytest.raises(ValueError, match="2D batch"): |
| 111 | build_typed_embeddings(embs, ["binary"]) |
| 112 | |
| 113 | def test_non_multiple_of_8_raises(self): |
| 114 | embs = [[0.1] * 7] |
| 115 | with pytest.raises(ValueError, match="multiple of 8"): |
| 116 | build_typed_embeddings(embs, ["binary"]) |
| 117 | |
| 118 | def test_ubinary_non_multiple_of_8_raises(self): |
| 119 | embs = [[0.1] * 10] |
| 120 | with pytest.raises(ValueError, match="multiple of 8"): |
| 121 | build_typed_embeddings(embs, ["ubinary"]) |
| 122 | |
| 123 | |
| 124 | class TestBuildTypedEmbeddingsBase64: |
| 125 | def test_base64_roundtrip(self, sample_embeddings: list[list[float]]): |
| 126 | result = build_typed_embeddings(sample_embeddings, ["base64"]) |
| 127 | assert result.base64 is not None |
| 128 | assert len(result.base64) == 2 |
| 129 | |
| 130 | for orig, b64_str in zip(sample_embeddings, result.base64): |
| 131 | decoded = base64.b64decode(b64_str) |
| 132 | n = len(orig) |
| 133 | values = struct.unpack(f"<{n}f", decoded) |
| 134 | np.testing.assert_allclose(orig, values, rtol=1e-5) |
| 135 | |
| 136 | def test_base64_byte_length(self): |
| 137 | embs = [[0.1, 0.2, 0.3]] |
| 138 | result = build_typed_embeddings(embs, ["base64"]) |
| 139 | assert result.base64 is not None |
| 140 | raw = base64.b64decode(result.base64[0]) |
| 141 | assert len(raw) == 3 * 4 # 3 floats * 4 bytes each |
| 142 | |
| 143 | |
| 144 | class TestBuildTypedEmbeddingsMultiple: |
| 145 | def test_all_types_at_once(self, sample_embeddings: list[list[float]]): |
| 146 | result = build_typed_embeddings( |
| 147 | sample_embeddings, |
| 148 | ["float", "binary", "ubinary", "base64"], |
| 149 | ) |
| 150 | assert result.float is not None |
| 151 | assert result.binary is not None |
| 152 | assert result.ubinary is not None |
| 153 | assert result.base64 is not None |
| 154 | |
| 155 | def test_subset_types(self, sample_embeddings: list[list[float]]): |
| 156 | result = build_typed_embeddings(sample_embeddings, ["float", "binary"]) |
| 157 | assert result.float is not None |
| 158 | assert result.binary is not None |
| 159 | assert result.ubinary is None |
| 160 | assert result.base64 is None |
| 161 | |
| 162 | def test_unknown_type_ignored(self, sample_embeddings: list[list[float]]): |
| 163 | result = build_typed_embeddings(sample_embeddings, ["float", "unknown_type"]) |
| 164 | assert result.float is not None |