You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trackable.cs 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using OneOf;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using Tensorflow.Checkpoint;
  19. using Tensorflow.Keras.Saving.SavedModel;
  20. using Tensorflow.Training;
  21. using Tensorflow.Training.Saving.SavedModel;
  22. using static Tensorflow.Binding;
  23. namespace Tensorflow.Train
  24. {
  25. public abstract class Trackable: IWithTrackable
  26. {
  27. /// <summary>
  28. /// Corresponding to tensorflow/python/trackable/constants.py
  29. /// </summary>
  30. public static class Constants
  31. {
  32. public static readonly string OBJECT_GRAPH_PROTO_KEY = "_CHECKPOINTABLE_OBJECT_GRAPH";
  33. public static readonly string VARIABLE_VALUE_KEY = "VARIABLE_VALUE";
  34. public static readonly string OBJECT_CONFIG_JSON_KEY = "OBJECT_CONFIG_JSON";
  35. }
  36. protected int _self_update_uid;
  37. protected IDictionary<string, Trackable> _unconditional_dependency_names;
  38. protected IList<TrackableReference> _unconditional_checkpoint_dependencies;
  39. protected Dictionary<string, IList<CheckpointPosition>> _unconditional_deferred_dependencies;
  40. protected IDictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>> _self_saveable_object_factories =
  41. new Dictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>>();
  42. private bool _manual_tracking = true;
  43. private static Trackable _none = new AutoTrackable();
  44. /// <summary>
  45. /// This is a trick for that CSharp does not allow the key of `Dictionary` to be null.
  46. /// The `None` can be any object that inherits `Trackable`.
  47. /// This Property is supposed to be used only internal.
  48. /// </summary>
  49. public static Trackable None
  50. {
  51. get
  52. {
  53. return _none;
  54. }
  55. }
  56. public Trackable GetTrackable()
  57. {
  58. return this;
  59. }
  60. public virtual string ObjectIdentifier
  61. {
  62. get => "_generic_user_object";
  63. }
  64. public int UpdateUid { get => _self_update_uid; set => _self_update_uid = value; }
  65. public IList<TrackableReference> UnconditionalCheckpointDependencies { get => _unconditional_checkpoint_dependencies; }
  66. public IDictionary<string, Trackable> UnconditionalDependencyNames { get => _unconditional_dependency_names; }
  67. public IList<TrackableReference> CheckpointDependencies { get => UnconditionalCheckpointDependencies; }
  68. public Dictionary<string, IList<CheckpointPosition>> DeferredDependencies => _unconditional_deferred_dependencies;
  69. public IDictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>> SelfSaveableObjectFactories
  70. {
  71. get
  72. {
  73. return _self_saveable_object_factories;
  74. }
  75. set
  76. {
  77. _self_saveable_object_factories = value;
  78. }
  79. }
  80. public Dictionary<string, object> CustomizedFields { get; set; } = new Dictionary<string, object>();
  81. public virtual void SetAttr(string name, object value)
  82. {
  83. var t = this.GetType();
  84. var field_info = t.GetField(name);
  85. if(field_info is not null)
  86. {
  87. field_info.SetValue(this, value);
  88. }
  89. else
  90. {
  91. CustomizedFields[name] = value;
  92. }
  93. // On account of performance, we don't use reflection to set the attribute if it exists in `Trackable`.
  94. // When adding new members or properties to this class, please add corresponding process to this method.
  95. //switch (name)
  96. //{
  97. // case "_manual_tracking":
  98. // {
  99. // _manual_tracking = (bool)value;
  100. // break;
  101. // }
  102. // case "_self_saveable_object_factories":
  103. // {
  104. // _self_saveable_object_factories = (IDictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>>)value;
  105. // break;
  106. // }
  107. // case "_self_update_uid":
  108. // {
  109. // _self_update_uid = (int)value;
  110. // break;
  111. // }
  112. // case "_unconditional_checkpoint_dependencies":
  113. // {
  114. // _unconditional_checkpoint_dependencies = (IList<TrackableReference>)value;
  115. // break;
  116. // }
  117. // case "_unconditional_deferred_dependencies":
  118. // {
  119. // _unconditional_deferred_dependencies = (Dictionary<string, IList<CheckpointPosition>>)value;
  120. // break;
  121. // }
  122. // case "_unconditional_dependency_names":
  123. // {
  124. // _unconditional_dependency_names = (IDictionary<string, Trackable>)value;
  125. // break;
  126. // }
  127. // case "SelfSaveableObjectFactories":
  128. // {
  129. // SelfSaveableObjectFactories = (IDictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>>)value;
  130. // break;
  131. // }
  132. // case "UpdateUid":
  133. // {
  134. // UpdateUid = (int)value;
  135. // break;
  136. // }
  137. // default:
  138. // {
  139. // CustomizedAttributes[name] = value;
  140. // break;
  141. // }
  142. // }
  143. }
  144. /// <summary>
  145. /// Restore-on-create for a variable be saved with this `Checkpointable`.
  146. /// </summary>
  147. /// <returns></returns>
  148. protected virtual IVariableV1 _add_variable_with_custom_getter(VariableArgs args)
  149. {
  150. tf_with(ops.init_scope(), delegate
  151. {
  152. #pragma warning disable CS0219 // Variable is assigned but its value is never used
  153. IInitializer checkpoint_initializer = null;
  154. #pragma warning restore CS0219 // Variable is assigned but its value is never used
  155. if (tf.Context.executing_eagerly())
  156. #pragma warning disable CS0642 // Possible mistaken empty statement
  157. ;
  158. #pragma warning restore CS0642 // Possible mistaken empty statement
  159. else
  160. checkpoint_initializer = null;
  161. });
  162. var new_variable = args.Getter(args);
  163. // If we set an initializer and the variable processed it, tracking will not
  164. // assign again. It will add this variable to our dependencies, and if there
  165. // is a non-trivial restoration queued, it will handle that. This also
  166. // handles slot variables.
  167. if (!args.Overwrite || new_variable is RefVariable || new_variable is Trackable)
  168. {
  169. var temp = new_variable as Trackable;
  170. var res = _track_trackable(temp, args.Name, args.Overwrite);
  171. Debug.Assert(res is IVariableV1);
  172. return res as IVariableV1;
  173. }
  174. else
  175. return new_variable;
  176. }
  177. /// <summary>
  178. /// Pop and load any deferred checkpoint restores into `trackable`.
  179. /// </summary>
  180. /// <param name="name"></param>
  181. /// <param name="trackable"></param>
  182. protected void _handle_deferred_dependencies(string name, IVariableV1 trackable)
  183. {
  184. _maybe_initialize_trackable();
  185. // TODO
  186. }
  187. protected IVariableV1 _track_checkpointable(IVariableV1 checkpointable, string name, bool overwrite = false)
  188. {
  189. return checkpointable;
  190. }
  191. /// <summary>
  192. /// Initialize dependency management.
  193. /// </summary>
  194. public void _maybe_initialize_trackable()
  195. {
  196. if(_unconditional_checkpoint_dependencies is not null)
  197. {
  198. return;
  199. }
  200. _self_update_uid = -1;
  201. _unconditional_checkpoint_dependencies = new List<TrackableReference>();
  202. _unconditional_dependency_names = new Dictionary<string, Trackable>();
  203. _unconditional_deferred_dependencies = new Dictionary<string, IList<CheckpointPosition>>();
  204. }
  205. public virtual IDictionary<string, Trackable> _trackable_children(SaveType save_type = SaveType.CHECKPOINT,
  206. IDictionary<string, IDictionary<Trackable, ISerializedAttributes>>? cache = null)
  207. {
  208. _maybe_initialize_trackable();
  209. return _unconditional_checkpoint_dependencies.ToDictionary(x => x.Name, x => x.Refer);
  210. }
  211. public virtual Trackable _track_trackable(Trackable trackable, string name, bool overwrite = false)
  212. {
  213. _maybe_initialize_trackable();
  214. if (!_manual_tracking) return trackable;
  215. var new_reference = new TrackableReference(name, trackable);
  216. var current_object = _lookup_dependency(name);
  217. if(current_object is null)
  218. {
  219. _unconditional_checkpoint_dependencies.Add(new_reference);
  220. _handle_deferred_dependencies(name, trackable);
  221. }
  222. _unconditional_dependency_names[name] = trackable;
  223. return trackable;
  224. }
  225. /// <summary>
  226. /// Pop and load any deferred checkpoint restores into `trackable`.
  227. /// This method does not add a new dependency on `trackable`, but it does check if any outstanding/deferred dependencies have been queued waiting for
  228. /// this dependency to be added (matched based on `name`). If so, `trackable` and its dependencies are restored. The restorations are
  229. /// considered fulfilled and so are deleted.
  230. /// `_track_trackable` is more appropriate for adding a normal/unconditional dependency, and includes handling for deferred restorations.
  231. /// This method allows objects such as `Optimizer` to use the same restoration logic while managing conditional dependencies themselves,
  232. /// by overriding `_checkpoint_dependencies` and `_lookup_dependency` to change the object's dependencies based on the context
  233. /// it is saved/restored in (a single optimizer instance can have state associated with multiple graphs).
  234. /// </summary>
  235. /// <param name="name"></param>
  236. /// <param name="trackable"></param>
  237. public virtual void _handle_deferred_dependencies(string name, Trackable trackable)
  238. {
  239. _maybe_initialize_trackable();
  240. trackable._maybe_initialize_trackable();
  241. if(_unconditional_deferred_dependencies.TryGetValue(name, out var dependencies))
  242. {
  243. _unconditional_deferred_dependencies.Remove(name);
  244. foreach(var checkpoint_position in dependencies.OrderByDescending(x => x.Checkpoint.RestoreUid))
  245. {
  246. checkpoint_position.restore(trackable);
  247. }
  248. }
  249. // TODO(Rinne): deal with `_self_name_based_restores`
  250. }
  251. public virtual Trackable? _lookup_dependency(string name)
  252. {
  253. if (_unconditional_dependency_names.TryGetValue(name, out var dependency)) return dependency;
  254. else return null;
  255. }
  256. public static Trackable convert_to_trackable(object obj, object? parent = null)
  257. {
  258. if (obj is Trackable)
  259. {
  260. return (Trackable)obj;
  261. }
  262. else
  263. {
  264. throw new NotImplementedException();
  265. }
  266. }
  267. public virtual IDictionary<string, Trackable> deserialization_dependencies(IDictionary<string, Trackable> children)
  268. {
  269. return new Dictionary<string, Trackable>();
  270. }
  271. public virtual (IDictionary<Trackable, Trackable>, IDictionary<Tensor, Tensor>) map_resources(
  272. SaveOptions? save_options)
  273. {
  274. return (new Dictionary<Trackable, Trackable>(), new Dictionary<Tensor, Tensor>());
  275. }
  276. public virtual List<Tensor> export_to_saved_model_graph(IDictionary<Trackable, Trackable> object_map,
  277. IDictionary<Tensor, Tensor> tensor_map, SaveOptions? options = null)
  278. {
  279. var (self_object_map, self_tensor_map) = map_resources(options);
  280. foreach (var pair in self_object_map)
  281. {
  282. object_map.Add(pair);
  283. }
  284. foreach (var pair in self_tensor_map)
  285. {
  286. tensor_map.Add(pair);
  287. }
  288. return self_tensor_map.Keys.ToList();
  289. }
  290. public virtual IDictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>> gather_saveables_for_checkpoint()
  291. {
  292. OneOf<BaseResourceVariable, MySaveableObject> create_saveable(string name = "")
  293. {
  294. throw new NotImplementedException();
  295. //return new TrackableSaveable(this, null, name, null, null);
  296. }
  297. if (saveable_object_util.trackable_has_serialize_to_tensor(this))
  298. {
  299. // TODO: complete the implementation (need to complete the class `saveable_object_util.TrackableSaveable`).
  300. Dictionary<string, Func<string, OneOf<BaseResourceVariable, MySaveableObject>>> res = new();
  301. res[""] = create_saveable;
  302. return res;
  303. }
  304. else
  305. {
  306. return _self_saveable_object_factories;
  307. }
  308. }
  309. /// <summary>
  310. /// Gathers tensors to save to the checkpoint. You should only override `serialize_to_tensors` and `restore_from_tensors`
  311. /// if you are defining a custom resource or variable with custom ops.
  312. /// Otherwise, please store the state of your trackable in `tf.Variable` objects
  313. /// and add them to Trackable object hierarchy using `setattr` (for subclasses
  314. /// of `AutoTrackable`) or overriding the `_trackable_children` method.
  315. /// </summary>
  316. /// <returns></returns>
  317. /// <exception cref="NotImplementedException"></exception>
  318. public virtual IDictionary<string, IDictionary<string, OneOf<Tensor, SaveSpec>>> serialize_to_tensors()
  319. {
  320. throw new NotImplementedException();
  321. }
  322. public virtual IDictionary<string, Operation> _restore_from_tensors(IDictionary<string, OneOf<Tensor, IDictionary<string, Tensor>>> restored_tensors)
  323. {
  324. throw new NotImplementedException();
  325. }
  326. }
  327. public record class TrackableReference(string Name, Trackable Refer);
  328. public record class SlotVariableRestoration(int OptimizerId, int SlotVariableId, string SlotName);
  329. }