
    xi^                        d Z ddlZddlmZ ddlmc mZ ddlmZ ddl	m
Z
 ddlZddlmZ ddlZddlZddlmZmZ  G d d      Zd#dej*                  ded	ed
ej*                  fdZd$dej*                  ded
ej*                  fdZ G d dej4                        Z G d dej4                        Z G d dej4                        Z G d dej4                        Z G d dej4                        Z G d dej@                        Z!	 	 d%dej4                  de
de
dejD                  dede#d
ee$e$e$e$f   fd Z%d! Z&e'd"k(  r e&       \  Z(Z)Z*yy)&u!  
Gravity & Lorentz Mechanics for Machine Learning
==================================================
Physics-inspired neural network using:
- Gravitational attention (1/r² attraction)
- Lorentz force updates (v × B cross-product)
- GM = v²r manifold constraint (singularity prevention)
    N)
DataLoader)TupleOptionalc                   $    e Zd ZdZdZdZdZdZdZy)PhysicsConfigz.Configuration for physics-inspired components.      ?{Gz?ư>TN)	__name__
__module____qualname____doc__Gqepsmanifold_radiususe_riemannian     #/home/per/Documents/gravity/ex01.pyr   r      s    8AA
CONr   r   xdimr   returnc                 D    t        j                  | d|d      }| ||z   z  S )z,Safe normalization with numerical stability.   Tpr   keepdim)torchnorm)r   r   r   r    s       r   safe_normalizer!   #   s%    ::a1#t4Ds
r   radiusc                 f    t        j                  | ddd      }| |t        j                  z   z  |z  S )uz   
    Project embeddings onto a Riemannian manifold.
    GM = v²r → keep ||embedding||² × ||gradient|| = constant
    r   Tr   )r   r    r   r   )r   r"   r    s      r   project_to_manifoldr%   )   s3    
 ::a1"d3D)))*f44r   c                        e Zd ZdZd
dededef fdZddej                  de	ej                     dej                  fd	Z
 xZS )GravitationalAttentionu   
    Attention mechanism based on gravitational attraction: F = GMm/r²
    
    Instead of softmax similarity, we use 1/r² attraction between query-key pairs.
    This creates a physically-motivated attention with inherent inductive biases.
    	embed_dim	num_headsr   c                     t         |           || _        || _        ||z  | _        ||z  dk(  sJ d       t        j                  t        j                  |      d      | _	        t        j                  ||      | _        t        j                  ||      | _        t        j                  ||      | _        t        j                  ||      | _        t        j                  t        j                  d            | _        y )Nr   z(embed_dim must be divisible by num_headsTrequires_grad   )super__init__r(   r)   head_dimnn	Parameterr   tensorr   Linearq_projk_projv_projout_projzerosbias)selfr(   r)   r   	__class__s       r   r/   zGravitationalAttention.__init__?   s    ""!Y.9$)U+UU) ell1oTB ii	95ii	95ii	95		)Y7 LLQ0	r   r   contextr   c                    ||}|j                   \  }}}|j                   d   }| j                  |      j                  ||| j                  | j                        j                  dd      }| j                  |      j                  ||| j                  | j                        j                  dd      }| j                  |      j                  ||| j                  | j                        j                  dd      }	|j                  d      }
|j                  d      }t        j                  |
|z
  dd      }| j                  |dz  t        j                  z   | j                  z   z  }||j                  dd      t        j                  z   z  }t        j                   ||	      }|j                  dd      j                  ||| j"                        }| j%                  |      S )z
        Args:
            x: [batch, seq_len, embed_dim] - query/source
            context: [batch, ctx_len, embed_dim] - key/value context (if None, use x)
        r-   r      r$   )r   r   T)r   r   )shaper5   reshaper)   r0   	transposer6   r7   	unsqueezer   r    r   r   r   r:   summatmulr(   r8   )r;   r   r=   batchseq_len_ctx_lenQKV
