diff --git a/PhysicsTools/PyTorch/test/BuildFile.xml b/PhysicsTools/PyTorch/test/BuildFile.xml index 01aeb83b73738..4d473fc91c191 100644 --- a/PhysicsTools/PyTorch/test/BuildFile.xml +++ b/PhysicsTools/PyTorch/test/BuildFile.xml @@ -22,3 +22,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PhysicsTools/PyTorch/test/create_simple_dnn.py b/PhysicsTools/PyTorch/test/create_simple_dnn.py index 868980d910102..aeb2a16449f75 100644 --- a/PhysicsTools/PyTorch/test/create_simple_dnn.py +++ b/PhysicsTools/PyTorch/test/create_simple_dnn.py @@ -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__() @@ -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!") diff --git a/PhysicsTools/PyTorch/test/simple_dnn.pt b/PhysicsTools/PyTorch/test/simple_dnn.pt deleted file mode 100644 index d4f602ea0b07c..0000000000000 Binary files a/PhysicsTools/PyTorch/test/simple_dnn.pt and /dev/null differ diff --git a/PhysicsTools/PyTorch/test/testBase.h b/PhysicsTools/PyTorch/test/testBase.h index 03998bf885897..70e93a0447aef 100644 --- a/PhysicsTools/PyTorch/test/testBase.h +++ b/PhysicsTools/PyTorch/test/testBase.h @@ -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 buffer; + std::string result; + std::shared_ptr 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() { diff --git a/PhysicsTools/PyTorch/test/testBaseCUDA.h b/PhysicsTools/PyTorch/test/testBaseCUDA.h index c7d643c113da4..80b5ce868f73d 100644 --- a/PhysicsTools/PyTorch/test/testBaseCUDA.h +++ b/PhysicsTools/PyTorch/test/testBaseCUDA.h @@ -27,12 +27,13 @@ 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; }; @@ -40,10 +41,25 @@ 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 buffer; + std::string result; + std::shared_ptr 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_); diff --git a/PhysicsTools/PyTorch/test/testTorchSimpleDnn.cc b/PhysicsTools/PyTorch/test/testTorchSimpleDnn.cc index e28803a52dafd..37aba47a38e9a 100644 --- a/PhysicsTools/PyTorch/test/testTorchSimpleDnn.cc +++ b/PhysicsTools/PyTorch/test/testTorchSimpleDnn.cc @@ -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 { @@ -36,15 +39,3 @@ void testSimpleDNN::test() { CPPUNIT_ASSERT(output.item() == 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; -// } diff --git a/PhysicsTools/PyTorch/test/testTorchSimpleDnnCUDA.cc b/PhysicsTools/PyTorch/test/testTorchSimpleDnnCUDA.cc index 4378d2215e491..c14f578afce55 100644 --- a/PhysicsTools/PyTorch/test/testTorchSimpleDnnCUDA.cc +++ b/PhysicsTools/PyTorch/test/testTorchSimpleDnnCUDA.cc @@ -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; @@ -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 { @@ -60,15 +63,3 @@ process.add_(cms.Service('CUDAService')) CPPUNIT_ASSERT(output.item() == 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; -// }