Repository navigation
Expand file tree
/
Copy pathtensor_vcl_d_vcl.v
More file actions
161 lines (124 loc) · 4.31 KB
/
Copy pathtensor_vcl_d_vcl.v
File metadata and controls
161 lines (124 loc) · 4.31 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
module vtl
import vtl.storage
// VclTensor is the main structure defined by VTL to manage N Dimensional data
// VclTensor defines a public data structure for this module.
// VclTensor defines a public data structure for this module.
@[heap]
pub struct VclTensor[T] {
pub mut:
data &storage.VclStorage[T] = unsafe { nil }
memory MemoryFormat
size int
shape []int
strides []int
}
// vcl returns a VclTensor from a Tensor
pub fn (t &Tensor[T]) vcl(params storage.VclStorageParams) !&VclTensor[T] {
row_tensor := t.copy(.row_major)
cldata := row_tensor.data.vcl(params)!
return &VclTensor[T]{
data: cldata
memory: row_tensor.memory
size: row_tensor.size
shape: row_tensor.shape
strides: row_tensor.strides
}
}
// cpu returns a Tensor from a VclTensor
pub fn (t &VclTensor[T]) cpu() !&Tensor[T] {
data := t.data.cpu()!
return &Tensor[T]{
data: data
memory: t.memory
size: t.size
shape: t.shape
strides: t.strides
}
}
// vcl returns a VclTensor from a VclTensor
// vcl exposes this operation as part of the public API.
// vcl exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) vcl() !&VclTensor[T] {
return t
}
// release releases the VclTensor's data
pub fn (t &VclTensor[T]) release() ! {
return t.data.release()
}
// // str returns the string representation of a VclTensor
// [inline]
// pub fn (t &VclTensor<T>) str() string {
// return t.data.str()
// }
// rank returns the number of dimensions of a given VclTensor
pub fn (t &VclTensor[T]) rank() int {
return t.shape.len
}
// size returns the number of allocated elements for a given tensor
pub fn (t &VclTensor[T]) size() int {
return t.size
}
// is_matrix returns if a VclTensor is a nxm matrix or not
// is_matrix exposes this operation as part of the public API.
// is_matrix exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_matrix() bool {
return t.rank() == 2
}
// is_matrix returns if a VclTensor is a square matrix or not
// is_square_matrix exposes this operation as part of the public API.
// is_square_matrix exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_square_matrix() bool {
return t.rank() == 2 && t.shape[0] == t.shape[1]
}
// is_matrix returns if a VclTensor is a square 1D vector or not
// is_vector exposes this operation as part of the public API.
// is_vector exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_vector() bool {
return t.rank() == 1
}
// is_row_major returns if a VclTensor is supposed to store its data in Row-Major
// order
// is_row_major exposes this operation as part of the public API.
// is_row_major exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_row_major() bool {
// TODO: we need to ensure that t.memory is the source of truth
return t.memory == .row_major
}
// is_col_major returns if a VclTensor is supposed to store its data in Col-Major
// order
// is_col_major exposes this operation as part of the public API.
// is_col_major exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_col_major() bool {
// TODO: we need to ensure that t.memory is the source of truth
return t.memory == .col_major
}
// is_row_major verifies if a VclTensor stores its data in Row-Major
// order
// is_row_major_contiguous exposes this operation as part of the public API.
// is_row_major_contiguous exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_row_major_contiguous() bool {
return is_row_major_contiguous(t.shape, t.strides, t.rank())
}
// is_col_major verifies if a VclTensor stores its data in Col-Major
// order
// is_col_major_contiguous exposes this operation as part of the public API.
// is_col_major_contiguous exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_col_major_contiguous() bool {
return is_col_major_contiguous(t.shape, t.strides, t.rank())
}
// is_contiguous verifies that a VclTensor is contiguous independent of
// memory layout
// is_contiguous exposes this operation as part of the public API.
// is_contiguous exposes this operation as part of the public API.
@[inline]
pub fn (t &VclTensor[T]) is_contiguous() bool {
return t.is_row_major_contiguous() || t.is_col_major_contiguous()
}