-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides functionality to update a model at execution time (#49)
- Loading branch information
Showing
10 changed files
with
242 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <AMS.h> | ||
#include <ATen/core/interned_strings.h> | ||
#include <c10/core/TensorOptions.h> | ||
#include <torch/types.h> | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
#include <ml/surrogate.hpp> | ||
#include <umpire/ResourceManager.hpp> | ||
#include <umpire/Umpire.hpp> | ||
#include <vector> | ||
#include <wf/resource_manager.hpp> | ||
|
||
#define SIZE (32L) | ||
|
||
template <typename T> | ||
bool inference(SurrogateModel<T> &model, | ||
AMSResourceType resource, | ||
std::string update_path) | ||
{ | ||
using namespace ams; | ||
|
||
std::vector<const T *> inputs; | ||
std::vector<T *> outputs; | ||
auto &ams_rm = ams::ResourceManager::getInstance(); | ||
|
||
for (int i = 0; i < 2; i++) | ||
inputs.push_back(ams_rm.allocate<T>(SIZE, resource)); | ||
|
||
for (int i = 0; i < 4 * 2; i++) | ||
outputs.push_back(ams_rm.allocate<T>(SIZE, resource)); | ||
|
||
for (int repeat = 0; repeat < 2; repeat++) { | ||
model.evaluate( | ||
SIZE, inputs.size(), 4, inputs.data(), &(outputs.data()[repeat * 4])); | ||
if (repeat == 0) model.update(update_path); | ||
} | ||
|
||
// Verify | ||
bool errors = false; | ||
for (int i = 0; i < 4; i++) { | ||
T *first_model_out = outputs[i]; | ||
T *second_model_out = outputs[i + 4]; | ||
if (resource == AMSResourceType::DEVICE) { | ||
first_model_out = ams_rm.allocate<T>(SIZE, AMSResourceType::HOST); | ||
second_model_out = ams_rm.allocate<T>(SIZE, AMSResourceType::HOST); | ||
ams_rm.copy(outputs[i], first_model_out, SIZE * sizeof(T)); | ||
ams_rm.copy(outputs[i + 4], second_model_out, SIZE * sizeof(T)); | ||
} | ||
|
||
for (int j = 0; j < SIZE; j++) { | ||
if (first_model_out[j] != 1.0) { | ||
errors = true; | ||
std::cout << "One Model " << first_model_out << " " << j << " " | ||
<< first_model_out[j] << "\n"; | ||
} | ||
if (second_model_out[j] != 0.0) { | ||
std::cout << "Zero Model " << second_model_out << " " << j << " " | ||
<< second_model_out[j] << "\n"; | ||
errors = true; | ||
} | ||
} | ||
|
||
if (resource == AMSResourceType::DEVICE) { | ||
ams_rm.deallocate(first_model_out, resource); | ||
ams_rm.deallocate(second_model_out, resource); | ||
} | ||
} | ||
|
||
for (int i = 0; i < 2; i++) | ||
ams_rm.deallocate(const_cast<T *>(inputs[i]), resource); | ||
|
||
for (int i = 0; i < 4 * 2; i++) | ||
ams_rm.deallocate(outputs[i], resource); | ||
|
||
return errors; | ||
} | ||
|
||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
using namespace ams; | ||
auto &ams_rm = ams::ResourceManager::getInstance(); | ||
int use_device = std::atoi(argv[1]); | ||
char *data_type = argv[2]; | ||
char *zero_model = argv[3]; | ||
char *one_model = argv[4]; | ||
char *swap; | ||
|
||
AMSResourceType resource = AMSResourceType::HOST; | ||
if (use_device == 1) { | ||
resource = AMSResourceType::DEVICE; | ||
} | ||
|
||
|
||
ams_rm.init(); | ||
int ret = 0; | ||
if (std::strcmp("double", data_type) == 0) { | ||
std::shared_ptr<SurrogateModel<double>> model = | ||
SurrogateModel<double>::getInstance(one_model, resource); | ||
assert(model->is_double()); | ||
ret = inference<double>(*model, resource, zero_model); | ||
} else if (std::strcmp("single", data_type) == 0) { | ||
std::shared_ptr<SurrogateModel<float>> model = | ||
SurrogateModel<float>::getInstance(one_model, resource); | ||
assert(!model->is_double()); | ||
ret = inference<float>(*model, resource, zero_model); | ||
} | ||
std::cout << "Zero Model is " << zero_model << "\n"; | ||
std::cout << "One Model is " << one_model << "\n"; | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import torch | ||
import os | ||
import sys | ||
import numpy as np | ||
from torch.autograd import Variable | ||
from torch import jit | ||
|
||
class ConstantModel(torch.nn.Module): | ||
def __init__(self, inputSize, outputSize, constant): | ||
super(ConstantModel, self).__init__() | ||
self.linear = torch.nn.Linear(inputSize, outputSize) | ||
self.linear.weight.data.fill_(0.0) | ||
self.linear.bias.data.fill_(constant) | ||
|
||
def forward(self, x): | ||
y = self.linear(x) | ||
return y | ||
|
||
def main(args): | ||
inputDim = int(args[1]) | ||
outputDim = int(args[2]) | ||
device = args[3] | ||
enable_cuda = True | ||
if device == "cuda": | ||
enable_cuda = True | ||
suffix = '_gpu' | ||
elif device == "cpu": | ||
enable_cuda = False | ||
suffix = '_cpu' | ||
|
||
model = ConstantModel(inputDim, outputDim, 1.0).double() | ||
if torch.cuda.is_available() and enable_cuda: | ||
model = model.cuda() | ||
|
||
model.eval() | ||
with torch.jit.optimized_execution(True): | ||
traced = torch.jit.trace(model, (torch.randn(inputDim, dtype=torch.double), )) | ||
traced.save(f"ConstantOneModel_{suffix}.pt") | ||
|
||
model = ConstantModel(inputDim, outputDim, 0.0).double() | ||
if torch.cuda.is_available() and enable_cuda: | ||
model = model.cuda() | ||
|
||
model.eval() | ||
with torch.jit.optimized_execution(True): | ||
traced = torch.jit.trace(model, (torch.randn(inputDim, dtype=torch.double), )) | ||
traced.save(f"ConstantZeroModel_{suffix}.pt") | ||
|
||
inputs = Variable(torch.from_numpy(np.zeros((1, inputDim)))) | ||
zero_model = jit.load(f"ConstantZeroModel_{suffix}.pt") | ||
print("ZeroModel", zero_model(inputs)) | ||
|
||
one_model = jit.load(f"ConstantOneModel_{suffix}.pt") | ||
print("OneModel", one_model(inputs)) | ||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
main(sys.argv) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters