Repository navigation
Expand file tree
/
Copy pathbuild.v
More file actions
131 lines (121 loc) · 3.45 KB
/
Copy pathbuild.v
File metadata and controls
131 lines (121 loc) · 3.45 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
module vtl
import vtl.storage
// TensorData defines a public data structure for this module.
// TensorData defines a public data structure for this module.
@[params]
pub struct TensorData {
pub:
memory MemoryFormat = .row_major
}
// from_varray takes a one dimensional array of T values
// and coerces it into an arbitrary shaped Tensor if possible.
// Panics if the shape provided does not hold the provided array
pub fn from_array[T](arr []T, shape []int, params TensorData) !&Tensor[T] {
size := size_from_shape(shape)
if size != arr.len {
return error('Bad shape for array, shape [${arr.len}] cannot fit into shape ${shape}')
}
data_storage := storage.from_array[T](arr)
if shape.len == 0 {
return &Tensor[T]{
memory: params.memory
strides: [1]
shape: []
size: size
data: data_storage
}
}
sh := shape.clone()
strides := strides_from_shape(sh, params.memory)
return &Tensor[T]{
shape: sh
strides: strides
memory: params.memory
size: size
data: data_storage
}
}
// tensor allocates a Tensor onto specified device with a given data
pub fn tensor[T](init T, shape []int, params TensorData) &Tensor[T] {
if shape.len == 0 {
data_storage := storage.storage[T](1, 0, init)
return &Tensor[T]{
memory: params.memory
strides: [1]
shape: []
size: 1
data: data_storage
}
}
strides := strides_from_shape(shape, params.memory)
size := size_from_shape(shape)
data_storage := storage.storage[T](size, 0, init)
return &Tensor[T]{
shape: shape.clone()
memory: params.memory
strides: strides
size: size
data: data_storage
}
}
// empty_uninitialized_float allocates writable f32/f64 storage without clearing it.
// Callers must initialize every element before exposing or reading the tensor.
fn empty_uninitialized_float[T](shape []int, params TensorData) &Tensor[T] {
$if T is f32 || T is f64 {
size := size_from_shape(shape)
mut values := []T{cap: size}
unsafe {
values.len = size
}
return &Tensor[T]{
shape: shape.clone()
strides: strides_from_shape(shape, params.memory)
memory: params.memory
size: size
data: &storage.CpuStorage[T]{
data: values
}
}
} $else {
panic('empty_uninitialized_float supports only f32 and f64')
}
}
// tensor_like returns a new tensor created with similar storage properties
// as the Tensor t
pub fn tensor_like[T](t &Tensor[T]) &Tensor[T] {
data_storage := t.data.like[T]()
return &Tensor[T]{
shape: t.shape.clone()
strides: t.strides.clone()
memory: t.memory
size: t.size
data: data_storage
}
}
// tensor_like_with_shape returns a new tensor created with similar storage properties
// as the Tensor `t` with a given shape
pub fn tensor_like_with_shape[T](t &Tensor[T], shape []int) &Tensor[T] {
strides := strides_from_shape(shape, t.memory)
size := size_from_shape(shape)
data_storage := t.data.like_with_len[T](size)
return &Tensor[T]{
shape: shape.clone()
strides: strides
memory: t.memory
size: size
data: data_storage
}
}
// tensor_like_with_shape_and_strides returns a new tensor created with similar storage properties
// as the Tensor `t` with a given shape and strides
fn tensor_like_with_shape_and_strides[T](t &Tensor[T], shape []int, strides []int) &Tensor[T] {
size := size_from_shape(shape)
data_storage := t.data.like_with_len[T](size)
return &Tensor[T]{
shape: shape.clone()
strides: strides.clone()
memory: t.memory
size: size
data: data_storage
}
}