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.

Context.cs 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. namespace Tensorflow.Eager
  3. {
  4. public class Context : DisposableObject
  5. {
  6. public const int GRAPH_MODE = 0;
  7. public const int EAGER_MODE = 1;
  8. public int default_execution_mode;
  9. public string device_name = "";
  10. public string scope_name = "";
  11. bool _initialized = false;
  12. public Context(ContextOptions opts, Status status)
  13. {
  14. _handle = c_api.TFE_NewContext(opts, status);
  15. status.Check(true);
  16. }
  17. /// <summary>
  18. /// Initialize handle and devices if not already done so.
  19. /// </summary>
  20. public void ensure_initialized()
  21. {
  22. if (_initialized)
  23. return;
  24. _initialized = true;
  25. }
  26. public void start_step()
  27. => c_api.TFE_ContextStartStep(_handle);
  28. public void end_step()
  29. => c_api.TFE_ContextEndStep(_handle);
  30. /// <summary>
  31. /// Dispose any unmanaged resources related to given <paramref name="handle"/>.
  32. /// </summary>
  33. protected sealed override void DisposeUnmanagedResources(IntPtr handle)
  34. => c_api.TFE_DeleteContext(_handle);
  35. public bool executing_eagerly()
  36. => default_execution_mode == EAGER_MODE;
  37. public string shared_name(string name = null)
  38. => !string.IsNullOrEmpty(name) || !executing_eagerly() ?
  39. name :
  40. "cd2c89b7-88b7-44c8-ad83-06c2a9158347";
  41. public static implicit operator IntPtr(Context ctx)
  42. => ctx._handle;
  43. public static implicit operator TFE_Context(Context ctx)
  44. => new TFE_Context(ctx._handle);
  45. }
  46. }