-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTensor.cs
More file actions
246 lines (207 loc) · 7.19 KB
/
Tensor.cs
File metadata and controls
246 lines (207 loc) · 7.19 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
namespace TensorStack.Common.Tensor
{
/// <summary>
/// Tensor for long lifespan operations (heap allocated)
/// Implements the <see cref="IDisposable" />
/// </summary>
/// <typeparam name="T">Tensor element type</typeparam>
/// <seealso cref="IDisposable" />
public class Tensor<T> : IDisposable
{
private Memory<T> _memory;
private int[] _dimensions;
private int[] _strides;
/// <summary>
/// Initializes a new instance of the <see cref="Tensor{T}"/> class.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="dimensions">The dimensions.</param>
public Tensor(Memory<T> data, ReadOnlySpan<int> dimensions)
{
_memory = data;
_dimensions = dimensions.ToArray();
_strides = dimensions.GetStrides();
}
/// <summary>
/// Initializes a new instance of the <see cref="Tensor{T}"/> class.
/// </summary>
/// <param name="dimensions">The dimensions.</param>
public Tensor(ReadOnlySpan<int> dimensions)
: this(new T[dimensions.GetProduct()], dimensions) { }
/// <summary>
/// Initializes a new instance of the <see cref="Tensor{T}"/> class.
/// </summary>
/// <param name="dimensions">The dimensions.</param>
/// <param name="fillWith">The fill with.</param>
public Tensor(ReadOnlySpan<int> dimensions, T fillWith)
: this(dimensions)
{
Fill(fillWith);
}
/// <summary>
/// Gets element type.
/// </summary>
public Type Type => typeof(T);
/// <summary>
/// Gets the tensor memory buffer.
/// </summary>
public Memory<T> Memory => _memory;
/// <summary>
/// Gets the tensor buffer span.
/// </summary>
public ReadOnlySpan<T> Span => _memory.Span;
/// <summary>
/// Gets the length.
/// </summary>
public long Length => _memory.Length;
/// <summary>
/// Gets the strides.
/// </summary>
public ReadOnlySpan<int> Strides => _strides;
/// <summary>
/// Gets the dimensions/shape.
/// </summary>
public ReadOnlySpan<int> Dimensions => _dimensions;
/// <summary>
/// Gets the rank.
/// </summary>
public int Rank => _dimensions.Length;
/// <summary>
/// Gets or sets the <see cref="T"/> with the specified indices.
/// </summary>
/// <param name="indices">The indices.</param>
/// <returns>T.</returns>
/// <exception cref="ArgumentNullException"></exception>
public T this[params int[] indices]
{
get
{
ArgumentNullException.ThrowIfNull(indices);
var span = new ReadOnlySpan<int>(indices);
return this[span];
}
set
{
ArgumentNullException.ThrowIfNull(indices);
var span = new ReadOnlySpan<int>(indices);
this[span] = value;
}
}
/// <summary>
/// Gets or sets the <see cref="T"/> with the specified indices.
/// </summary>
/// <param name="indices">The indices.</param>
/// <returns>T.</returns>
public T this[ReadOnlySpan<int> indices]
{
get { return GetValue(indices.GetIndex(_strides)); }
set { SetValue(indices.GetIndex(_strides), value); }
}
/// <summary>
/// Gets the value with the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>T.</returns>
public T GetValue(int index)
{
return Memory.Span[index];
}
/// <summary>
/// Sets the value with the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="value">The value.</param>
public void SetValue(int index, T value)
{
Memory.Span[index] = value;
}
/// <summary>
/// Fills the tensor with the specified value.
/// </summary>
/// <param name="value">The value.</param>
public void Fill(T value)
{
for (int i = 0; i < Length; i++)
{
SetValue(i, value);
}
}
/// <summary>
/// Clones this instance.
/// </summary>
/// <returns>Tensor<T>.</returns>
public Tensor<T> Clone()
{
return new Tensor<T>(Memory.ToArray(), Dimensions.ToArray());
}
/// <summary>
/// Reshapes the Tensor with the specified dimensions.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tensor">The tensor.</param>
/// <param name="dimensions">The dimensions.</param>
/// <returns>Tensor<T>.</returns>
/// <exception cref="ArgumentException">Cannot reshape array due to mismatch in lengths - dimensions</exception>
public void ReshapeTensor(ReadOnlySpan<int> dimensions)
{
var newSize = dimensions.GetProduct();
if (newSize != Length)
throw new ArgumentException($"Cannot reshape array due to mismatch in lengths", nameof(dimensions));
_dimensions = dimensions.ToArray();
_strides = dimensions.GetStrides();
}
/// <summary>
/// Updates the the tensor data, Buffer.
/// </summary>
/// <param name="tensor">The tensor.</param>
protected void UpdateTensor(Tensor<T> tensor)
{
_memory = tensor.Memory;
_dimensions = tensor.Dimensions.ToArray();
_strides = Dimensions.GetStrides();
OnTensorDataChanged();
}
/// <summary>
/// Called when Tensor data has changed
/// </summary>
protected virtual void OnTensorDataChanged()
{
}
#region IDisposable
private bool disposed = false;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
_memory = null;
}
// Dispose unmanaged resources here (if any).
disposed = true;
}
/// <summary>
/// Finalizes an instance of the <see cref="ImageTensor"/> class.
/// </summary>
~Tensor()
{
Dispose(false);
}
#endregion
}
}