Skip to content

Commit 460c04f

Browse files
committed
make op_id as int
1 parent 392fa09 commit 460c04f

File tree

11 files changed

+15
-73
lines changed

11 files changed

+15
-73
lines changed

src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Tensorflow.Util;
2-
using static Tensorflow.tensorflow;
32

43
namespace Tensorflow.Gradients
54
{
@@ -15,13 +14,13 @@ public class BackpropInitialState
1514
/// Maps from op ID to how many output tensors of this op still need to have
1615
/// their gradients computed.
1716
/// </summary>
18-
public UnorderedMap<Tensor, long> op_missing_tensor { get; set; }
17+
public UnorderedMap<long, long> op_missing_tensor { get; set; }
1918

2019
public BackpropInitialState()
2120
{
2221
op_tape = new OpTape();
2322
tensor_usage_counts = new UnorderedMap<Tensor, long>();
24-
op_missing_tensor = new UnorderedMap<Tensor, long>();
23+
op_missing_tensor = new UnorderedMap<long, long>();
2524
}
2625
}
2726
}

src/TensorFlowNET.Core/Gradients/OpTape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Tensorflow.Gradients
55
/// <summary>
66
/// Map from operation-id to tape entry.
77
/// </summary>
8-
public class OpTape : UnorderedMap<Tensor, OpTapeEntry>
8+
public class OpTape : UnorderedMap<long, OpTapeEntry>
99
{
1010

1111
}

src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Tensor[] ComputeGradient(Tensor[] target_tensor_ids,
144144
}
145145

146146
var op_id = tape_it;
147-
if (op_id == null)
147+
if (op_id == -1)
148148
continue;
149149

150150
if (state.op_missing_tensor.find(op_id, out var missing_it))
@@ -235,10 +235,10 @@ UnorderedMapEnumerable<Tensor, List<Tensor>> InitialGradients(Tensor[] target_te
235235
return result;
236236
}
237237

