File indexing completed on 2025-02-23 09:22:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifdef USE_INFERENCE_TORCH
0028 # include "Par04TorchInference.hh"
0029
0030 # include "Par04InferenceInterface.hh" // for Par04InferenceInterface
0031
0032 # include <algorithm> // for copy, max
0033 # include <cassert> // for assert
0034 # include <cstddef> // for size_t
0035 # include <cstdint> // for int64_t
0036 # include <torch/torch.h>
0037 # include <utility> // for move
0038
0039
0040
0041 Par04TorchInference::Par04TorchInference(G4String modelPath) : Par04InferenceInterface()
0042 {
0043 fModule = torch::jit::load(modelPath);
0044 }
0045
0046
0047
0048 void Par04TorchInference::RunInference(std::vector<float> aGenVector,
0049 std::vector<G4double>& aEnergies, int aSize)
0050 {
0051
0052
0053 int latentSize = aGenVector.size() - 4;
0054
0055 std::vector<float> latent;
0056 for (int i = 0; i < latentSize; i++) {
0057 latent.push_back(aGenVector[i]);
0058 }
0059 std::vector<float> energy;
0060 energy.push_back(aGenVector[latentSize + 1]);
0061 std::vector<float> angle;
0062 energy.push_back(aGenVector[latentSize + 2]);
0063 std::vector<float> geo;
0064 for (int i = latentSize + 2; i < latentSize + 4; i++) {
0065 geo.push_back(aGenVector[i]);
0066 }
0067
0068
0069 torch::Tensor latentVector = torch::tensor(latent);
0070 torch::Tensor eTensor = torch::tensor(energy);
0071 torch::Tensor angleTensor = torch::tensor(angle);
0072 torch::Tensor geoTensor = torch::tensor(geo);
0073
0074 std::vector<torch::jit::IValue> genInput;
0075
0076 genInput.push_back(latentVector);
0077 genInput.push_back(eTensor);
0078 genInput.push_back(angleTensor);
0079 genInput.push_back(geoTensor);
0080
0081 at::Tensor outTensor = fModule.forward(genInput).toTensor().contiguous();
0082
0083 std::vector<G4double> output(outTensor.data_ptr<float>(),
0084 outTensor.data_ptr<float>() + outTensor.numel());
0085
0086 aEnergies.assign(aSize, 0);
0087 for (int i = 0; i < aSize; i++) {
0088 aEnergies[i] = output[i];
0089 }
0090 }
0091
0092 #endif