-
Notifications
You must be signed in to change notification settings - Fork 241
/
TensorsInfo.cs
executable file
·607 lines (514 loc) · 22.9 KB
/
TensorsInfo.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq;
using System.Collections.Generic;
namespace Tizen.MachineLearning.Inference
{
/// <summary>
/// The TensorsInfo class manages each Tensor information such as Name, Type and Dimension.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public class TensorsInfo : IDisposable, IEquatable<TensorsInfo>
{
private List<TensorInfo> _infoList;
private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
/// <summary>
/// Get the number of Tensor information which is added.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public int Count => _infoList.Count;
/// <summary>
/// Creates a TensorsInfo instance.
/// </summary>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public TensorsInfo()
{
NNStreamer.CheckNNStreamerSupport();
Log.Info(NNStreamer.TAG, "TensorsInfo is created");
_infoList = new List<TensorInfo>();
}
/// <summary>
/// Destroys the TensorsInfo resource.
/// </summary>
/// <since_tizen> 6 </since_tizen>
~TensorsInfo()
{
Dispose(false);
}
/// <summary>
/// Add a Tensor information to the TensorsInfo instance. Note that we support up to 16 tensors in TensorsInfo.
/// </summary>
/// <param name="type">Data element type of Tensor.</param>
/// <param name="dimension">Dimension of Tensor. Note that we support up to 4th ranks.</param>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="IndexOutOfRangeException">Thrown when the number of Tensor already exceeds the size limits (i.e. Tensor.SizeLimit)</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public void AddTensorInfo(TensorType type, int[] dimension)
{
NNStreamer.CheckNNStreamerSupport();
AddTensorInfo(null, type, dimension);
}
/// <summary>
/// Add a Tensor information to the TensorsInfo instance. Note that we support up to 16 tensors in TensorsInfo.
/// </summary>
/// <param name="name">Name of Tensor.</param>
/// <param name="type">Data element type of Tensor.</param>
/// <param name="dimension">Dimension of Tensor. Note that we support up to 4th ranks.</param>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="IndexOutOfRangeException">Thrown when the number of Tensor already exceeds the size limits (i.e. Tensor.SizeLimit)</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public void AddTensorInfo(string name, TensorType type, int[] dimension)
{
NNStreamer.CheckNNStreamerSupport();
int idx = _infoList.Count;
if (idx >= Tensor.SizeLimit) {
throw new IndexOutOfRangeException("Max size of the tensors is " + Tensor.SizeLimit);
}
int[] dim = ConvertDimension(dimension);
_infoList.Add(new TensorInfo(name, type, dim));
if (_handle != IntPtr.Zero)
{
NNStreamerError ret = NNStreamerError.None;
ret = Interop.Util.SetTensorsCount(_handle, _infoList.Count);
NNStreamer.CheckException(ret, "Failed to set the number of tensors");
UpdateInfoHandle(_handle, idx, name, type, dim);
}
}
/// <summary>
/// Sets the tensor name with given index.
/// </summary>
/// <param name="idx">The index of the tensor to be updated.</param>
/// <param name="name">The tensor name to be set.</param>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public void SetTensorName(int idx, string name)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
_infoList[idx].Name = name;
if (_handle != IntPtr.Zero)
{
NNStreamerError ret = NNStreamerError.None;
ret = Interop.Util.SetTensorName(_handle, idx, name);
NNStreamer.CheckException(ret, "unable to set the name of tensor: " + idx.ToString());
}
}
/// <summary>
/// Gets the tensor name with given index.
/// </summary>
/// <param name="idx">The index of the tensor.</param>
/// <returns>The tensor name</returns>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public string GetTensorName(int idx)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
return _infoList[idx].Name;
}
/// <summary>
/// Sets the tensor type with given index and its type.
/// </summary>
/// <param name="idx">The index of the tensor to be updated.</param>
/// <param name="type">The tensor type to be set.</param>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public void SetTensorType(int idx, TensorType type)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
_infoList[idx].Type = type;
if (_handle != IntPtr.Zero)
{
NNStreamerError ret = NNStreamerError.None;
ret = Interop.Util.SetTensorType(_handle, idx, type);
NNStreamer.CheckException(ret, "unable to set the type of tensor: " + idx.ToString());
}
}
/// <summary>
/// Gets the tensor type with given index.
/// </summary>
/// <param name="idx">The index of the tensor.</param>
/// <returns>The tensor type</returns>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public TensorType GetTensorType(int idx)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
return _infoList[idx].Type;
}
/// <summary>
/// Sets the tensor dimension with given index and dimension.
/// </summary>
/// <param name="idx">The index of the tensor to be updated.</param>
/// <param name="dimension">The tensor dimension to be set.</param>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public void SetDimension(int idx, int[] dimension)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
int[] dim = ConvertDimension(dimension);
_infoList[idx].SetDimension(dim);
if (_handle != IntPtr.Zero)
{
NNStreamerError ret = NNStreamerError.None;
ret = Interop.Util.SetTensorDimension(_handle, idx, dim);
NNStreamer.CheckException(ret, "unable to set the dimension of tensor: " + idx.ToString());
}
}
/// <summary>
/// Gets the tensor dimension with given index.
/// </summary>
/// <param name="idx">The index of the tensor.</param>
/// <returns>The tensor dimension.</returns>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public int[] GetDimension(int idx)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
return _infoList[idx].Dimension;
}
/// <summary>
/// Creates a TensorsData instance based on informations of TensorsInfo
/// </summary>
/// <returns>TensorsData instance</returns>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to TensorsInfo's information is invalid.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 6 </since_tizen>
public TensorsData GetTensorsData()
{
IntPtr tensorsData_h = IntPtr.Zero;
TensorsData retTensorData;
NNStreamerError ret = NNStreamerError.None;
NNStreamer.CheckNNStreamerSupport();
if (_handle == IntPtr.Zero)
{
Log.Info(NNStreamer.TAG, "_handle is IntPtr.Zero\n" + " GetTensorsInfoHandle() is called");
GetTensorsInfoHandle();
}
ret = Interop.Util.CreateTensorsData(_handle, out tensorsData_h);
NNStreamer.CheckException(ret, "Failed to create the TensorsData object");
retTensorData = TensorsData.CreateFromNativeHandle(tensorsData_h, _handle, false);
return retTensorData;
}
/// <summary>
/// Calculates the byte size of tensor data.
/// </summary>
/// <param name="idx">The index of the tensor information in the list</param>
/// <returns>The byte size of tensor</returns>
/// <feature>http://tizen.org/feature/machine_learning.inference</feature>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
/// <since_tizen> 8 </since_tizen>
public int GetTensorSize(int idx)
{
NNStreamer.CheckNNStreamerSupport();
CheckIndexBoundary(idx);
return _infoList[idx].Size;
}
/// <summary>
/// Gets the the hash code of this TensorsInfo object
/// </summary>
/// <returns>The hash code</returns>
/// <since_tizen> 8 </since_tizen>
public override int GetHashCode()
{
unchecked
{
int hash = 19;
foreach (var info in _infoList)
{
hash = hash * 31 + info.GetHashCode();
}
return hash;
}
}
/// <summary>
/// Compare TensorsInfo, which is its contents are the same or not.
/// </summary>
/// <param name="obj">Object to compare</param>
/// <returns>True if the given object is the same object or its contents are the same</returns>
/// <since_tizen> 8 </since_tizen>
public override bool Equals(object obj)
{
if (obj == null)
return false;
TensorsInfo cInfo = obj as TensorsInfo;
return this.Equals(cInfo);
}
/// <summary>
/// Compare TensorsInfo, which is its contents are the same or not.
/// </summary>
/// <param name="other">TensorsInfo instance to compare</param>
/// <returns>True if the given object is the same object or its contents are the same</returns>
/// <since_tizen> 8 </since_tizen>
public bool Equals(TensorsInfo other)
{
if (other == null)
return false;
if (this.Count != other.Count)
return false;
for (int i = 0; i < this.Count; ++i)
{
// Type
if (this.GetTensorType(i) != other.GetTensorType(i))
return false;
// Dimension
if (!this.GetDimension(i).SequenceEqual(other.GetDimension(i)))
return false;
}
return true;
}
/// <summary>
/// Create a new TensorsInfo object cloned from the current tensors information.
/// </summary>
/// <returns>Hard-copied TensorsInfo object</returns>
/// <since_tizen> 9 </since_tizen>
internal TensorsInfo Clone()
{
TensorsInfo retInfo = null;
retInfo = new TensorsInfo();
foreach (TensorInfo t in _infoList) {
retInfo.AddTensorInfo(t.Name, t.Type, t.Dimension);
}
return retInfo;
}
/// <summary>
/// Make TensorsInfo object from Native handle
/// </summary>
/// <param name="handle">Handle of TensorsInfo object</param>
/// <returns>TensorsInfo object</returns>
internal static TensorsInfo ConvertTensorsInfoFromHandle(IntPtr handle)
{
TensorsInfo retInfo = null;
NNStreamerError ret = NNStreamerError.None;
int count;
ret = Interop.Util.GetTensorsCount(handle, out count);
NNStreamer.CheckException(ret, "Fail to get Tensors' count");
retInfo = new TensorsInfo();
for (int i = 0; i < count; ++i)
{
string name;
TensorType type;
uint[] dim = new uint[Tensor.RankLimit];
ret = Interop.Util.GetTensorName(handle, i, out name);
NNStreamer.CheckException(ret, "Fail to get Tensor's name");
ret = Interop.Util.GetTensorType(handle, i, out type);
NNStreamer.CheckException(ret, "Fail to get Tensor's type");
ret = Interop.Util.GetTensorDimension(handle, i, dim);
NNStreamer.CheckException(ret, "Fail to get Tensor's dimension");
retInfo.AddTensorInfo(name, type, (int[])(object)dim);
}
return retInfo;
}
/// <summary>
/// Return TensorsInfo handle
/// </summary>
/// <returns>IntPtr TensorsInfo handle</returns>
internal IntPtr GetTensorsInfoHandle()
{
NNStreamerError ret = NNStreamerError.None;
IntPtr ret_handle = IntPtr.Zero;
int idx;
/* Already created */
if (_handle != IntPtr.Zero)
return _handle;
/* Check required parameters */
int num = _infoList.Count;
if (num <= 0 || num > Tensor.SizeLimit)
ret = NNStreamerError.InvalidParameter;
NNStreamer.CheckException(ret, "number of Tensor in TensorsInfo is invalid: " + _infoList.Count);
/* Create TensorsInfo object */
ret = Interop.Util.CreateTensorsInfoExtended(out ret_handle);
NNStreamer.CheckException(ret, "fail to create TensorsInfo object");
/* Set the number of tensors */
ret = Interop.Util.SetTensorsCount(ret_handle, _infoList.Count);
NNStreamer.CheckException(ret, "unable to set the number of tensors");
/* Set each Tensor info */
idx = 0;
foreach (TensorInfo t in _infoList)
{
UpdateInfoHandle(ret_handle, idx, t.Name, t.Type, t.Dimension);
idx += 1;
}
_handle = ret_handle;
return ret_handle;
}
/// <summary>
/// Releases any unmanaged resources used by this object.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
/// </summary>
/// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
// release managed objects
_infoList.Clear();
}
// release unmanaged objects
if (_handle != IntPtr.Zero)
{
NNStreamerError ret = Interop.Util.DestroyTensorsInfo(_handle);
if (ret != NNStreamerError.None)
{
Log.Error(NNStreamer.TAG, "failed to destroy TensorsInfo object");
}
}
_disposed = true;
}
private static int[] ConvertDimension(int[] dimension)
{
if (dimension == null) {
throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "The dimension is null, it should be a valid array.");
}
if (dimension.Length > Tensor.RankLimit) {
throw new IndexOutOfRangeException("Max rank limit is " + Tensor.RankLimit);
}
int[] dim = new int[Tensor.RankLimit];
int i;
for (i = 0 ; i < dimension.Length ; i++) {
dim[i] = dimension[i];
}
for (; i < Tensor.RankLimit ; i++) {
dim[i] = 0;
}
return dim;
}
private void UpdateInfoHandle(IntPtr handle, int idx, string name, TensorType type, int[] dimension)
{
if (handle != IntPtr.Zero)
{
NNStreamerError ret = NNStreamerError.None;
ret = Interop.Util.SetTensorName(handle, idx, name);
NNStreamer.CheckException(ret, "Failed to set the name of tensor at index " + idx.ToString());
ret = Interop.Util.SetTensorType(handle, idx, type);
NNStreamer.CheckException(ret, "Failed to set the type of tensor at index " + idx.ToString());
ret = Interop.Util.SetTensorDimension(handle, idx, dimension);
NNStreamer.CheckException(ret, "Failed to set the dimension of tensor at index " + idx.ToString());
}
}
private void CheckIndexBoundary(int idx)
{
if (idx < 0 || idx >= _infoList.Count)
{
string msg = "Invalid index [" + idx + "] of the tensors";
throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);
}
}
private class TensorInfo
{
public TensorInfo(TensorType type, int[] dimension)
{
Type = type;
SetDimension(dimension);
}
public TensorInfo(string name, TensorType type, int[] dimension)
{
Name = name;
Type = type;
SetDimension(dimension);
}
public void SetDimension(int[] dimension)
{
if (dimension == null) {
throw new ArgumentException("The dimension is null, it should be a valid array.");
}
if (dimension.Length != Tensor.RankLimit) {
throw new ArgumentException("The length of the dimension should be " + Tensor.RankLimit);
}
Dimension = (int[])dimension.Clone();
}
private int GetSize()
{
int size = 0;
switch (Type) {
case TensorType.Int32:
case TensorType.UInt32:
case TensorType.Float32:
size = 4;
break;
case TensorType.Int16:
case TensorType.UInt16:
size = 2;
break;
case TensorType.Int8:
case TensorType.UInt8:
size = 1;
break;
case TensorType.Float64:
case TensorType.Int64:
case TensorType.UInt64:
size = 8;
break;
default:
/* Unknown Type */
break;
}
for (int i = 0; i < Tensor.RankLimit; ++i)
{
if (Dimension[i] == 0)
break;
size *= Dimension[i];
}
return size;
}
public int Size
{
get {
return GetSize();
}
}
public string Name { get; set; } = string.Empty;
public TensorType Type { get; set; } = TensorType.Int32;
public int[] Dimension { get; private set; } = new int[Tensor.RankLimit];
}
}
}