|
| 1 | +#include <AMS.h> |
| 2 | +#include <ATen/core/interned_strings.h> |
| 3 | +#include <c10/core/TensorOptions.h> |
| 4 | +#include <torch/types.h> |
| 5 | + |
| 6 | +#include <cstring> |
| 7 | +#include <iostream> |
| 8 | +#include <ml/surrogate.hpp> |
| 9 | +#include <umpire/ResourceManager.hpp> |
| 10 | +#include <umpire/Umpire.hpp> |
| 11 | +#include <vector> |
| 12 | +#include <wf/resource_manager.hpp> |
| 13 | + |
| 14 | +#define SIZE (32L) |
| 15 | + |
| 16 | +template <typename T> |
| 17 | +bool inference(SurrogateModel<T> &model, |
| 18 | + AMSResourceType resource, |
| 19 | + std::string update_path) |
| 20 | +{ |
| 21 | + using namespace ams; |
| 22 | + |
| 23 | + std::vector<const T *> inputs; |
| 24 | + std::vector<T *> outputs; |
| 25 | + auto &ams_rm = ams::ResourceManager::getInstance(); |
| 26 | + |
| 27 | + for (int i = 0; i < 2; i++) |
| 28 | + inputs.push_back(ams_rm.allocate<T>(SIZE, resource)); |
| 29 | + |
| 30 | + for (int i = 0; i < 4 * 2; i++) |
| 31 | + outputs.push_back(ams_rm.allocate<T>(SIZE, resource)); |
| 32 | + |
| 33 | + for (int repeat = 0; repeat < 2; repeat++) { |
| 34 | + model.evaluate( |
| 35 | + SIZE, inputs.size(), 4, inputs.data(), &(outputs.data()[repeat * 4])); |
| 36 | + if (repeat == 0) model.update(update_path); |
| 37 | + } |
| 38 | + |
| 39 | + // Verify |
| 40 | + bool errors = false; |
| 41 | + for (int i = 0; i < 4; i++) { |
| 42 | + T *first_model_out = outputs[i]; |
| 43 | + T *second_model_out = outputs[i + 4]; |
| 44 | + if (resource == AMSResourceType::DEVICE) { |
| 45 | + first_model_out = ams_rm.allocate<T>(SIZE, AMSResourceType::HOST); |
| 46 | + second_model_out = ams_rm.allocate<T>(SIZE, AMSResourceType::HOST); |
| 47 | + ams_rm.copy(outputs[i], first_model_out, SIZE * sizeof(T)); |
| 48 | + ams_rm.copy(outputs[i + 4], second_model_out, SIZE * sizeof(T)); |
| 49 | + } |
| 50 | + |
| 51 | + for (int j = 0; j < SIZE; j++) { |
| 52 | + if (first_model_out[j] != 1.0) { |
| 53 | + errors = true; |
| 54 | + std::cout << "One Model " << first_model_out << " " << j << " " |
| 55 | + << first_model_out[j] << "\n"; |
| 56 | + } |
| 57 | + if (second_model_out[j] != 0.0) { |
| 58 | + std::cout << "Zero Model " << second_model_out << " " << j << " " |
| 59 | + << second_model_out[j] << "\n"; |
| 60 | + errors = true; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (resource == AMSResourceType::DEVICE) { |
| 65 | + ams_rm.deallocate(first_model_out, resource); |
| 66 | + ams_rm.deallocate(second_model_out, resource); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (int i = 0; i < 2; i++) |
| 71 | + ams_rm.deallocate(const_cast<T *>(inputs[i]), resource); |
| 72 | + |
| 73 | + for (int i = 0; i < 4 * 2; i++) |
| 74 | + ams_rm.deallocate(outputs[i], resource); |
| 75 | + |
| 76 | + return errors; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +int main(int argc, char *argv[]) |
| 81 | +{ |
| 82 | + using namespace ams; |
| 83 | + auto &ams_rm = ams::ResourceManager::getInstance(); |
| 84 | + int use_device = std::atoi(argv[1]); |
| 85 | + char *data_type = argv[2]; |
| 86 | + char *zero_model = argv[3]; |
| 87 | + char *one_model = argv[4]; |
| 88 | + char *swap; |
| 89 | + |
| 90 | + AMSResourceType resource = AMSResourceType::HOST; |
| 91 | + if (use_device == 1) { |
| 92 | + resource = AMSResourceType::DEVICE; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + ams_rm.init(); |
| 97 | + int ret = 0; |
| 98 | + if (std::strcmp("double", data_type) == 0) { |
| 99 | + std::shared_ptr<SurrogateModel<double>> model = |
| 100 | + SurrogateModel<double>::getInstance(one_model, resource); |
| 101 | + assert(model->is_double()); |
| 102 | + ret = inference<double>(*model, resource, zero_model); |
| 103 | + } else if (std::strcmp("single", data_type) == 0) { |
| 104 | + std::shared_ptr<SurrogateModel<float>> model = |
| 105 | + SurrogateModel<float>::getInstance(one_model, resource); |
| 106 | + assert(!model->is_double()); |
| 107 | + ret = inference<float>(*model, resource, zero_model); |
| 108 | + } |
| 109 | + std::cout << "Zero Model is " << zero_model << "\n"; |
| 110 | + std::cout << "One Model is " << one_model << "\n"; |
| 111 | + return ret; |
| 112 | +} |
0 commit comments