@@ -4,6 +4,7 @@ use crate::domain::{
44 AdamConfig , LbfgsConfig , NelderMeadConfig , NewtonCgConfig , OptimizerMethod , SgdConfig ,
55 SteepestDescentConfig ,
66} ;
7+ use std:: hash:: { Hash , Hasher } ;
78
89use super :: { C1_MIN , C2_MAX , STEP_MAX_MAX , STEP_MIN_MIN , UiLanguage } ;
910
@@ -88,6 +89,30 @@ fn normalize_wolfe_line_search_inputs(
8889 * step_max = ( * step_max) . clamp ( * step_min + 1e-6 , STEP_MAX_MAX ) ;
8990}
9091
92+ fn hash_f64_normalized < H : Hasher > ( hasher : & mut H , value : f64 ) {
93+ let normalized_bits = if value == 0.0 {
94+ 0.0f64 . to_bits ( )
95+ } else {
96+ value. to_bits ( )
97+ } ;
98+ normalized_bits. hash ( hasher) ;
99+ }
100+
101+ fn hash_wolfe_line_search_inputs < H : Hasher > (
102+ hasher : & mut H ,
103+ c1 : f64 ,
104+ c2 : f64 ,
105+ step_min : f64 ,
106+ step_max : f64 ,
107+ width_tolerance : f64 ,
108+ ) {
109+ hash_f64_normalized ( hasher, c1) ;
110+ hash_f64_normalized ( hasher, c2) ;
111+ hash_f64_normalized ( hasher, step_min) ;
112+ hash_f64_normalized ( hasher, step_max) ;
113+ hash_f64_normalized ( hasher, width_tolerance) ;
114+ }
115+
91116pub ( super ) fn lbfgs_config_from_preset ( preset : OptimizerPreset ) -> LbfgsConfig {
92117 match preset {
93118 OptimizerPreset :: Fast => {
@@ -247,6 +272,21 @@ impl LbfgsInputState {
247272 )
248273 . map_err ( |error| error. to_string ( ) )
249274 }
275+
276+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
277+ self . history_size . hash ( hasher) ;
278+ self . max_iters . hash ( hasher) ;
279+ hash_f64_normalized ( hasher, self . tol_grad ) ;
280+ hash_f64_normalized ( hasher, self . tol_cost ) ;
281+ hash_wolfe_line_search_inputs (
282+ hasher,
283+ self . c1 ,
284+ self . c2 ,
285+ self . step_min ,
286+ self . step_max ,
287+ self . width_tolerance ,
288+ ) ;
289+ }
250290}
251291
252292#[ derive( Debug , Clone , PartialEq ) ]
@@ -294,6 +334,16 @@ impl NelderMeadInputState {
294334 )
295335 . map_err ( |error| error. to_string ( ) )
296336 }
337+
338+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
339+ self . max_iters . hash ( hasher) ;
340+ hash_f64_normalized ( hasher, self . simplex_scale ) ;
341+ hash_f64_normalized ( hasher, self . sd_tolerance ) ;
342+ hash_f64_normalized ( hasher, self . alpha ) ;
343+ hash_f64_normalized ( hasher, self . gamma ) ;
344+ hash_f64_normalized ( hasher, self . rho ) ;
345+ hash_f64_normalized ( hasher, self . sigma ) ;
346+ }
297347}
298348
299349#[ derive( Debug , Clone , PartialEq ) ]
@@ -338,6 +388,18 @@ impl SteepestDescentInputState {
338388 )
339389 . map_err ( |error| error. to_string ( ) )
340390 }
391+
392+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
393+ self . max_iters . hash ( hasher) ;
394+ hash_wolfe_line_search_inputs (
395+ hasher,
396+ self . c1 ,
397+ self . c2 ,
398+ self . step_min ,
399+ self . step_max ,
400+ self . width_tolerance ,
401+ ) ;
402+ }
341403}
342404
343405#[ derive( Debug , Clone , PartialEq ) ]
@@ -391,6 +453,20 @@ impl NewtonCgInputState {
391453 )
392454 . map_err ( |error| error. to_string ( ) )
393455 }
456+
457+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
458+ self . max_iters . hash ( hasher) ;
459+ hash_f64_normalized ( hasher, self . tol ) ;
460+ hash_f64_normalized ( hasher, self . curvature_threshold ) ;
461+ hash_wolfe_line_search_inputs (
462+ hasher,
463+ self . c1 ,
464+ self . c2 ,
465+ self . step_min ,
466+ self . step_max ,
467+ self . width_tolerance ,
468+ ) ;
469+ }
394470}
395471
396472#[ derive( Debug , Clone , PartialEq ) ]
@@ -414,6 +490,11 @@ impl SgdInputState {
414490 pub ( super ) fn to_config ( & self ) -> Result < SgdConfig , String > {
415491 SgdConfig :: try_new ( self . max_iters , self . learning_rate ) . map_err ( |error| error. to_string ( ) )
416492 }
493+
494+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
495+ self . max_iters . hash ( hasher) ;
496+ hash_f64_normalized ( hasher, self . learning_rate ) ;
497+ }
417498}
418499
419500#[ derive( Debug , Clone , PartialEq ) ]
@@ -437,4 +518,9 @@ impl AdamInputState {
437518 pub ( super ) fn to_config ( & self ) -> Result < AdamConfig , String > {
438519 AdamConfig :: try_new ( self . max_iters , self . learning_rate ) . map_err ( |error| error. to_string ( ) )
439520 }
521+
522+ pub ( super ) fn hash_into < H : Hasher > ( & self , hasher : & mut H ) {
523+ self . max_iters . hash ( hasher) ;
524+ hash_f64_normalized ( hasher, self . learning_rate ) ;
525+ }
440526}
0 commit comments