-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathp_accessors.nim
300 lines (265 loc) · 13.4 KB
/
p_accessors.nim
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
# Copyright 2017 the Arraymancer contributors
#
# 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.
import ../backend/[global_config, memory_optimization_hints],
../../private/ast_utils,
../data_structure,
./p_checks
# ######################################################
# This file implements iterators to iterate on Tensors.
# ##############################################################
# The reference implementation below went through several optimizations:
# - Using non-initialized stack allocation (array instead of seq)
# - Avoiding closures in all higher-order functions, even when iterating on 2 tensors at the same time
# ###### Reference implementation ######
# template strided_iteration[T](t: Tensor[T], strider: IterKind): untyped =
# ## Iterate over a Tensor, displaying data as in C order, whatever the strides.
#
# ## Iterator init
# var coord = newSeq[int](t.rank) # Coordinates in the n-dimentional space
# var backstrides: seq[int] = @[] # Offset between end of dimension and beginning
# for i,j in zip(t.strides,t.shape):
# backstrides.add(i*(j-1))
#
# var iter_pos = t.offset
#
# ## Iterator loop
# for i in 0 ..< t.shape.product:
#
# ## Templating the return value
# when strider == IterKind.Values: yield t.unsafe_raw_buf[iter_pos]
# elif strider == IterKind.Coord_Values: yield (coord, t.unsafe_raw_buf[iter_pos])
# elif strider == IterKind.MemOffset: yield iter_pos
# elif strider == IterKind.MemOffset_Values: yield (iter_pos, t.unsafe_raw_buf[iter_pos])
#
# ## Computing the next position
# for k in countdown(t.rank - 1,0):
# if coord[k] < t.shape[k]-1:
# coord[k] += 1
# iter_pos += t.strides[k]
# break
# else:
# coord[k] = 0
# iter_pos -= backstrides[k]
proc getIndex*[T](t: Tensor[T], idx: varargs[int]): int {.noSideEffect,inline.} =
## Convert [i, j, k, l ...] to the proper index.
when compileOption("boundChecks"):
t.check_index(idx)
result = t.offset
for i in 0..<idx.len:
result += t.strides[i]*idx[i]
proc getContiguousIndex*[T](t: Tensor[T], idx: int): int {.noSideEffect,inline.} =
result = t.offset
if idx != 0:
var z = 1
for i in countdown(t.rank - 1,0):
let coord = (idx div z) mod t.shape[i]
result += coord*t.strides[i]
z *= t.shape[i]
proc atIndex*[T](t: Tensor[T], idx: varargs[int]): T {.noSideEffect,inline.} =
## Get the value at input coordinates
## This used to be `[]` before slicing was implemented
when T is KnownSupportsCopyMem:
result = t.unsafe_raw_buf[t.getIndex(idx)]
else:
result = t.storage.raw_buffer[t.getIndex(idx)]
proc atIndex*[T](t: var Tensor[T], idx: varargs[int]): var T {.noSideEffect,inline.} =
## Get the value at input coordinates
## This allows inplace operators t[1,2] += 10 syntax
when T is KnownSupportsCopyMem:
result = t.unsafe_raw_buf[t.getIndex(idx)]
else:
result = t.storage.raw_buffer[t.getIndex(idx)]
proc atIndexMut*[T](t: var Tensor[T], idx: varargs[int], val: T) {.noSideEffect,inline.} =
## Set the value at input coordinates
## This used to be `[]=` before slicing was implemented
when T is KnownSupportsCopyMem:
t.unsafe_raw_buf[t.getIndex(idx)] = val
else:
t.storage.raw_buffer[t.getIndex(idx)] = val
#[
The following accessors represent a ``very`` specific workaround.
The templates used for the iterators in this file make use of `unsafe_raw_offset`.
This is not valid for `not KnownSupportsCopyMem` types. That's why we
define these helper accessors, which access the corresponding position
for `seq` based Tensors, including the offset!
Instead of defining a `Raw(Im)MutableView` type, we simply define a template
to the input tensor, like so:
.. code-block:: nim
when getSubType(t) is KnownSupportsCopyMem:
let data = t.unsafe_raw_offset()
else:
template data: untyped = t
The `data` template is then given to the following code, which simply accesses
the input tensor. Since it is a seq based tensor, it will use the accessors
below.
]#
func `[]`[T: not KnownSupportsCopyMem](t: Tensor[T], idx: int): T =
t.storage.raw_buffer[t.offset + idx]
func `[]`[T: not KnownSupportsCopyMem](t: var Tensor[T], idx: int): var T =
t.storage.raw_buffer[t.offset + idx]
func `[]=`[T: not KnownSupportsCopyMem](t: var Tensor[T], idx: int, val: T) =
t.storage.raw_buffer[t.offset + idx] = val
## Iterators
type
IterKind* = enum
Values, Iter_Values, Offset_Values
template initStridedIteration*(coord, backstrides, iter_pos: untyped, t, iter_offset, iter_size: typed): untyped =
## Iterator init
var iter_pos = 0
withMemoryOptimHints() # MAXRANK = 8, 8 ints = 64 Bytes, cache line = 64 Bytes --> profit !
var coord {.align64, noinit.}: array[MAXRANK, int]
var backstrides {.align64, noinit.}: array[MAXRANK, int]
for i in 0..<t.rank:
backstrides[i] = t.strides[i]*(t.shape[i]-1)
coord[i] = 0
# Calculate initial coords and iter_pos from iteration offset
if iter_offset != 0:
var z = 1
for i in countdown(t.rank - 1,0):
coord[i] = (iter_offset div z) mod t.shape[i]
iter_pos += coord[i]*t.strides[i]
z *= t.shape[i]
template advanceStridedIteration*(coord, backstrides, iter_pos, t, iter_offset, iter_size: typed): untyped =
## Computing the next position
for k in countdown(t.rank - 1,0):
if coord[k] < t.shape[k]-1:
coord[k] += 1
iter_pos += t.strides[k]
break
else:
coord[k] = 0
iter_pos -= backstrides[k]
template stridedIterationYield*(strider: IterKind, data, i, iter_pos: typed) =
## Iterator the return value
when strider == IterKind.Values: yield data[iter_pos]
elif strider == IterKind.Iter_Values: yield (i, data[iter_pos])
elif strider == IterKind.Offset_Values: yield (iter_pos, data[iter_pos]) ## TODO: remove workaround for C++ backend
template stridedIteration*(strider: IterKind, t, iter_offset, iter_size: typed): untyped =
## Iterate over a Tensor, displaying data as in C order, whatever the strides.
# Get tensor data address with offset builtin
# only reading here, pointer access is safe even for ref types
when getSubType(type(t)) is KnownSupportsCopyMem:
let data = t.unsafe_raw_offset()
else:
template data: untyped = t
# Optimize for loops in contiguous cases
if t.is_C_contiguous:
for i in iter_offset..<(iter_offset+iter_size):
stridedIterationYield(strider, data, i, i)
else:
initStridedIteration(coord, backstrides, iter_pos, t, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
stridedIterationYield(strider, data, i, iter_pos)
advanceStridedIteration(coord, backstrides, iter_pos, t, iter_offset, iter_size)
template stridedCoordsIteration*(t, iter_offset, iter_size: typed): untyped =
## Iterate over a Tensor, displaying data as in C order, whatever the strides. (coords)
# Get tensor data address with offset builtin
# only reading here, pointer access is safe even for ref types
when getSubType(type(t)) is KnownSupportsCopyMem:
let data = t.unsafe_raw_offset()
else:
template data: untyped = t
let rank = t.rank
initStridedIteration(coord, backstrides, iter_pos, t, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
yield (coord[0..<rank], data[iter_pos])
advanceStridedIteration(coord, backstrides, iter_pos, t, iter_offset, iter_size)
template dualStridedIterationYield*(strider: IterKind, t1data, t2data, i, t1_iter_pos, t2_iter_pos: typed) =
## Iterator the return value
when strider == IterKind.Values: yield (t1data[t1_iter_pos], t2data[t2_iter_pos])
elif strider == IterKind.Iter_Values: yield (i, t1data[t1_iter_pos], t2data[t2_iter_pos])
elif strider == IterKind.Offset_Values: yield (t1_iter_pos, t1data[t1_iter_pos], t2data[t2_iter_pos]) ## TODO: remove workaround for C++ backend
template dualStridedIteration*(strider: IterKind, t1, t2, iter_offset, iter_size: typed): untyped =
## Iterate over two Tensors, displaying data as in C order, whatever the strides.
let t1_contiguous = t1.is_C_contiguous()
let t2_contiguous = t2.is_C_contiguous()
when getSubType(type(t1)) is KnownSupportsCopyMem:
let t1data = t1.unsafe_raw_offset()
else:
template t1data: untyped = t1
when getSubType(type(t2)) is KnownSupportsCopyMem:
let t2data = t2.unsafe_raw_offset()
else:
template t2data: untyped = t2
# Optimize for loops in contiguous cases
if t1_contiguous and t2_contiguous:
for i in iter_offset..<(iter_offset+iter_size):
dualStridedIterationYield(strider, t1data, t2data, i, i, i)
elif t1_contiguous:
initStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
dualStridedIterationYield(strider, t1data, t2data, i, i, t2_iter_pos)
advanceStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
elif t2_contiguous:
initStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
dualStridedIterationYield(strider, t1data, t2data, i, t1_iter_pos, i)
advanceStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
else:
initStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
initStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
dualStridedIterationYield(strider, t1data, t2data, i, t1_iter_pos, t2_iter_pos)
advanceStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
advanceStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
template tripleStridedIterationYield*(strider: IterKind, t1data, t2data, t3data, i, t1_iter_pos, t2_iter_pos, t3_iter_pos: typed) =
## Iterator the return value
when strider == IterKind.Values: yield (t1data[t1_iter_pos], t2data[t2_iter_pos], t3data[t3_iter_pos])
elif strider == IterKind.Iter_Values: yield (i, t1data[t1_iter_pos], t2data[t2_iter_pos], t3data[t3_iter_pos])
elif strider == IterKind.Offset_Values: yield (t1_iter_pos, t1data[t1_iter_pos], t2data[t2_iter_pos], t3data[t3_iter_pos]) ## TODO: remove workaround for C++ backend
template tripleStridedIteration*(strider: IterKind, t1, t2, t3, iter_offset, iter_size: typed): untyped =
## Iterate over two Tensors, displaying data as in C order, whatever the strides.
let t1_contiguous = t1.is_C_contiguous()
let t2_contiguous = t2.is_C_contiguous()
let t3_contiguous = t3.is_C_contiguous()
# Get tensor data address with offset builtin
withMemoryOptimHints()
when getSubType(type(t1)) is KnownSupportsCopyMem:
let t1data = t1.unsafe_raw_offset()
else:
template t1data: untyped = t1
when getSubType(type(t2)) is KnownSupportsCopyMem:
let t2data = t2.unsafe_raw_offset()
else:
template t2data: untyped = t2
when getSubType(type(t3)) is KnownSupportsCopyMem:
let t3data = t3.unsafe_raw_offset()
else:
template t3data: untyped = t3
# Optimize for loops in contiguous cases
# Note that not all cases are handled here, just some probable ones
if t1_contiguous and t2_contiguous and t3_contiguous:
for i in iter_offset..<(iter_offset+iter_size):
tripleStridedIterationYield(strider, t1data, t2data, t3data, i, i, i, i)
elif t1_contiguous and t2_contiguous:
initStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
tripleStridedIterationYield(strider, t1data, t2data, t3data, i, i, i, t3_iter_pos)
advanceStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)
elif t1_contiguous:
initStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
initStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
tripleStridedIterationYield(strider, t1data, t2data, t3data, i, i, t2_iter_pos, t3_iter_pos)
advanceStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
advanceStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)
else:
initStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
initStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
initStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)
for i in iter_offset..<(iter_offset+iter_size):
tripleStridedIterationYield(strider, t1data, t2data, t3data, i, t1_iter_pos, t2_iter_pos, t3_iter_pos)
advanceStridedIteration(t1_coord, t1_backstrides, t1_iter_pos, t1, iter_offset, iter_size)
advanceStridedIteration(t2_coord, t2_backstrides, t2_iter_pos, t2, iter_offset, iter_size)
advanceStridedIteration(t3_coord, t3_backstrides, t3_iter_pos, t3, iter_offset, iter_size)