Skip to content

Commit

Permalink
Test DNN created from apptainer image
Browse files Browse the repository at this point in the history
  • Loading branch information
valsdav committed Dec 19, 2023
1 parent 4579ebc commit 07c40c0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 35 deletions.
36 changes: 36 additions & 0 deletions PhysicsTools/PyTorch/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,39 @@
<use name="HeterogeneousCore/CUDAUtilities"/>
</bin>
</iftool>


<iftool name="cuda">
<bin name="testTorchFromBufferPinnedMemory" file="testTorchFromBufferPinnedMemory.cu">
<use name="boost_filesystem"/>
<use name="catch2"/>
<use name="cppunit"/>
<use name="cuda"/>
<use name="pytorch"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/ParameterSetReader"/>
<use name="FWCore/PluginManager"/>
<use name="FWCore/ServiceRegistry"/>
<use name="FWCore/Utilities"/>
<use name="HeterogeneousCore/CUDAServices"/>
<use name="HeterogeneousCore/CUDAUtilities"/>
</bin>
</iftool>


<iftool name="cuda">
<bin name="testTorchFromBufferModelEval" file="testTorchFromBufferModelEval.cu">
<use name="boost_filesystem"/>
<use name="catch2"/>
<use name="cppunit"/>
<use name="cuda"/>
<use name="pytorch"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/ParameterSetReader"/>
<use name="FWCore/PluginManager"/>
<use name="FWCore/ServiceRegistry"/>
<use name="FWCore/Utilities"/>
<use name="HeterogeneousCore/CUDAServices"/>
<use name="HeterogeneousCore/CUDAUtilities"/>
</bin>
</iftool>
15 changes: 13 additions & 2 deletions PhysicsTools/PyTorch/test/create_simple_dnn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import sys
import os
import torch

# prepare the datadir
if len(sys.argv) >= 2:
datadir = sys.argv[1]
else:
thisdir = os.path.dirname(os.path.abspath(__file__))
datadir = os.path.join(os.path.dirname(thisdir), "bin", "data")

os.makedirs(datadir, exist_ok=True)

class MyModule(torch.nn.Module):
def __init__(self, N, M):
super(MyModule, self).__init__()
Expand All @@ -15,6 +26,6 @@ def forward(self, input):

tm = torch.jit.trace(module.eval(), x)

print(tm.graph)
tm.save(f"{datadir}/simple_dnn.pt")

tm.save("simple_dnn.pt")
print("simple_dnn.pt created successfully!")
Binary file removed PhysicsTools/PyTorch/test/simple_dnn.pt
Binary file not shown.
23 changes: 20 additions & 3 deletions PhysicsTools/PyTorch/test/testBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,38 @@
class testBasePyTorch : public CppUnit::TestFixture {
public:
std::string dataPath_;
std::string testPath_;

void setUp();
void tearDown();
std::string cmsswPath(std::string path);

virtual void test() = 0;

virtual std::string pyScript() const = 0;
};

void testBasePyTorch::setUp() {
dataPath_ =
cmsswPath("/test/" + std::string(std::getenv("SCRAM_ARCH")) + "/" + boost::filesystem::unique_path().string());

// create the graph
testPath_ = cmsswPath("/src/PhysicsTools/PyTorch/test");
// create the graph using apptainer
std::string testPath = cmsswPath("/src/PhysicsTools/PyTorch/test");
std::string cmd = "apptainer exec -B " + cmsswPath("") +
" /cvmfs/unpacked.cern.ch/registry.hub.docker.com/cmsml/cmsml:3.11 python " + testPath + "/" +
pyScript() + " " + dataPath_;
std::cout << "cmd: " << cmd << std::endl;
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != NULL) {
result += buffer.data();
}
}
std::cout << std::endl << result << std::endl;
}

