
    s(j                     j    d Z ddlZddlmc mZ ddlZddl	Z	ddl
mZmZ  G d d      Z G d d      Zy)au  
TDD RED tests for the MLP forward pass in strategies/strategy_mlp_scores.py.

Contract under test:
  - _tanh(x): clamped closed-form tanh, identical formula to the Pine implementation:
        tanh(x) = x >= 20 ?  1.0
                : x <= -20 ? -1.0
                : (exp(2x) - 1) / (exp(2x) + 1)
  - mlp_forward(X, layers): X is a (T, F) float64 matrix; layers is a list of
    (W, b) tuples with W shape (n_out, n_in) and b shape (n_out,).
    Every layer (including the output layer) applies _tanh; the final scalar
    is scaled by 1000 so scores live in [-1000, +1000] like the existing
    activation-score convention.
    N)_tanhmlp_forwardc                       e Zd Zd Zd Zd Zy)TestTanhc                 2   t        j                  ddd      }t         j                  }t         j                  }t	        |      }t         j
                  } ||      }||z
  } ||      } ||      }	d}
|	|
k  }|st        j                  d|fd|	|
f      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndd	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      t        j                  |	      t        j                  |
      d
z  }dd|iz  }t        t        j                  |            d x}x}x}x}x}x}x}x}	x}}
y )Ng      3g      3@i  -q=<)z%(py20)s
{%(py20)s = %(py2)s
{%(py2)s = %(py0)s.max
}(%(py18)s
{%(py18)s = %(py5)s
{%(py5)s = %(py3)s.abs
}((%(py9)s
{%(py9)s = %(py6)s(%(py7)s)
} - %(py15)s
{%(py15)s = %(py12)s
{%(py12)s = %(py10)s.tanh
}(%(py13)s)
}))
})
} < %(py23)snpr   x)py0py2py3py5py6py7py9py10py12py13py15py18py20py23zassert %(py25)spy25)r   linspacemaxabsr   tanh
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)selfr   @py_assert1@py_assert4@py_assert8@py_assert11@py_assert14@py_assert16@py_assert17@py_assert19@py_assert22@py_assert21@py_format24@py_format26s                 A/Users/jameslopez/projects/TradingBot25/tests/test_mlp_forward.pytest_matches_numpy_inside_clampz(TestTanh.test_matches_numpy_inside_clamp   s   KKtT*vv<bff<U1X<<
<X
2<f23<v34<u<4u<<<<4u<<<<<<r<<<r<<<v<<<<<<b<<<b<<<f<<<<<<U<<<U<<<<<<1<<<1<<<X<<<<<<<<<<<<<<<<<<<<<<<<
<<<3<<<4<<<u<<<<<<<<<    c           
      Z	   t        t        j                  g d            d   }d}||k(  }|slt        j                  d|fd||f      t        j
                  |      t        j
                  |      dz  }dd|iz  }t        t        j                  |            d x}x}}t        j                  }t        j                  }g d} ||      }	t        |	      }
d}|
|k(  } ||      }|st        j                  d|fd	|
|f      d
t        j                         v st        j                  t               rt        j
                  t               nd
dt        j                         v st        j                  t              rt        j
                  t              ndt        j
                  |      t        j
                  |      t        j
                  |	      t        j
                  |
      t        j
                  |      dz  }ddt        j                         v st        j                  t              rt        j
                  t              ndt        j
                  |      |t        j
                  |      dz  }t        t        j                  |            d x}x}x}x}	x}
x}x}}t        j                  }t        j                  }g d} ||      }	t        |	      }
d}| }|
|k(  } ||      }|st        j                  d|fd|
|f      d
t        j                         v st        j                  t               rt        j
                  t               nd
dt        j                         v st        j                  t              rt        j
                  t              ndt        j
                  |      t        j
                  |      t        j
                  |	      t        j
                  |
      t        j
                  |      dz  }ddt        j                         v st        j                  t              rt        j
                  t              ndt        j
                  |      |t        j
                  |      dz  }t        t        j                  |            d x}x}x}x}	x}