Q_expanded
K_expanded	distancesgravity_scoresattention_weightsattendeds                   r   forwardzGravitationalAttention.forwardR   s    ?GGGw--" KKN""5'4>>4==Q[[\]_`aKK ((WaabcefgKK ((Waabcefg [[^
[[^
JJzJ6!D	 9>M4E4E#E		#QR +n.@.@RQU.@.VYfYjYj.jk << 115 %%a+33E7DNNS}}X&&r   )   r   N)r   r   r   r   intfloatr/   r   Tensorr   rS   __classcell__r<   s   @r   r'   r'   7   sP    1# 1# 1e 1&%' %'0F %'RWR^R^ %'r   r'   c            	            e Zd ZdZddedef fdZdej                  dej                  fdZ		 ddej                  dej                  d	e
ej                     dej                  fd
Z xZS )LorentzGradientModifieru   
    Implements Lorentz force: F = q(E + v × B)
    
    The cross-product v × B creates updates perpendicular to both
    velocity (gradient direction) and B-field (hidden state).
    This geometric structure stabilizes training dynamics.
    r(   r   c                    t         |           || _        t        j                  t        j                  |      d      | _        t        j                  t        j                  ||      t        j                         t        j                  ||            | _        t        j                  t        j                  d      dz        | _        y )NTr+   r-   皙?)r.   r/   r(   r1   r2   r   r3   r   
Sequentialr4   Tanhb_field_generatoronesrotation_speed)r;   r(   r   r<   s      r   r/   z LorentzGradientModifier.__init__   s    "ell1oTB "$IIi+GGIIIi+"
 !ll5::a=3+>?r   hidden_stater   c                 $    | j                  |      S )z
        Generate B-field from hidden state.
        In physics, changing magnetic fields induce electric fields.
        Here, the B-field is derived from hidden representations.
        )ra   )r;   rd   s     r   create_b_fieldz&LorentzGradientModifier.create_b_field   s     %%l33r   	gradientsvelocityc                    |5t        j                  |j                  d      d      j                  |      }| j	                  |      }|j                  dd| j                  dz        }|j                  dd| j                  dz        }t        j                  ||d      j                  |      }|| j                  |z  z   }|S )a  
        Apply Lorentz force-inspired update.
        
        Args:
            gradients: Standard gradients from backprop (E-field)
            hidden_state: Current hidden representations
            velocity: Movement direction (if None, use gradient direction)
        r-   r$   r   r?   )
F	normalizeflatten
reshape_asrf   rA   r(   r   crossr   )	r;   rg   rd   rh   Bvb
cross_termlorentz_forces	            r   apply_lorentz_updatez,LorentzGradientModifier.apply_lorentz_update   s     {{9#4#4Q#7R@KKIVH -
 RDNNa$78IIb!T^^q01[[A1-88C
 "DFFZ$77r   )r	   rU   )r   r   r   r   rV   rW   r/   r   rX   rf   r   ru   rY   rZ   s   @r   r\   r\   ~   s~    @# @% @45<< 4ELL 4 ,0	<< ll 5<<(	
 
r   r\   c                        e Zd ZdZddedef fdZddej                  dedej                  fdZd	ej                  dej                  fd
Z	 xZ
S )DifferentialRotationz
    Inspired by Sun's differential rotation generating magnetic fields.
    Different attention heads have different "rotation speeds".
    r(   r)   c                     t         |           || _        || _        ||z  | _        t        j                  t        j                  |      dz        | _	        t        j                  ||      | _        y )Nr^   )r.   r/   r(   r)   r0   r1   r2   r   randnhead_speedsr4   rotation_proj)r;   r(   r)   r<   s      r   r/   zDifferentialRotation.__init__   s^    ""!Y. <<I(>(DE  YYy)<r   angler   r   c           	      d   t        j                  |      }t        j                  |      }t        j                  |      }t        j                  |      }t        j
                  t        j
                  || |g      t        j
                  |||g      t        j
                  |||g      g      }|S )z'Create rotation matrix for given angle.)r   cossin
zeros_like	ones_likestack)r;   r|   r   cos_asin_azeroonerotations           r   create_rotation_matrixz+DifferentialRotation.create_rotation_matrix   s    		% 		% &ooe$;;KK-.KKt,-KKtS)* 
 
 r   r   c                    |j                   \  }}}|j                  ||| j                  | j                        }t	        j
                  |      }t        | j                        D ]  }| j                  |   }| j                  |      }	|dddd|ddf   }
t	        j                  d|	|
      }|dddd|| j                  z  |dz   | j                  z  f   ddddddfxx   |z  cc<    | j                  |      S )u   
        Apply differential rotation to generate B-field.
        Different heads rotate at different speeds → richer representations.
        Nr?   zij,bkj->bkir-   )r@   rA   r)   r0   r   r   rangerz   r   einsumr{   )r;   r   rF   rG   r(   x_headsb_fieldhspeedr   head_featuresrotateds               r   rS   zDifferentialRotation.forward   s    
 %&GG!w	 ))E7DNNDMMJ ""1%t~~& 		ZA$$Q'E2259H $Aq!RaRK0Mll=(MJG Aq!dmm+QUdmm,CCCDQ2A2XNRYYN		Z !!'**r   )rT   )r   )r   r   r   r   rV   r/   r   rX   r   rS   rY   rZ   s   @r   rw   rw      sX    

=# 
=# 
=ELL s 5<< + +%,, +r   rw   c                   j     e Zd ZdZd	dededef fdZdej                  dej                  fdZ	 xZ
S )
GravitationalConvBlockz;Convolutional block with gravitational attraction features.in_channelsout_channelsuse_gravityc                 Z   t         |           || _        t        j                  ||dd      | _        t        j                  |      | _        |r[t        j                  t        j                  d            | _        t        j                  t        j                  d            | _        y y )Nr?   r-   kernel_sizepaddingr   )r.   r/   r   r1   Conv2dconvBatchNorm2dbnr2   r   r3   	G_spatialrb   scale)r;   r   r   r   r<   s       r   r/   zGravitationalConvBlock.__init__  sv    &IIk<QPQR	...\\%,,s*;<DNejjm4DJ r   r   r   c                 R   | j                  |      }| j                  |      }| j                  r|j                  \  }}}}|j	                  ||d      }t        j                  |d      }t        j                  |j                  dd      |      }	| j                  d|	z
  t        j                  z   z  }
t        j                  |
d      }
t        j                  |j	                  ||d      |
      j	                  ||||      }|| j                  z  }t        j                  |      }|S )Nr$   r-   rj   r   )r   r   r   r@   rA   rk   rl   r   rE   rB   r   r   r   softmaxr   gelu)r;   r   outrp   CHWfeaturesfeatures_norm
similaritygravity_weightss              r   rS   zGravitationalConvBlock.forward  s    iilggcl JAq!Q {{1a,H KKa8Mm&=&=a&C]SJ #nnJARAR0RSOiiR@O ,,s{{1a4oFNNqRSUVXYZC

"CffSk
r   )T)r   r   r   r   rV   boolr/   r   rX   rS   rY   rZ   s   @r   r   r     s;    E
5C 
5s 
5 
5 %,, r   r   c            	            e Zd ZdZ	 	 	 	 ddedededef fdZddej                  ded	ej                  fd
Z	de
de
de
d	e
fdZ xZS )GravitonCIFARu=  
    CIFAR-10 classifier using gravitational and Lorentz mechanics.
    
    Key components:
    - Gravitational attention for feature aggregation
    - Lorentz force updates for gradient modification
    - Differential rotation for B-field generation
    - GM=v²r manifold projection for stable representations
    num_classesr(   r)   use_physicsc                     t         |           || _        || _        t	        j
                  t	        j                  dddd      t	        j                  d      t	        j                         t        dd|      t	        j                  d      t        dd|      t	        j                  d      t        dd	|      t	        j                  d      	      | _        t	        j                  d
      | _        |rt        ||      nd | _        |rt!        ||      nd | _        |rt%        |      nd | _        t	        j
                  t	        j(                  |dz  d	      t	        j                         t	        j*                  d      t	        j(                  d	d      t	        j                         t	        j*                  d      t	        j(                  d|            | _        | j/                  dt1        j2                  d             y )Nr?   @   r-   r      )r   r      i   )   r      g333333?g?r   r   )r.   r/   r(   r   r1   r_   r   r   GELUr   	MaxPool2d
conv_embedAdaptiveAvgPool2dspatial_to_seqr'   grav_attentionrw   differential_rotr\   lorentz_modifierr4   Dropout
classifierregister_bufferr   r3   )r;   r   r(   r)   r   r<   s        r   r/   zGravitonCIFAR.__init__?  s    	"& --IIaA6NN2GGI"2sDLLO"3ELLO"3ELLO

 !226: OZ4Y	J_c OZ 4Y	 J_c GR 7	 BW[ --IIi"nc*GGIJJsOIIc3GGIJJsOIIc;'
 	.S0ABr   r   return_attentionr   c                    | j                  |      }|j                  \  }}}}|j                  ||d      j                  dd      }t	        j
                  |t        j                  | j                  |      j                  |j                        d |d d f         }| j                  r6| j                  *| j                  |      }	| j                  |      }