void testBasePyTorch::tearDown() {
Expand Down
24 changes: 20 additions & 4 deletions PhysicsTools/PyTorch/test/testBaseCUDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,39 @@
class testBasePyTorchCUDA : public CppUnit::TestFixture {
public:
std::string dataPath_;
std::string testPath_;

void setUp();
void tearDown();
std::string cmsswPath(std::string path);

virtual std::string pyScript() const = 0;

virtual void test() = 0;
};

void testBasePyTorchCUDA::setUp() {
dataPath_ =
cmsswPath("/test/" + std::string(std::getenv("SCRAM_ARCH")) + "/" + boost::filesystem::unique_path().string());

// create the graph
testPath_ = cmsswPath("/src/PhysicsTools/PyTorch/test");
// create the graph using apptainer
std::string testPath = cmsswPath("/src/PhysicsTools/PyTorch/test");
std::string cmd = "apptainer exec -B " + cmsswPath("") +
" /cvmfs/unpacked.cern.ch/registry.hub.docker.com/cmsml/cmsml:3.11 python " + testPath + "/" +
pyScript() + " " + dataPath_;
std::cout << "cmd: " << cmd << std::endl;
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != NULL) {
result += buffer.data();
}
}
std::cout << std::endl << result << std::endl;
}

void testBasePyTorchCUDA::tearDown() {
if (std::filesystem::exists(dataPath_)) {
std::filesystem::remove_all(dataPath_);
Expand Down
17 changes: 4 additions & 13 deletions PhysicsTools/PyTorch/test/testTorchSimpleDnn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class testSimpleDNN : public testBasePyTorch {
CPPUNIT_TEST_SUITE_END();

public:
std::string pyScript() const override;
void test() override;
};

CPPUNIT_TEST_SUITE_REGISTRATION(testSimpleDNN);

std::string testSimpleDNN::pyScript() const { return "create_simple_dnn.py"; }

void testSimpleDNN::test() {
std::string model_path = testPath_ + "/simple_dnn.pt";
std::string model_path = dataPath_ + "/simple_dnn.pt";
torch::Device device(torch::kCPU);
torch::jit::script::Module module;
try {
Expand All @@ -36,15 +39,3 @@ void testSimpleDNN::test() {
CPPUNIT_ASSERT(output.item<float_t>() == 110.);
std::cout << "ok\n";
}

// int main(int argc, const char* argv[]) {
// std::cout << "Running model on CPU" << std::endl;
// torch::Device cpu(torch::kCPU);
// runModel("/data/user/dvalsecc/simple_dnn.pt", cpu);

// std::cout << "Running model on CUDA" << std::endl;
// torch::Device cuda(torch::kCUDA);
// runModel("/data/user/dvalsecc/simple_dnn.pt", cuda);

// return 0;
// }
17 changes: 4 additions & 13 deletions PhysicsTools/PyTorch/test/testTorchSimpleDnnCUDA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ class testSimpleDNNCUDA : public testBasePyTorchCUDA {
CPPUNIT_TEST_SUITE_END();

public:
std::string pyScript() const override;
void test() override;
};

CPPUNIT_TEST_SUITE_REGISTRATION(testSimpleDNNCUDA);

std::string testSimpleDNNCUDA::pyScript() const { return "create_simple_dnn.py"; }

void testSimpleDNNCUDA::test() {
if (!cms::cudatest::testDevices())
return;
Expand All @@ -40,7 +43,7 @@ process.add_(cms.Service('CUDAService'))

std::cout << "Testing CUDA backend" << std::endl;

std::string model_path = testPath_ + "/simple_dnn.pt";
std::string model_path = dataPath_ + "/simple_dnn.pt";
torch::Device device(torch::kCUDA);
torch::jit::script::Module module;
try {
Expand All @@ -60,15 +63,3 @@ process.add_(cms.Service('CUDAService'))
CPPUNIT_ASSERT(output.item<float_t>() == 110.);
std::cout << "ok\n";
}

// int main(int argc, const char* argv[]) {
// std::cout << "Running model on CPU" << std::endl;
// torch::Device cpu(torch::kCPU);
// runModel("/data/user/dvalsecc/simple_dnn.pt", cpu);

// std::cout << "Running model on CUDA" << std::endl;
// torch::Device cuda(torch::kCUDA);
// runModel("/data/user/dvalsecc/simple_dnn.pt", cuda);

// return 0;
// }

0 comments on commit 07c40c0

Please sign in to comment.