Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
248 lines (221 loc) · 6.4 KB

File metadata and controls

248 lines (221 loc) · 6.4 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package lambdacloud.core;
import java.util.Map;
import lambdacloud.core.graph.GraphBuilder;
import lambdacloud.core.graph.Node;
import symjava.symbolic.Expr;
import symjava.symbolic.Matrix;
import symjava.symbolic.Vector;
public class Session {
CloudConfig config;
public Session() {
config = CloudConfig.setGlobalConfig("job_local.conf");
}
public Session(CloudConfig config) {
this.config = config;
}
/**
* In this mode (either in local or on server), we always fetch data to local before evaluating a function
*
* @param expr
* @param dict
* @return
*/
public double runSimple(Expr expr, Map<String, Double> dict) {
//CloudConfig.setGlobalTarget("job_local.conf");
GraphBuilder gb = new GraphBuilder(config);
Node n = gb.build(expr);
return runSimple(n, dict);
}
/**
*
* @param expr
* @param dict
* @return
*/
public double runSimpleAsync(Expr expr, Map<String, Double> dict) {
//CloudConfig.setGlobalTarget("job_local.conf");
GraphBuilder gb = new GraphBuilder(config);
Node n = gb.build(expr);
CloudSD output = runSimpleAsync(n, dict);
output.fetch();
return output.getData(0);
}
/**
* For matrix and vector
*
* Allow data fetch from server node
*
* @param expr
* @param dict
* @return
*/
public CloudSD runVec(Expr expr, Map<String, double[]> dict) {
//CloudConfig.setGlobalTarget("job_local.conf");
GraphBuilder gb = new GraphBuilder(config);
Node n = gb.build(expr);
return runVec(n, dict);
}
/**
* Allow data fetch from server node
* It is use to test for scalar. The optimization is suitable for matrix and vectors
*
* @param expr
* @param dict
* @return
*/
public CloudSD runOpt(Expr expr, Map<String, Double> dict) {
//CloudConfig.setGlobalTarget("job_local.conf");
GraphBuilder gb = new GraphBuilder(config);
Node n = gb.build(expr);
return runOpt(n, dict);
}
public CloudSD runVec(Node root, Map<String, double[]> dict) {
int nArgs = root.args.size();
CloudSD[] inputs = new CloudSD[nArgs];
for(int i=0; i<nArgs; i++) {
Expr arg = root.args.get(i);
double[] d = dict.get(arg.toString());
if(d == null && arg.getParent() != null) {
// Extract sub-matrix or sub-vector from parent matrix or vector
d = dict.get(arg.getParent().toString());
if(d != null) {
if(arg instanceof Matrix) {
Matrix m = (Matrix)arg;
Matrix p = (Matrix)arg.getParent();
Jama.Matrix mat = new Jama.Matrix(d, p.nRow);
d = mat.getMatrix(m.nRowStart, m.nRowStart+m.nRow-1, m.nColStart, m.nColStart+m.nCol-1).getColumnPackedCopy();
} else if (arg instanceof Vector) {
Vector m = (Vector)arg;
Vector p = (Vector)arg.getParent();
Jama.Matrix mat = new Jama.Matrix(d, p.nDim);
d = mat.getMatrix(m.nStart, m.nStart+m.nDim-1, 0, 0).getColumnPackedCopy();
}
}
}
if(d == null) {
Node child = root.children.get(root.args.get(i).toString());
CloudSD ret = runVec(child, dict);
inputs[i] = ret;
} else {
inputs[i] = new CloudSD(arg.toString()).init(d);
}
}
CloudSD output;
output = new CloudSD();
System.out.print(">>Session eval: "+root.cfunc.getFullName()+"="+root+"; args:\n[");
for(int i=0; i<inputs.length; i++) {
System.out.println("\t"+inputs[i]);
}
System.out.print("]");
root.cfunc.apply(output, inputs);
// if(output.fetch()) {
// System.out.print("Return: [");
// for(double d : output.getData()) {
// System.out.print(d+" ");
// }
// System.out.println("]");
// }
return output;
}
/**
* TODO
* run() return a double
* run() could return an array of Tensor
*
* @param root
* @param dict
* @return
*/
public double runSimple(Node root, Map<String, Double> dict) {
double[] args = new double[root.args.size()];
for(int i=0; i<root.args.size(); i++) {
Double d = dict.get(root.args.get(i).toString());
if(d == null) {
Node child = root.children.get(root.args.get(i).toString());
args[i] = runSimple(child, dict);
} else {
args[i] = d;
}
}
CloudSD input = new CloudSD().init(args);
CloudSD output = new CloudSD();
root.cfunc.apply(output, input);
//Will block
output.fetch();
return output.getData(0);
}
public CloudSD runSimpleAsync(Node root, Map<String, Double> dict) {
CloudSD[] args = new CloudSD[root.args.size()];
for(int i=0; i<root.args.size(); i++) {
Double d = dict.get(root.args.get(i).toString());
if(d == null) {
Node child = root.children.get(root.args.get(i).toString());
args[i] = runSimpleAsync(child, dict);
} else {
args[i] = new CloudSD(config).init(d);
}
}
for(CloudSD csd : args) {
csd.fetch();
}
CloudSD output = new CloudSD();
root.cfunc.apply(output, args);
return output;
}
/**
* The run_opt method does not fetch a CloudSD from another server.
* That is to say, only the name is passed to apply() function.
* This avoid to fetch data from different server every time.
*
* @param root
* @param dict
* @return
*/
public CloudSD runOpt(Node root, Map<String, Double> dict) {
CloudSD[] args = new CloudSD[root.args.size()];
for(int i=0; i<root.args.size(); i++) {
Double d = dict.get(root.args.get(i).toString());
if(d == null) {
Node child = root.children.get(root.args.get(i).toString());
args[i] = runOpt(child, dict);
} else {
//for arguments in the dict will be changed to a CloudSD
args[i] = new CloudSD(root.args.get(i).toString()).init(new double[]{d});
}
}
CloudSD output = new CloudSD();
System.out.print(">>Session eval: "+root.cfunc.getFullName()+"="+root+"; args:\n[");
//Utils.joinLabels(args, ", ");
for(int i=0; i<args.length; i++) {
System.out.println("\t"+args[i]);
}
System.out.println("]");
root.cfunc.apply(output, args);
return output;
}
/**
* Run locally in one thread
* @param expr
* @param dict
* @return
*/
public double runLocal(Expr expr, Map<String, Double> dict) {
GraphBuilder gb = new GraphBuilder(config);
gb.enableRunLocal();
Node n = gb.build(expr);
return runLocal(n, dict);
}
public double runLocal(Node root, Map<String, Double> dict) {
double[] args = new double[root.args.size()];
for(int i=0; i<root.args.size(); i++) {
Double d = dict.get(root.args.get(i).toString());
if(d == null) {
Node child = root.children.get(root.args.get(i).toString());
args[i] = runLocal(child, dict);
} else {
args[i] = d;
}
}
return root.func.apply(args);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.