File indexing completed on 2023-04-13 23:19:20
0001
0002
0003
0004
0005
0006
0007
0008 #include <cppunit/extensions/HelperMacros.h>
0009 #include <stdexcept>
0010
0011 #include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
0012
0013 #include "testBaseCUDA.h"
0014
0015 class testGraphLoadingCUDA : public testBaseCUDA {
0016 CPPUNIT_TEST_SUITE(testGraphLoadingCUDA);
0017 CPPUNIT_TEST(test);
0018 CPPUNIT_TEST_SUITE_END();
0019
0020 public:
0021 std::string pyScript() const override;
0022 void test() override;
0023 };
0024
0025 CPPUNIT_TEST_SUITE_REGISTRATION(testGraphLoadingCUDA);
0026
0027 std::string testGraphLoadingCUDA::pyScript() const { return "createconstantgraph.py"; }
0028
0029 void testGraphLoadingCUDA::test() {
0030 if (!cms::cudatest::testDevices())
0031 return;
0032
0033 std::vector<edm::ParameterSet> psets;
0034 edm::ServiceToken serviceToken = edm::ServiceRegistry::createSet(psets);
0035 edm::ServiceRegistry::Operate operate(serviceToken);
0036
0037
0038 edmplugin::PluginManager::configure(edmplugin::standard::config());
0039
0040 std::string const config = R"_(import FWCore.ParameterSet.Config as cms
0041 process = cms.Process('Test')
0042 process.add_(cms.Service('ResourceInformationService'))
0043 process.add_(cms.Service('CUDAService'))
0044 )_";
0045 std::unique_ptr<edm::ParameterSet> params;
0046 edm::makeParameterSets(config, params);
0047 edm::ServiceToken tempToken(edm::ServiceRegistry::createServicesFromConfig(std::move(params)));
0048 edm::ServiceRegistry::Operate operate2(tempToken);
0049 edm::Service<CUDAInterface> cuda;
0050 std::cout << "CUDA service enabled: " << cuda->enabled() << std::endl;
0051
0052 std::cout << "Testing CUDA backend" << std::endl;
0053 tensorflow::Backend backend = tensorflow::Backend::cuda;
0054 tensorflow::Options options{backend};
0055
0056
0057 int nThreads = 4;
0058 tensorflow::TBBThreadPool::instance(nThreads);
0059 options.setThreading(nThreads);
0060
0061
0062 std::string pbFile = dataPath_ + "/constantgraph.pb";
0063 tensorflow::setLogging();
0064 tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile);
0065 CPPUNIT_ASSERT(graphDef != nullptr);
0066
0067
0068 tensorflow::Session* session = tensorflow::createSession(graphDef, options);
0069 CPPUNIT_ASSERT(session != nullptr);
0070
0071
0072 tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10});
0073 float* d = input.flat<float>().data();
0074 for (size_t i = 0; i < 10; i++, d++) {
0075 *d = float(i);
0076 }
0077 tensorflow::Tensor scale(tensorflow::DT_FLOAT, {});
0078 scale.scalar<float>()() = 1.0;
0079
0080
0081 std::vector<tensorflow::Tensor> outputs;
0082 tensorflow::run(session, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "no_threads");
0083 CPPUNIT_ASSERT(outputs.size() == 1);
0084 std::cout << outputs[0].DebugString() << std::endl;
0085 CPPUNIT_ASSERT(outputs[0].matrix<float>()(0, 0) == 46.);
0086
0087
0088 outputs.clear();
0089 tensorflow::run(session, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "tbb");
0090 CPPUNIT_ASSERT(outputs.size() == 1);
0091 std::cout << outputs[0].DebugString() << std::endl;
0092 CPPUNIT_ASSERT(outputs[0].matrix<float>()(0, 0) == 46.);
0093
0094
0095 tensorflow::Session* session2 = tensorflow::createSession(graphDef, options);
0096 CPPUNIT_ASSERT(session != nullptr);
0097 outputs.clear();
0098 tensorflow::run(session2, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "tensorflow");
0099 CPPUNIT_ASSERT(outputs.size() == 1);
0100 std::cout << outputs[0].DebugString() << std::endl;
0101 CPPUNIT_ASSERT(outputs[0].matrix<float>()(0, 0) == 46.);
0102
0103
0104 CPPUNIT_ASSERT_THROW(
0105 tensorflow::run(session, {{"input", input}, {"scale", scale}}, {"output"}, &outputs, "not_existing"),
0106 cms::Exception);
0107
0108
0109 CPPUNIT_ASSERT(tensorflow::closeSession(session));
0110 CPPUNIT_ASSERT(tensorflow::closeSession(session2));
0111 delete graphDef;
0112 }