forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTape.cs
More file actions
115 lines (103 loc) · 3.33 KB
/
Tape.cs
File metadata and controls
115 lines (103 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow.Util;
using static Tensorflow.Binding;
namespace Tensorflow.Gradients
{
public partial class Tape : ITape
{
int _id;
// static int tape_nesting_id_counter = 0;
bool _persistent;
public bool Persistent => _persistent;
bool _recording;
bool _created_eagerly;
TensorTape tensor_tape_;
OpTape op_tape_;
/// <summary>
/// A deque-backed stack, whose element references are not invalidated by
/// pushes and pops at the back.
/// </summary>
// Stack<AccumulatorCallState> call_state_;
public Tape(bool persistent, bool watch_accessed_variables)
{
_persistent = persistent;
_created_eagerly = tf.Context.executing_eagerly();
tensor_tape_ = new TensorTape();
op_tape_ = new OpTape();
tensor_usage_ = new UnorderedMap<Tensor, long>();
if(_created_eagerly)
tf.Context.start_step();
// nesting_id = ++tape_nesting_id_counter;
}
/// <summary>
/// Marks this tensor to be watched by the given tape.
/// </summary>
/// <param name="x"></param>
public void Watch(Tensor x)
{
tf.Logger.Debug($"Watch tensor id={x.Id}, name={x.name}");
tensor_tape_.emplace(x, -1);
}
public bool ShouldRecord(Tensor[] tensors)
{
var dtypes = tensors.Select(x => x.dtype).ToArray();
for (int i = 0; i < tensors.Length; ++i)
{
if (tensor_tape_.find(tensors[i]))
{
if (IsDtypeTrainable(dtypes[i]))
return true;
}
}
return false;
}
public void VariableAccessed(ResourceVariable variable)
{
Watch(variable.Handle);
}
public ResourceVariable[] WatchedVariables()
{
return null;
}
public bool IsDtypeTrainable(TF_DataType dtype)
{
switch (dtype)
{
case TF_DataType.TF_HALF:
case TF_DataType.TF_BFLOAT16:
case TF_DataType.TF_FLOAT:
case TF_DataType.TF_DOUBLE:
case TF_DataType.TF_COMPLEX64:
case TF_DataType.TF_COMPLEX128:
case TF_DataType.TF_RESOURCE:
case TF_DataType.TF_VARIANT:
return true;
default:
return false;
}
}
public void StartRecord()
{
if (_recording)
throw new ValueError("Tape is still recording, This can happen if you try to " +
"re-enter an already-active tape.");
_recording = true;
}
public void StopRecord()
{
if (!_recording)
throw new ValueError("Tape is not recording.");
if (_created_eagerly)
tf.Context.end_step();
_recording = false;
}
public void SetTapeId(int id)
{
_id = id;
}
public override string ToString()
=> $"Tape {_id} {(_recording ? "Recording" : "Stopped")}";
}
}