Repository navigation
Expand file tree
/
Copy pathassignment.v
More file actions
305 lines (286 loc) · 9.71 KB
/
Copy pathassignment.v
File metadata and controls
305 lines (286 loc) · 9.71 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
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
module vtl
// PutMode controls how flat indices outside a tensor's range are handled.
pub enum PutMode {
raise
wrap
clip
}
// set copies a scalar value into a Tensor at the provided index. Negative
// coordinates count from the end of each axis; invalid coordinates panic. Use
// set_checked when bounds errors must be returned to the caller.
@[inline]
pub fn (mut t Tensor[T]) set[T](index []int, val T) {
offset := t.offset_index(index)
t.data.set[T](offset, val)
}
// set_checked copies a value at index, or returns an error if the index rank
// or any coordinate is outside the tensor bounds.
pub fn (mut t Tensor[T]) set_checked[T](index []int, val T) ! {
offset := t.checked_offset_index(index)!
t.data.set[T](offset, val)
}
// set_nth copies a scalar value into a Tensor at the provided offset
// set_nth exposes this operation as part of the public API.
// set_nth exposes this operation as part of the public API.
@[inline]
pub fn (mut t Tensor[T]) set_nth[T](n int, val T) {
index := t.nth_index(n)
t.set[T](index, val)
}
// fill fills an entire Tensor with a given value
// fill exposes this operation as part of the public API.
// fill exposes this operation as part of the public API.
@[inline]
pub fn (mut t Tensor[T]) fill[T](val T) &Tensor[T] {
t.data.fill[T](val)
return t
}
// assign sets the values of an Tensor equal to the values of another
// Tensor of the same shape
pub fn (mut t Tensor[T]) assign[T](other &Tensor[T]) !&Tensor[T] {
mut iters, _ := t.iterators[T]([other])!
for {
vals, i := iters.next() or { break }
t.set(i, vals[1])
}
return t
}
// put replaces values at row-major logical flat indices. Values repeat when
// there are fewer values than indices. Negative indices count from the end.
// Invalid indices return an error without changing the tensor.
pub fn (mut t Tensor[T]) put[T](indices &Tensor[int], values &Tensor[T]) ! {
t.put_with_mode[T](indices, values, .raise)!
}
// putmask replaces values where mask is true, in row-major logical order.
// Update values repeat cyclically by row-major flat position, matching NumPy's
// putmask semantics. The mask must match the tensor shape exactly.
@[direct_array_access]
pub fn (mut t Tensor[T]) putmask[T](mask &Tensor[bool], values &Tensor[T]) ! {
if mask.shape != t.shape {
return error('putmask: mask shape ${mask.shape} must match tensor shape ${t.shape}')
}
mask_aliases_target := tensor_shares_storage[T, bool](t, mask)
mut selected := []bool{}
if mask_aliases_target {
selected = []bool{len: t.size}
}
mask_is_flat := mask.is_row_major_contiguous() && mask.data.data.len == mask.size
if mask_aliases_target || values.size == 0 {
mut has_selected := false
for flat_index in 0 .. t.size {
is_selected := if mask_is_flat {
mask.data.data[flat_index]
} else {
mask.get_nth[bool](flat_index)
}
if mask_aliases_target {
selected[flat_index] = is_selected
}
if is_selected {
has_selected = true
}
}
if values.size == 0 {
if has_selected {
return error('putmask: at least one value is required for selected positions')
}
return
}
}
mut update_values := []T{}
if tensor_shares_storage[T, T](t, values) {
update_values = values.to_array()
}
values_are_flat := values.is_row_major_contiguous() && values.data.data.len == values.size
if t.is_row_major_contiguous() && t.data.data.len == t.size && mask_is_flat {
mut target_data := t.data.data
mask_data := mask.data.data
mut value_index := 0
for flat_index in 0 .. t.size {
is_selected := if mask_aliases_target {
selected[flat_index]
} else {
mask_data[flat_index]
}
if is_selected {
value := if update_values.len > 0 {
update_values[value_index]
} else if values_are_flat {
values.data.data[value_index]
} else {
values.get_nth[T](value_index)
}
target_data[flat_index] = value
}
value_index++
if value_index == values.size {
value_index = 0
}
}
return
}
mut value_index := 0
for flat_index in 0 .. t.size {
is_selected := if mask_aliases_target {
selected[flat_index]
} else {
mask.get_nth[bool](flat_index)
}
if is_selected {
value := if update_values.len > 0 {
update_values[value_index]
} else {
values.get_nth[T](value_index)
}
t.set_nth[T](flat_index, value)
}
value_index++
if value_index == values.size {
value_index = 0
}
}
}
// put_with_mode replaces values at row-major logical flat indices, using mode
// to handle indices outside the flattened tensor's range. Repeated indices
// follow row-major write order, so the last value wins. All indices are
// validated before any writes are made.
pub fn (mut t Tensor[T]) put_with_mode[T](indices &Tensor[int], values &Tensor[T], mode PutMode) ! {
if indices.size == 0 {
return
}
if t.size == 0 {
return error('put cannot index an empty tensor')
}
if values.size == 0 {
return error('put requires at least one value when indices are non-empty')
}
mut selected_indices := []int{}
if tensor_shares_storage[T, int](t, indices) {
selected_indices = indices.to_array()
}
for position in 0 .. indices.size {
selected := if selected_indices.len > 0 {
selected_indices[position]
} else {
indices.get_nth[int](position)
}
_ = normalize_put_index(selected, t.size, mode)!
}
mut update_values := []T{}
if tensor_shares_storage[T, T](t, values) {
update_values = values.to_array()
}
for position in 0 .. indices.size {
selected := if selected_indices.len > 0 {
selected_indices[position]
} else {
indices.get_nth[int](position)
}
index := normalize_put_index(selected, t.size, mode)!
update := if update_values.len > 0 {
update_values[position % values.size]
} else {
values.get_nth[T](position % values.size)
}
t.set_nth[T](index, update)
}
}
fn tensor_shares_storage[T, U](a &Tensor[T], b &Tensor[U]) bool {
// u64 holds a full 64-bit address; uint would truncate it. usize is deprecated in V 0.5.2.
a_start := unsafe { u64(a.data.data.data) }
a_end := a_start + u64(a.data.data.len) * u64(sizeof(T))
b_start := unsafe { u64(b.data.data.data) }
b_end := b_start + u64(b.data.data.len) * u64(sizeof(U))
return a_start < b_end && b_start < a_end
}
fn normalize_put_index(selected int, size int, mode PutMode) !int {
return match mode {
.raise {
index := if selected < 0 { selected + size } else { selected }
if index < 0 || index >= size {
return error('put index ${selected} is out of range for flattened size ${size}')
}
index
}
.wrap {
wrapped_index := selected % size
if wrapped_index < 0 { wrapped_index + size } else { wrapped_index }
}
.clip {
if selected < 0 {
0
} else if selected >= size {
size - 1
} else {
selected
}
}
}
}
// put_along_axis writes values at indices along axis. Indices and values must
// have the same rank as the target and values must match the index shape. The
// index dimensions outside axis may be smaller than the target dimensions.
// Negative indices count from the end.
// When an index is repeated, the last value in row-major iteration order wins.
// Out-of-range indices return an error before any target values are changed.
pub fn (mut t Tensor[T]) put_along_axis[T](indices &Tensor[int], values &Tensor[T], axis int) ! {
axis_index := validate_axis_update_shapes(t.shape, indices, values, axis, 'put_along_axis')!
normalized_indices := normalize_axis_update_indices(indices, t.shape[axis_index], 'put_along_axis')!
update_values := values.to_array()
mut index_iter := indices.iterator[int]()
mut position := 0
for {
_, mut index := index_iter.next() or { break }
index[axis_index] = normalized_indices[position]
t.set(index, update_values[position])
position++
}
}
// scatter_add adds values at indices along axis. Duplicate indices accumulate
// in row-major iteration order; negative indices count from the end. An
// out-of-range index returns an error before any target values are changed.
pub fn (mut t Tensor[T]) scatter_add[T](indices &Tensor[int], values &Tensor[T], axis int) ! {
axis_index := validate_axis_update_shapes(t.shape, indices, values, axis, 'scatter_add')!
normalized_indices := normalize_axis_update_indices(indices, t.shape[axis_index], 'scatter_add')!
update_values := values.to_array()
mut index_iter := indices.iterator[int]()
mut position := 0
for {
_, mut index := index_iter.next() or { break }
index[axis_index] = normalized_indices[position]
t.set(index, t.get(index) + update_values[position])
position++
}
}
fn normalize_axis_update_indices(indices &Tensor[int], axis_size int, operation string) ![]int {
mut normalized_indices := []int{cap: indices.size}
mut index_iter := indices.iterator[int]()
for {
selected, _ := index_iter.next() or { break }
normalized := if selected < 0 { selected + axis_size } else { selected }
if normalized < 0 || normalized >= axis_size {
return error('${operation} index ${selected} is out of range for axis size ${axis_size}')
}
normalized_indices << normalized
}
return normalized_indices
}
fn validate_axis_update_shapes[T](target_shape []int, indices &Tensor[int], values &Tensor[T], axis int, operation string) !int {
rank := target_shape.len
axis_index := if axis < 0 { axis + rank } else { axis }
if rank == 0 || axis_index < 0 || axis_index >= rank {
return error('${operation} axis ${axis} is out of range for rank ${rank}')
}
if indices.rank() != rank || values.rank() != rank {
return error('${operation} indices and values must have rank ${rank}')
}
if indices.shape != values.shape {
return error('${operation} values shape must match indices shape')
}
for dimension in 0 .. rank {
if dimension != axis_index && indices.shape[dimension] > target_shape[dimension] {
return error('${operation} index shape exceeds target shape outside the selected axis')
}
}
return axis_index
}