x}x}x}}y )N)g      4@g      9@g    .Ar         ?==)z%(py1)s == %(py4)s)py1py4zassert %(py6)sr   )zn%(py12)s
{%(py12)s = %(py3)s(%(py10)s
{%(py10)s = %(py6)s
{%(py6)s = %(py4)s.array
}(%(py8)s)
})
} == %(py15)sr   r   )r   r=   r   py8r   r   r   zHassert %(py19)s
{%(py19)s = %(py2)s
{%(py2)s = %(py0)s.all
}(%(py17)s)
})r   r   py17py19)g      4g      9g    .)zo%(py12)s
{%(py12)s = %(py3)s(%(py10)s
{%(py10)s = %(py6)s
{%(py6)s = %(py4)s.array
}(%(py8)s)
})
} == -%(py15)szHassert %(py20)s
{%(py20)s = %(py2)s
{%(py2)s = %(py0)s.all
}(%(py18)s)
})r   r   r   r   )r   r   arrayr    r!   r%   r&   r'   allr"   r#   r$   )r(   @py_assert0@py_assert3@py_assert2@py_format5@py_format7r)   @py_assert5@py_assert7@py_assert9r,   r-   @py_assert13@py_assert18@py_format16@py_format20r.   r0   @py_format17@py_format21s                       r5    test_clamps_to_exact_one_outsidez)TestTanh.test_clamps_to_exact_one_outside   s   RXX/01!4;;4;;;;4;;;4;;;;;;;;;;vv@BHH@%6@H%67@e78@C@8C?@v@@@@@@@8C@@@@@@e@@@e@@@@@@B@@@B@@@H@@@%6@@@7@@@8@@@C@@@@@@r@@@r@@@v@@@@@@@@@@@vvDBHHD%9DH%9:De:;DDtD;tCDvDDDDDDD;tDDDDDDeDDDeDDDDDDBDDDBDDDHDDD%9DDD:DDD;DDDDDDDDDrDDDrDDDvDDDDDDDDDDDDr7   c                 x   d}t        |      }t        |      }t        j                  }d} ||      }||z
  }t	        |      }d}	||	k  }
|
st        j                  d|
fd||	f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |	      d	z  }d
d|iz  }t        t        j                  |            d x}x}x}x}x}x}x}x}x}
}	d}t        |      }t        |      }d}||k(  }|s&t        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}y )N      ?r   r	   )z%(py18)s
{%(py18)s = %(py0)s((%(py8)s
{%(py8)s = %(py1)s(%(py6)s
{%(py6)s = %(py2)s(%(py4)s)
})
} - %(py15)s
{%(py15)s = %(py11)s
{%(py11)s = %(py9)s.tanh
}(%(py13)s)
}))
} < %(py21)sr   floatr   r   )r   r<   r   r=   r   r>   r   py11r   r   r   py21zassert %(py23)sr           r:   )zO%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py1)s(%(py3)s)
})
} == %(py10)s)r   r<   r   r   r   r   zassert %(py12)sr   )r   rT   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   )r(   rD   rH   rI   @py_assert10@py_assert12r-   r.   r/   @py_assert20r0   @py_format22r3   rE   r*   @py_assert6rJ   r+   @py_format11@py_format13s                       r5   test_scalar_inputzTestTanh.test_scalar_input    s   "<s<5$<rww<s<ws|<$|3<s34<u<4u<<<<4u<<<<<<s<<<s<<<<<<5<<<5<<<<<<<<<<<<s<<<<<<$<<<<<<r<<<r<<<w<<<s<<<|<<<4<<<u<<<<<<<<<'U3Z'uZ 'C' C'''' C''''''u'''u''''''U'''U'''3'''Z''' '''C''''''''r7   N)__name__
__module____qualname__r6   rQ   r_    r7   r5   r   r      s    =E(r7   r   c                   $    e Zd Zd Zd Zd Zd Zy)TestForwardc                 ,   t        j                  g dg dgt         j                        }t        j                  ddgt         j                        }t        j                  ddggt         j                        }t        j                  dgt         j                        }t        j                  g d	gt         j                        }t        j                  ||d
   z  |z         }dt        j                  ||z  |z         d
   z  }t	        |||f||fg      }|j
                  }	d}
