| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ea5d35e commit ff51917
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,9 +77,9 @@ private T _as_graph_element_locked<T>(T obj, bool allow_tensor = true, bool allo | |||
| 77 | 77 | ||
| 78 | 78 | var temp_obj = _as_graph_element(obj); | |
| 79 | 79 | ||
| 80 | - if(obj is Tensor && allow_tensor) | ||
| 80 | + if (obj is Tensor tensor && allow_tensor) | ||
| 81 | 81 | { | |
| 82 | - if ((obj as Tensor).Graph.Equals(this)) | ||
| 82 | + if (tensor.Graph.Equals(this)) | ||
| 83 | 83 | { | |
| 84 | 84 | return obj; | |
| 85 | 85 | } | |
@@ -88,6 +88,17 @@ private T _as_graph_element_locked<T>(T obj, bool allow_tensor = true, bool allo | |||
| 88 | 88 | throw new Exception($"Tensor {obj} is not an element of this graph."); | |
| 89 | 89 | } | |
| 90 | 90 | } | |
| 91 | + else if (obj is Operation op && allow_operation) | ||
| 92 | + { | ||
| 93 | + if (op.Graph.Equals(this)) | ||
| 94 | + { | ||
| 95 | + return obj; | ||
| 96 | + } | ||
| 97 | + else | ||
| 98 | + { | ||
| 99 | + throw new Exception($"Operation {obj} is not an element of this graph."); | ||
| 100 | + } | ||
| 101 | + } | ||
| 91 | 102 | ||
| 92 | 103 | throw new Exception($"Can not convert a {typeof(T).Name} into a {types_str}."); | |
| 93 | 104 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Generic; | |
| 3 | + using System.Linq; | ||
| 3 | 4 | using System.Text; | |
| 4 | 5 | ||
| 5 | 6 | namespace Tensorflow | |
@@ -10,14 +11,35 @@ public static Operation group(List<Operation> inputs, string name = "") | |||
| 10 | 11 | { | |
| 11 | 12 | using(var namescope = new ops.name_scope<Operation>(name, "group_deps", inputs)) | |
| 12 | 13 | { | |
| 14 | + name = namescope; | ||
| 15 | + | ||
| 16 | + var ops_on_device = new Dictionary<string, Operation[]>(); | ||
| 17 | + | ||
| 13 | 18 | // Sorts *inputs according to their devices. | |
| 19 | + foreach (var inp in inputs) | ||
| 20 | + { | ||
| 21 | + ops_on_device[inp.Device] = new Operation[] { inp }; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + // 1-level tree. The root node is the returned NoOp node. | ||
| 25 | + if (ops_on_device.Count == 1) | ||
| 26 | + { | ||
| 27 | + return _GroupControlDeps(ops_on_device.Keys.First(), ops_on_device.Values.First(), name); | ||
| 28 | + } | ||
| 14 | 29 | ||
| 15 | - return _GroupControlDeps("", name); | ||
| 30 | + // 2-level tree. The root node is the returned NoOp node. | ||
| 31 | + // deps contains 1 NoOp node for each device. | ||
| 32 | + return null; | ||
| 16 | 33 | } | |
| 17 | 34 | } | |
| 18 | 35 | ||
| 19 | - private static Operation _GroupControlDeps(string dev, string name = "") | ||
| 36 | + private static Operation _GroupControlDeps(string dev, Operation[] deps, string name = "") | ||
| 20 | 37 | { | |
| 38 | + if (string.IsNullOrEmpty(dev)) | ||
| 39 | + { | ||
| 40 | + return gen_control_flow_ops.no_op(name); | ||
| 41 | + } | ||
| 42 | + | ||
| 21 | 43 | return null; | |
| 22 | 44 | } | |
| 23 | 45 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow | ||
| 6 | + { | ||
| 7 | + public class gen_control_flow_ops | ||
| 8 | + { | ||
| 9 | + public static OpDefLibrary _op_def_lib = new OpDefLibrary(); | ||
| 10 | + | ||
| 11 | + public static Operation no_op(string name = "") | ||
| 12 | + { | ||
| 13 | + var _op = _op_def_lib._apply_op_helper("NoOp", name); | ||
| 14 | + | ||
| 15 | + return _op; | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,7 +40,12 @@ public virtual NDArray run(Tensor fetches, Dictionary<Tensor, NDArray> feed_dict | |||
| 40 | 40 | return _run(fetches, feed_dict); | |
| 41 | 41 | } | |
| 42 | 42 | ||
| 43 | - private NDArray _run(Tensor fetches, Dictionary<Tensor, NDArray> feed_dict = null) | ||
| 43 | + public virtual NDArray run(Operation fetches, Dictionary<Tensor, NDArray> feed_dict = null) | ||
| 44 | + { | ||
| 45 | + return _run(fetches, feed_dict); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + private NDArray _run<T>(T fetches, Dictionary<Tensor, NDArray> feed_dict = null) | ||
| 44 | 49 | { | |
| 45 | 50 | var feed_dict_tensor = new Dictionary<Tensor, NDArray>(); | |
| 46 | 51 | ||
@@ -53,7 +58,7 @@ private NDArray _run(Tensor fetches, Dictionary<Tensor, NDArray> feed_dict = nul | |||
| 53 | 58 | } | |
| 54 | 59 | ||
| 55 | 60 | // Create a fetch handler to take care of the structure of fetches. | |
| 56 | - var fetch_handler = new _FetchHandler(_graph, fetches, feed_dict_tensor); | ||
| 61 | + var fetch_handler = new _FetchHandler<T>(_graph, fetches, feed_dict_tensor); | ||
| 57 | 62 | ||
| 58 | 63 | // Run request and get response. | |
| 59 | 64 | // We need to keep the returned movers alive for the following _do_run(). | |
@@ -65,20 +70,34 @@ private NDArray _run(Tensor fetches, Dictionary<Tensor, NDArray> feed_dict = nul | |||
| 65 | 70 | ||
| 66 | 71 | // We only want to really perform the run if fetches or targets are provided, | |
| 67 | 72 | // or if the call is a partial run that specifies feeds. | |
| 68 | - var results = _do_run(final_fetches, feed_dict_tensor); | ||
| 73 | + var results = _do_run(final_targets.Select(x => (Operation)(object)x).ToList(), final_fetches, feed_dict_tensor); | ||
| 69 | 74 | ||
| 70 | 75 | return fetch_handler.build_results(null, results); | |
| 71 | 76 | } | |
| 72 | 77 | ||
| 73 | - private NDArray[] _do_run(List<Tensor> fetch_list, Dictionary<Tensor, NDArray> feed_dict) | ||
| 78 | + /// <summary> | ||
| 79 | + /// Runs a step based on the given fetches and feeds. | ||
| 80 | + /// </summary> | ||
| 81 | + /// <typeparam name="T"></typeparam> | ||
| 82 | + /// <param name="target_list">A list of operations to be run, but not fetched.</param> | ||
| 83 | + /// <param name="fetch_list"></param> | ||
| 84 | + /// <param name="feed_dict"></param> | ||
| 85 | + /// <returns> | ||
| 86 | + /// A list of numpy ndarrays, corresponding to the elements of | ||
| 87 | + /// `fetch_list`. If the ith element of `fetch_list` contains the | ||
| 88 | + /// name of an operation, the first Tensor output of that operation | ||
| 89 | + /// will be returned for that element. | ||
| 90 | + /// </returns> | ||
| 91 | + private NDArray[] _do_run(List<Operation> target_list, List<Tensor> fetch_list, Dictionary<Tensor, NDArray> feed_dict) | ||
| 74 | 92 | { | |
| 75 | 93 | var feeds = feed_dict.Select(x => new KeyValuePair<TF_Output, Tensor>(x.Key._as_tf_output(), new Tensor(x.Value))).ToArray(); | |
| 76 | 94 | var fetches = fetch_list.Select(x => x._as_tf_output()).ToArray(); | |
| 95 | + var targets = target_list; | ||
| 77 | 96 | ||
| 78 | - return _call_tf_sessionrun(feeds, fetches); | ||
| 97 | + return _call_tf_sessionrun(feeds, fetches, target_list); | ||
| 79 | 98 | } | |
| 80 | 99 | ||
| 81 | - private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] feed_dict, TF_Output[] fetch_list) | ||
| 100 | + private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] feed_dict, TF_Output[] fetch_list, List<Operation> target_list) | ||
| 82 | 101 | { | |
| 83 | 102 | // Ensure any changes to the graph are reflected in the runtime. | |
| 84 | 103 | _extend_graph(); | |
@@ -95,8 +114,8 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f | |||
| 95 | 114 | outputs: fetch_list, | |
| 96 | 115 | output_values: output_values, | |
| 97 | 116 | noutputs: fetch_list.Length, | |
| 98 | - target_opers: IntPtr.Zero, | ||
| 99 | - ntargets: 0, | ||
| 117 | + target_opers: target_list.Select(f => (IntPtr)f).ToArray(), | ||
| 118 | + ntargets: target_list.Count, | ||
| 100 | 119 | run_metadata: IntPtr.Zero, | |
| 101 | 120 | status: status); | |
| 102 | 121 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,26 +8,37 @@ namespace Tensorflow | |||
| 8 | 8 | /// <summary> | |
| 9 | 9 | /// Fetch mapper for singleton tensors and ops. | |
| 10 | 10 | /// </summary> | |
| 11 | - public class _ElementFetchMapper : _FetchMapper | ||
| 11 | + public class _ElementFetchMapper<T> : _FetchMapper<T> | ||
| 12 | 12 | { | |
| 13 | - private List<Object> _unique_fetches = new List<object>(); | ||
| 14 | - private Action _contraction_fn; | ||
| 13 | + private List<object> _unique_fetches = new List<object>(); | ||
| 14 | + private Func<List<object>> _contraction_fn; | ||
| 15 | 15 | ||
| 16 | - public _ElementFetchMapper(List<Tensor> fetches, Action contraction_fn) | ||
| 16 | + public _ElementFetchMapper(List<T> fetches, Func<List<object>> contraction_fn) | ||
| 17 | 17 | { | |
| 18 | - foreach(var tensor in fetches) | ||
| 18 | + foreach(var fetch in fetches) | ||
| 19 | 19 | { | |
| 20 | - var fetch = ops.get_default_graph().as_graph_element(tensor, allow_tensor: true, allow_operation: true); | ||
| 21 | - _unique_fetches.Add(fetch); | ||
| 20 | + var g = ops.get_default_graph(); | ||
| 21 | + var el = g.as_graph_element(fetch, allow_tensor: true, allow_operation: true); | ||
| 22 | + _unique_fetches.Add(el); | ||
| 22 | 23 | } | |
| 24 | + | ||
| 25 | + _contraction_fn = contraction_fn; | ||
| 23 | 26 | } | |
| 24 | 27 | ||
| 25 | - public NDArray build_results(NDArray[] values) | ||
| 28 | + /// <summary> | ||
| 29 | + /// Build results matching the original fetch shape. | ||
| 30 | + /// </summary> | ||
| 31 | + /// <param name="values"></param> | ||
| 32 | + /// <returns></returns> | ||
| 33 | + public NDArray build_results(List<object> values) | ||
| 26 | 34 | { | |
| 27 | - return values[0]; | ||
| 35 | + if (values.Count == 0) | ||
| 36 | + return null; | ||
| 37 | + else | ||
| 38 | + return _contraction_fn(values); | ||
| 28 | 39 | } | |
| 29 | 40 | ||
| 30 | - public List<Object> unique_fetches() | ||
| 41 | + public List<object> unique_fetches() | ||
| 31 | 42 | { | |
| 32 | 43 | return _unique_fetches; | |
| 33 | 44 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,21 +8,26 @@ namespace Tensorflow | |||
| 8 | 8 | /// <summary> | |
| 9 | 9 | /// Handler for structured fetches. | |
| 10 | 10 | /// </summary> | |
| 11 | - public class _FetchHandler | ||
| 11 | + public class _FetchHandler<T> | ||
| 12 | 12 | { | |
| 13 | - private _ElementFetchMapper _fetch_mapper; | ||
| 13 | + private _ElementFetchMapper<T> _fetch_mapper; | ||
| 14 | 14 | private List<Tensor> _fetches = new List<Tensor>(); | |
| 15 | 15 | private List<bool> _ops = new List<bool>(); | |
| 16 | 16 | private List<Tensor> _final_fetches = new List<Tensor>(); | |
| 17 | - private List<object> _targets = new List<object>(); | ||
| 17 | + private List<T> _targets = new List<T>(); | ||
| 18 | 18 | ||
| 19 | - public _FetchHandler(Graph graph, Tensor fetches, Dictionary<Tensor, NDArray> feeds = null, object feed_handles = null) | ||
| 19 | + public _FetchHandler(Graph graph, T fetches, Dictionary<Tensor, NDArray> feeds = null, object feed_handles = null) | ||
| 20 | 20 | { | |
| 21 | - _fetch_mapper = new _FetchMapper().for_fetch(fetches); | ||
| 21 | + _fetch_mapper = new _FetchMapper<T>().for_fetch(fetches); | ||
| 22 | 22 | foreach(var fetch in _fetch_mapper.unique_fetches()) | |
| 23 | 23 | { | |
| 24 | 24 | switch (fetch) | |
| 25 | 25 | { | |
| 26 | + case Operation val: | ||
| 27 | + _assert_fetchable(graph, val); | ||
| 28 | + _targets.Add((T)(object)val); | ||
| 29 | + _ops.Add(true); | ||
| 30 | + break; | ||
| 26 | 31 | case Tensor val: | |
| 27 | 32 | _assert_fetchable(graph, val.op); | |
| 28 | 33 | _fetches.Add(val); | |
@@ -35,9 +40,19 @@ public _FetchHandler(Graph graph, Tensor fetches, Dictionary<Tensor, NDArray> fe | |||
| 35 | 40 | _final_fetches = _fetches; | |
| 36 | 41 | } | |
| 37 | 42 | ||
| 38 | - public NDArray build_results(Session session, NDArray[] results) | ||
| 43 | + public NDArray build_results(Session session, NDArray[] tensor_values) | ||
| 39 | 44 | { | |
| 40 | - return _fetch_mapper.build_results(results); | ||
| 45 | + var full_values = new List<object>(); | ||
| 46 | + | ||
| 47 | + foreach(var is_op in _ops) | ||
| 48 | + { | ||
| 49 | + if (is_op) | ||
| 50 | + { | ||
| 51 | + full_values.Add(null); | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + return _fetch_mapper.build_results(full_values); | ||
| 41 | 56 | } | |
| 42 | 57 | ||
| 43 | 58 | private void _assert_fetchable(Graph graph, Operation op) | |
@@ -53,7 +68,7 @@ public List<Tensor> fetches() | |||
| 53 | 68 | return _final_fetches; | |
| 54 | 69 | } | |
| 55 | 70 | ||
| 56 | - public List<Object> targets() | ||
| 71 | + public List<T> targets() | ||
| 57 | 72 | { | |
| 58 | 73 | return _targets; | |
| 59 | 74 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,13 +4,13 @@ | |||
| 4 | 4 | ||
| 5 | 5 | namespace Tensorflow | |
| 6 | 6 | { | |
| 7 | - public class _FetchMapper | ||
| 7 | + public class _FetchMapper<T> | ||
| 8 | 8 | { | |
| 9 | - public _ElementFetchMapper for_fetch(Tensor fetch) | ||
| 9 | + public _ElementFetchMapper<T> for_fetch(T fetch) | ||
| 10 | 10 | { | |
| 11 | - var fetches = new List<Tensor> { fetch }; | ||
| 11 | + var fetches = new List<T> { fetch }; | ||
| 12 | 12 | ||
| 13 | - return new _ElementFetchMapper(fetches, null); | ||
| 13 | + return new _ElementFetchMapper<T>(fetches, null); | ||
| 14 | 14 | } | |
| 15 | 15 | } | |
| 16 | 16 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,7 +87,7 @@ public partial class c_api | |||
| 87 | 87 | public static extern unsafe void TF_SessionRun(IntPtr session, TF_Buffer* run_options, | |
| 88 | 88 | TF_Output[] inputs, IntPtr[] input_values, int ninputs, | |
| 89 | 89 | TF_Output[] outputs, IntPtr[] output_values, int noutputs, | |
| 90 | - IntPtr target_opers, int ntargets, | ||
| 90 | + IntPtr[] target_opers, int ntargets, | ||
| 91 | 91 | IntPtr run_metadata, | |
| 92 | 92 | IntPtr status); | |
| 93 | 93 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,7 @@ public static List<RefVariable> global_variables(string scope = "") | |||
| 42 | 42 | /// <returns>An Op that run the initializers of all the specified variables.</returns> | |
| 43 | 43 | public static Operation variables_initializer(RefVariable[] var_list, string name = "init") | |
| 44 | 44 | { | |
| 45 | - return control_flow_ops.group(var_list.Select(x => x.initializer).ToList()); | ||
| 45 | + return control_flow_ops.group(var_list.Select(x => x.initializer).ToList(), name); | ||
| 46 | 46 | } | |
| 47 | 47 | } | |
| 48 | 48 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments