File size: 8,436 Bytes
aee08b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Programmatic Index Creation
===========================
Use this script to create an index from a DataFrame without the web interface.

Example usage:
    python create_index.py --input data.csv --sequence-column text
    
Or use programmatically:
    from create_index import create_index_from_dataframe
    import pandas as pd
    
    df = pd.DataFrame({
        'sequence': ['Hello world', 'Machine learning is great', ...],
        'category': ['greeting', 'tech', ...],
        'id': [1, 2, ...]
    })
    
    create_index_from_dataframe(df, sequence_column='sequence')
"""

import argparse
import pickle
from pathlib import Path
import numpy as np
import pandas as pd
from pickle import dump, load
import tiktoken
import faiss
import torch.nn as nn
from x_transformers import TransformerWrapper, Encoder
import torch
from tqdm import tqdm

# Set device and check available GPUs
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_gpus = torch.cuda.device_count()
print(f"Using device: {device}")
print(f"Available GPUs: {n_gpus}")
for i in range(n_gpus):
    print(f"  GPU {i}: {torch.cuda.get_device_name(i)}")


class GenomicTransformer(nn.Module):
    def __init__(self, vocab_size=40000, hidden_dim=32, layers=2, heads=3, max_length=6000):
        super().__init__()
        self.model = TransformerWrapper(
            num_tokens=vocab_size,
            max_seq_len=max_length,
            attn_layers=Encoder(
                dim=hidden_dim,
                depth=layers,
                heads=heads,
                rotary_pos_emb=True,
                attn_orthog_projected_values=True,
                attn_orthog_projected_values_per_head=True, 
                attn_flash=True
            )
        )
        
    def forward(self, input_ids, return_embeddings=False):
        logits = self.model(input_ids, return_embeddings=return_embeddings)
        return logits


# Configuration - must match app.py
DATA_DIR = Path("data")
DATA_DIR.mkdir(exist_ok=True)
INDEX_PATH = DATA_DIR / "faiss.index"
METADATA_PATH = DATA_DIR / "metadata.pkl"
EMBEDDINGS_PATH = DATA_DIR / "embeddings.npy"
EMBEDDING_MODEL = "all-MiniLM-L6-v2"

pattern = load(open("/user/hassanahmed.hassan/u21055/.project/dir.project/towards_better_genomic_models/data/tokenizer_components_bpe_with_repeats.pkl", "rb"))['pattern']
mergable_ranks = load(open("/user/hassanahmed.hassan/u21055/.project/dir.project/towards_better_genomic_models/data/tokenizer_components_bpe_with_repeats.pkl", "rb"))['mergable_ranks']

recreated_enc = tiktoken.Encoding(
    name="genomic_bpe_recreated",
    pat_str=pattern,
    mergeable_ranks=mergable_ranks,
    special_tokens={}
)

# Initialize model
MODEL = GenomicTransformer(
    vocab_size=40_000, hidden_dim=512, layers=12, heads=8
)

# Wrap with DataParallel if multiple GPUs available
if n_gpus > 1:
    print(f"Using DataParallel across {n_gpus} GPUs")
    MODEL = nn.DataParallel(MODEL)

MODEL = MODEL.to(device)
MODEL.eval()


def create_index_from_dataframe(
    df: pd.DataFrame,
    sequence_column: str = "sequence",
    model=MODEL,
    encoder=recreated_enc,
    batch_size: int = 8  # Increased batch size for multi-GPU
) -> dict:
    """
    Create a FAISS index from a pandas DataFrame.
    
    Args:
        df: DataFrame containing sequences and metadata
        sequence_column: Name of the column containing text sequences
        model: The transformer model to use
        encoder: The tokenizer/encoder
        batch_size: Batch size for encoding (increase for multi-GPU)
        
    Returns:
        dict with index statistics
    """
    if sequence_column not in df.columns:
        raise ValueError(f"Column '{sequence_column}' not found. Available: {list(df.columns)}")
    
    # Get sequences
    sequences = df[sequence_column].astype(str).tolist()[:10]
    df = df.iloc[:10].copy()
    df["__sequence__"] = sequences
    
    # Create embeddings
    print(f"Creating embeddings for {len(sequences)} sequences...")
    encodings = encoder.encode_batch(sequences)
    embeddings = []
    print(f"Total encodings: {len(encodings)}")
    
    # Adjust batch size to be divisible by number of GPUs for efficiency
    effective_batch_size = batch_size * n_gpus if n_gpus > 1 else batch_size
    print(f"Using effective batch size: {effective_batch_size}")
    
    for i in tqdm(range(0, len(encodings), effective_batch_size)):
        batch_encodings = encodings[i:i+effective_batch_size]
        # pad to max length in batch
        max_len = max(len(enc) for enc in batch_encodings)
        batch_encodings = [enc + [0]*(max_len - len(enc)) for enc in batch_encodings]
        
        # Move tensor to GPU
        batch_tensor = torch.tensor(batch_encodings).long().to(device)
        print(f"Batch tensor shape: {batch_tensor.shape}")
        print(f"Sample batch tensor: {batch_tensor[0][:10]}")
        
        with torch.no_grad():
            print("Generating embeddings...")
            batch_embeddings = model(batch_tensor, return_embeddings=True)
            print(f"Raw batch embeddings shape: {batch_embeddings.shape}")
            # Move back to CPU for numpy conversion
            batch_embeddings = batch_embeddings.mean(dim=1).cpu().numpy().tolist()
            print(f"Batch embeddings shape: {np.array(batch_embeddings).shape}")

        if i == 0:
            embeddings = batch_embeddings
        else:
            embeddings = embeddings + batch_embeddings

    embeddings = np.array(embeddings)
    embeddings = embeddings.astype(np.float32)
    
    # Create FAISS index
    dimension = embeddings.shape[1]
    index = faiss.IndexFlatIP(dimension)  # Inner product = cosine sim for normalized vectors
    index.add(embeddings)
    
    # Save everything
    print("Saving index to disk...")
    faiss.write_index(index, str(INDEX_PATH))
    with open(METADATA_PATH, "wb") as f:
        pickle.dump(df, f)
    np.save(EMBEDDINGS_PATH, embeddings)
    
    stats = {
        "documents_indexed": index.ntotal,
        "embedding_dimension": dimension,
        "model": 'MODEL',
        "index_path": str(INDEX_PATH),
        "metadata_path": str(METADATA_PATH),
        "gpus_used": n_gpus
    }
    
    print(f"Index created successfully!")
    print(f"  - Documents: {stats['documents_indexed']}")
    print(f"  - Dimensions: {stats['embedding_dimension']}")
    print(f"  - GPUs used: {stats['gpus_used']}")
    
    return stats


def search_index(
    query: str,
    top_k: int = 10,
    model=MODEL, 
    encoder=recreated_enc
) -> list[dict]:
    """
    Search the index for similar sequences.
    """
    if not INDEX_PATH.exists():
        raise FileNotFoundError("No index found. Create one first with create_index_from_dataframe()")
    
    # Load resources
    index = faiss.read_index(str(INDEX_PATH))
    with open(METADATA_PATH, "rb") as f:
        metadata = pickle.load(f)
    
    # Encode query
    encodings = encoder.encode_ordinary(query)
    query_tensor = torch.tensor([encodings]).long().to(device)
    
    with torch.no_grad():
        query_embedding = model(query_tensor, return_embeddings=True).mean(dim=1).cpu().numpy()
    
    query_embedding = query_embedding.astype(np.float32)
    
    # Search
    k = min(top_k, index.ntotal)
    scores, indices = index.search(query_embedding, k)
    
    # Build results
    results = []
    for score, idx in zip(scores[0], indices[0]):
        if idx == -1:
            continue
        
        row = metadata.iloc[idx].to_dict()
        sequence = row.pop("__sequence__", "")
        
        results.append({
            "score": float(score),
            "sequence": sequence,
            "metadata": row
        })
    
    return results


def main():
    parser = argparse.ArgumentParser(description="Create semantic search index from CSV")
    parser.add_argument("--sequence-column", "-c", default="seq_with_repeat_tokens", help="Column containing sequences")
    parser.add_argument("--batch-size", "-b", type=int, default=8, help="Batch size per GPU")
    
    args = parser.parse_args()
    
    df = pd.read_parquet("/user/hassanahmed.hassan/u21055/.project/dir.project/towards_better_genomic_models/data/sample.parquet")
    print(f"Loaded {len(df)} rows with columns: {list(df.columns)}")
    
    create_index_from_dataframe(df, args.sequence_column, MODEL, recreated_enc, batch_size=args.batch_size)


if __name__ == "__main__":
    main()