File size: 8,943 Bytes
de071e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import faiss
import numpy as np
import torch
from typing import Dict, Tuple, List, NamedTuple
import os
import pickle
import yaml
from transformers import AutoModelForCausalLM


class WeightInfo(NamedTuple):
    """
    A named tuple containing metadata about a weight matrix.

    Attributes:
        model_name: Name or identifier of the model
        param_name: Name of the parameter in the model's state dict
        dimensions: Tuple containing the shape of the weight matrix (d1, d2)
    """

    model_name: str
    param_name: str
    dimensions: Tuple[int, int]


class CSWSearch:
    """
    CSWSearch (Cosine Similarity of Weights Search) using FAISS for efficient similarity search.

    This class enables fast indexing and retrieval of similar weight matrices across models,
    organizing weight matrices by their dimensions to ensure comparable searches.
    """

    def __init__(self):
        # Keep track of what each index position corresponds to
        self.metadata: Dict[Tuple[int, int], List[WeightInfo]] = {}
        # Track dimensions and index file locations
        self.index_files: Dict[Tuple[int, int], str] = {}
        # Directory where indices are stored
        self.index_dir: str = "indexes"
        # Currently loaded index
        self.current_index: Tuple[Tuple[int, int], faiss.Index] = None

    def add_weight_matrix(
        self, model_name: str, param_name: str, weight_matrix: np.ndarray
    ) -> None:
        """
        Add a weight matrix to the appropriate index based on its dimensions.

        Args:
            model_name: Name or identifier of the model
            param_name: Name of the parameter in the model's state dict
            weight_matrix: The weight matrix tensor to index

        Returns:
            None
        """
        print(f"Adding {model_name} {param_name}")
        d1, d2 = weight_matrix.shape
        dim_key = (d1, d2)

        # First time seeing this dimension combination
        if dim_key not in self.index_files:
            self.metadata[dim_key] = []
            self.index_files[dim_key] = f"index_{d1}x{d2}.index"

        # Load the appropriate index
        index = self._load_index(dim_key)

        # Flatten matrix in row-major order and normalize
        flat_weights = np.array(weight_matrix.to(dtype=torch.float32).reshape(1, -1).numpy())
        faiss.normalize_L2(flat_weights)  # for cosine similarity

        # Add to appropriate index
        index.add(flat_weights)

        # Store metadata
        self.metadata[dim_key].append(WeightInfo(model_name, param_name, (d1, d2)))

        # Save the updated index
        self._save_index(dim_key, index)

    def find_similar_weights(
        self, model_name: str, weight_matrix: np.ndarray, k: int = 5
    ) -> List[Tuple[WeightInfo, float]]:
        """
        Find similar weight matrices with matching dimensions.

        Searches for weight matrices most similar to the provided one,
        but only among those with the same dimensions.

        Args:
            model_name: Name or identifier of the model (used to exclude self-matches)
            weight_matrix: The weight matrix tensor to search for
            k: Number of similar matrices to return (default: 5)

        Returns:
            List of tuples containing (WeightInfo, similarity_score)

        Raises:
            ValueError: If no weight matrices with matching dimensions are found
        """
        d1, d2 = weight_matrix.shape
        dim_key = (d1, d2)

        if dim_key not in self.index_files:
            raise ValueError(f"No weight matrices found with dimensions {dim_key}")

        # Load the appropriate index
        index = self._load_index(dim_key)

        # Prepare query in same way as stored matrices
        query = np.array(weight_matrix.to(dtype=torch.float32).reshape(1, -1).numpy())
        faiss.normalize_L2(query)

        # Search
        distances, indices = index.search(query, k + 1)  # +1 for self-match

        # Format results (excluding self-match)
        results = []
        for idx, sim in zip(indices[0], distances[0]):
            info = self.metadata[dim_key][idx]
            if info.model_name != model_name:  # Skip self-match
                results.append((info, float(sim)))

        return results[:k]

    def _load_index(self, dim_key: Tuple[int, int]) -> faiss.Index:
        """
        Load or create the FAISS index for a specific dimension.

        Args:
            dim_key: Tuple of dimensions (d1, d2)

        Returns:
            faiss.Index: The loaded or newly created index
        """
        if self.current_index and self.current_index[0] == dim_key:
            return self.current_index[1]

        d1, d2 = dim_key
        index_path = os.path.join(self.index_dir, self.index_files[dim_key])

        if os.path.exists(index_path):
            try:
                index = faiss.read_index(index_path)
            except RuntimeError:
                print(f"Error reading index file {index_path}. Creating a new index.")
                index = faiss.IndexFlatIP(d1 * d2)
        else:
            print(f"Index file {index_path} not found. Creating a new index.")
            index = faiss.IndexFlatIP(d1 * d2)

        self.current_index = (dim_key, index)
        return index

    def _save_index(self, dim_key: Tuple[int, int], index: faiss.Index):
        """
        Save the index for the given dimensions to disk.

        Args:
            dim_key: Tuple of dimensions (d1, d2)
            index: The FAISS index to save

        Returns:
            None
        """
        index_path = os.path.join(self.index_dir, self.index_files[dim_key])
        faiss.write_index(index, index_path)

    def save(self, directory: str):
        """
        Save the entire search system (metadata and indexes) to disk.

        Args:
            directory: Directory where indices and metadata will be stored

        Returns:
            None
        """
        self.index_dir = directory
        os.makedirs(directory, exist_ok=True)

        if self.current_index:
            self._save_index(self.current_index[0], self.current_index[1])

        metadata_path = os.path.join(directory, "metadata.pkl")
        with open(metadata_path, "wb") as f:
            pickle.dump(self.metadata, f)

        index_files_path = os.path.join(directory, "index_files.pkl")
        with open(index_files_path, "wb") as f:
            pickle.dump(self.index_files, f)

    @classmethod
    def load(cls, directory: str):
        """
        Load a previously saved search system from disk.

        Args:
            directory: Directory where indices and metadata are stored

        Returns:
            CSWSearch: The loaded search system
        """
        csw_search = cls()
        csw_search.index_dir = directory

        metadata_path = os.path.join(directory, "metadata.pkl")
        with open(metadata_path, "rb") as f:
            csw_search.metadata = pickle.load(f)

        index_files_path = os.path.join(directory, "index_files.pkl")
        with open(index_files_path, "rb") as f:
            csw_search.index_files = pickle.load(f)

        return csw_search


