1
+ #include < stdexcept>
1
2
#ifdef __AMS_ENABLE_MPI__
2
3
#include < mpi.h>
3
4
#endif
7
8
#include < cstdlib>
8
9
#include < cstring>
9
10
#include < limits>
10
- #include < ml/uq.hpp>
11
11
#include < wf/basedb.hpp>
12
12
#include < wf/resource_manager.hpp>
13
13
14
14
#include " AMS.h"
15
- #include " wf/debug.h"
15
+ #include " ml/surrogate.hpp"
16
+
17
+ using namespace ams ;
16
18
17
19
AMSDType getDataType (char *d_type)
18
20
{
@@ -34,7 +36,7 @@ struct Problem {
34
36
int multiplier;
35
37
Problem (int ni, int no) : num_inputs(ni), num_outputs(no), multiplier(100 ) {}
36
38
37
- void run (long num_elements, DType **inputs, DType **outputs)
39
+ void run (long num_elements, DType **inputs, DType **outputs, DType scalar )
38
40
{
39
41
for (int i = 0 ; i < num_elements; i++) {
40
42
DType sum = 0 ;
@@ -43,13 +45,13 @@ struct Problem {
43
45
}
44
46
45
47
for (int j = 0 ; j < num_outputs; j++) {
46
- outputs[j][i] = sum;
48
+ outputs[j][i] = sum + scalar ;
47
49
}
48
50
}
49
51
}
50
52
51
53
52
- const DType *initialize_inputs (DType *inputs, long length)
54
+ DType *initialize_inputs (DType *inputs, long length)
53
55
{
54
56
for (int i = 0 ; i < length; i++) {
55
57
inputs[i] = static_cast <DType>(i);
@@ -60,65 +62,83 @@ struct Problem {
60
62
void ams_run (AMSExecutor &wf,
61
63
AMSResourceType resource,
62
64
int iterations,
63
- int num_elements)
65
+ int num_elements,
66
+ int scalar)
64
67
{
65
68
for (int i = 0 ; i < iterations; i++) {
66
69
int elements = num_elements; // * ((DType)(rand()) / RAND_MAX) + 1;
67
- std::vector< const DType *> inputs ;
68
- std::vector<DType *> outputs ;
70
+ SmallVector<AMSTensor> input_tensors ;
71
+ SmallVector<AMSTensor> output_tensors ;
69
72
70
73
// Allocate Input memory
71
74
for (int j = 0 ; j < num_inputs; j++) {
72
75
DType *data = new DType[elements];
73
- inputs.push_back (initialize_inputs (data, elements));
76
+ DType *ptr = initialize_inputs (data, elements);
77
+ input_tensors.push_back (AMSTensor::view (
78
+ ptr,
79
+ SmallVector<ams::AMSTensor::IntDimType>({num_elements, 1 }),
80
+ SmallVector<ams::AMSTensor::IntDimType>({1 , 1 }),
81
+ resource));
74
82
}
75
83
76
84
// Allocate Output memory
77
85
for (int j = 0 ; j < num_outputs; j++) {
78
- outputs.push_back (new DType[elements]);
86
+ auto tmp = new DType[elements];
87
+ output_tensors.push_back (AMSTensor::view (
88
+ initialize_inputs (tmp, elements),
89
+ SmallVector<ams::AMSTensor::IntDimType>({num_elements, 1 }),
90
+ SmallVector<ams::AMSTensor::IntDimType>({1 , 1 }),
91
+ resource));
79
92
}
80
93
81
- AMSExecute (wf,
82
- (void *)this ,
83
- elements,
84
- reinterpret_cast <const void **>(inputs.data ()),
85
- reinterpret_cast <void **>(outputs.data ()),
86
- inputs.size (),
87
- outputs.size ());
88
-
89
- for (int j = 0 ; j < num_outputs; j++) {
90
- delete[] outputs[j];
94
+ EOSLambda OrigComputation =
95
+ [&](const ams::SmallVector<ams::AMSTensor> &ams_ins,
96
+ ams::SmallVector<ams::AMSTensor> &ams_inouts,
97
+ ams::SmallVector<ams::AMSTensor> &ams_outs) {
98
+ DType *ins[num_inputs];
99
+ DType *outs[num_outputs];
100
+ if (num_inputs != ams_ins.size ())
101
+ throw std::runtime_error (
102
+ " Expecting dimensions of inputs to remain the same" );
103
+ else if (num_outputs != ams_outs.size ())
104
+ throw std::runtime_error (
105
+ " Expecting dimensions of outputs to remain the same" );
106
+
107
+ // Here I can use domain knowledge (inouts is empty)
108
+ int num_elements = ams_ins[0 ].shape ()[0 ];
109
+ for (int i = 0 ; i < num_inputs; i++) {
110
+ ins[i] = ams_ins[i].data <DType>();
111
+ if (ams_ins[i].shape ()[0 ] != num_elements)
112
+ throw std::runtime_error (
113
+ " Expected tensors to have the same shape" );
114
+ }
115
+ for (int i = 0 ; i < num_outputs; i++) {
116
+ outs[i] = ams_outs[i].data <DType>();
117
+ if (ams_outs[i].shape ()[0 ] != num_elements)
118
+ throw std::runtime_error (
119
+ " Expected tensors to have the same shape" );
120
+ }
121
+ run (num_elements, ins, outs, scalar);
122
+ };
123
+
124
+ ams::SmallVector<AMSTensor> inouts;
125
+ AMSExecute (wf, OrigComputation, input_tensors, inouts, output_tensors);
126
+
127
+ for (int i = 0 ; i < input_tensors.size (); i++) {
128
+ delete input_tensors[i].data <DType>();
91
129
}
92
130
93
131
94
- for (int j = 0 ; j < num_inputs; j ++) {
95
- delete[] inputs[j] ;
132
+ for (int i = 0 ; i < output_tensors. size (); i ++) {
133
+ delete output_tensors[i]. data <DType>() ;
96
134
}
97
135
}
98
136
}
99
137
};
100
138
101
- void callBackDouble (void *cls, long elements, void **inputs, void **outputs)
102
- {
103
- std::cout << " Called the double model\n " ;
104
- static_cast <Problem<double > *>(cls)->run (elements,
105
- (double **)(inputs),
106
- (double **)(outputs));
107
- }
108
-
109
-
110
- void callBackSingle (void *cls, long elements, void **inputs, void **outputs)
111
- {
112
- std::cout << " Called the single model\n " ;
113
- static_cast <Problem<float > *>(cls)->run (elements,
114
- (float **)(inputs),
115
- (float **)(outputs));
116
- }
117
-
118
-
119
139
int main (int argc, char **argv)
120
140
{
121
- if (argc != 15 ) {
141
+ if (argc != 12 ) {
122
142
std::cout << " Wrong cli\n " ;
123
143
std::cout << argv[0 ]
124
144
<< " use_device(0|1) num_inputs num_outputs model_path "
@@ -131,20 +151,17 @@ int main(int argc, char **argv)
131
151
132
152
133
153
int use_device = std::atoi (argv[1 ]);
134
- int num_inputs_1 = std::atoi (argv[2 ]);
135
- int num_outputs_1 = std::atoi (argv[3 ]);
136
- char *model_path_1 = argv[4 ];
154
+ int num_inputs = std::atoi (argv[2 ]);
155
+ int num_outputs = std::atoi (argv[3 ]);
156
+ char *model_path = argv[4 ];
137
157
AMSDType data_type = getDataType (argv[5 ]);
138
158
std::string uq_name = std::string (argv[6 ]);
139
- const AMSUQPolicy uq_policy = BaseUQ ::UQPolicyFromStr (uq_name);
159
+ const AMSUQPolicy uq_policy = UQ ::UQPolicyFromStr (uq_name);
140
160
float threshold = std::atof (argv[7 ]);
141
161
int num_iterations = std::atoi (argv[8 ]);
142
162
int avg_elements = std::atoi (argv[9 ]);
143
163
std::string db_type_str = std::string (argv[10 ]);
144
164
std::string fs_path = std::string (argv[11 ]);
145
- int num_inputs_2 = std::atoi (argv[12 ]);
146
- int num_outputs_2 = std::atoi (argv[13 ]);
147
- char *model_path_2 = argv[14 ];
148
165
AMSDBType db_type = ams::db::getDBType (db_type_str);
149
166
AMSResourceType resource = AMSResourceType::AMS_HOST;
150
167
srand (time (NULL ));
@@ -156,68 +173,28 @@ int main(int argc, char **argv)
156
173
uq_policy == AMSUQPolicy::AMS_RANDOM) &&
157
174
" Test only supports duq models" );
158
175
159
- AMSCAbstrModel model_descr_1 = AMSRegisterAbstractModel (
160
- " test_1" , uq_policy, threshold, model_path_1, nullptr , " test_1" , -1 );
161
-
162
- AMSCAbstrModel model_descr_2 = AMSRegisterAbstractModel (
163
- " test_2" , uq_policy, threshold, model_path_2, nullptr , " test_2" , -1 );
164
-
165
-
166
- if (data_type == AMSDType::AMS_SINGLE) {
167
- Problem<float > prob1 (num_inputs_1, num_outputs_1);
168
- Problem<float > prob2 (num_inputs_2, num_outputs_2);
169
-
170
- AMSExecutor wf_1 = AMSCreateExecutor (model_descr_1,
171
- AMSDType::AMS_SINGLE,
172
- resource,
173
- (AMSPhysicFn)callBackSingle,
174
- 0 ,
175
- 1 );
176
-
177
- AMSExecutor wf_2 = AMSCreateExecutor (model_descr_2,
178
- AMSDType::AMS_SINGLE,
179
- resource,
180
- (AMSPhysicFn)callBackSingle,
181
- 0 ,
182
- 1 );
183
-
184
- for (int i = 0 ; i < num_iterations; i++) {
185
- size_t elems =
186
- (static_cast <double >(rand ()) / RAND_MAX) * 2 * avg_elements -
187
- avg_elements + avg_elements;
188
- prob1.ams_run (wf_1, resource, 1 , elems);
189
- size_t elems1 =
190
- (static_cast <double >(rand ()) / RAND_MAX) * 2 * avg_elements -
191
- avg_elements + avg_elements;
192
- prob2.ams_run (wf_2, resource, 1 , elems1);
193
- }
194
- } else {
195
- Problem<double > prob1 (num_inputs_1, num_outputs_1);
196
- Problem<double > prob2 (num_inputs_2, num_outputs_2);
197
-
198
- AMSExecutor wf_1 = AMSCreateExecutor (model_descr_1,
199
- AMSDType::AMS_DOUBLE,
200
- resource,
201
- (AMSPhysicFn)callBackDouble,
202
- 0 ,
203
- 1 );
204
-
205
- AMSExecutor wf_2 = AMSCreateExecutor (model_descr_2,
206
- AMSDType::AMS_DOUBLE,
207
- resource,
208
- (AMSPhysicFn)callBackDouble,
209
- 0 ,
210
- 1 );
211
-
212
- for (int i = 0 ; i < num_iterations; i++) {
213
- size_t elems = avg_elements;
214
- // (static_cast<double>(rand()) / RAND_MAX) * 2 * avg_elements -
215
- // avg_elements + avg_elements;
216
- prob1.ams_run (wf_1, resource, 1 , elems);
217
- size_t elems1 = avg_elements;
218
- // (static_cast<double>(rand()) / RAND_MAX) * 2 * avg_elements -
219
- // avg_elements + avg_elements;
220
- prob2.ams_run (wf_2, resource, 1 , elems1);
176
+ AMSCAbstrModel model_descr = AMSRegisterAbstractModel (
177
+ " test_1" , uq_policy, threshold, nullptr , " test_1" );
178
+
179
+ AMSCAbstrModel model_descr1 = AMSRegisterAbstractModel (
180
+ " test_2" , uq_policy, threshold, nullptr , " test_2" );
181
+
182
+ std::cout << " Running with " << num_iterations << " \n " ;
183
+ AMSExecutor wf1 = AMSCreateExecutor (model_descr, 0 , 1 );
184
+ AMSExecutor wf2 = AMSCreateExecutor (model_descr1, 0 , 1 );
185
+ for (int i = 0 ; i < 10 ; i++) {
186
+ if (data_type == AMSDType::AMS_SINGLE) {
187
+ Problem<float > prob1 (num_inputs, num_outputs);
188
+ Problem<float > prob2 (num_inputs + 1 , num_outputs + 1 );
189
+
190
+
191
+ prob1.ams_run (wf1, resource, num_iterations, avg_elements, 0 );
192
+ prob2.ams_run (wf2, resource, num_iterations, avg_elements, 1 );
193
+ } else {
194
+ Problem<double > prob1 (num_inputs, num_outputs);
195
+ Problem<double > prob2 (num_inputs + 1 , num_outputs + 1 );
196
+ prob2.ams_run (wf2, resource, num_iterations, avg_elements, 1 );
197
+ prob1.ams_run (wf1, resource, num_iterations, avg_elements, 0 );
221
198
}
222
199
}
223
200
0 commit comments