Skip to content

Commit 330a109

Browse files
committed
Adding extra tests for C interface
1 parent 60dfdd0 commit 330a109

File tree

3 files changed

+95
-111
lines changed

3 files changed

+95
-111
lines changed

tests/AMSlib/ams_interface/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ BUILD_TEST(ams_ete_broadcast ams_ete_broadcast.cpp)
178178
INTEGRATION_TEST()
179179
BUILD_TEST(ams_end_to_end_env ams_ete_env.cpp)
180180
INTEGRATION_TEST_ENV()
181-
#BUILD_TEST(ams_multi_model_end_to_end ams_multi_model_ete.cpp)
181+
BUILD_TEST(ams_multi_model_end_to_end ams_multi_model_ete.cpp)
182+
ADD_API_UNIT_TEST(API_DIRECT_QUERY AMS::API::MultiModelDefine::Random::Double::DB::OnlyPhysics::None::HOST "${CMAKE_CURRENT_BINARY_DIR}/ams_multi_model_end_to_end 0 8 8 ${TORCH_MODEL_DIR}/linear_scripted_single_cpu_random.pt \"double\" \"random\" 0.0 1 128 \"none\" \"./\"; python3 ${CMAKE_CURRENT_SOURCE_DIR}/verify_ete.py 0 8 8 ${CMAKE_CURRENT_SOURCE_DIR}/linear_scripted_cpu.pt \"double\" \"random\" 0.0 1 128 \"none\" \"./\"")
183+
184+
185+
BUILD_TEST(ams_multi_model_end_to_end_env ams_multi_model_ete_env.cpp)
186+
ADD_API_UNIT_TEST(APIEnvAPI AMS::ENV::MultiModelDefine::Random10::Random50::Double::DB::${db_type}::HOST "AMS_OBJECTS=${JSON_FP} ${CMAKE_CURRENT_BINARY_DIR}/ams_end_to_end_env 0 8 8 \"double\" 1 128 app_random_10 app_random_50;AMS_OBJECTS=${JSON_FP} python3 ${CMAKE_CURRENT_SOURCE_DIR}/verify_ete.py 0 8 8 \"double\" 128 app_random_10 app_random_50")
187+
182188

183189
#BUILD_TEST(ams_rmq ams_rmq_env.cpp)
184190
#INTEGRATION_TEST_RMQ()

tests/AMSlib/ams_interface/ams_ete.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "AMS.h"
1515
#include "ml/surrogate.hpp"
16+
#include "wf/debug.h"
1617

1718
using namespace ams;
1819

tests/AMSlib/ams_interface/ams_multi_model_ete.cpp

+87-110
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdexcept>
12
#ifdef __AMS_ENABLE_MPI__
23
#include <mpi.h>
34
#endif
@@ -7,12 +8,13 @@
78
#include <cstdlib>
89
#include <cstring>
910
#include <limits>
10-
#include <ml/uq.hpp>
1111
#include <wf/basedb.hpp>
1212
#include <wf/resource_manager.hpp>
1313

1414
#include "AMS.h"
15-
#include "wf/debug.h"
15+
#include "ml/surrogate.hpp"
16+
17+
using namespace ams;
1618

1719
AMSDType getDataType(char *d_type)
1820
{
@@ -34,7 +36,7 @@ struct Problem {
3436
int multiplier;
3537
Problem(int ni, int no) : num_inputs(ni), num_outputs(no), multiplier(100) {}
3638

37-
void run(long num_elements, DType **inputs, DType **outputs)
39+
void run(long num_elements, DType **inputs, DType **outputs, DType scalar)
3840
{
3941
for (int i = 0; i < num_elements; i++) {
4042
DType sum = 0;
@@ -43,13 +45,13 @@ struct Problem {
4345
}
4446

4547
for (int j = 0; j < num_outputs; j++) {
46-
outputs[j][i] = sum;
48+
outputs[j][i] = sum + scalar;
4749
}
4850
}
4951
}
5052

5153

52-
const DType *initialize_inputs(DType *inputs, long length)
54+
DType *initialize_inputs(DType *inputs, long length)
5355
{
5456
for (int i = 0; i < length; i++) {
5557
inputs[i] = static_cast<DType>(i);
@@ -60,65 +62,83 @@ struct Problem {
6062
void ams_run(AMSExecutor &wf,
6163
AMSResourceType resource,
6264
int iterations,
63-
int num_elements)
65+
int num_elements,
66+
int scalar)
6467
{
6568
for (int i = 0; i < iterations; i++) {
6669
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;
6972

7073
// Allocate Input memory
7174
for (int j = 0; j < num_inputs; j++) {
7275
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));
7482
}
7583

7684
// Allocate Output memory
7785
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));
7992
}
8093

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>();
91129
}
92130

93131

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>();
96134
}
97135
}
98136
}
99137
};
100138

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-
119139
int main(int argc, char **argv)
120140
{
121-
if (argc != 15) {
141+
if (argc != 12) {
122142
std::cout << "Wrong cli\n";
123143
std::cout << argv[0]
124144
<< " use_device(0|1) num_inputs num_outputs model_path "
@@ -131,20 +151,17 @@ int main(int argc, char **argv)
131151

132152

133153
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];
137157
AMSDType data_type = getDataType(argv[5]);
138158
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);
140160
float threshold = std::atof(argv[7]);
141161
int num_iterations = std::atoi(argv[8]);
142162
int avg_elements = std::atoi(argv[9]);
143163
std::string db_type_str = std::string(argv[10]);
144164
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];
148165
AMSDBType db_type = ams::db::getDBType(db_type_str);
149166
AMSResourceType resource = AMSResourceType::AMS_HOST;
150167
srand(time(NULL));
@@ -156,68 +173,28 @@ int main(int argc, char **argv)
156173
uq_policy == AMSUQPolicy::AMS_RANDOM) &&
157174
"Test only supports duq models");
158175

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);
221198
}
222199
}
223200

0 commit comments

Comments
 (0)