csw = CSWSearch()


def add_params(model_list):
    """
    Index weight matrices from a list of HuggingFace model IDs.

    Loads each model, extracts its parameters, and adds all 2D weight matrices
    to the CSWSearch index for later similarity search.

    Args:
        model_list: List of HuggingFace model IDs to index

    Returns:
        None: Updates the global csw search index
    """
    for model_id in model_list:
        model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16)
        weights = model.state_dict()
        params = list(weights.keys())
        for param in params:
            # Skip 1D tensors (like bias terms or layer norms)
            if len(weights[param].shape) == 1:
                continue
            csw.add_weight_matrix(model_id, param_name=param, weight_matrix=weights[param])


def get_similar_param(param, k=5):
    """
    Find similar parameters to the given weight matrix across indexed models.

    Args:
        param: Weight matrix tensor to search for
        k: Number of similar matrices to return (default: 5)

    Returns:
        List of tuples containing (WeightInfo, similarity_score)
    """
    return csw.find_similar_weights("--", param, k=k)


def main():
    # Model list to add from yaml
    model_list = yaml.safe_load(open("config/llama7b.yaml", "r"))
    add_params(model_list)
    csw.save("indexes")

    # Weight matrix to search for
    model = AutoModelForCausalLM.from_pretrained(
        "meta-llama/Llama-2-7b-hf", torch_dtype=torch.bfloat16
    )
    weights = model.state_dict()
    attn_name = "model.layers.0.self_attn.o_proj.weight"

    print(get_similar_param(weights[attn_name]))

    return


if __name__ == "__main__":
    main()