Download runtime/retrieval_math.py from guga-guga/GeoPOI: direct link, hf CLI and curl.
- Browser
- Download file 4.62 kB
-
https://huggingface.co/guga-guga/GeoPOI/resolve/main/runtime/retrieval_math.py
- Command line
-
hf download hf://guga-guga/GeoPOI/runtime/retrieval_math.py
-
curl -L -o retrieval_math.py https://huggingface.co/guga-guga/GeoPOI/resolve/main/runtime/retrieval_math.py
4.62 kB
| import math | |
| import torch | |
| from torch.nn import functional as functional | |
| def bounded_shortlist(queries, query_valid, embeddings, surface_ids, local_queries, total_k=256, fallback_fraction=0.25, chunk_size=4096): | |
| if total_k < 1 or chunk_size < 1 or (not 0 <= fallback_fraction <= 1): | |
| raise ValueError('invalid retrieval budget') | |
| if embeddings.ndim != 2 or embeddings.shape[1] != 128 or len(surface_ids) != len(embeddings): | |
| raise ValueError('pool embeddings and decoder-surface ids must align') | |
| if queries.ndim != 2 or queries.shape[1] != 128 or query_valid.shape != queries.shape[:1]: | |
| raise ValueError('query shapes differ') | |
| if not 0 <= local_queries <= len(queries): | |
| raise ValueError('invalid local query count') | |
| rankings = [dict() for _ in range(len(queries))] | |
| for start in range(0, len(embeddings), chunk_size): | |
| chunk = functional.normalize(embeddings[start:start + chunk_size].to(queries.device), dim=-1) | |
| scores = (queries @ chunk.T / 0.1).cpu() | |
| for query in query_valid.nonzero().flatten().tolist(): | |
| current = rankings[query] | |
| for offset, score in enumerate(scores[query].tolist()): | |
| index = start + offset | |
| surface = str(surface_ids[index]) | |
| previous = current.get(surface) | |
| if previous is None or score > previous[0]: | |
| current[surface] = (score, index) | |
| rankings[query] = dict(sorted(current.items(), key=lambda item: (-item[1][0], item[0]))[:total_k]) | |
| ordered = [sorted(values.items(), key=lambda item: (-item[1][0], item[0])) for values in rankings] | |
| local = [query for query in range(local_queries) if query_valid[query]] | |
| fallback = [query for query in range(local_queries, len(queries)) if query_valid[query]] | |
| selected = {} | |
| def allocate(query_ids, budget): | |
| cursors = {query: 0 for query in query_ids} | |
| added = 0 | |
| while added < budget: | |
| progress = False | |
| for query in query_ids: | |
| while cursors[query] < len(ordered[query]): | |
| surface, (score, index) = ordered[query][cursors[query]] | |
| cursors[query] += 1 | |
| if surface in selected: | |
| continue | |
| selected[surface] = {'index': index, 'coarse': score} | |
| added += 1 | |
| progress = True | |
| break | |
| if added >= budget: | |
| break | |
| if not progress: | |
| break | |
| fallback_budget = math.ceil(total_k * fallback_fraction) if local else total_k | |
| allocate(local, total_k - fallback_budget) | |
| allocate(fallback, fallback_budget) | |
| allocate(local + fallback, total_k - len(selected)) | |
| for surface, result in selected.items(): | |
| support = {str(query): values[surface][0] for query, values in enumerate(rankings) if surface in values} | |
| best_query = max(support, key=support.get) | |
| result['index'] = rankings[int(best_query)][surface][1] | |
| result['coarse'] = support[best_query] | |
| result['support'] = support | |
| result['surface_id'] = surface | |
| return sorted(selected.values(), key=lambda item: (-item['coarse'], item['surface_id'])) | |
| def reader_scores(audio, mask, embeddings, attention_temperature, score_temperature, chunk_size=256): | |
| if attention_temperature <= 0 or score_temperature <= 0 or (not mask.any()): | |
| raise ValueError('invalid frozen reader configuration') | |
| clean = audio.masked_fill(~mask[:, None], 0) | |
| pieces = [] | |
| for start in range(0, len(embeddings), chunk_size): | |
| chunk = embeddings[start:start + chunk_size].to(audio.device) | |
| attention = (chunk @ clean.T / attention_temperature).masked_fill(~mask[None], -torch.inf).softmax(-1) | |
| pooled = functional.normalize(attention @ clean, dim=-1) | |
| pieces.append((pooled * chunk).sum(-1) / score_temperature) | |
| return torch.cat(pieces) if pieces else audio.new_empty(0) | |
| def select_names(scores, none_score, surface_ids, top_k=10, margin=0.0): | |
| if len(surface_ids) != scores.numel() or top_k < 1: | |
| raise ValueError('invalid selection input') | |
| chosen = [] | |
| seen = set() | |
| for index in torch.argsort(scores, descending=True, stable=True).tolist(): | |
| surface = str(surface_ids[index]) | |
| if scores[index] <= none_score + margin or surface in seen: | |
| continue | |
| seen.add(surface) | |
| chosen.append(index) | |
| if len(chosen) == top_k: | |
| break | |
| return chosen | |