55using Tensorflow . Keras . Utils ;
66using Tensorflow . Train ;
77using static Tensorflow . Binding ;
8+ using Tensorflow ;
9+ using Tensorflow . Eager ;
810
911namespace Tensorflow . Keras . Optimizers
1012{
@@ -17,18 +19,32 @@ public class OptimizerV2 : Trackable, IOptimizer
1719 protected virtual string _name { get ; }
1820
1921 ResourceVariable _iterations ;
20- List < ResourceVariable > _weight = new List < ResourceVariable > ( ) ;
21- Dictionary < string , float > _hyper = new Dictionary < string , float > ( ) ;
22- Dictionary < string , ResourceVariable > _hyper_variables = new Dictionary < string , ResourceVariable > ( ) ;
22+ List < ResourceVariable > _weight ;
23+ Dictionary < string , float > _hyper ;
24+ Dictionary < string , ResourceVariable > _hyper_variables ;
2325 protected bool _momentum ;
2426 protected float _initial_decay = 0.0f ;
27+ protected bool _use_locking = true ;
28+
29+ Dictionary < DeviceDType , Dictionary < string , Tensor > > apply_state ;
2530
2631 public OptimizerV2 ( ) : base ( )
2732 {
28-
33+ _weight = new List < ResourceVariable > ( ) ;
34+ _hyper = new Dictionary < string , float > ( ) ;
35+ _hyper_variables = new Dictionary < string , ResourceVariable > ( ) ;
36+ apply_state = new Dictionary < DeviceDType , Dictionary < string , Tensor > > ( ) ;
2937 }
3038
31- public void apply_gradients ( IEnumerable < ( Tensor , ResourceVariable ) > grads_and_vars )
39+ /// <summary>
40+ /// Apply gradients to variables.
41+ /// </summary>
42+ /// <param name="grads_and_vars"></param>
43+ /// <param name="name"></param>
44+ /// <param name="experimental_aggregate_gradients"></param>
45+ public void apply_gradients ( IEnumerable < ( Tensor , ResourceVariable ) > grads_and_vars ,
46+ string name = null ,
47+ bool experimental_aggregate_gradients = true )
3248 {
3349 var var_list = grads_and_vars . Select ( x => x . Item2 ) . ToArray ( ) ;
3450 tf_with ( ops . name_scope ( _name ) , delegate
@@ -38,49 +54,91 @@ public void apply_gradients(IEnumerable<(Tensor, ResourceVariable)> grads_and_va
3854 if ( grads_and_vars == null || grads_and_vars . Count ( ) == 0 )
3955 return control_flow_ops . no_op ( ) ;
4056
41- //var apply_state =
42- _prepare ( var_list ) ;
43-
44- _aggregate_gradients ( grads_and_vars ) ;
57+ apply_state = _prepare ( var_list ) ;
58+ if ( experimental_aggregate_gradients )
59+ {
60+ // var reduced_grads = _aggregate_gradients(grads_and_vars);
61+ _distributed_apply ( grads_and_vars , name , apply_state ) ;
62+ }
4563
4664 return null ;
4765 } ) ;
4866 }
4967
50- void _aggregate_gradients ( IEnumerable < ( Tensor , ResourceVariable ) > grads_and_vars )
68+ void apply_grad_to_update_var ( ResourceVariable var , EagerTensor grad )
69+ {
70+ _resource_apply_dense ( var , grad , apply_state ) ;
71+ }
72+
73+ protected virtual Operation _resource_apply_dense ( ResourceVariable var ,
74+ EagerTensor grad ,
75+ Dictionary < DeviceDType , Dictionary < string , Tensor > > _apply_state )
76+ {
77+ throw new NotImplementedException ( "_resource_apply_dense" ) ;
78+ }
79+
80+ void _distributed_apply ( IEnumerable < ( Tensor , ResourceVariable ) > grads_and_vars ,
81+ string name ,
82+ Dictionary < DeviceDType , Dictionary < string , Tensor > > _apply_state )
83+ {
84+ tf_with ( ops . name_scope ( name , "" , new { skip_on_eager = true } ) , delegate
85+ {
86+ foreach ( var ( grad , var ) in grads_and_vars )
87+ {
88+ tf_with ( ops . name_scope ( "update" ) , delegate
89+ {
90+ apply_grad_to_update_var ( var , grad as EagerTensor ) ;
91+ } ) ;
92+ }
93+
94+ _iterations . assign_add ( ops . convert_to_tensor ( 1 , dtype : _iterations . dtype ) ) ;
95+ } ) ;
96+ }
97+
98+ Tensor [ ] _aggregate_gradients ( IEnumerable < ( Tensor , ResourceVariable ) > grads_and_vars )
99+ {
100+ return grads_and_vars . Select ( x => x . Item1 ) . ToArray ( ) ;
101+ }
102+
103+ Dictionary < DeviceDType , Dictionary < string , Tensor > > _prepare ( ResourceVariable [ ] var_list )
51104 {
52- var lr_t = _hyper_variables [ "learning_rate" ] ;
53- foreach ( var grad_and_var in grads_and_vars )
105+ var _apply_state = new Dictionary < DeviceDType , Dictionary < string , Tensor > > ( ) ;
106+ var keys = var_list . Select ( x => new DeviceDType
54107 {
55- var grad = grad_and_var . Item1 ;
56- var variable = grad_and_var . Item2 ;
57- // variable.Handle - grad * lr_t.Handle;
108+ Device = x . Device ,
109+ DType = x . dtype . as_base_dtype ( )
110+ } ) . Distinct ( new DeviceDType ( ) ) . ToArray ( ) ;
111+
112+ foreach ( var device_dtype in keys )
113+ {
114+ _apply_state [ device_dtype ] = new Dictionary < string , Tensor > ( ) ;
115+ _prepare_local ( device_dtype , _apply_state ) ;
58116 }
117+
118+ return _apply_state ;
59119 }
60120
61- void _prepare ( ResourceVariable [ ] var_list )
121+ protected virtual void _prepare_local ( DeviceDType device_dtype ,
122+ Dictionary < DeviceDType , Dictionary < string , Tensor > > _apply_state )
62123 {
63- var keys = new HashSet < ( string , TF_DataType ) > ( ) ;
64- foreach ( var variable in var_list )
124+ if ( _hyper . ContainsKey ( "learning_rate" ) )
65125 {
66- var lr_t = _prepare_local ( variable . Device , variable . dtype . as_base_dtype ( ) ) ;
67- var momentum = _get_hyper ( "momentum" , variable . dtype ) ;
68- array_ops . identity ( momentum ) ;
126+ var lr_t = array_ops . identity ( _decayed_lr ( device_dtype . DType ) ) ;
127+ _apply_state [ device_dtype ] [ "lr_t" ] = lr_t ;
69128 }
70129 }
71130
72- ResourceVariable _prepare_local ( string var_device , TF_DataType var_dtype )
131+ Tensor _decayed_lr ( TF_DataType var_dtype )
73132 {
74133 var lr_t = _get_hyper ( "learning_rate" , var_dtype ) ;
75- if ( _initial_decay > 0 )
134+ if ( _initial_decay > 0.0f )
76135 {
77-
136+ throw new NotImplementedException ( "" ) ;
78137 }
79-
80138 return lr_t ;
81139 }
82140
83- ResourceVariable _get_hyper ( string name , TF_DataType dtype = TF_DataType . DtInvalid )
141+ protected ResourceVariable _get_hyper ( string name , TF_DataType dtype = TF_DataType . DtInvalid )
84142 {
85143 var value = _hyper_variables [ name ] ;
86144 return math_ops . cast ( value , dtype ) ;
0 commit comments