-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.java
221 lines (172 loc) · 6.12 KB
/
Array.java
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
package arrayfire;
import arrayfire.numbers.N;
import arrayfire.numbers.Num;
import arrayfire.numbers.U;
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.function.Function;
public class Array<T extends DataType<?>, S extends Shape<?, ?, ?, ?>> implements MemoryContainer {
// Contains a single device pointer.
public static final AddressLayout LAYOUT = ValueLayout.ADDRESS;
private final T type;
private final S shape;
private final MemorySegment segment;
public Array(Prototype<T, S> prototype) {
this(prototype.type(), prototype.shape());
}
Array(T type, S shape) {
this.type = type;
this.shape = shape;
this.segment = Arena.ofAuto().allocate(LAYOUT);
}
MemorySegment segment() {
return segment;
}
/**
* @return the wrapped void* pointer of the C af_array.
*/
public MemorySegment dereference() {
var value = segment.get(LAYOUT, 0L);
if (MemorySegment.NULL.equals(value)) {
throw new IllegalStateException(
String.format("Cannot dereference an uninitialized segment (nullptr) %s", shape));
}
return value;
}
public boolean materialized() {
return !MemorySegment.NULL.equals(segment.get(LAYOUT, 0L));
}
public int capacity() {
return shape.capacity();
}
public S shape() {
return shape;
}
public Prototype<T, S> prototype() {
return new Prototype<>(type, shape);
}
public T type() {
return type;
}
@Override
public String toString() {
return "AfTensor{" + "type=" + type + ", shape=" + shape + '}';
}
public Array<T, Shape<N, U, U, U>> reshape(int d0) {
return af.reshape(this, af.shape(d0));
}
public Array<T, Shape<N, N, U, U>> reshape(int d0, int d1) {
return af.reshape(this, af.shape(d0, d1));
}
public Array<T, Shape<N, N, N, U>> reshape(int d0, int d1, int d2) {
return af.reshape(this, af.shape(d0, d1, d2));
}
public Array<T, Shape<N, N, N, N>> reshape(int d0, int d1, int d2, int d3) {
return af.reshape(this, af.shape(d0, d1, d2, d3));
}
public <OD0 extends Num<OD0>> Array<T, Shape<OD0, U, U, U>> reshape(OD0 d0) {
return af.reshape(this, af.shape(d0));
}
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>> Array<T, Shape<OD0, OD1, U, U>> reshape(OD0 d0, OD1 d1) {
return af.reshape(this, af.shape(d0, d1));
}
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>, OD2 extends Num<OD2>> Array<T, Shape<OD0, OD1, OD2, U>> reshape(
OD0 d0, OD1 d1, OD2 d2) {
return af.reshape(this, af.shape(d0, d1, d2));
}
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>, OD2 extends Num<OD2>, OD3 extends Num<OD3>> Array<T, Shape<OD0, OD1, OD2, OD3>> reshape(
OD0 d0, OD1 d1, OD2 d2, OD3 d3) {
return af.reshape(this, af.shape(d0, d1, d2, d3));
}
public <NS extends Shape<?, ?, ?, ?>> Array<T, NS> reshape(NS newShape) {
return af.reshape(this, newShape);
}
/**
* Change the type of the array's D0 dimension to the given type variable provider.
*/
public <OD0 extends Num<OD0>> Array<T, Shape<OD0, U, U, U>> castshape(Function<Integer, OD0> d0) {
return af.castshape(this, d0, af::u, af::u, af::u);
}
/**
* Change the type of the array's D0, D1 dimensions to the given type variable providers.
*/
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>> Array<T, Shape<OD0, OD1, U, U>> castshape(
Function<Integer, OD0> d0, Function<Integer, OD1> d1) {
return af.castshape(this, d0, d1, af::u, af::u);
}
/**
* Change the type of the array's D0, D1, D2 dimensions to the given type variable providers.
*/
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>, OD2 extends Num<OD2>> Array<T, Shape<OD0, OD1, OD2, U>> castshape(
Function<Integer, OD0> d0, Function<Integer, OD1> d1, Function<Integer, OD2> d2) {
return af.castshape(this, d0, d1, d2, af::u);
}
/**
* Change the type of the array's dimensions to the given type variable providers.
*/
public <OD0 extends Num<OD0>, OD1 extends Num<OD1>, OD2 extends Num<OD2>, OD3 extends Num<OD3>> Array<T, Shape<OD0, OD1, OD2, OD3>> castshape(
Function<Integer, OD0> d0, Function<Integer, OD1> d1, Function<Integer, OD2> d2, Function<Integer, OD3> d3) {
return af.castshape(this, d0, d1, d2, d3);
}
public void release() {
af.release(this);
}
Array<T, S> retain() {
return af.retain(this);
}
public Array<T, S> eval() {
return af.eval(this);
}
public Array<T, S> clamp(Array<T, S> lo, Array<T, S> hi) {
return af.clamp(this, lo, hi);
}
public Array<T, S> relu() {
return af.relu(this);
}
public Array<T, S> negate() {
return af.negate(this);
}
public Array<T, S> exp() {
return af.exp(this);
}
public Array<T, S> abs() {
return af.abs(this);
}
public Array<T, S> sqrt() {
return af.sqrt(this);
}
public Array<T, S> sigmoid() {
return af.sigmoid(this);
}
public Array<T, S> sparse(Storage storage) {
return af.sparse(this, storage);
}
public Tileable<T, S> tile() {
return new Tileable<>(this);
}
public <NS extends Shape<?, ?, ?, ?>> Array<T, NS> tileAs(Array<T, NS> newShapeArray) {
return af.tileAs(this, newShapeArray.shape());
}
public <NS extends Shape<?, ?, ?, ?>> Array<T, NS> tileAs(NS newShape) {
return af.tileAs(this, newShape);
}
public Array<T, Shape<N, U, U, U>> flatten() {
return af.flatten(this);
}
public Array<T, S> flip() {
return af.flip(this);
}
public Array<T, S> move(Scope scope) {
Scope.move(this, scope);
return this;
}
public <TN extends DataType<?>> Array<TN, S> cast(TN t) {
return af.cast(this, t);
}
@Override
public void dispose() {
release();
}
}