Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 #ifdef USE_INFERENCE 26 #ifdef USE_INFERENCE 27 # ifndef PAR04INFEERENCESETUP_HH << 27 #ifndef PAR04INFEERENCESETUP_HH 28 # define PAR04INFEERENCESETUP_HH << 28 #define PAR04INFEERENCESETUP_HH 29 29 30 # include "CLHEP/Units/SystemOfUnits.h" // << 30 #include "CLHEP/Units/SystemOfUnits.h" // for mm 31 << 31 #include "G4ThreeVector.hh" // for G4ThreeVector 32 # include "G4ThreeVector.hh" // for G4Thre << 32 #include <G4String.hh> // for G4String 33 << 33 #include <G4SystemOfUnits.hh> // for mm 34 # include <G4String.hh> // for G4String << 34 #include <G4Types.hh> // for G4int, G4double, G4bool, G4f... 35 # include <G4SystemOfUnits.hh> // for mm << 35 #include <memory> // for unique_ptr 36 # include <G4Types.hh> // for G4int, G4dou << 36 #include <vector> // for vector 37 # include <memory> // for unique_ptr << 38 # include <vector> // for vector << 39 class Par04DetectorConstruction; 37 class Par04DetectorConstruction; 40 class Par04InferenceInterface; 38 class Par04InferenceInterface; 41 class Par04InferenceMessenger; 39 class Par04InferenceMessenger; 42 40 43 /** 41 /** 44 * @brief Inference setup. 42 * @brief Inference setup. 45 * 43 * 46 * Constructs the input vector of size b+c to 44 * Constructs the input vector of size b+c to run the inference, b represents 47 * the size of the latent space (or the encode 45 * the size of the latent space (or the encoded space in a Variational 48 * Autoencoder based model), c represents the 46 * Autoencoder based model), c represents the size of the conditional vector. 49 *The b values of the input vector are randoml 47 *The b values of the input vector are randomly sampled from b-dimensional 50 *Gaussian distribution. The c values represen 48 *Gaussian distribution. The c values represent respectively the condition 51 *values of the particle energy, angle and det 49 *values of the particle energy, angle and detector geometry. These condition 52 *values are user-specific application. The en 50 *values are user-specific application. The energy rescaling is used to retrieve 53 *the original energy scale in MeV. Computes t 51 *the original energy scale in MeV. Computes the cell position in the detector 54 *of each inferred energy value. 52 *of each inferred energy value. 55 * 53 * 56 **/ 54 **/ 57 55 58 class Par04InferenceSetup << 56 class Par04InferenceSetup { 59 { << 57 public: 60 public: << 58 Par04InferenceSetup(); 61 Par04InferenceSetup(); << 59 ~Par04InferenceSetup(); 62 ~Par04InferenceSetup(); << 60 63 << 61 /// Geometry setup 64 /// Geometry setup << 62 /// Check if inference should be performed for the particle 65 /// Check if inference should be performed << 63 /// @param[in] aEnergy Particle's energy 66 /// @param[in] aEnergy Particle's energy << 64 G4bool IfTrigger(G4double aEnergy); 67 G4bool IfTrigger(G4double aEnergy); << 65 /// Set mesh size. 68 /// Set mesh size. << 66 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 69 /// @param aSize (x,y,x) size for Carthesi << 67 /// cylindrical coordinates. 70 /// cylindrical coordinates. << 68 inline void SetMeshSize(const G4ThreeVector &aSize) { fMeshSize = aSize; }; 71 inline void SetMeshSize(const G4ThreeVecto << 69 /// Get mesh size. 72 /// Get mesh size. << 70 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 73 /// @return G4ThreeVector (x,y,x) size for << 71 /// z) for cylindrical coordinates. 74 /// z) for cylindrical coordinates. << 72 inline G4ThreeVector GetMeshSize() const { return fMeshSize; }; 75 inline G4ThreeVector GetMeshSize() const { << 73 /// Set number of mesh cells. 76 /// Set number of mesh cells. << 74 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 77 /// @param aSize (x,y,x) size for Carthesi << 75 /// cylindrical coordinates. 78 /// cylindrical coordinates. << 76 inline void SetMeshNumber(const G4ThreeVector &aSize) { 79 inline void SetMeshNumber(const G4ThreeVec << 77 fMeshNumber = aSize; 80 /// Get number of mesh cells. << 78 }; 81 /// @return G4ThreeVector (x,y,x) size for << 79 /// Get number of mesh cells. 82 /// z) for cylindrical coordinates. << 80 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 83 inline G4ThreeVector GetMeshNumber() const << 81 /// z) for cylindrical coordinates. 84 /// Set size of the condition vector << 82 inline G4ThreeVector GetMeshNumber() const { return fMeshNumber; }; 85 inline void SetSizeConditionVector(G4int a << 83 /// Set size of the condition vector 86 /// Get size of the condition vector << 84 inline void SetSizeConditionVector(G4int aNumber) { 87 inline G4int GetSizeConditionVector() cons << 85 fSizeConditionVector = aNumber; 88 /// Set size of the latent space vector << 86 }; 89 inline void SetSizeLatentVector(G4int aNum << 87 /// Get size of the condition vector 90 /// Get size of the latent space vector << 88 inline G4int GetSizeConditionVector() const { return fSizeConditionVector; }; 91 inline G4int GetSizeLatentVector() const { << 89 /// Set size of the latent space vector 92 /// Set path and name of the model << 90 inline void SetSizeLatentVector(G4int aNumber) { 93 inline void SetModelPathName(G4String aNam << 91 fSizeLatentVector = aNumber; 94 /// Get path and name of the model << 92 }; 95 inline G4String GetModelPathName() const { << 93 /// Get size of the latent space vector 96 /// Set profiling flag << 94 inline G4int GetSizeLatentVector() const { return fSizeLatentVector; }; 97 inline void SetProfileFlag(G4int aNumber) << 95 /// Set path and name of the model 98 /// Get profiling flag << 96 inline void SetModelPathName(G4String aName) { fModelPathName = aName; }; 99 inline G4int GetProfileFlag() const { retu << 97 /// Get path and name of the model 100 /// Set optimization flag << 98 inline G4String GetModelPathName() const { return fModelPathName; }; 101 inline void SetOptimizationFlag(G4int aNum << 99 /// Set profiling flag 102 /// Get optimization flag << 100 inline void SetProfileFlag(G4int aNumber) { fProfileFlag = aNumber; }; 103 inline G4int GetOptimizationFlag() const { << 101 /// Get profiling flag 104 /// Get name of the inference library << 102 inline G4int GetProfileFlag() const { return fProfileFlag; }; 105 inline G4String GetInferenceLibrary() cons << 103 /// Set optimization flag 106 /// Set name of the inference library and << 104 inline void SetOptimizationFlag(G4int aNumber) { 107 /// interface << 105 fOptimizationFlag = aNumber; 108 void SetInferenceLibrary(G4String aName); << 106 }; 109 /// Check settings of the inference librar << 107 /// Get optimization flag 110 void CheckInferenceLibrary(); << 108 inline G4int GetOptimizationFlag() const { return fOptimizationFlag; }; 111 /// Set number of Mesh cells in cylindrica << 109 /// Get name of the inference library 112 inline void SetMeshNbOfCells(G4ThreeVector << 110 inline G4String GetInferenceLibrary() const { return fInferenceLibrary; }; 113 /// Set number of Mesh cells in cylindrica << 111 /// Set name of the inference library and create a pointer to chosen inference 114 /// @param[in] aIndex index of cylindrical << 112 /// interface 115 inline void SetMeshNbOfCells(G4int aIndex, << 113 void SetInferenceLibrary(G4String aName); 116 /// Get number of Mesh cells in cylindrica << 114 /// Check settings of the inference library 117 inline G4ThreeVector GetMeshNbOfCells() co << 115 void CheckInferenceLibrary(); 118 /// Set size of Mesh cells in cylindrical << 116 /// Set number of Mesh cells in cylindrical coordinates (r, phi, z) 119 inline void SetMeshSizeOfCells(G4ThreeVect << 117 inline void SetMeshNbOfCells(G4ThreeVector aNb) { fMeshNumber = aNb; }; 120 /// Set size of Mesh cells in cylindrical << 118 /// Set number of Mesh cells in cylindrical coordinates 121 /// @param[in] aIndex index of cylindrical << 119 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 122 inline void SetMeshSizeOfCells(G4int aInde << 120 inline void SetMeshNbOfCells(G4int aIndex, G4double aNb) { 123 /// Get size of Mesh cells in cylindrical << 121 fMeshNumber[aIndex] = aNb; 124 inline G4ThreeVector GetMeshSizeOfCells() << 122 }; 125 /// Setting execution providers flags << 123 /// Get number of Mesh cells in cylindrical coordinates (r, phi, z) 126 /// GPU << 124 inline G4ThreeVector GetMeshNbOfCells() const { return fMeshNumber; }; 127 inline void SetCudaFlag(G4int aNumber) { f << 125 /// Set size of Mesh cells in cylindrical coordinates (r, phi, z) 128 inline G4int GetCudaFlag() const { return << 126 inline void SetMeshSizeOfCells(G4ThreeVector aNb) { fMeshSize = aNb; }; 129 /// Setting execution providers Options << 127 /// Set size of Mesh cells in cylindrical coordinates 130 /// Cuda << 128 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 131 inline void SetCudaDeviceId(G4String aNumb << 129 inline void SetMeshSizeOfCells(G4int aIndex, G4double aNb) { 132 inline G4String GetCudaDeviceId() const { << 130 fMeshSize[aIndex] = aNb; 133 inline void SetCudaGpuMemLimit(G4String aN << 131 }; 134 inline G4String GetCudaGpuMemLimit() const << 132 /// Get size of Mesh cells in cylindrical coordinates (r, phi, z) 135 inline void SetCudaArenaExtendedStrategy(G << 133 inline G4ThreeVector GetMeshSizeOfCells() const { return fMeshSize; }; 136 { << 134 /// Setting execution providers flags 137 fCudaArenaExtendedStrategy = aNumber; << 135 /// GPU 138 }; << 136 inline void SetCudaFlag(G4int aNumber) { fCudaFlag = aNumber; }; 139 inline G4String GetCudaArenaExtendedStrate << 137 inline G4int GetCudaFlag() const { return fCudaFlag; }; 140 inline void SetCudaCudnnConvAlgoSearch(G4S << 138 /// Setting execution providers Options 141 { << 139 /// Cuda 142 fCudaCudnnConvAlgoSearch = aNumber; << 140 inline void SetCudaDeviceId(G4String aNumber) { fCudaDeviceId = aNumber; }; 143 }; << 141 inline G4String GetCudaDeviceId() const { return fCudaDeviceId; }; 144 inline G4String GetCudaCudnnConvAlgoSearch << 142 inline void SetCudaGpuMemLimit(G4String aNumber) { 145 inline void SetCudaDoCopyInDefaultStream(G << 143 fCudaGpuMemLimit = aNumber; 146 { << 144 }; 147 fCudaDoCopyInDefaultStream = aNumber; << 145 inline G4String GetCudaGpuMemLimit() const { return fCudaGpuMemLimit; }; 148 }; << 146 inline void SetCudaArenaExtendedStrategy(G4String aNumber) { 149 inline G4String GetCudaDoCopyInDefaultStre << 147 fCudaArenaExtendedStrategy = aNumber; 150 inline void SetCudaCudnnConvUseMaxWorkspac << 148 }; 151 { << 149 inline G4String GetCudaArenaExtendedStrategy() const { 152 fCudaCudnnConvUseMaxWorkspace = aNumber; << 150 return fCudaArenaExtendedStrategy; 153 }; << 151 }; 154 inline G4String GetCudaCudnnConvUseMaxWork << 152 inline void SetCudaCudnnConvAlgoSearch(G4String aNumber) { 155 { << 153 fCudaCudnnConvAlgoSearch = aNumber; 156 return fCudaCudnnConvUseMaxWorkspace; << 154 }; 157 }; << 155 inline G4String GetCudaCudnnConvAlgoSearch() const { 158 << 156 return fCudaCudnnConvAlgoSearch; 159 /// Execute inference << 157 }; 160 /// @param[out] aDepositsEnergies of infer << 158 inline void SetCudaDoCopyInDefaultStream(G4String aNumber) { 161 /// detector << 159 fCudaDoCopyInDefaultStream = aNumber; 162 /// @param[in] aParticleEnergy Energy of i << 160 }; 163 void GetEnergies(std::vector<G4double>& aE << 161 inline G4String GetCudaDoCopyInDefaultStream() const { 164 G4float aInitialAngle); << 162 return fCudaDoCopyInDefaultStream; 165 << 163 }; 166 /// Calculate positions << 164 inline void SetCudaCudnnConvUseMaxWorkspace(G4String aNumber) { 167 /// @param[out] aDepositsPositions Vector << 165 fCudaCudnnConvUseMaxWorkspace = aNumber; 168 /// energies deposited in the detector << 166 }; 169 /// @param[in] aParticlePosition Initial p << 167 inline G4String GetCudaCudnnConvUseMaxWorkspace() const { 170 /// transverse plane of the mesh << 168 return fCudaCudnnConvUseMaxWorkspace; 171 /// and beginning of the mesh i << 169 }; 172 /// @param[in] aParticleDirection Initial << 170 173 /// rotation << 171 /// Execute inference 174 void GetPositions(std::vector<G4ThreeVecto << 172 /// @param[out] aDepositsEnergies of inferred energies deposited in the 175 G4ThreeVector aParticleP << 173 /// detector 176 << 174 /// @param[in] aParticleEnergy Energy of initial particle 177 private: << 175 void GetEnergies(std::vector<G4double> &aEnergies, G4double aParticleEnergy, 178 /// Cell's size: (x,y,x) for Carthesian, a << 176 G4float aInitialAngle); 179 /// coordinates Can be changed with UI com << 177 180 /// phi z> <unit>`. For cylindrical coordi << 178 /// Calculate positions 181 /// from fMeshNumber. << 179 /// @param[out] aDepositsPositions Vector of positions corresponding to 182 G4ThreeVector fMeshSize = G4ThreeVector(2. << 180 /// energies deposited in the detector 183 /// Number of cells: (x,y,x) for Carthesia << 181 /// @param[in] aParticlePosition Initial particle position which is centre of 184 /// coordinates. Can be changed with UI co << 182 /// transverse plane of the mesh 185 /// Nz>/<Nr Nphi Nz>` << 183 /// and beginning of the mesh in the longitudinal direction 186 G4ThreeVector fMeshNumber = G4ThreeVector( << 184 /// @param[in] aParticleDirection Initial particle direction for the mesh 187 /// Inference interface << 185 /// rotation 188 std::unique_ptr<Par04InferenceInterface> f << 186 void GetPositions(std::vector<G4ThreeVector> &aDepositsPositions, 189 /// Inference messenger << 187 G4ThreeVector aParticlePosition, 190 Par04InferenceMessenger* fInferenceMesseng << 188 G4ThreeVector aParticleDirection); 191 /// Maximum particle energy value (in MeV) << 189 192 float fMaxEnergy = 1024000.0; << 190 private: 193 /// Maximum particle angle (in degrees) in << 191 /// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 194 float fMaxAngle = 90.0; << 192 /// coordinates Can be changed with UI command `/example/mesh/size <x y z>/<r 195 /// Name of the inference library << 193 /// phi z> <unit>`. For cylindrical coordinates phi is ignored and calculated 196 G4String fInferenceLibrary = "ONNX"; << 194 /// from fMeshNumber. 197 /// Size of the latent space vector << 195 G4ThreeVector fMeshSize = 198 G4int fSizeLatentVector = 10; << 196 G4ThreeVector(2.325 * CLHEP::mm, 1, 3.4 * CLHEP::mm); 199 /// Size of the condition vector << 197 /// Number of cells: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 200 G4int fSizeConditionVector = 4; << 198 /// coordinates. Can be changed with UI command `/example/mesh/number <Nx Ny 201 /// Name of the inference library << 199 /// Nz>/<Nr Nphi Nz>` 202 G4String fModelPathName = "MLModels/Genera << 200 G4ThreeVector fMeshNumber = G4ThreeVector(18, 50, 45); 203 /// ONNX specific << 201 /// Inference interface 204 /// Profiling flag << 202 std::unique_ptr<Par04InferenceInterface> fInferenceInterface; 205 G4bool fProfileFlag = false; << 203 /// Inference messenger 206 /// Optimization flag << 204 Par04InferenceMessenger *fInferenceMessenger; 207 G4bool fOptimizationFlag = false; << 205 /// Maximum particle energy value (in MeV) in the training range 208 /// Optimization file << 206 float fMaxEnergy = 1024000.0; 209 G4String fModelSavePath = "MLModels/Optimi << 207 /// Maximum particle angle (in degrees) in the training range 210 /// Profiling file << 208 float fMaxAngle = 90.0; 211 G4String fProfilingOutputSavePath = "opt.j << 209 /// Name of the inference library 212 /// Intra-operation number of threads << 210 G4String fInferenceLibrary = "ONNX"; 213 G4int fIntraOpNumThreads = 1; << 211 /// Size of the latent space vector 214 /// Flags for execution providers << 212 G4int fSizeLatentVector = 10; 215 /// GPU << 213 /// Size of the condition vector 216 G4bool fCudaFlag = false; << 214 G4int fSizeConditionVector = 4; 217 /// Execution Provider Options << 215 /// Name of the inference library 218 /// Cuda options << 216 G4String fModelPathName = "MLModels/Generator.onnx"; 219 G4String fCudaDeviceId = "0"; << 217 /// ONNX specific 220 G4String fCudaGpuMemLimit = "2147483648"; << 218 /// Profiling flag 221 G4String fCudaArenaExtendedStrategy = "kSa << 219 G4bool fProfileFlag = false; 222 G4String fCudaCudnnConvAlgoSearch = "DEFAU << 220 /// Optimization flag 223 G4String fCudaDoCopyInDefaultStream = "1"; << 221 G4bool fOptimizationFlag = false; 224 G4String fCudaCudnnConvUseMaxWorkspace = " << 222 /// Optimization file 225 std::vector<const char*> cuda_keys{ << 223 G4String fModelSavePath = "MLModels/Optimized-Generator.onnx"; >> 224 /// Profiling file >> 225 G4String fProfilingOutputSavePath = "opt.json"; >> 226 /// Intra-operation number of threads >> 227 G4int fIntraOpNumThreads = 1; >> 228 /// Flags for execution providers >> 229 /// GPU >> 230 G4bool fCudaFlag = false; >> 231 /// Execution Provider Options >> 232 /// Cuda options >> 233 G4String fCudaDeviceId = "0"; >> 234 G4String fCudaGpuMemLimit = "2147483648"; >> 235 G4String fCudaArenaExtendedStrategy = "kSameAsRequested"; >> 236 G4String fCudaCudnnConvAlgoSearch = "DEFAULT"; >> 237 G4String fCudaDoCopyInDefaultStream = "1"; >> 238 G4String fCudaCudnnConvUseMaxWorkspace = "1"; >> 239 std::vector<const char *> cuda_keys{ 226 "device_id", 240 "device_id", 227 "gpu_mem_limit", 241 "gpu_mem_limit", 228 "arena_extend_strategy", 242 "arena_extend_strategy", 229 "cudnn_conv_algo_search", 243 "cudnn_conv_algo_search", 230 "do_copy_in_default_stream", 244 "do_copy_in_default_stream", 231 "cudnn_conv_use_max_workspace", 245 "cudnn_conv_use_max_workspace", 232 }; << 246 }; 233 std::vector<const char*> cuda_values{ << 247 std::vector<const char *> cuda_values{ 234 fCudaDeviceId.c_str(), 248 fCudaDeviceId.c_str(), 235 fCudaGpuMemLimit.c_str(), 249 fCudaGpuMemLimit.c_str(), 236 fCudaArenaExtendedStrategy.c_str(), 250 fCudaArenaExtendedStrategy.c_str(), 237 fCudaCudnnConvAlgoSearch.c_str(), 251 fCudaCudnnConvAlgoSearch.c_str(), 238 fCudaDoCopyInDefaultStream.c_str(), 252 fCudaDoCopyInDefaultStream.c_str(), 239 fCudaCudnnConvUseMaxWorkspace.c_str(), 253 fCudaCudnnConvUseMaxWorkspace.c_str(), 240 }; << 254 }; 241 }; 255 }; 242 256 243 # endif /* PAR04INFEERENCESETUP_HH */ << 257 #endif /* PAR04INFEERENCESETUP_HH */ 244 #endif 258 #endif 245 259