MCPcopy Create free account
hub / github.com/LUMIA-Group/MemoryDecoder / build_index

Method build_index

knn_utils/saveEmbedMulti.py:440–477  ·  view source on GitHub ↗
(self, num_keys_to_add_at_a_time=1_000_000, ncentroids=4096, seed=42, code_size=32, probe=8)

Source from the content-addressed store, hash-verified

438 self.model.broken_into = None
439
440 def build_index(self, num_keys_to_add_at_a_time=1_000_000, ncentroids=4096, seed=42, code_size=32, probe=8):
441 logger.info('Loading Dataset...')
442 dstore_path = self._get_arrow_file_path()
443 dstore = Dataset.from_file(dstore_path)
444 # Set format to numpy for proper array conversion
445 dstore.set_format(type='numpy', columns=['keys', 'vals'])
446
447 logger.info('Building index...')
448 index_name = get_index_path(self.dstore_dir, self.model.config.model_type, self.eval_subset, self.dimension)
449
450 quantizer = faiss.IndexFlatL2(self.dimension)
451 index = faiss.IndexIVFPQ(quantizer, self.dimension, ncentroids, code_size, 8)
452 index.nprobe = probe
453
454 logger.info('Training Index...')
455 np.random.seed(seed)
456 sample_size = min(200000, len(dstore))
457 random_sample = np.random.choice(len(dstore), size=sample_size, replace=False)
458
459 train_data = np.array(dstore.select(random_sample)['keys']).astype(np.float32)
460 start = time.time()
461 index.train(train_data)
462 logger.info(f'Training took {time.time() - start:.2f} s')
463
464 logger.info('Adding Keys...')
465 start_time = time.time()
466
467 for start in tqdm(range(0, len(dstore), num_keys_to_add_at_a_time)):
468 end = min(len(dstore), start + num_keys_to_add_at_a_time)
469 to_add = np.array(dstore.select(range(start,end))['keys']).astype(np.float32)
470
471 index.add_with_ids(to_add, np.arange(start, end))
472
473 if (start // num_keys_to_add_at_a_time) % 10 == 0:
474 faiss.write_index(index, index_name)
475
476 faiss.write_index(index, index_name)
477 logger.info(f'Added {len(dstore)} keys in {time.time() - start_time:.2f} s')
478
479class ActivationCapturer(nn.Module):
480 def __init__(self, layer, capture_input=False):

Callers

nothing calls this directly

Calls 2

_get_arrow_file_pathMethod · 0.95
get_index_pathFunction · 0.70

Tested by

no test coverage detected