Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-08-02 05:16:40

0001 /*
0002  * Base class for tests.
0003  *
0004  */
0005 
0006 #ifndef PHYSICSTOOLS_PYTORCH_TEST_TESTBASECUDA_H
0007 #define PHYSICSTOOLS_PYTORCH_TEST_TESTBASECUDA_H
0008 
0009 #include <boost/filesystem.hpp>
0010 #include <filesystem>
0011 #include <cppunit/extensions/HelperMacros.h>
0012 #include <stdexcept>
0013 
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0017 #include "FWCore/PluginManager/interface/PluginManager.h"
0018 #include "FWCore/PluginManager/interface/standard.h"
0019 #include "FWCore/ServiceRegistry/interface/Service.h"
0020 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0021 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0022 #include "FWCore/Utilities/interface/Exception.h"
0023 #include "FWCore/Utilities/interface/ResourceInformation.h"
0024 
0025 class testBasePyTorchCUDA : public CppUnit::TestFixture {
0026 public:
0027   std::string dataPath_;
0028 
0029   void setUp();
0030   void tearDown();
0031   std::string cmsswPath(std::string path);
0032 
0033   virtual std::string pyScript() const = 0;
0034 
0035   virtual void test() = 0;
0036 };
0037 
0038 void testBasePyTorchCUDA::setUp() {
0039   dataPath_ =
0040       cmsswPath("/test/" + std::string(std::getenv("SCRAM_ARCH")) + "/" + boost::filesystem::unique_path().string());
0041 
0042   // create the graph using apptainer
0043   std::string testPath = cmsswPath("/src/PhysicsTools/PyTorch/test");
0044   std::string cmd = "apptainer exec -B " + cmsswPath("") +
0045                     "  /cvmfs/unpacked.cern.ch/registry.hub.docker.com/cmsml/cmsml:3.11  python " + testPath + "/" +
0046                     pyScript() + " " + dataPath_;
0047   std::cout << "cmd: " << cmd << std::endl;
0048   std::array<char, 128> buffer;
0049   std::string result;
0050   std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
0051   if (!pipe) {
0052     throw std::runtime_error("Failed to run apptainer to prepare the PyTorch test model: " + cmd);
0053   }
0054   while (!feof(pipe.get())) {
0055     if (fgets(buffer.data(), 128, pipe.get()) != NULL) {
0056       result += buffer.data();
0057     }
0058   }
0059   std::cout << std::endl << result << std::endl;
0060 }
0061 void testBasePyTorchCUDA::tearDown() {
0062   if (std::filesystem::exists(dataPath_)) {
0063     std::filesystem::remove_all(dataPath_);
0064   }
0065 }
0066 
0067 std::string testBasePyTorchCUDA::cmsswPath(std::string path) {
0068   if (path.size() > 0 && path.substr(0, 1) != "/") {
0069     path = "/" + path;
0070   }
0071 
0072   std::string base = std::string(std::getenv("CMSSW_BASE"));
0073   std::string releaseBase = std::string(std::getenv("CMSSW_RELEASE_BASE"));
0074 
0075   return (std::filesystem::exists(base.c_str()) ? base : releaseBase) + path;
0076 }
0077 
0078 #endif  // PHYSICSTOOLS_PYTORCH_TEST_TESTBASE_H