File size: 8,415 Bytes
bd05e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Binding via Convolution Circulaire

Le binding est une opération fondamentale pour composer et décomposer des
représentations distribuées. On utilise la convolution circulaire qui :
- Est commutative dans le domaine fréquentiel (FFT)
- Permet binding/unbinding par multiplication/déconvolution
- Supporte la composition de structures

Optimisé avec numpy FFT et support pour binding chaîné.
"""

import numpy as np
from numba import njit, prange, complex128, float64
from typing import List, Tuple, Optional, Dict
import logging

logger = logging.getLogger(__name__)

VECTOR_SIZE = 4096


class CircularBinder:
    """
    Binder circulaire pour composition/décomposition de vecteurs.
    
    Le binding utilise la convolution circulaire qui peut être calculée
    efficacement via FFT : 
    a ⊗ b = IFFT(FFT(a) * FFT(b))
    
    Avantages :
    - Commutatif : a ⊗ b = b ⊗ a
    - Associatif pour composition chaînée
    - Unbinding par déconvolution (division dans le domaine fréquentiel)
    """
    
    def __init__(self, noise_tolerance: float = 0.1):
        self.noise_tolerance = noise_tolerance
        
        # Cache FFT pour binding chaîné
        self.fft_cache: Dict[int, np.ndarray] = {}
        self.cache_limit = 1000
        
        # Stats
        self.bind_count = 0
        self.unbind_count = 0
    
    def _normalize(self, vec: np.ndarray) -> np.ndarray:
        """
        Normalise un vecteur pour le binding.
        Convertit binaire en {-1, 1} pour la convolution.
        """
        # Binaire (0, 1) -> (-1, 1)
        return 2.0 * vec.astype(np.float64) - 1.0
    
    def _denormalize(self, vec: np.ndarray, threshold: float = 0.0) -> np.ndarray:
        """Reconvertit en binaire."""
        return (vec > threshold).astype(np.uint8)
    
    def bind(self, a: np.ndarray, b: np.ndarray, use_fft: bool = True) -> np.ndarray:
        """
        Binding de deux vecteurs : a ⊗ b.
        
        Args:
            a, b: vecteurs binaires (4096,) uint8
            use_fft: utilise FFT pour efficacité
        
        Returns:
            vecteur binaire résultant
        """
        if use_fft:
            # FFT-based convolution
            a_norm = self._normalize(a)
            b_norm = self._normalize(b)
            
            fft_a = np.fft.fft(a_norm)
            fft_b = np.fft.fft(b_norm)
            
            fft_result = fft_a * fft_b
            result = np.fft.ifft(fft_result).real
            
            # Normalise et seuille
            result = result / (np.std(result) + 1e-8)
            return self._denormalize(result)
        else:
            # Convolution directe (plus lente mais pas de distorsion FFT)
            a_norm = self._normalize(a).astype(np.float64)
            b_norm = self._normalize(b).astype(np.float64)
            result = np.convolve(a_norm, b_norm, mode='same')
            result = result / (np.std(result) + 1e-8)
            return self._denormalize(result)
    
    def bind_multiple(self, vectors: List[np.ndarray], use_cache: bool = True) -> np.ndarray:
        """
        Binding de plusieurs vecteurs : a ⊗ b ⊗ c ⊗ ...
        Utilise le cache FFT pour éviter de recalculer les FFT.
        """
        if len(vectors) == 0:
            return np.ones(VECTOR_SIZE, dtype=np.uint8)  # Identité du binding
        
        if len(vectors) == 1:
            return vectors[0].copy()
        
        # Cache les FFT si demandé
        if use_cache:
            ids = [id(v) for v in vectors]
            fft_vec = np.ones(VECTOR_SIZE, dtype=np.complex128)
            
            for vid, v in zip(ids, vectors):
                if vid in self.fft_cache:
                    fft_v = self.fft_cache[vid]
                else:
                    v_norm = self._normalize(v)
                    fft_v = np.fft.fft(v_norm)
                    if len(self.fft_cache) < self.cache_limit:
                        self.fft_cache[vid] = fft_v
                fft_vec *= fft_v
            
            result = np.fft.ifft(fft_vec).real
            result = result / (np.std(result) + 1e-8)
            return self._denormalize(result)
        else:
            # Bind séquentiel
            result = vectors[0].copy()
            for v in vectors[1:]:
                result = self.bind(result, v)
            return result
    
    def unbind(self, bound: np.ndarray, a: np.ndarray, use_fft: bool = True) -> np.ndarray:
        """
        Déconvolution : résout bound = a ⊗ b pour trouver b.
        
        Dans le domaine fréquentiel : b = IFFT(FFT(bound) / FFT(a))
        """
        if use_fft:
            bound_norm = self._normalize(bound)
            a_norm = self._normalize(a)
            
            fft_bound = np.fft.fft(bound_norm)
            fft_a = np.fft.fft(a_norm)
            
            # Évite division par zéro avec tolérance
            fft_a = np.where(np.abs(fft_a) < 1e-8, 1e-8, fft_a)
            fft_result = fft_bound / fft_a
            
            result = np.fft.ifft(fft_result).real
            result = result / (np.std(result) + 1e-8)
            return self._denormalize(result)
        else:
            # Déconvolution dans le domaine temporel (plus stable)
            bound_norm = self._normalize(bound).astype(np.float64)
            a_norm = self._normalize(a).astype(np.float64)
            
            # Corrélation comme approximation de déconvolution
            result = np.correlate(bound_norm, a_norm, mode='same')
            result = result / (np.std(result) + 1e-8)
            return self._denormalize(result)
    
    def bind_role_filler(self, role: np.ndarray, filler: np.ndarray) -> np.ndarray:
        """
        Binding spécialisé role-filler (structure propositionnelle).
        Utile pour représenter "sujet-agent", "objet-patient", etc.
        """
        # Shift circulaire du filler pour éviter collision avec le role
        shifted_filler = np.roll(filler, VECTOR_SIZE // 4)
        return self.bind(role, shifted_filler)
    
    def unbind_role_filler(self, bound: np.ndarray, role: np.ndarray) -> np.ndarray:
        """Extrait le filler d'un binding role-filler."""
        unshifted = self.unbind(bound, role)
        return np.roll(unshifted, -VECTOR_SIZE // 4)
    
    def extract_similar(
        self,
        bound: np.ndarray,
        candidates: List[np.ndarray],
        top_k: int = 3
    ) -> List[Tuple[np.ndarray, float]]:
        """
        Extrait les candidats les plus similaires à partir d'un vecteur bound.
        Utile pour "décoder" une structure composée.
        """
        results = []
        for cand in candidates:
            # Déconvolution
            decoded = self.unbind(bound, cand)
            # Similarité avec chaque candidat
            best_sim = 0.0
            for other in candidates:
                sim = np.mean(decoded == other)
                if sim > best_sim:
                    best_sim = sim
            results.append((cand, best_sim))
        
        results.sort(key=lambda x: -x[1])
        return results[:top_k]
    
    def compose_structure(
        self,
        role_filler_pairs: List[Tuple[np.ndarray, np.ndarray]]
    ) -> np.ndarray:
        """
        Compose une structure complète à partir de paires role-filler.
        Ex: [(agent, john), (action, run), (patient, ball)]
        """
        bound_pairs = []
        for role, filler in role_filler_pairs:
            bound_pairs.append(self.bind_role_filler(role, filler))
        
        # Somme des bindings (superposition)
        result = np.zeros(VECTOR_SIZE, dtype=np.float64)
        for bp in bound_pairs:
            result += self._normalize(bp)
        
        result = result / (np.std(result) + 1e-8)
        return self._denormalize(result)
    
    def decompose_structure(
        self,
        composite: np.ndarray,
        roles: List[np.ndarray]
    ) -> List[np.ndarray]:
        """
        Décompose une structure en ses fillers.
        """
        fillers = []
        for role in roles:
            filler = self.unbind_role_filler(composite, role)
            fillers.append(filler)
        return fillers
    
    def get_stats(self) -> Dict:
        return {
            'bind_count': self.bind_count,
            'unbind_count': self.unbind_count,
            'fft_cache_size': len(self.fft_cache),
        }