-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationBuilder.java
238 lines (201 loc) · 6.89 KB
/
OperationBuilder.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package org.tensorflow;
import java.nio.charset.Charset;
public final class OperationBuilder {
private Graph graph;
private long unsafeNativeHandle;
private static native void addInput(long j, long j2, int i);
private static native void addInputList(long j, long[] jArr, int[] iArr);
private static native long allocate(long j, String str, String str2);
private static native long finish(long j);
private static native void setAttrBool(long j, String str, boolean z);
private static native void setAttrBoolList(long j, String str, boolean[] zArr);
private static native void setAttrFloat(long j, String str, float f);
private static native void setAttrFloatList(long j, String str, float[] fArr);
private static native void setAttrInt(long j, String str, long j2);
private static native void setAttrIntList(long j, String str, long[] jArr);
private static native void setAttrShape(long j, String str, long[] jArr, int i);
private static native void setAttrString(long j, String str, byte[] bArr);
private static native void setAttrTensor(long j, String str, long j2);
private static native void setAttrTensorList(long j, String str, long[] jArr);
private static native void setAttrType(long j, String str, int i);
private static native void setAttrTypeList(long j, String str, int[] iArr);
private static native void setDevice(long j, String str);
OperationBuilder(Graph graph, String str, String str2) {
this.graph = graph;
Reference ref = graph.ref();
try {
this.unsafeNativeHandle = allocate(ref.nativeHandle(), str, str2);
} finally {
ref.close();
}
}
public Operation build() {
Reference ref = this.graph.ref();
try {
Operation operation = new Operation(this.graph, finish(this.unsafeNativeHandle));
this.unsafeNativeHandle = 0;
return operation;
} finally {
ref.close();
}
}
public OperationBuilder addInput(Output output) {
Reference ref = this.graph.ref();
try {
addInput(this.unsafeNativeHandle, output.op().getUnsafeNativeHandle(), output.index());
return this;
} finally {
ref.close();
}
}
public OperationBuilder addInputList(Output[] outputArr) {
Reference ref = this.graph.ref();
try {
long[] jArr = new long[outputArr.length];
int[] iArr = new int[outputArr.length];
for (int i = 0; i < outputArr.length; i++) {
jArr[i] = outputArr[i].op().getUnsafeNativeHandle();
iArr[i] = outputArr[i].index();
}
addInputList(this.unsafeNativeHandle, jArr, iArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setDevice(String str) {
Reference ref = this.graph.ref();
try {
setDevice(this.unsafeNativeHandle, str);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, String str2) {
setAttr(str, str2.getBytes(Charset.forName("UTF-8")));
return this;
}
public OperationBuilder setAttr(String str, byte[] bArr) {
Reference ref = this.graph.ref();
try {
setAttrString(this.unsafeNativeHandle, str, bArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, long j) {
Reference ref = this.graph.ref();
try {
setAttrInt(this.unsafeNativeHandle, str, j);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, long[] jArr) {
Reference ref = this.graph.ref();
try {
setAttrIntList(this.unsafeNativeHandle, str, jArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, float f) {
Reference ref = this.graph.ref();
try {
setAttrFloat(this.unsafeNativeHandle, str, f);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, float[] fArr) {
Reference ref = this.graph.ref();
try {
setAttrFloatList(this.unsafeNativeHandle, str, fArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, boolean z) {
Reference ref = this.graph.ref();
try {
setAttrBool(this.unsafeNativeHandle, str, z);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, boolean[] zArr) {
Reference ref = this.graph.ref();
try {
setAttrBoolList(this.unsafeNativeHandle, str, zArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, DataType dataType) {
Reference ref = this.graph.ref();
try {
setAttrType(this.unsafeNativeHandle, str, dataType.m19c());
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, DataType[] dataTypeArr) {
int[] iArr = new int[dataTypeArr.length];
for (int i = 0; i < dataTypeArr.length; i++) {
iArr[i] = dataTypeArr[i].m19c();
}
Reference ref = this.graph.ref();
try {
setAttrTypeList(this.unsafeNativeHandle, str, iArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, Tensor tensor) {
Reference ref = this.graph.ref();
try {
setAttrTensor(this.unsafeNativeHandle, str, tensor.getNativeHandle());
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, Tensor[] tensorArr) {
int i = 0;
long[] jArr = new long[tensorArr.length];
int length = tensorArr.length;
int i2 = 0;
while (i < length) {
int i3 = i2 + 1;
jArr[i2] = tensorArr[i].getNativeHandle();
i++;
i2 = i3;
}
Reference ref = this.graph.ref();
try {
setAttrTensorList(this.unsafeNativeHandle, str, jArr);
return this;
} finally {
ref.close();
}
}
public OperationBuilder setAttr(String str, Shape shape) {
Reference ref = this.graph.ref();
try {
setAttrShape(this.unsafeNativeHandle, str, shape.asArray(), shape.numDimensions());
return this;
} finally {
ref.close();
}
}
}