238-
Queue<Tensor> InitialStack(OpTape op_tape,
239-
UnorderedMap<Tensor, long> op_missing_tensor)
238+
Queue<long> InitialStack(OpTape op_tape,
239+
UnorderedMap<long, long> op_missing_tensor)
240240
{
241-
var result = new Queue<Tensor>();
241+
var result = new Queue<long>();
242242
foreach (var op_entry in op_tape)
243243
{
244244
if (!op_missing_tensor.find(op_entry.Key))

src/TensorFlowNET.Core/Gradients/Tape.PrepareBackprop.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using Tensorflow.Util;
3-
using static Tensorflow.tensorflow;
43

54
namespace Tensorflow.Gradients
65
{
@@ -21,7 +20,7 @@ public BackpropInitialState PrepareBackprop(Tensor[] target,
2120
if (!tensor_tape.find(tensor_id, out var op_id))
2221
continue;
2322

24-
if (op_id == null ||
23+
if (op_id == -1 ||
2524
!op_tape.find(op_id, out var op_it) ||
2625
result.op_tape.find(op_id, out var result_op_it))
2726
continue;
@@ -46,7 +45,7 @@ public BackpropInitialState PrepareBackprop(Tensor[] target,
4645

4746
foreach (var pair in result.tensor_usage_counts)
4847
{
49-
if (tensor_tape.find(pair.Key, out var it) && it != null)
48+
if (tensor_tape.find(pair.Key, out var it) && it != -1)
5049
result.op_missing_tensor[it] += 1;
5150
}
5251

src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Tensorflow.Util;
4-
using static Tensorflow.tensorflow;
54
using static Tensorflow.Binding;
6-
using System.Linq;
7-
using Tensorflow.Eager;
85

96
namespace Tensorflow.Gradients
107
{
@@ -21,7 +18,7 @@ public void RecordOperation(string op_type,
2118
if (!ShouldRecord(input_tensors))
2219
return;
2320

24-
var op_id = new EagerTensor(next_op_id_++);
21+
var op_id = next_op_id_++;
2522
foreach (var i in input_tensors)
2623
tensor_usage_[i]++;
2724

src/TensorFlowNET.Core/Gradients/Tape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Tape(bool persistent, bool watch_accessed_variables)
4242
public void Watch(Tensor x)
4343
{
4444
tf.Logger.Debug($"Watch tensor id={x.Id}, name={x.name}");
45-
tensor_tape_.emplace(x, null);
45+
tensor_tape_.emplace(x, -1);
4646
}
4747

4848
public bool ShouldRecord(Tensor[] tensors)

src/TensorFlowNET.Core/Gradients/TensorTape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Tensorflow.Gradients
77
/// produced this tensor. A value of -1 means that the tensor was directly
88
/// watched and not the result of any operation in the tape.
99
/// </summary>
10-
public class TensorTape : UnorderedMap<Tensor, Tensor>
10+
public class TensorTape : UnorderedMap<Tensor, long>
1111
{
1212

1313
}

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,6 @@ private static Tensor _ReductionDims(Tensor x, Tensor axis)
656656
}
657657
else
658658
{
659-
if (x.rank > -1 && tf.executing_eagerly())
660-
return constant_op.constant(np.arange(x.rank));
661-
662659
var rank = array_ops.rank(x);
663660
return range(0, rank, 1);
664661
}
@@ -678,11 +675,6 @@ private static Tensor _ReductionDims(Tensor x, Axis? axis)
678675
// we rely on Range and Rank to do the right thing at run-time.
679676
if (rank == -1) return range(0, array_ops.rank(x));
680677

681-
if (rank.HasValue && rank.Value > -1)
682-
{
683-
return constant_op.constant(np.arange(rank.Value), TF_DataType.TF_INT32);
684-
}
685-
686678
return range(0, rank, 1);
687679
}
688680
}

src/TensorFlowNET.Core/Tensorflow.Binding.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>2.2.0</TargetTensorFlow>
88
<Version>0.60.5</Version>
9-
<LangVersion>9.0</LangVersion>
9+
<LangVersion>10.0</LangVersion>
1010
<Nullable>enable</Nullable>
1111
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
1212
<Company>SciSharp STACK</Company>

src/TensorFlowNET.Core/Tensors/c_api.tensor.cs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,7 @@ public partial class c_api
7171
/// <param name="deallocator_arg"></param>
7272
/// <returns></returns>
7373
[DllImport(TensorFlowLibName)]
74-
public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, UIntPtr len, Deallocator deallocator, ref DeallocatorArgs deallocator_arg);
75-
76-
[DllImport(TensorFlowLibName)]
77-
public static extern TF_Tensor TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, long len, DeallocatorV2 deallocator, IntPtr args);
78-
79-
/// <summary>
80-
/// Return a new tensor that holds the bytes data[0,len-1]
81-
/// </summary>
82-
/// <param name="dataType"></param>
83-
/// <param name="dims"></param>
84-
/// <param name="num_dims"></param>
85-
/// <param name="data"></param>
86-
/// <param name="len">num_bytes, ex: 6 * sizeof(float)</param>
87-
/// <param name="deallocator"></param>
88-
/// <param name="deallocator_arg"></param>
89-
/// <returns></returns>
90-
[DllImport(TensorFlowLibName)]
91-
public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg);
92-
93-
/// <summary>
94-
/// Return a new tensor that holds the bytes data[0,len-1]
95-
/// </summary>
96-
/// <param name="dataType"></param>
97-
/// <param name="dims"></param>
98-
/// <param name="num_dims"></param>
99-
/// <param name="data"></param>
100-
/// <param name="len">num_bytes, ex: 6 * sizeof(float)</param>
101-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
102-
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len)
103-
{
104-
return TF_NewTensor(dataType, dims, num_dims, data, len, EmptyDeallocator, DeallocatorArgs.Empty);
105-
}
74+
public static extern SafeTensorHandle TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg);
10675

10776
public static unsafe SafeTensorHandle TF_NewTensor(byte[] data, Shape shape, TF_DataType dtype)
10877
{
@@ -137,20 +106,6 @@ public static unsafe SafeTensorHandle TF_NewTensor<T>(T value)
137106
return handle;
138107
}
139108

140-
/// <summary>
141-
/// Return a new tensor that holds the bytes data[0,len-1]
142-
/// </summary>
143-
/// <param name="dataType"></param>
144-
/// <param name="dims"></param>
145-
/// <param name="num_dims"></param>
146-
/// <param name="data"></param>
147-
/// <param name="len">num_bytes, ex: 6 * sizeof(float)</param>
148-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149-
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, void* data, ulong len)
150-
{
151-
return TF_NewTensor(dataType, dims, num_dims, new IntPtr(data), len);
152-
}
153-
154109
/// <summary>
155110
/// Return the number of dimensions that the tensor has.
156111
/// </summary>

0 commit comments

Comments
 (0)