Skip to content

Commit 459abbc

Browse files
committed
restore SaveableObject.
1 parent b87aadc commit 459abbc

8 files changed

Lines changed: 83 additions & 10 deletions

File tree

src/TensorFlowNET.Core/IReturnTensorOrOperation.cs renamed to src/TensorFlowNET.Core/ITensorOrOperation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace Tensorflow
88
/// in order to limit function return value
99
/// is Tensor or Operation
1010
/// </summary>
11-
public interface IReturnTensorOrOperation
11+
public interface ITensorOrOperation
1212
{
13+
string Device { get; }
1314
}
1415
}

‎src/TensorFlowNET.Core/Operations/Operation.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Tensorflow
99
{
10-
public partial class Operation : IReturnTensorOrOperation
10+
public partial class Operation : ITensorOrOperation
1111
{
1212
private readonly IntPtr _handle; // _c_op in python
1313

‎src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@ namespace Tensorflow
77
{
88
public class control_flow_ops
99
{
10-
public static Operation group(Operation[] inputs, string name = "")
10+
public static Operation group<T>(T[] inputs, string name = "") where T : ITensorOrOperation
1111
{
1212
return Python.with<ops.name_scope, Operation>(new ops.name_scope(name, "group_deps", inputs), scope =>
1313
{
1414
name = scope;
1515

1616
// Sorts *inputs according to their devices.
17-
var ops_on_device = new Dictionary<string, List<Operation>>();
17+
var ops_on_device = new Dictionary<string, List<T>>();
1818
foreach (var inp in inputs)
1919
{
2020
if (ops_on_device.ContainsKey(inp.Device))
2121
ops_on_device[inp.Device].Add(inp);
2222
else
23-
ops_on_device[inp.Device] = new List<Operation> { inp };
23+
ops_on_device[inp.Device] = new List<T> { inp };
2424
}
2525

2626
// 1-level tree. The root node is the returned NoOp node.
2727
if (ops_on_device.Count == 1)
2828
{
2929
var dev = ops_on_device.Keys.First();
3030
var deps = ops_on_device.Values.First();
31-
return _GroupControlDeps(dev, deps.ToArray(), name);
31+
if (typeof(T).Name == "Operation")
32+
return _GroupControlDeps(dev, deps.Select(x => x as Operation).ToArray(), name);
33+
else
34+
throw new NotImplementedException("control_flow_ops.group");
3235
}
3336

3437
// 2-level tree. The root node is the returned NoOp node.
3538
// deps contains 1 NoOp node for each device.
36-
return null;
39+
throw new NotImplementedException("control_flow_ops.group");
3740
});
3841
}
3942

‎src/TensorFlowNET.Core/Operations/gen_io_ops.py.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ public static Operation save_v2(Tensor prefix, string[] tensor_names, string[] s
1414

1515
return _op;
1616
}
17+
18+
public static Tensor[] restore_v2(Tensor prefix, string[] tensor_names, string[] shape_and_slices, TF_DataType[] dtypes, string name = "")
19+
{
20+
var _op = _op_def_lib._apply_op_helper("RestoreV2", name: name, args: new { prefix, tensor_names, shape_and_slices, dtypes });
21+
22+
return _op.outputs;
23+
}
1724
}
1825
}

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Tensorflow
1212
/// A tensor is a generalization of vectors and matrices to potentially higher dimensions.
1313
/// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.
1414
/// </summary>
15-
public partial class Tensor : IDisposable, IReturnTensorOrOperation
15+
public partial class Tensor : IDisposable, ITensorOrOperation
1616
{
1717
private readonly IntPtr _handle;
1818

@@ -175,6 +175,9 @@ public Tensor(Operation op, int value_index, TF_DataType dtype)
175175
}
176176

177177
public Operation[] Consumers => consumers();
178+
179+
public string Device => op.Device;
180+
178181
public Operation[] consumers()
179182
{
180183
var output = _as_tf_output();

‎src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ public virtual Operation save_op(Tensor filename_tensor, SaveableObject[] saveab
4242

4343
public virtual Tensor[] bulk_restore(Tensor filename_tensor, SaveableObject[] saveables, int preferred_shard, bool restore_sequentially)
4444
{
45-
throw new NotImplementedException();
45+
var names = new List<string>();
46+
var slices = new List<string>();
47+
var dtypes = new List<TF_DataType>();
48+
foreach (var saveable in saveables)
49+
foreach (var spec in saveable.specs)
50+
{
51+
names.Add(spec.name);
52+
slices.Add(spec.slice_spec);
53+
dtypes.Add(spec.dtype);
54+
}
55+
56+
return gen_io_ops.restore_v2(filename_tensor, names.ToArray(), slices.ToArray(), dtypes.ToArray());
4657
}
4758

4859
public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
@@ -83,6 +94,9 @@ public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
8394
{
8495
if (build_save)
8596
_AddSaveOps(filename_tensor, saveables);
97+
98+
if (build_restore)
99+
_AddRestoreOps(filename_tensor, saveables, restore_sequentially, reshape);
86100
}
87101
});
88102

@@ -94,5 +108,42 @@ public Tensor _AddSaveOps(Tensor filename_tensor, SaveableObject[] saveables)
94108
var save = save_op(filename_tensor, saveables);
95109
return control_flow_ops.with_dependencies(new Operation[] { save }, filename_tensor);
96110
}
111+
112+
/// <summary>
113+
/// Add operations to restore saveables.
114+
/// </summary>
115+
/// <param name="filename_tensor"></param>
116+
/// <param name="saveables"></param>
117+
/// <param name="restore_sequentially"></param>
118+
/// <param name="reshape"></param>
119+
/// <param name="preferred_shard"></param>
120+
/// <param name="name"></param>
121+
/// <returns>An Operation that restores the variables.</returns>
122+
public Operation _AddRestoreOps(Tensor filename_tensor,
123+
SaveableObject[] saveables,
124+
bool restore_sequentially,
125+
bool reshape,
126+
int preferred_shard = -1,
127+
string name = "restore_all")
128+
{
129+
var all_tensors = bulk_restore(filename_tensor, saveables, preferred_shard, restore_sequentially);
130+
var assign_ops = new List<Tensor>();
131+
int idx = 0;
132+
133+
foreach(var saveable in saveables)
134+
{
135+
List<TensorShape> shapes = null;
136+
if (reshape)
137+
{
138+
throw new NotImplementedException("_AddRestoreOps");
139+
}
140+
141+
var saveable_tensors = all_tensors.Skip(idx).Take(saveable.specs.Length);
142+
idx += saveable.specs.Length;
143+
assign_ops.Add(saveable.restore(saveable_tensors.ToArray(), shapes == null ? null : shapes.ToArray()));
144+
}
145+
146+
return control_flow_ops.group(assign_ops.ToArray(), name: name);
147+
}
97148
}
98149
}

‎src/TensorFlowNET.Core/Train/Saving/SaveableObject.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,13 @@ public SaveableObject(Tensor op, SaveSpec[] specs, string name)
2727
this.specs = specs;
2828
this.name = name;
2929
}
30+
31+
public virtual Tensor restore(Tensor[] restored_tensors, TensorShape[] restored_shapes = null)
32+
{
33+
var restored_tensor = restored_tensors[0];
34+
return gen_state_ops.assign(op,
35+
restored_tensor,
36+
validate_shape: restored_shapes == null && tensor_util.to_shape(op.shape).is_fully_defined());
37+
}
3038
}
3139
}

‎src/TensorFlowNET.Core/Variables/RefVariable.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private Operation _safe_initial_value_from_op(Operation op, Dictionary<string, O
186186
/// the assignment has completed.
187187
/// </returns>
188188
public T assign<T>(Tensor value, bool use_locking = false, string name = "", bool read_value = true)
189-
where T : IReturnTensorOrOperation
189+
where T : ITensorOrOperation
190190
{
191191
var assign = gen_state_ops.assign(_variable, value, use_locking: use_locking, name: name);
192192
if (read_value)

0 commit comments

Comments
 (0)