|	|
k(  }|st        j                  d|fd|	|
f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |	      t        j                  |
      dz  }dd|iz  }t        t        j                  |            d x}	x}}
|d
   }	|	|z
  }
t        |
      }d}||k  }|s
t        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |	      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}	x}
x}x}}y )N)rS   g      п皙?)g?g333333?gٿdtyperg   gɿgffffff?g333333g?)r9   g      g      ?r        @@)   r:   z-%(py2)s
{%(py2)s = %(py0)s.shape
} == %(py5)soutr   r   r   assert %(py7)sr   &.>r	   )z;%(py6)s
{%(py6)s = %(py0)s((%(py2)s - %(py3)s))
} < %(py9)sr   expected)r   r   r   r   r   zassert %(py11)srU   )r   rA   float64r   r   shaper    r!   r"   r#   r$   r%   r&   r'   r   )r(   W1b1W2b2r   hrq   rm   r)   r*   rD   @py_format6@py_format8rH   r+   rI   @py_format10@py_format12s                      r5   test_hand_computed_two_layerz(TestForward.test_hand_computed_two_layer&   s    XX((*13=XXsDk4XXT{m2::6XXtfBJJ/HH'(

;GGB1IN#BGGBFRK033!r2hR12yy D yD    yD      s   s   y   D       q6,6H$,s$%,,%,,,,%,,,,,,s,,,s,,,6,,,,,,H,,,H,,,%,,,,,,,,,,,r7   c           
         t         j                  j                  d      }dg d}}|j                  dd||d   f      }t	        t        |      dz
        D cg c]9  }|j                  d||dz      ||   f	      |j                  d
||dz      	      f; }}t        ||      }|j                  }|f}	||	k(  }
|
st        j                  d|
fd||	f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      dz  }dd|iz  }t        t        j                   |            d x}x}
}	|j"                  }t         j$                  }||k(  }
|
st        j                  d|
fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      dz  }dd|iz  }t        t        j                   |            d x}x}
}t         j&                  }t         j(                  }	 |	|      }d}||k  } ||      }|s{t        j                  d|fd||f      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |	      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }ddt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      |t        j                  |      dz  }t        t        j                   |            d x}x}	x}x}x}}y c c}w )N*      1         rk   rk   r   sizerS   scaler   rg   r:   rl   rm   rn   ro   r   )zJ%(py2)s
{%(py2)s = %(py0)s.dtype
} == %(py6)s
{%(py6)s = %(py4)s.float64
}r   )r   r   r=   r   zassert %(py8)sr>   rj   )<=)zJ%(py8)s
{%(py8)s = %(py5)s
{%(py5)s = %(py3)s.abs
}(%(py6)s)
} <= %(py11)s)r   r   r   r>   rU   zHassert %(py15)s
{%(py15)s = %(py2)s
{%(py2)s = %(py0)s.all
}(%(py13)s)
})r   r   r   r   )r   randomdefault_rnguniformrangelennormalr   rs   r    r!   r"   r#   r$   r%   r&   r'   ri   rr   rB   r   )r(   rngTarchXilayersrm   r)   r*   rD   ry   rz   rH   rG   @py_format9rI   rX   rJ   r-   r|   rM   s                         r5   test_output_shape_and_rangez'TestForward.test_output_shape_and_range8   s   ii##B'^4KKAQQLK1 3t9q=)
 * ZZca!ed1g(>Z?ZZcQUZ46) 	 

 !V$yy QD yD    yD      s   s   y   D       yy&BJJ&yJ&&&&yJ&&&&&&s&&&s&&&y&&&&&&B&&&B&&&J&&&&&&&vv,bff,fSk,V,kV+,v,,,,,,,kV,,,,,,b,,,b,,,f,,,,,,S,,,S,,,k,,,V,,,,,,r,,,r,,,v,,,,,,,,,,,
s   >Qc                    t        j                  dt         j                        }t        j                  d      t        j                  d      ft        j                  d      t        j                  d      fg}t         j                  }t        ||      }d}||k(  } ||      }|st        j                  d|fd	||f      d
t        j                         v st        j                  t
              rt        j                  t
              nd
dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }ddt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      |t        j                  |      dz  }	t        t        j                  |	            d x}x}x}x}}y )N)      rh   )   r   r   )rk   r   rk   rW   r:   )z:%(py7)s
{%(py7)s = %(py3)s(%(py4)s, %(py5)s)
} == %(py10)sr   r   r   )r   r=   r   r   r   zHassert %(py14)s
{%(py14)s = %(py2)s
{%(py2)s = %(py0)s.all
}(%(py12)s)
}r   )r   r   r   py14)r   onesrr   zerosrB   r   r    r!   r"   r#   r$   r%   r&   r'   )
r(   r   r   r)   r\   rJ   r+   rK   r]   @py_format15s
             r5   !test_zero_weights_give_zero_scorez-TestForward.test_zero_weights_give_zero_scoreF   sM   GGF"**-88F#RXXa[1BHHV4Dbhhqk3RSvv4k!V,44,34v4444444,444444k444k444444!444!444444V444V444,444444444r444r444v44444444444r7   c                 (   t        j                  d      }t        j                  j	                  d      }g d}|j                  ddd|d   f      }t        t        |      dz
        D cg c]9  }|j                  d	||dz      ||   f
      |j                  d||dz      
      f; }}|j                         5  |j                  ||j                        }|D ]D  \  }}	|j                  ||j                  |j                        z  |j                  |	      z         }F |j                  d      dz  j                         }
d d d        t        j                   }t        j"                  }t%        ||      }|
z
  } ||      } ||      }d}||k  }|sZt'        j(                  d|fd||f      dt+        j,                         v st'        j.                  t              rt'        j0                  t              ndt'        j0                  |      dt+        j,                         v st'        j.                  t              rt'        j0                  t              ndt'        j0                  |      dt+        j,                         v st'        j.                  t$              rt'        j0                  t$              nddt+        j,                         v st'        j.                  |      rt'        j0                  |      nddt+        j,                         v st'        j.                  |      rt'        j0                  |      ndt'        j0                  |      dt+        j,                         v st'        j.                  |
      rt'        j0                  |
      ndt'        j0                  |      t'        j0                  |      t'        j0                  |      dz  }dd|iz  }t3        t'        j4                  |            d x}x}x}x}x}x}x}}y c c}w # 1 sw Y   xY w)Ntorchr   r   r   rk   @   r   r   rS   r   rg   rh   rj   rp   r	   )z%(py16)s
{%(py16)s = %(py2)s
{%(py2)s = %(py0)s.max
}(%(py14)s
{%(py14)s = %(py5)s
{%(py5)s = %(py3)s.abs
}((%(py10)s
{%(py10)s = %(py6)s(%(py7)s, %(py8)s)
} - %(py11)s))
})
} < %(py19)sr   r   r   r   ref)r   r   r   r   r   r   r>   r   rU   r   py16r@   zassert %(py21)srV   )pytestimportorskipr   r   r   r   r   r   r   no_gradtensorrr   r   r   squeezenumpyr   r   r   r    r!   r"   r#   r$   r%   r&   r'   )r(   r   r   r   r   r   r   aWbr   r)   r*   rJ   rY   rK   @py_assert15rL   r/   rN   r[   s                        r5   test_matches_torch_referencez(TestForward.test_matches_torch_referenceK   s   ##G,ii##A&KKARaMK2 3t9q=)
 * ZZca!ed1g(>Z?ZZcQUZ46) 	 

 ]]_Qemm4A1JJq5<<#44u||AFG 99R=6)002C	 
 vvBbffB[F3B3c9Bf9:Bv:;BdB;dBBBB;dBBBBBBrBBBrBBBvBBBBBBbBBBbBBBfBBBBBB[BBB[BBBBBBBBBBBBBBBFBBBFBBB3BBBBBBcBBBcBBB:BBB;BBBdBBBBBBBB

 _s   +>P;B	PPN)r`   ra   rb   r}   r   r   r   rc   r7   r5   re   re   %   s    -$-5
Cr7   re   )__doc__builtinsr"   _pytest.assertion.rewrite	assertionrewriter    r   r   r   strategies.strategy_mlp_scoresr   r   r   re   rc   r7   r5   <module>r      s0        =( ( 5C 5Cr7   