|
| 1 | +#include <vector> |
| 2 | +#include <map> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <fstream> |
| 6 | + |
| 7 | +#include "SonicCMS/Core/interface/SonicEDProducer.h" |
| 8 | +#include "SonicCMS/TensorRT/interface/TRTClientFPGA.h" |
| 9 | +#include "FWCore/Framework/interface/Event.h" |
| 10 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 11 | +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" |
| 12 | +#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" |
| 13 | +#include "FWCore/Framework/interface/EventSetup.h" |
| 14 | +#include "FWCore/Framework/interface/Frameworkfwd.h" |
| 15 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 16 | +#include "FWCore/MessageLogger/interface/MessageLogger.h" |
| 17 | + |
| 18 | +template <typename Client> |
| 19 | +class HcalProducerFPGA : public SonicEDProducer<Client> |
| 20 | +{ |
| 21 | + public: |
| 22 | + //needed because base class has dependent scope |
| 23 | + using typename SonicEDProducer<Client>::Input; |
| 24 | + using typename SonicEDProducer<Client>::Output; |
| 25 | + //std::vector<float> fInVals; => debug |
| 26 | + //std::vector<float> fPrVals; |
| 27 | + //int fCount; |
| 28 | + explicit HcalProducerFPGA(edm::ParameterSet const& cfg) : SonicEDProducer<Client>(cfg), topN_(cfg.getParameter<unsigned>("topN")) { |
| 29 | + //for debugging |
| 30 | + this->setDebugName("HcalProducerFPGA"); |
| 31 | + /* Validation on temporary files |
| 32 | + std::ifstream lFIn("tb_input_features.dat"); |
| 33 | + std::ifstream lFPr("tb_output_predictions.dat"); |
| 34 | + std::string iline; |
| 35 | + std::string pline; |
| 36 | + bool hit_end = false; |
| 37 | + bool valid_data = true; |
| 38 | + for (int istream = 0; istream < 16000; istream++) { |
| 39 | + if (valid_data && !hit_end){ |
| 40 | + if(std::getline(lFIn,iline) && std::getline(lFPr,pline)) { |
| 41 | + char* cstr=const_cast<char*>(iline.c_str()); |
| 42 | + char* current; |
| 43 | + std::vector<float> in; |
| 44 | + current=strtok(cstr," "); |
| 45 | + while(current!=NULL){ |
| 46 | + fInVals.push_back(atof(current)); |
| 47 | + current=strtok(NULL," "); |
| 48 | + } |
| 49 | + cstr=const_cast<char*>(pline.c_str()); |
| 50 | + current=strtok(cstr," "); |
| 51 | + while(current!=NULL){ |
| 52 | + fPrVals.push_back(atof(current)); |
| 53 | + current=strtok(NULL," "); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + fCount = 0; |
| 59 | + */ |
| 60 | + |
| 61 | + } |
| 62 | + void acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput) override { |
| 63 | + auto ninput = client_.ninput(); |
| 64 | + auto batchSize = client_.batchSize(); |
| 65 | + iInput = Input(ninput*batchSize, 0.f); |
| 66 | + //make some random channels |
| 67 | + for(unsigned ib = 0; ib < batchSize; ib++) { |
| 68 | + //Current Hcal setup takes 11 inputs but it is sent in chunks of 16 to the FPGA |
| 69 | + iInput[ib*ninput+0] = f_to_ui<16,6>(1); |
| 70 | + iInput[ib*ninput+1] = f_to_ui<16,6>(2); |
| 71 | + iInput[ib*ninput+3] = f_to_ui<16,6>(int(rand() % 30)-15); |
| 72 | + iInput[ib*ninput+4] = f_to_ui<16,6>(int(rand() % 36)-36); |
| 73 | + for(unsigned i0 = 5; i0 < 18; i0++) iInput[ib*ninput+i0] = f_to_ui<16,8>(1); |
| 74 | + for(unsigned i0 = 18; i0 < ninput; i0++) iInput[ib*ninput+i0] = f_to_ui<16,8>(0); |
| 75 | + /* for debug |
| 76 | + for(unsigned i0 = 0; i0 < 11; i0++) iInput[ib*ninput+i0] = f_to_ui<32,14>(fInVals[i0+11*fCount]); |
| 77 | + for(unsigned i0 = 11; i0 < ninput; i0++) iInput[ib*ninput+i0] = 0; |
| 78 | + fCount++; |
| 79 | + */ |
| 80 | + } |
| 81 | + } |
| 82 | + void produce(edm::Event& iEvent, edm::EventSetup const& iSetup, Output const& iOutput) override { |
| 83 | + //check the results |
| 84 | + findTopN(iOutput); |
| 85 | + } |
| 86 | + ~HcalProducerFPGA() override {} |
| 87 | + //to ensure distinct cfi names - specialized below |
| 88 | + static std::string getCfiName(); |
| 89 | + static void fillDescriptions(edm::ConfigurationDescriptions & descriptions) { |
| 90 | + edm::ParameterSetDescription desc; |
| 91 | + Client::fillPSetDescription(desc); |
| 92 | + desc.add<unsigned>("topN",5); |
| 93 | + descriptions.add(getCfiName(),desc); |
| 94 | + } |
| 95 | + //Just putting something in for the hell of it |
| 96 | + void findTopN(const Output& scores) const { |
| 97 | + auto dim = client_.noutput(); |
| 98 | + //int batchsize = client_.batchSize(); |
| 99 | + for(unsigned i0 = 0; i0 < client_.batchSize(); i0++) { |
| 100 | + //match score to type by index, then put in largest-first map |
| 101 | + std::map<float,std::string,std::greater<float>> score_map; |
| 102 | + for(unsigned i = 0; i < (unsigned)dim; ++i){ |
| 103 | + std::stringstream pSS; pSS << "Dummy Channel " << i; |
| 104 | + score_map.emplace(scores[i0*dim+i],pSS.str()); |
| 105 | + } |
| 106 | + //get top n |
| 107 | + std::stringstream msg; |
| 108 | + msg << "Scores:\n"; |
| 109 | + unsigned counter = 0; |
| 110 | + for(const auto& item: score_map){ |
| 111 | + msg << item.second << " : " << item.first << "\n"; |
| 112 | + ++counter; |
| 113 | + if(counter>=topN_) break; |
| 114 | + } |
| 115 | + edm::LogInfo("HcalProducerFPGA") << msg.str(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private: |
| 121 | + using SonicEDProducer<Client>::client_; |
| 122 | + template<unsigned int B, unsigned int I> |
| 123 | + unsigned short f_to_ui(float f) { |
| 124 | + bool isPos = f > 0.; |
| 125 | + short tmpIs = int(fabs(f)); |
| 126 | + unsigned short tmpI = tmpIs; |
| 127 | + if (not isPos) { |
| 128 | + //unsigned int comp = ((unsigned int)((1<<(sizeof(unsigned int)*8-I+1))-1)<<I); |
| 129 | + unsigned short comp = ((unsigned short)((1<<(sizeof(unsigned short)*4-I+1))-1)<<I); |
| 130 | + tmpI = -tmpIs; |
| 131 | + tmpI = tmpI-comp; |
| 132 | + } |
| 133 | + float tmpF = fabs(f) - float(tmpIs); |
| 134 | + unsigned short fracs = tmpF*float(1<<(B-I)); |
| 135 | + unsigned short val = (tmpI << (B-I)) + fracs; |
| 136 | + return val; |
| 137 | + } |
| 138 | + template<unsigned int B, unsigned int I> |
| 139 | + float ui_to_f(const unsigned short ui) { |
| 140 | + unsigned short i = ui >> (B-I); |
| 141 | + unsigned short mask = (1 << (B-I))-1; |
| 142 | + unsigned short dec = ui & mask; |
| 143 | + float lDec = float(dec)/float(1 << (B-I)); |
| 144 | + return float(i)+lDec; |
| 145 | + } |
| 146 | + uint32_t merge(unsigned short iA,unsigned short iB) { |
| 147 | + uint32_t result = (uint32_t) iA << 16 | iB; |
| 148 | + return result; |
| 149 | + } |
| 150 | + unsigned topN_; |
| 151 | +}; |
| 152 | + |
| 153 | +typedef HcalProducerFPGA<TRTClientFPGASync> HcalProducerFPGASync; |
| 154 | +typedef HcalProducerFPGA<TRTClientFPGAAsync> HcalProducerFPGAAsync; |
| 155 | +typedef HcalProducerFPGA<TRTClientFPGAPseudoAsync> HcalProducerFPGAPseudoAsync; |
| 156 | + |
| 157 | +template<> std::string HcalProducerFPGASync::getCfiName() { return "HcalProducerFPGASync"; } |
| 158 | +template<> std::string HcalProducerFPGAAsync::getCfiName() { return "HcalProducerFPGAAsync"; } |
| 159 | +template<> std::string HcalProducerFPGAPseudoAsync::getCfiName() { return "HcalProducerFPGAPseudoAsync"; } |
| 160 | + |
| 161 | +DEFINE_FWK_MODULE(HcalProducerFPGASync); |
| 162 | +DEFINE_FWK_MODULE(HcalProducerFPGAAsync); |
| 163 | +DEFINE_FWK_MODULE(HcalProducerFPGAPseudoAsync); |
0 commit comments