-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTensorType.cs
More file actions
63 lines (61 loc) · 2.43 KB
/
Copy pathTensorType.cs
File metadata and controls
63 lines (61 loc) · 2.43 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
using Tensornet.Native;
using Tensornet.Exceptions;
namespace Tensornet.Common{
internal sealed class TensorTypeInfo
{
private static readonly Dictionary<Type, TensorTypeInfo> _typeInfoMap;
private static readonly Dictionary<DType, Type> _dtypeMap;
private static readonly Dictionary<DType, int> _sizeMap;
public readonly DType _dtype;
public readonly int _size;
/// <summary>
/// The priority in type conversion. The larger the value is, the higher priority it has.
/// </summary>
public readonly int _priority;
static TensorTypeInfo(){
_typeInfoMap = new Dictionary<Type, TensorTypeInfo>()
{
{ typeof(float), new TensorTypeInfo( DType.Float32, sizeof(float), 4) },
{ typeof(int), new TensorTypeInfo( DType.Int32, sizeof(int), 2) },
{ typeof(long), new TensorTypeInfo( DType.Int64, sizeof(long), 3) },
{ typeof(bool), new TensorTypeInfo( DType.Bool, sizeof(bool), 1) },
{ typeof(double), new TensorTypeInfo( DType.Float64, sizeof(double), 5) }
};
_dtypeMap = _typeInfoMap.ToDictionary(k => k.Value._dtype, v => v.Key);
_sizeMap = _typeInfoMap.ToDictionary(k => k.Value._dtype, v => v.Value._size);
}
public TensorTypeInfo(DType dtype, int size, int priority){
_dtype = dtype;
_size = size;
_priority = priority;
}
public static TensorTypeInfo GetTypeInfo(Type type){
TensorTypeInfo res;
if(!_typeInfoMap.TryGetValue(type, out res)){
throw new UnsupportedTypeException();
}
return res;
}
public static Type GetTypeInfo(DType type){
Type res;
if(!_dtypeMap.TryGetValue(type, out res)){
throw new UnsupportedTypeException();
}
return res;
}
public static int GetTypeSize(DType type){
int res;
if(!_sizeMap.TryGetValue(type, out res)){
throw new UnsupportedTypeException();
}
return res;
}
public static int GetTypeSize(Type type){
TensorTypeInfo typeInfo;
if(!_typeInfoMap.TryGetValue(type, out typeInfo)){
throw new UnsupportedTypeException();
}
return typeInfo._size;
}
}
}