File size: 10,836 Bytes
626eca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

import hydra
import numpy
import torch
from omegaconf import OmegaConf
from rich.pretty import pprint

from relik.common import upload
from relik.common.log import get_console_logger, get_logger
from relik.common.utils import (
    from_cache,
    is_remote_url,
    is_str_a_path,
    relative_to_absolute_path,
    sapienzanlp_model_urls,
)
from relik.retriever.data.labels import Labels

# from relik.retriever.models.model import GoldenRetriever, RetrievedSample


logger = get_logger(__name__)
console_logger = get_console_logger()


@dataclass
class IndexerOutput:
    indices: Union[torch.Tensor, numpy.ndarray]
    distances: Union[torch.Tensor, numpy.ndarray]


class BaseDocumentIndex:
    CONFIG_NAME = "config.yaml"
    DOCUMENTS_FILE_NAME = "documents.json"
    EMBEDDINGS_FILE_NAME = "embeddings.pt"

    def __init__(
        self,
        documents: Union[str, List[str], Labels, os.PathLike, List[os.PathLike]] = None,
        embeddings: Optional[torch.Tensor] = None,
        name_or_dir: Optional[Union[str, os.PathLike]] = None,
    ) -> None:
        if documents is not None:
            if isinstance(documents, Labels):
                self.documents = documents
            else:
                documents_are_paths = False

                # normalize the documents to list if not already
                if not isinstance(documents, list):
                    documents = [documents]

                # now check if the documents are a list of paths (either str or os.PathLike)
                if isinstance(documents[0], str) or isinstance(
                    documents[0], os.PathLike
                ):
                    # check if the str is a path
                    documents_are_paths = is_str_a_path(documents[0])

                # if the documents are a list of paths, then we load them
                if documents_are_paths:
                    logger.info("Loading documents from paths")
                    _documents = []
                    for doc in documents:
                        with open(relative_to_absolute_path(doc)) as f:
                            _documents += [line.strip() for line in f.readlines()]
                    # remove duplicates
                    documents = list(set(_documents))

                self.documents = Labels()
                self.documents.add_labels(documents)
        else:
            self.documents = Labels()

        self.embeddings = embeddings
        self.name_or_dir = name_or_dir

    @property
    def config(self) -> Dict[str, Any]:
        """
        The configuration of the document index.

        Returns:
            `Dict[str, Any]`: The configuration of the retriever.
        """

        def obj_to_dict(obj):
            match obj:
                case dict():
                    data = {}
                    for k, v in obj.items():
                        data[k] = obj_to_dict(v)
                    return data

                case list() | tuple():
                    return [obj_to_dict(x) for x in obj]

                case object(__dict__=_):
                    data = {
                        "_target_": f"{obj.__class__.__module__}.{obj.__class__.__name__}",
                    }
                    for k, v in obj.__dict__.items():
                        if not k.startswith("_"):
                            data[k] = obj_to_dict(v)
                    return data

                case _:
                    return obj

        return obj_to_dict(self)

    def index(
        self,
        retriever,
        *args,
        **kwargs,
    ) -> "BaseDocumentIndex":
        raise NotImplementedError

    def search(self, query: Any, k: int = 1, *args, **kwargs) -> List:
        raise NotImplementedError

    def get_index_from_passage(self, document: str) -> int:
        """
        Get the index of the passage.

        Args:
            document (`str`):
                The document to get the index for.

        Returns:
            `int`: The index of the document.
        """
        return self.documents.get_index_from_label(document)

    def get_passage_from_index(self, index: int) -> str:
        """
        Get the document from the index.

        Args:
            index (`int`):
                The index of the document.

        Returns:
            `str`: The document.
        """
        return self.documents.get_label_from_index(index)

    def get_embeddings_from_index(self, index: int) -> torch.Tensor:
        """
        Get the document vector from the index.

        Args:
            index (`int`):
                The index of the document.

        Returns:
            `torch.Tensor`: The document vector.
        """
        if self.embeddings is None:
            raise ValueError(
                "The documents must be indexed before they can be retrieved."
            )
        if index >= self.embeddings.shape[0]:
            raise ValueError(
                f"The index {index} is out of bounds. The maximum index is {len(self.embeddings) - 1}."
            )
        return self.embeddings[index]

    def get_embeddings_from_passage(self, document: str) -> torch.Tensor:
        """
        Get the document vector from the document label.

        Args:
            document (`str`):
                The document to get the vector for.

        Returns:
            `torch.Tensor`: The document vector.
        """
        if self.embeddings is None:
            raise ValueError(
                "The documents must be indexed before they can be retrieved."
            )
        return self.get_embeddings_from_index(self.get_index_from_passage(document))

    def save_pretrained(
        self,
        output_dir: Union[str, os.PathLike],
        config: Optional[Dict[str, Any]] = None,
        config_file_name: Optional[str] = None,
        document_file_name: Optional[str] = None,
        embedding_file_name: Optional[str] = None,
        push_to_hub: bool = False,
        **kwargs,
    ):
        """
        Save the retriever to a directory.

        Args:
            output_dir (`str`):
                The directory to save the retriever to.
            config (`Optional[Dict[str, Any]]`, `optional`):
                The configuration to save. If `None`, the current configuration of the retriever will be
                saved. Defaults to `None`.
            config_file_name (`Optional[str]`, `optional`):
                The name of the configuration file. Defaults to `config.yaml`.
            document_file_name (`Optional[str]`, `optional`):
                The name of the document file. Defaults to `documents.json`.
            embedding_file_name (`Optional[str]`, `optional`):
                The name of the embedding file. Defaults to `embeddings.pt`.
            push_to_hub (`bool`, `optional`):
                Whether to push the saved retriever to the hub. Defaults to `False`.
        """
        if config is None:
            # create a default config
            config = self.config

        config_file_name = config_file_name or self.CONFIG_NAME
        document_file_name = document_file_name or self.DOCUMENTS_FILE_NAME
        embedding_file_name = embedding_file_name or self.EMBEDDINGS_FILE_NAME

        # create the output directory
        output_dir = Path(output_dir)
        output_dir.mkdir(parents=True, exist_ok=True)

        logger.info(f"Saving retriever to {output_dir}")
        logger.info(f"Saving config to {output_dir / config_file_name}")
        # pretty print the config
        pprint(config, console=console_logger, expand_all=True)
        OmegaConf.save(config, output_dir / config_file_name)

        # save the current state of the retriever
        embedding_path = output_dir / embedding_file_name
        logger.info(f"Saving retriever state to {output_dir / embedding_path}")
        torch.save(self.embeddings, embedding_path)

        # save the passage index
        documents_path = output_dir / document_file_name
        logger.info(f"Saving passage index to {documents_path}")
        self.documents.save(documents_path)

        logger.info("Saving document index to disk done.")

        if push_to_hub:
            # push to hub
            logger.info(f"Pushing to hub")
            model_id = model_id or output_dir.name
            upload(output_dir, model_id, **kwargs)

    @classmethod
    def from_pretrained(
        cls,
        name_or_dir: Union[str, os.PathLike],
        device: str = "cpu",
        precision: Optional[str] = None,
        config_file_name: Optional[str] = None,
        document_file_name: Optional[str] = None,
        embedding_file_name: Optional[str] = None,
        *args,
        **kwargs,
    ) -> "BaseDocumentIndex":
        cache_dir = kwargs.pop("cache_dir", None)
        force_download = kwargs.pop("force_download", False)

        config_file_name = config_file_name or cls.CONFIG_NAME
        document_file_name = document_file_name or cls.DOCUMENTS_FILE_NAME
        embedding_file_name = embedding_file_name or cls.EMBEDDINGS_FILE_NAME

        model_dir = from_cache(
            name_or_dir,
            filenames=[config_file_name, document_file_name, embedding_file_name],
            cache_dir=cache_dir,
            force_download=force_download,
        )

        config_path = model_dir / config_file_name
        if not config_path.exists():
            raise FileNotFoundError(
                f"Model configuration file not found at {config_path}."
            )

        config = OmegaConf.load(config_path)
        pprint(OmegaConf.to_container(config), console=console_logger, expand_all=True)

        # load the documents
        documents_path = model_dir / document_file_name

        if not documents_path.exists():
            raise ValueError(f"Document file `{documents_path}` does not exist.")
        logger.info(f"Loading documents from {documents_path}")
        documents = Labels.from_file(documents_path)

        # load the passage embeddings
        embedding_path = model_dir / embedding_file_name
        # run some checks
        embeddings = None
        if embedding_path.exists():
            logger.info(f"Loading embeddings from {embedding_path}")
            embeddings = torch.load(embedding_path, map_location="cpu")
        else:
            logger.warning(f"Embedding file `{embedding_path}` does not exist.")

        document_index = hydra.utils.instantiate(
            config,
            documents=documents,
            embeddings=embeddings,
            device=device,
            precision=precision,
            name_or_dir=name_or_dir,
            *args,
            **kwargs,
        )

        return document_index