|
d|	z  z   }| j                  r$t        || j                  j!                               }|j                  |d      }|r| j                  rt        j"                         5  t        j$                  ||      }| j                  j&                  j!                         |dz  dz   z  }t	        j(                  |d      }d d d        | j+                  |      fS | j+                  |      S # 1 sw Y   -xY w)Nr$   r-   r   r^   r
   rj   )r   r@   rA   rB   rk   linearr   eyer(   todevicer   r   r   r%   r   itemno_gradcdistr   r   r   )r;   r   r   r   rF   channelsr   wseq_featuresr   rR   flatrO   	attentions                 r   rS   zGravitonCIFAR.forwardq  s   ??1% !)xA''x<FFq!L xx$yyBEEahhOPYQYPY[\P\]_  3 3 ?++L9G **<8H $cGm3L .|T=Q=Q=V=V=XYL ##E2. 0 0 9!KKlC	 //11668INT<QR	IIiR8	9 ??4()33t$$9 9s   AG		Gmodel_staterg   hidden_statesc                     | j                   s|S i }|j                         D ]5  \  }}|)||v r%||   }| j                  j                  ||      ||<   1|||<   7 |S )us   
        Apply Lorentz force to gradient updates.
        This modifies gradients using: F = q(E + v × B)
        )r   itemsr   ru   )r;   r   rg   r   modified_gradsnamegradh_states           r   lorentz_gradient_stepz#GravitonCIFAR.lorentz_gradient_step  s}     #//+ 	,JD$DM$9'-'+'<'<'Q'Q'(t$ (,t$	, r   )
   r   rT   T)F)r   r   r   r   rV   r   r/   r   rX   rS   dictr   rY   rZ   s   @r   r   r   4  s      0C0C 0C 	0C
 0Cd&% &% &%%,, &%P $ .27;r   r   c                   ,     e Zd ZdZd fd	ZddZ xZS )LorentzOptimizeruE  
    Custom optimizer applying Lorentz force modifications to gradients.
    
    The update rule: θ = θ - α * (∇E + q * v × B)
    
    Where:
    - ∇E is the standard gradient (E-field)
    - v is the gradient direction (velocity)
    - B is generated from current state
    - q controls cross-product strength
    c                 P    t        |||      }t        | 	  ||       || _        y )N)lrr   momentum)r   r.   r/   r   )r;   paramsr   r   r   defaultsr<   s         r   r/   zLorentzOptimizer.__init__  s'    2X6*r   c                    d }| |       }| j                   D ]8  }|d   }|d   }|d   }|d   D ]  }|j                  |j                  j                  }| j                  |   }	t	        |	      dk(  rDt        j                  |j                        |	d<   t        j                  |j                        |	d<   ||	d   z  |z   |	d<   t        j                  |j                        |	d<   t        j                  |	d   j                         |	d   j                               j                  |      }
|||
z  z   }|j                  ||z  z
  |_          ; |S )Nr   r   r   r   r   rh   r   )param_groupsr   datastatelenr   r   tanhro   rm   rn   )r;   closurelossgroupr   r   r   r   r   r   rs   lorentz_grads               r   stepzLorentzOptimizer.step  sa   9D&& !	4EtBc
AZ(H8_ 466>vv{{

1 u:?(-(8(8(@E*%','7'7'?E)$ %-uZ/@$@4$Gj! $)::aff#5i  #[[*%--/)$,,. *T" 
  $a*n4 "|"3394!	4F r   )MbP?r	   g?rU   )r   r   r   r   r/   r   rY   rZ   s   @r   r   r     s    

(r   r   modeltrain_loadertest_loaderr   epochsuse_lorentz_optimizerc                 F   t        j                         }|rt        | j                         dd      }n&t	        j
                  | j                         dd      }t        j                  j                  ||      }g }	g }
g }g }t        |      D ]  }| j                          d}d}d}t        |      D ]  \  }\  }}|j                  |      |j                  |      }}|j                           | |      } |||      }|j                          |j                          ||j                         z  }|j!                  d      \  }}||j#                  d      z  }||j%                  |      j'                         j                         z  } |t)        |      z  }d	|z  |z  }| j+                          d}d}d}t-        j.                         5  |D ]  \  }}|j                  |      |j                  |      }} | |      } |||      }||j                         z  }|j!                  d      \  }}||j#                  d      z  }||j%                  |      j'                         j                         z  } 	 d
d
d
       |t)        |      z  }d	|z  |z  }|j                          |	j1                  |       |
