-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Graph export for Exa.TrkX (#2730)
Allows to export the Graph after the GNN to a csv file. Needed for the GNN+CKF workflow
- Loading branch information
1 parent
7e9c2f0
commit 8f4162f
Showing
15 changed files
with
241 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvExaTrkXGraphWriter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2020 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Utilities/Logger.hpp" | ||
#include "ActsExamples/Framework/ProcessCode.hpp" | ||
#include "ActsExamples/Framework/WriterT.hpp" | ||
#include "ActsExamples/Utilities/Paths.hpp" | ||
|
||
#include <cstddef> | ||
#include <limits> | ||
#include <string> | ||
|
||
namespace ActsExamples { | ||
struct AlgorithmContext; | ||
|
||
class CsvExaTrkXGraphWriter final | ||
: public WriterT<std::pair<std::vector<int64_t>, std::vector<float>>> { | ||
public: | ||
struct Config { | ||
/// Which simulated (truth) hits collection to use. | ||
std::string inputGraph; | ||
/// Where to place output files | ||
std::string outputDir; | ||
/// Output filename stem. | ||
std::string outputStem = "exatrkx-graph"; | ||
}; | ||
|
||
/// Construct the cluster writer. | ||
/// | ||
/// @param config is the configuration object | ||
/// @param level is the logging level | ||
CsvExaTrkXGraphWriter(const Config& config, Acts::Logging::Level level); | ||
|
||
/// Readonly access to the config | ||
const Config& config() const { return m_cfg; } | ||
|
||
protected: | ||
/// Type-specific write implementation. | ||
/// | ||
/// @param[in] ctx is the algorithm context | ||
/// @param[in] simHits are the simhits to be written | ||
ProcessCode writeT(const AlgorithmContext& ctx, | ||
const std::pair<std::vector<int64_t>, std::vector<float>>& | ||
graph) override; | ||
|
||
private: | ||
Config m_cfg; | ||
}; | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2020 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "ActsExamples/Io/Csv/CsvExaTrkXGraphWriter.hpp" | ||
|
||
#include "Acts/Definitions/Algebra.hpp" | ||
#include "Acts/Definitions/Common.hpp" | ||
#include "Acts/Definitions/Units.hpp" | ||
#include "ActsExamples/Framework/AlgorithmContext.hpp" | ||
#include "ActsExamples/Utilities/Paths.hpp" | ||
#include "ActsFatras/EventData/Barcode.hpp" | ||
|
||
#include <stdexcept> | ||
#include <vector> | ||
|
||
#include <dfe/dfe_io_dsv.hpp> | ||
#include <dfe/dfe_namedtuple.hpp> | ||
|
||
struct GraphData { | ||
int64_t edge0; | ||
int64_t edge1; | ||
float weight; | ||
DFE_NAMEDTUPLE(GraphData, edge0, edge1, weight); | ||
}; | ||
|
||
ActsExamples::CsvExaTrkXGraphWriter::CsvExaTrkXGraphWriter( | ||
const ActsExamples::CsvExaTrkXGraphWriter::Config& config, | ||
Acts::Logging::Level level) | ||
: WriterT(config.inputGraph, "CsvExaTrkXGraphWriter", level), | ||
m_cfg(config) {} | ||
|
||
ActsExamples::ProcessCode ActsExamples::CsvExaTrkXGraphWriter::writeT( | ||
const ActsExamples::AlgorithmContext& ctx, | ||
const std::pair<std::vector<int64_t>, std::vector<float>>& graph) { | ||
std::string path = perEventFilepath( | ||
m_cfg.outputDir, m_cfg.outputStem + ".csv", ctx.eventNumber); | ||
|
||
dfe::NamedTupleCsvWriter<GraphData> writer(path); | ||
|
||
const auto& [edges, weights] = graph; | ||
|
||
for (auto i = 0ul; i < weights.size(); ++i) { | ||
GraphData edge{}; | ||
edge.edge0 = edges[2 * i]; | ||
edge.edge1 = edges[2 * i + 1]; | ||
edge.weight = weights[i]; | ||
writer.append(edge); | ||
} | ||
|
||
return ActsExamples::ProcessCode::SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Plugins/ExaTrkX/include/Acts/Plugins/ExaTrkX/TorchGraphStoreHook.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Plugins/ExaTrkX/ExaTrkXPipeline.hpp" | ||
#include "Acts/Plugins/ExaTrkX/detail/CantorEdge.hpp" | ||
#include "Acts/Utilities/Logger.hpp" | ||
|
||
namespace Acts { | ||
|
||
class TorchGraphStoreHook : public ExaTrkXHook { | ||
public: | ||
using Graph = std::pair<std::vector<int64_t>, std::vector<float>>; | ||
|
||
private: | ||
std::unique_ptr<Graph> m_storedGraph; | ||
|
||
public: | ||
TorchGraphStoreHook(); | ||
~TorchGraphStoreHook() override {} | ||
|
||
void operator()(const std::any &, const std::any &edges, | ||
const std::any &weights) const override; | ||
|
||
const Graph &storedGraph() const { return *m_storedGraph; } | ||
}; | ||
|
||
} // namespace Acts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "Acts/Plugins/ExaTrkX/TorchGraphStoreHook.hpp" | ||
|
||
#include "Acts/Plugins/ExaTrkX/detail/TensorVectorConversion.hpp" | ||
|
||
#include <torch/torch.h> | ||
|
||
Acts::TorchGraphStoreHook::TorchGraphStoreHook() { | ||
m_storedGraph = std::make_unique<Graph>(); | ||
} | ||
|
||
void Acts::TorchGraphStoreHook::operator()(const std::any&, | ||
const std::any& edges, | ||
const std::any& weights) const { | ||
if (not weights.has_value()) { | ||
return; | ||
} | ||
|
||
m_storedGraph->first = detail::tensor2DToVector<int64_t>( | ||
std::any_cast<torch::Tensor>(edges).t()); | ||
|
||
auto cpuWeights = std::any_cast<torch::Tensor>(weights).to(torch::kCPU); | ||
m_storedGraph->second = | ||
std::vector<float>(cpuWeights.data_ptr<float>(), | ||
cpuWeights.data_ptr<float>() + cpuWeights.numel()); | ||
} |
Oops, something went wrong.