|
| 1 | +/***************************************************************************** |
| 2 | + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using Tensorflow.Eager; |
| 19 | + |
| 20 | +namespace Tensorflow.Contexts |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Environment in which eager operations execute. |
| 24 | + /// </summary> |
| 25 | + public sealed class Context : IDisposable |
| 26 | + { |
| 27 | + public const int GRAPH_MODE = 0; |
| 28 | + public const int EAGER_MODE = 1; |
| 29 | + |
| 30 | + int defaultExecutionMode = EAGER_MODE; |
| 31 | + public string DeviceName { get; set; } = ""; |
| 32 | + public string ScopeName { get; set; } = ""; |
| 33 | + bool initialized = false; |
| 34 | + bool isEager; |
| 35 | + ContextSwitchStack contextSwitches; |
| 36 | + |
| 37 | + public SafeContextHandle Handle { get; } |
| 38 | + |
| 39 | + public Context(ContextOptions opts, Status status) |
| 40 | + { |
| 41 | + Handle = c_api.TFE_NewContext(opts.Handle, status.Handle); |
| 42 | + status.Check(true); |
| 43 | + isEager = defaultExecutionMode == EAGER_MODE; |
| 44 | + contextSwitches = new ContextSwitchStack(isEager); |
| 45 | + initialized = true; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Initialize handle and devices if not already done so. |
| 50 | + /// </summary> |
| 51 | + public void ensure_initialized() |
| 52 | + { |
| 53 | + if (initialized) |
| 54 | + return; |
| 55 | + initialized = true; |
| 56 | + } |
| 57 | + |
| 58 | + public void start_step() |
| 59 | + => c_api.TFE_ContextStartStep(Handle); |
| 60 | + |
| 61 | + public void end_step() |
| 62 | + => c_api.TFE_ContextEndStep(Handle); |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Checks whether the current thread has eager execution enabled. |
| 66 | + /// </summary> |
| 67 | + /// <returns></returns> |
| 68 | + public bool executing_eagerly() |
| 69 | + => isEager; |
| 70 | + |
| 71 | + public string shared_name(string name = null) |
| 72 | + => !string.IsNullOrEmpty(name) || !executing_eagerly() ? |
| 73 | + name : |
| 74 | + "cd2c89b7-88b7-44c8-ad83-06c2a9158347"; |
| 75 | + |
| 76 | + public void graph_mode() |
| 77 | + => mode(false); |
| 78 | + |
| 79 | + public void eager_mode() |
| 80 | + => mode(true); |
| 81 | + |
| 82 | + void mode(bool mode) |
| 83 | + { |
| 84 | + isEager = mode; |
| 85 | + } |
| 86 | + |
| 87 | + public void Dispose() |
| 88 | + => Handle.Dispose(); |
| 89 | + } |
| 90 | +} |
0 commit comments