j1                  |       |j1                  |       |j1                  |       t3        d|dz   dd| d|dd|dd|dd|dd        |	|
||fS # 1 sw Y   xY w)z%Train the Graviton model on CIFAR-10.r   r	   )r   r   )r   weight_decay)T_maxg        r   r-   g      Y@NzEpoch 3d/z | Train Loss: .4fz Acc: .2fz% | Test Loss: %)r1   CrossEntropyLossr   
parametersoptimAdamWlr_schedulerCosineAnnealingLRr   train	enumerater   	zero_gradbackwardr   r   maxsizeeqrD   r   evalr   r   appendprint)r   r   r   r   r   r   	criterion	optimizer	schedulertrain_losses
train_accstest_losses	test_accsepochrunning_losscorrecttotal	batch_idxinputstargetsoutputsr   rH   	predicted
train_loss	train_acc	test_losstest_correct
test_totaltest_accs                                 r   train_gravitonr!    s"    ##%I$U%5%5%7ETJ	KK 0 0 2u4P	""44Yf4MILJKIv 5B,5l,C 	:(I($ii/F1CGF!FmGWg.DMMONNDIIK'L";;q>LAyW\\!_$Ey||G,0027799G	: "C$55
7NU*	 	

	
]]_ 		C#. C"())F"3WZZ5G- '2TYY[(	&{{1~9gll1o-
	W 5 9 9 ; @ @ BBC		C K 00	,&3J')$9%"uQwrl!F8 ,',F9S/ B%c?&#aA 	Bg5Bn [);;3		C 		Cs   B)LL 	c            	      4   t        j                  t         j                  j                         rdnd      } t	        d|         t        j                  t        j                  dd      t        j                         t        j                         t        j                  dd      g      }t        j                  j                  d	d
d
|      }t        j                  j                  d	dd
|      }t        |dd
d      }t        |ddd      }t	        dt        |       dt        |              t	        d       t	        d       t	        d       t!        dddd
      j#                  |       }t%        |||| dd      \  }}}	}
t        j&                  |j)                         |||	|
|j*                  r$|j*                  j,                  j/                         nd dd       t	        d       t	        d       t	        d|j*                  j,                  j/                         d       t	        d|j0                  j2                  j/                         d       t	        d       t5        |
      }|
j7                  |      d z   }t	        d!|d"d#|        |||
fS )$NcudacpuzUsing device:     r   )r   )gHPs?gec]?g~jt?)gۊe?ggDio?g|?5^?z../dataT)rootr  download	transformFr   )
batch_sizeshufflenum_workerszTraining samples: z, Test samples: z=
============================================================z?Training with Physics (Gravitational Attention + Lorentz Force)z<============================================================r   r   rT   )r   r(   r)   r      )r   r   )r   r  r  r  r  r   zgraviton_cifar_results.pthzPhysics parameters learned:z  Gravitational constant G: r   z  Lorentz charge q: r-   z
Best test accuracy: r   z% at epoch )r   r   r#  is_availabler
  
transformsCompose
RandomCropRandomHorizontalFlipToTensor	NormalizetorchvisiondatasetsCIFAR10r   r   r   r   r!  save
state_dictr   r   r   r   r   r  index)r   r(  trainsettestsettrainloader
testloadermodel_physicslossesaccsr  r  best_acc
best_epochs                r   mainrC  J  sq   \\EJJ$;$;$=&5IF	N6(
#$ ""b!,'')57OP	$ I ##++dTY , H ""**edi + G X#tQRSKGUPQRJ	s8}o-=c'l^
LM 
-	
KL	&M!	
 	bj  ,:{JrY^,(FD+y
 
JJ$//1"6C6R6R]))++002X\ $% 
-	
'(	()E)E)G)G)L)L)Ns(S
TU	 !?!?!A!A!F!F!H M
NO	&M 9~H*Q.J	"8C.J<
HI&)++r   __main__)r$   g:0yE>)r   )2   F)+r   r   torch.nnr1   torch.nn.functional
functionalrk   torch.optimr   torch.utils.datar   r4  torchvision.transformsr.  mathnumpynptypingr   r   r   rX   rV   rW   r!   r%   Moduler'   r\   rw   r   r   	Optimizerr   r   r   listr!  rC  r   r   r  r   r   r   r   <module>rS     s}        '  +   " ell    55<< 5 5 5@'RYY @'N?bii ?L8+299 8+~*RYY *bxBII x~:u :L "'O<99O<O< O< LL	O<
 O<  O< 4tT!"O<l>,B z"&&E:x r   