From ac5d8d23b0d22133a247a87b8eeee0a6b7b24932 Mon Sep 17 00:00:00 2001 From: kasinadhsarma Date: Thu, 26 Dec 2024 19:48:38 +0530 Subject: [PATCH] Integrate LongTermMemory into ConsciousnessModel; add tests for long-term memory retrieval and integration; update existing tests to validate retrieved_memory metrics --- .../__pycache__/consciousness.cpython-310.pyc | Bin 6311 -> 7537 bytes .../consciousness_model.cpython-310.pyc | Bin 20111 -> 20448 bytes .../consciousness_state.cpython-310.pyc | Bin 4737 -> 5133 bytes .../long_term_memory.cpython-310.pyc | Bin 0 -> 2888 bytes models/consciousness.py | 100 ++++++++++++++-- models/consciousness_model.py | 31 ++++- models/consciousness_state.py | 13 ++ models/long_term_memory.py | 111 ++++++++++++++++++ ...consciousness.cpython-310-pytest-8.3.4.pyc | Bin 32308 -> 33425 bytes ...mic_attention.cpython-310-pytest-8.3.4.pyc | Bin 13775 -> 13847 bytes ...g_term_memory.cpython-310-pytest-8.3.4.pyc | Bin 0 -> 6277 bytes ...elf_awareness.cpython-310-pytest-8.3.4.pyc | Bin 14951 -> 15030 bytes tests/test_consciousness.py | 19 +++ tests/test_dynamic_attention.py | 25 ++-- tests/test_long_term_memory.py | 50 ++++++++ tests/test_self_awareness.py | 16 ++- 16 files changed, 339 insertions(+), 26 deletions(-) create mode 100644 models/__pycache__/long_term_memory.cpython-310.pyc create mode 100644 models/long_term_memory.py create mode 100644 tests/__pycache__/test_long_term_memory.cpython-310-pytest-8.3.4.pyc create mode 100644 tests/test_long_term_memory.py diff --git a/models/__pycache__/consciousness.cpython-310.pyc b/models/__pycache__/consciousness.cpython-310.pyc index c5172ca6d2413e150faa06ed239f41cf9c81aff8..7f683715c811a57ddfd2ca69497e16edd2394952 100644 GIT binary patch delta 3676 zcmahMTZ|i5b?(e~JmbgM9zS;1`?63Uag!!(ND58TCLs!irdbMvhJ*-r;ydg0uI+K| zjI)U~#)$0)LJcIkfP@N?7YU&vK9I^6Pzex%FFp~{e3TCmqT&ZW_yO8*&K-O0t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zBA5qO2Zj1uO6iU7XkxRfII>0yMYao6J?_UOIj2$*1tb8rj7t$JLF>}T^s!xQ+1 z@@>pODF)zg!XCa}^ljQQq31$BpJXdTwLN^lw0^r0Yd3WQNQ&hC^(>%MZ7?=SoZgyJ^I yClB^mbiZM09z+3je&j%~D?NuL($7O2{;T5sQxmngwqIBjheQ%tg{6iox%w{;FBIJX diff --git a/models/__pycache__/consciousness_model.cpython-310.pyc b/models/__pycache__/consciousness_model.cpython-310.pyc index a42580dd00b00942d52283688a88d6e4556dd8c7..ba115034d4884d8240a09a2c03b5fc07b810b691 100644 GIT binary patch delta 4912 zcmb7IdvH|M8NYXTv%A^dB)dyqa;~0}rQF?=Byg1zn-|K($MXUkThRDFM~FyK>aIGeVpkC6_q4lbjH7@{}NPf|Hzl z;)Ikial(_Fu;RE)iq0Bt@O8$ubR?mtjfAH2CGscXsBGnK*3-S)RF>*gQ#u=(sCH~| z$h8y0u2RQ{xjZA&Zke~F+@T> z$pCHn(}Ss4N>>JxsxhTGZR8q8A?9Q1w!Bj=D_c!19S)~M#&6(@;Zh#TnZ=*+zY(k_4b}!&1enE-2O8$D1G%2yl+3n(-Zl-} zK{XRRIB_{p~u!x;m zzUHK6mAhgpBWtQL_;mVtD28p9tE9G4viY z72hAtvd?rkBrW{O@B+VB>)RM_=jX$f@|lVMhP!0rnlP_&9+ze zr1e-Yiz}=hax7>$EU(au_m$%Q$a|=r4*CuKT zavgkaQE>%(97awO^wNiZ&aLiUB7R<4&B8og)au1Va@{C@rl_P~#%tNly=q$BKfqcp z^M4e*Hut-v#s&eK0RwY}Wu@hhL0(W7S*NE>b-x+8&5UwAX92)s`5j%Y&TUAp{BSJ{w#}F0OMOsrjIPoNznT$1(KF(h#n=QmuI&{K46%`J*%pa=I z>W>j8;U<{+Sw)1OJ_s*kVL*~hX-pcI4!DnG4SzIIU0LWK$&~J{QoKsp1M-2!OygML zxTJXd9Y?a*<1{K#oa4kdou(@zk9o(XQF&Az%Mx5a$ptm3|3b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I_WYeWu*8WSky1bEPXRS(|~m zeSmJvS}5HS9_X%jiGsSq`??R_79kzl?>=Y=AAEvI`!W1GMD$w;3~L6c91_tBT>O84 z^?OKceR2PA-57WF)J}B~74onz4#EHB_-xw2Fa=FV;%C{4r7|xAFI3K|v1J%Dv*<#J z#;a3~6szLT_taH4LnIzh?Iag(P^)?~F?q&Io2H@Jbad1?Dt kIH#G_WI166S-0tx{F&b3fZZ+m?Y61;fBr82y26qF0V5nw8vpg9?;s8noOXmIf6a*;sR%qJ8ZzHiU%RVqMs7&|{Y41$`N3{JsR{Ug9qOz)I4vDw&X0&-Xf(WZaz{0gaM% wxGf#udk33HeneLaY8ex(eewvTV(L@fXy9nNYgCOqR5YaHC9Gis8E~x#_Ml6ZvEqBPMD`urdZSXtHmd&?LyD$vt_Zu=C`b!b$=> SKy@4pJd7fY9E?JYV(|dvq73u^ diff --git a/models/__pycache__/long_term_memory.cpython-310.pyc b/models/__pycache__/long_term_memory.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7abf7be46148c4c2518dc1b3acd8acd83b403538 GIT binary patch literal 2888 zcmZ8jTW=#t6|SnjxZQC)P9~bo&Y+>iMT^- zNIr1n7KFOe!|*X$${PhAvJsboY(L)|_%c+jNiY&}Lw26KgSOn1-RBGn+>t%JH$G%8 z8+4$|w&@<0#p#LC`D2xr`f`3m^j?jPD7QP&u*|1rp^7>hl{(6kw5T!C;xtMM8EG|4 zvWzG-O{-F-!zeAlm?vc0{>%S3jQ#s>f2HRiKQ!&Mn9k~0rnw2nsg$a)>y749ah1-M z=}28pOK|C=RzvTGLulQ?fJjzNFxBiCyW-af!GbSXEEWQ@vv4rGwKwsx3x6$KDWvnl z!)|NAt^(=S?W+)6;u=2$j!rge@2Z3Q*a5W(s*5#Ay(1YsBPmF5uH?-{R}O_er59K*479BZ;t z2Q(1|j3^4@9v+`OHsMNu#?BqBPSXmZil59zBi#2ObvQQOTp&}ttSSi?}aQrYO1E}HV@6K>jZJj{}+isMh%r@uNF zm$^DPORBU;7~Al z&yelaLrfv}?ay@tU;l#t?zDa0b$ZVHjn^@}zjmYX8+k*D-1?!+_A%bYsQ!s*!LQgg zGW<;FF4kPyuusI9s2=9yutAH&Rl(R zlip=Z+?Oo?%h_cgh&S1le@&bK;slBNfp~|YYfp)Kb&flZJ+)w#tz**j#@+Q-EDzF| zbod?<06y{dgZbVu(4f{yoEA|%RuOsnwdAfEAnxl8c;Y3&alZAq$@_6qMb$W&s%XCZ zD59thYbB%KHFze#KZ-u(f3O%n>IcT5tafSNA{U^h%lbZKHYoO`s*~YZqu#QZeVjORaNvPsep^J zh~A$meYvl{3V&KuG@$wn54Af!)o;-WC10Q0^?Y}QHQh*0vPfxN>hDMWEz4lqv!a4n zg}&gPr|P09ieDo+p99MR%J83>Hr1^pBLwb(1L>dC`7ATMHm#~0)p_#Nw14t+sBGUg z-c+NP8E=TJQ2o#X(syAO}NDXwPhQG@EPl8#1iYJk1iF)|aN8)HSM} zEv+lVlr&u3Ml|Yo8n<^3d590q=G#xdH|ETeTNj^Cdp+3KVf$Fdw^+p#c0(MoFDw^ryo zQAJg1y+>lLbw9_-Y_1yTP-Pi9y)AHdiinR&onE)7ssk!uE{{a-&ERhDkv-{eH1Y%6 z?e~j9?|{wRU6cExSz#MH{#b^u*7{q-<=2-}{8`h2zT?=P$JRP4`i3@ECDO(O?=<%R SfVx9P#WG+WzRi35p7=jAk^gi6 literal 0 HcmV?d00001 diff --git a/models/consciousness.py b/models/consciousness.py index 7be2ca1..5cfb30f 100644 --- a/models/consciousness.py +++ b/models/consciousness.py @@ -5,6 +5,7 @@ from .information_integration import InformationIntegration from .self_awareness import SelfAwareness # Add this import from .dynamic_attention import DynamicAttention +from .long_term_memory import LongTermMemory class MultiHeadAttention(nn.Module): """Custom MultiHeadAttention implementation""" @@ -71,6 +72,15 @@ def forward(self, inputs: torch.Tensor, memory_state: Optional[torch.Tensor] = N output = self.output_layer(integrated) output = self.layer_norm(output) + # Add assertion to ensure output has correct hidden_dim + assert output.shape[-1] == self.hidden_dim, ( + f"GlobalWorkspace output has hidden_dim {output.shape[-1]}, expected {self.hidden_dim}" + ) + + # Add logging for debugging + print(f"GlobalWorkspace output shape: {output.shape}") + print(f"memory_state shape: {memory_state.shape}") + return output, memory_state class ConsciousnessModel(nn.Module): @@ -121,6 +131,14 @@ def __init__(self, hidden_dim: int, num_heads: int, num_layers: int, num_states: dropout_rate=dropout_rate ) + # Long-term memory + self.long_term_memory = LongTermMemory( + input_dim=self.input_dim, + hidden_dim=hidden_dim, + memory_size=1000, + dropout_rate=dropout_rate + ) + # State tracking self.previous_state = None @@ -170,6 +188,8 @@ def update_goals(self, current_state: torch.Tensor): def forward(self, inputs: Dict[str, torch.Tensor], state: Optional[torch.Tensor] = None, deterministic: bool = True) -> Tuple[torch.Tensor, Dict]: + # Initialize metrics dictionary at the start + metrics = {} # Get device from inputs device = next(iter(inputs.values())).device @@ -199,12 +219,25 @@ def forward(self, inputs: Dict[str, torch.Tensor], # Process through global workspace with reshaped state conscious_out, memory_state = self.global_workspace(attn_out, state, deterministic) + # Add assertion to ensure conscious_out has correct hidden_dim + assert conscious_out.shape[-1] == self.hidden_dim, ( + f"conscious_out has hidden_dim {conscious_out.shape[-1]}, expected {self.hidden_dim}" + ) + + # Add logging to verify conscious_out dimensions + print(f"conscious_out shape: {conscious_out.shape}") + # Process through self-awareness aware_state, awareness_metrics = self.self_awareness( conscious_out, previous_state=self.previous_state ) + # Add assertion to ensure aware_state has correct hidden_dim + assert aware_state.shape[-1] == self.hidden_dim, ( + f"aware_state has hidden_dim {aware_state.shape[-1]}, expected {self.hidden_dim}" + ) + # Update previous state self.previous_state = aware_state.detach() @@ -213,17 +246,62 @@ def forward(self, inputs: Dict[str, torch.Tensor], # Update goals based on conscious output self.update_goals(conscious_out) - - # Update metrics - metrics = { - 'attention_weights': attention_metrics, - 'memory_state': memory_state, - 'phi': phi, - 'attention_maps': attention_metrics, - 'goal_state': self.goal_state, - 'context_state': self.context_state, - **awareness_metrics - } + + # Store memory with correct dimensions + memory_to_store = conscious_out.detach() # Remove mean reduction + + # Use long_term_memory instead of memory + try: + # Ensure memory_to_store has correct shape [batch_size, hidden_dim] + memory_to_store = conscious_out.mean(dim=1) if len(conscious_out.shape) == 3 else conscious_out + + # Store memory + self.long_term_memory.store_memory(memory_to_store) + + # Retrieve memory using current state as query + retrieved_memory = self.long_term_memory.retrieve_memory(memory_to_store) + + # Ensure retrieved memory has correct shape + if retrieved_memory.shape != (memory_to_store.shape[0], self.hidden_dim): + retrieved_memory = retrieved_memory.view(memory_to_store.shape[0], self.hidden_dim) + + metrics['retrieved_memory'] = retrieved_memory + + except Exception as e: + print(f"Memory operation error: {e}") + # Create zero tensor with correct shape + metrics['retrieved_memory'] = torch.zeros( + inputs['attention'].shape[0], + self.hidden_dim, + device=inputs['attention'].device + ) + + # Average over sequence length to get [batch_size, hidden_dim] + query = conscious_out.mean(dim=1) if len(conscious_out.shape) > 2 else conscious_out + print(f"query shape: {query.shape}") + + # Ensure query has correct shape before memory retrieval + if query.dim() == 1: + query = query.unsqueeze(0) + + # Retrieve memory and ensure it's in metrics + try: + retrieved_memory = self.long_term_memory.retrieve_memory(query) + print(f"retrieved_memory shape: {retrieved_memory.shape}") + metrics['retrieved_memory'] = retrieved_memory + except Exception as e: + print(f"Memory retrieval error: {e}") + metrics['retrieved_memory'] = torch.zeros( + query.size(0), + self.hidden_dim, + device=query.device + ) + + # Update remaining metrics + metrics.update(attention_metrics) + metrics['goal_state'] = self.goal_state + metrics['context_state'] = self.context_state + metrics['phi'] = phi return aware_state, metrics diff --git a/models/consciousness_model.py b/models/consciousness_model.py index 45376b5..ed498c8 100644 --- a/models/consciousness_model.py +++ b/models/consciousness_model.py @@ -12,6 +12,7 @@ from .memory import WorkingMemory, InformationIntegration from .consciousness_state import CognitiveProcessIntegration, ConsciousnessStateManager from .error_handling import ErrorHandler, ErrorCorrection, validate_state +from .long_term_memory import LongTermMemory # Add import torch.set_default_dtype(torch.float32) torch.set_default_device('cpu') # or 'cuda' if using GPU @@ -131,6 +132,13 @@ def __init__(self, hidden_dim: int, num_heads: int, num_layers: int, num_states: self.target_cognition_percentage = 90.0 # Target cognition percentage + # Add long-term memory component + self.long_term_memory = LongTermMemory( + input_dim=hidden_dim, + hidden_dim=hidden_dim, + dropout_rate=dropout_rate + ) + def add_meta_learning_layer(self): """Add meta-learning capabilities""" self.meta_learner = nn.ModuleDict({ @@ -669,6 +677,27 @@ def forward(self, inputs, state=None, initial_state=None, deterministic=True, co metrics['cognition_progress'] = cognition_progress self.logger.debug(f"Cognition Progress: {cognition_progress}%") + # Add long-term memory retrieval with proper shape handling + query = consciousness_state.mean(dim=1) # [batch_size, hidden_dim] + if query.dim() == 1: + query = query.unsqueeze(0) # Add batch dimension if missing + + # Ensure query has correct batch size + batch_size = next(iter(inputs.values())).size(0) + if query.size(0) == 1 and batch_size > 1: + query = query.expand(batch_size, -1) + + # Retrieve and store memory + retrieved_memory = self.long_term_memory.retrieve_memory(query) # Should return [batch_size, hidden_dim] + self.long_term_memory.store_memory(new_state.detach()) + + # Double check retrieved memory shape + if retrieved_memory.size(0) != batch_size: + retrieved_memory = retrieved_memory.expand(batch_size, -1) + + # Include retrieved_memory in metrics + metrics['retrieved_memory'] = retrieved_memory + end_time = time.time() # End profiling self.logger.debug(f"forward pass took {end_time - start_time:.6f} seconds") @@ -699,7 +728,7 @@ def get_config(self) -> Dict[str, Any]: } @classmethod - def create_default_config(cls) -> Dict[str, Any]: + def create_default_config(cls) -> Dict[str, Any]: # Fixed syntax error here """Create default model configuration.""" return { 'hidden_dim': 512, diff --git a/models/consciousness_state.py b/models/consciousness_state.py index 7913ddd..6a7eaba 100644 --- a/models/consciousness_state.py +++ b/models/consciousness_state.py @@ -187,3 +187,16 @@ def get_rl_loss(self, state_value, reward, next_state_value, gamma=0.99): # Value loss (MSE) value_loss = torch.mean(td_error ** 2) return value_loss, td_error + +def test_long_term_memory_integration(self, model, sample_input): + # ...existing code... + output, metrics = model(sample_input) + + # ...existing code... + + # Add assertion to check if 'retrieved_memory' is in metrics and has correct shape + assert 'retrieved_memory' in metrics, "retrieved_memory not found in metrics" + assert metrics['retrieved_memory'].shape == (sample_input['query'].size(0), 128), ( + f"retrieved_memory has shape {metrics['retrieved_memory'].shape}, expected ({sample_input['query'].size(0)}, 128)" + ) + # ...existing code... diff --git a/models/long_term_memory.py b/models/long_term_memory.py new file mode 100644 index 0000000..519a0d6 --- /dev/null +++ b/models/long_term_memory.py @@ -0,0 +1,111 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from typing import Optional, Tuple + +class LongTermMemory(nn.Module): + """Long-term memory component for maintaining and recalling episodic information.""" + + def __init__(self, input_dim: int, hidden_dim: int, memory_size: int = 1000, dropout_rate: float = 0.1): + super().__init__() + self.input_dim = input_dim + self.hidden_dim = hidden_dim + self.memory_size = memory_size + + # Memory cells + self.memory_rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=2, + dropout=dropout_rate, + batch_first=True + ) + + # Initialize memory storage with correct device and shape + self.register_buffer('memory_storage', torch.zeros(memory_size, hidden_dim)) + self.memory_index = 0 + + # Output projection + self.output_projection = nn.Linear(hidden_dim, hidden_dim) + + # Layer normalization + self.layer_norm = nn.LayerNorm(hidden_dim) + + def store_memory(self, memory: torch.Tensor): + """Store memory in the long-term memory storage.""" + # Add assertion to ensure memory has shape [batch_size, hidden_dim] + assert memory.dim() == 2 and memory.size(1) == self.hidden_dim, ( + f"Memory has shape {memory.shape}, expected [batch_size, {self.hidden_dim}]" + ) + + batch_size = memory.size(0) + + # Store exact memory values without normalization + if self.memory_index + batch_size > self.memory_size: + overflow = (self.memory_index + batch_size) - self.memory_size + self.memory_storage[self.memory_index:] = memory[:batch_size - overflow].detach() + self.memory_storage[:overflow] = memory[batch_size - overflow:].detach() + self.memory_index = overflow + else: + self.memory_storage[self.memory_index:self.memory_index + batch_size] = memory.detach() + self.memory_index += batch_size + + def retrieve_memory(self, query): + """Retrieve relevant memories based on query.""" + try: + # Ensure query has correct shape [batch_size, hidden_dim] + if query.dim() == 1: + query = query.unsqueeze(0) + elif query.dim() > 2: + query = query.view(-1, self.hidden_dim) + + batch_size = query.size(0) + + # Handle empty memory case + if self.memory_index == 0: + return query # Return query itself if no memories stored + + # Get valid memories + if self.memory_index < self.memory_size: + valid_memories = self.memory_storage[:self.memory_index] + else: + valid_memories = self.memory_storage + + # Ensure we have at least one memory + if valid_memories.size(0) == 0: + return query + + # Normalize for similarity computation only + query_norm = F.normalize(query, p=2, dim=1) + memories_norm = F.normalize(valid_memories, p=2, dim=1) + + # Compute cosine similarity + similarity = torch.matmul(query_norm, memories_norm.t()) + + # Get attention weights through softmax + attention = F.softmax(similarity / 0.1, dim=1) # Temperature scaling + + # Use original memories for weighted sum + retrieved = torch.matmul(attention, valid_memories) + + return retrieved + + except Exception as e: + print(f"Memory retrieval error: {e}") + return query.clone() # Return query itself in case of error + + def forward(self, x): + # Run LSTM + output, (h_n, c_n) = self.memory_rnn(x) + + # Project output + output = self.output_projection(output) + output = self.layer_norm(output) + + # Retrieve memory ensuring batch size consistency + retrieved_memory = self.retrieve_memory(x) + # Ensure retrieved_memory has the same batch size as input + retrieved_memory = retrieved_memory.view(x.size(0), -1) + metrics = {'retrieved_memory': retrieved_memory} + + return output, (h_n, c_n) diff --git a/tests/__pycache__/test_consciousness.cpython-310-pytest-8.3.4.pyc b/tests/__pycache__/test_consciousness.cpython-310-pytest-8.3.4.pyc index 6374fb18613eb3773626afabf38fe17fa56a27c9..1294fc21f971089959a34479a011902747a50329 100644 GIT binary patch delta 1025 zcmaJ;-Afcv6u_-tmKe+8oz+!$S7s(ZwmU7T z(L+SUeN+5LZ^m8PmiLQGnGqlsn?~b&7cuT%5cT7nl>)^z}MnG|&=y(47>y&U%Ft&`!lIjHV3-gA!H{YpGePW4NX!6Y??PhW z0WmG~%{mUPAl9@gg_H!*X3=^B(IbbO!#ybofNj~*AfpvVX=9PXu$aXVO?N=3 zKA~<3)eo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