-
Notifications
You must be signed in to change notification settings - Fork 2
Load DNN from Binary #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Load DNN from Binary #143
Changes from 2 commits
fdc37be
fda1a28
a175d1c
f71b44c
f636e69
cc5c65b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_DenseLayer_h | ||
| #define RecoTracker_LSTCore_interface_DenseLayer_h | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| /** | ||
| * Represents a dense (fully connected) layer with fixed input and output sizes. | ||
| * | ||
| * IN: Number of input neurons | ||
| * OUT: Number of output neurons | ||
| */ | ||
| template <std::size_t IN, std::size_t OUT> | ||
| struct DenseLayer { | ||
| /** | ||
| * Biases: one float per output neuron. | ||
| */ | ||
| std::array<float, OUT> biases{}; | ||
|
|
||
| /** | ||
| * Weights: stored as IN rows of OUT columns. | ||
| */ | ||
| std::array<std::array<float, OUT>, IN> weights{}; | ||
|
|
||
| /** | ||
| * Returns the weight from input neuron index `in` to output neuron index `out`. | ||
| */ | ||
| float getWeight(std::size_t in, std::size_t out) const { return weights[in][out]; } | ||
|
|
||
| static constexpr std::size_t inputSize = IN; | ||
| static constexpr std::size_t outputSize = OUT; | ||
| }; | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_Dnn_h | ||
| #define RecoTracker_LSTCore_interface_Dnn_h | ||
|
|
||
| #include <tuple> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| /** | ||
| * A general Dnn class that holds a sequence (tuple) of DenseLayer<T> types, | ||
| * each with compile-time fixed dimensions. | ||
| * | ||
| * Layers: A parameter pack of layer types (e.g. DenseLayer<23,32>, DenseLayer<32,1>, etc.) | ||
| */ | ||
| template <class... Layers> | ||
| class Dnn { | ||
| public: | ||
| Dnn() = default; | ||
| explicit Dnn(const std::string& filename) { load(filename); } | ||
|
|
||
| /** | ||
| * Loads biases and weights for each layer in the tuple from a binary file. | ||
| */ | ||
| void load(const std::string& filename) { | ||
| std::ifstream file(filename, std::ios::binary); | ||
| if (!file) { | ||
| throw std::runtime_error("Failed to open file: " + filename); | ||
| } | ||
|
|
||
| loadLayers<0>(file); | ||
|
|
||
| if (!file.good()) { | ||
| throw std::runtime_error("Error reading from file: " + filename); | ||
| } | ||
| file.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Prints the biases and weights of each layer to stdout. | ||
| */ | ||
| void print() const { printLayers<0>(); } | ||
|
|
||
| /** | ||
| * A const reference to the underlying tuple of layers. | ||
| */ | ||
| const std::tuple<Layers...>& getLayers() const { return layers_; } | ||
|
|
||
| /** | ||
| * A reference to the underlying tuple of layers. | ||
| */ | ||
| std::tuple<Layers...>& getLayers() { return layers_; } | ||
|
|
||
| private: | ||
| // Store all layers in a compile-time tuple | ||
| std::tuple<Layers...> layers_; | ||
|
|
||
| /** | ||
| * Internal compile-time recursion for loading each layer from file | ||
| */ | ||
| template <std::size_t I> | ||
| typename std::enable_if<I == sizeof...(Layers), void>::type loadLayers(std::ifstream&) { | ||
| // Base case: no more layers to load | ||
| } | ||
|
|
||
| template <std::size_t I> | ||
| typename std::enable_if < I<sizeof...(Layers), void>::type loadLayers(std::ifstream& file) { | ||
| auto& layer = std::get<I>(layers_); | ||
|
|
||
| // Read and verify header information | ||
| uint32_t layer_id, num_inputs, num_outputs; | ||
| file.read(reinterpret_cast<char*>(&layer_id), sizeof(layer_id)); | ||
| file.read(reinterpret_cast<char*>(&num_inputs), sizeof(num_inputs)); | ||
| file.read(reinterpret_cast<char*>(&num_outputs), sizeof(num_outputs)); | ||
|
|
||
| // Verify the dimensions match our template parameters | ||
| if (num_inputs != layer.inputSize || num_outputs != layer.outputSize) { | ||
| throw std::runtime_error("Layer " + std::to_string(I) + | ||
| " dimension mismatch: " | ||
| "expected " + | ||
| std::to_string(layer.inputSize) + "x" + std::to_string(layer.outputSize) + ", got " + | ||
| std::to_string(num_inputs) + "x" + std::to_string(num_outputs)); | ||
| } | ||
|
|
||
| // Verify layer index matches | ||
| if (layer_id != I + 1) { // Assumes 1-based layer IDs | ||
| throw std::runtime_error("Layer index mismatch: expected " + std::to_string(I + 1) + ", got " + | ||
| std::to_string(layer_id)); | ||
| } | ||
|
|
||
| // Read biases | ||
| file.read(reinterpret_cast<char*>(layer.biases.data()), layer.biases.size() * sizeof(float)); | ||
|
|
||
| // Read weights row by row | ||
| for (auto& row : layer.weights) { | ||
| file.read(reinterpret_cast<char*>(row.data()), row.size() * sizeof(float)); | ||
| } | ||
|
|
||
| if (!file.good()) { | ||
| throw std::runtime_error("Failed to read parameters for layer " + std::to_string(I)); | ||
| } | ||
|
|
||
| // Recurse to next layer | ||
| loadLayers<I + 1>(file); | ||
| } | ||
|
|
||
| /** | ||
| * Internal compile-time recursion for printing each layer | ||
| */ | ||
| template <std::size_t I> | ||
| typename std::enable_if<I == sizeof...(Layers), void>::type printLayers() const { | ||
| // Base case: no more layers to print | ||
| } | ||
|
|
||
| template <std::size_t I> | ||
| typename std::enable_if < I<sizeof...(Layers), void>::type printLayers() const { | ||
| const auto& layer = std::get<I>(layers_); | ||
| std::cout << "\n=== Layer " << I + 1 << " ===\nInputs=" << layer.inputSize << ", Outputs=" << layer.outputSize | ||
| << "\n\nBiases:\n"; | ||
|
|
||
| for (float b : layer.biases) { | ||
| std::cout << b << " "; | ||
| } | ||
| std::cout << "\n\nWeights:\n"; | ||
|
|
||
| for (std::size_t in = 0; in < layer.inputSize; ++in) { | ||
| std::cout << " [ "; | ||
| for (std::size_t out = 0; out < layer.outputSize; ++out) { | ||
| std::cout << layer.getWeight(in, out) << " "; | ||
| } | ||
| std::cout << "]\n"; | ||
| } | ||
|
|
||
| // Recurse to next layer | ||
| printLayers<I + 1>(); | ||
| } | ||
| }; | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
| #define RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
|
|
||
| #include "RecoTracker/LSTCore/interface/DenseLayer.h" | ||
|
|
||
| namespace lst { | ||
|
|
||
| /** | ||
| * Data structure holding multiple dense layers for the DNN weights. | ||
| */ | ||
| struct DnnWeightsDevData { | ||
| DenseLayer<23, 32> layer1; | ||
| DenseLayer<32, 32> layer2; | ||
| DenseLayer<32, 1> layer3; | ||
|
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we have named constants for these 23 and 32? |
||
| }; | ||
|
|
||
| } // namespace lst | ||
|
|
||
| #endif // RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |
| #include "RecoTracker/LSTCore/interface/ModuleConnectionMap.h" | ||
| #include "RecoTracker/LSTCore/interface/TiltedGeometry.h" | ||
| #include "RecoTracker/LSTCore/interface/PixelMap.h" | ||
|
|
||
| #include "RecoTracker/LSTCore/interface/Dnn.h" | ||
| #include "RecoTracker/LSTCore/interface/DenseLayer.h" | ||
| #include "RecoTracker/LSTCore/interface/DnnWeightsDevSoA.h" | ||
| #include "ModuleMethods.h" | ||
|
|
||
| #include <filesystem> | ||
|
|
@@ -111,11 +113,31 @@ std::unique_ptr<lst::LSTESData<alpaka_common::DevHost>> lst::loadAndFillESHost(s | |
| tiltedGeometry, | ||
| moduleConnectionMap); | ||
| auto pixelMappingPtr = std::make_shared<PixelMap>(std::move(pixelMapping)); | ||
|
|
||
| // === Load from the DNN instance === | ||
| auto model = Dnn<DenseLayer<23, 32>, DenseLayer<32, 32>, DenseLayer<32, 1>>( | ||
| "/mnt/data1/gsn27/cmssw-fresh/CMSSW_14_2_0_pre4/src/RecoTracker/LSTCore/standalone/analysis/DNN/" | ||
| "network_weights.bin"); | ||
|
|
||
| // Copy the loaded model into a host DnnWeightsDevData struct | ||
| lst::DnnWeightsDevData hostDnn; | ||
| { | ||
| auto const& layers = model.getLayers(); | ||
| hostDnn.layer1 = std::get<0>(layers); | ||
| hostDnn.layer2 = std::get<1>(layers); | ||
| hostDnn.layer3 = std::get<2>(layers); | ||
| } | ||
|
||
|
|
||
| // Wrap it in a PortableHostObject so it can be copied to device | ||
| auto hostDnnWeights = std::make_shared<PortableHostObject<lst::DnnWeightsDevData>>(cms::alpakatools::host()); | ||
| hostDnnWeights->value() = hostDnn; | ||
|
|
||
| return std::make_unique<LSTESData<alpaka_common::DevHost>>(nModules, | ||
| nLowerModules, | ||
| nPixels, | ||
| endcapGeometry.nEndCapMap, | ||
| std::move(modulesBuffers), | ||
| std::move(endcapGeometryDev), | ||
| pixelMappingPtr); | ||
| pixelMappingPtr, | ||
| hostDnnWeights); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps keep everything in
lstnamespace for now