ImFusion C++ SDK 4.5.0
ImFusion::TorchEngine Class Reference

#include <TorchPlugin/include/ImFusion/Torch/TorchEngine.h>

Main wrapper around a Torch neural network performing a prediction on an image. More...

Inheritance diagram for ImFusion::TorchEngine:

Detailed Description

Main wrapper around a Torch neural network performing a prediction on an image.

Public Member Functions

 TorchEngine (const Properties &properties)
 Constructor from properties Note: the creation of the correct properties is typically taken care by the ModelConfiguration object, via the method 'getEngineProperties()'.
ML::DataItem predict (const ML::DataItem &input) override
 Compute predictons of all classes for multiple images.
std::vector< ML::ExecutionProvideravailableProviders () const override
 Returns which provider is supported by the engine and available on the host.
std::optional< ML::ExecutionProviderprovider () const override
 Returns which provider is used by the engine to run the model.
Public Member Functions inherited from ImFusion::ML::Engine
 Engine (const std::string &name)
std::string name () const
 Returns the name of the operation as defined in the factories.
virtual void configure (const Properties *properties) override
 Configure the Engine according to the properties.
virtual void configuration (Properties *properties) const override
 Serialize the current object configuration into the given Properties object.
virtual bool isIdentical (const Engine *other) const
 Check whether this engine instance is the same as other.
virtual void checkInputFields (const DataItem &input)
 Check that inputs specified in the config are present in the input item.
virtual void checkOutputFields (const DataItem &input)
 Check that outputs specified in the config are present in the predicted item.
std::optional< ByteBufferloadModelArtifact () const
 Convenience function to load the model artifact specified in p_modelFile either from the model file or from a resource repository.
Public Member Functions inherited from ImFusion::Configurable
virtual void configureDefaults ()
 Retrieve the properties of this object, replaces values with their defaults and sets it again.
void registerParameter (ParameterBase *param)
 Register the given Parameter or SubProperty, so that it will be configured during configure()/configuration().
void unregisterParameter (const ParameterBase *param)
 Remove the given Parameter or SubProperty from the list of registered parameters.
 Configurable (const Configurable &rhs)
 Configurable (Configurable &&rhs) noexcept
Configurable & operator= (const Configurable &)
Configurable & operator= (Configurable &&) noexcept

Public Attributes

Parameter< bool > p_squeezeInput = {"EngineSqueezeInput", false, this}
 Run squeeze on input tensor before running the model Squeezing will remove the batch dimension if tensor is single batch.
Parameter< bool > p_disableJITRecompile = {"DisableJITRecompile", false, this}
 Disable JIT recompilation only for the next forward pass of the model.
Parameter< std::stringp_autocast = {"Autocast", "None", this}
 Configure autocast precision mode for improved performance.
Public Attributes inherited from ImFusion::ML::Engine
Parameter< std::stringp_modelFile = {"ModelFile", "", nullptr}
 Parameters shared by all engines.
Parameter< bool > p_forceCPU = {"ForceCPU", false, nullptr}
 Whether to force run on CPU even if GPU is present.
Parameter< std::vector< std::string > > p_inputFields = {"EngineInputFields", std::vector<std::string>{}, nullptr}
 Input field names expected by the model run by the engine.
Parameter< std::vector< std::string > > p_outputFields = {"EngineOutputFields", std::vector<std::string>{}, nullptr}
 Output field names returned by the model run by the engine.
Parameter< std::vector< std::string > > p_ignoreOutputFields = {"EngineOutputFieldsToIgnore", std::vector<std::string>{}, nullptr}
 Output field names to be ignored by the engine.
Parameter< std::stringp_version = {"Version", "", nullptr}
 Version of the engine used to train and save the model.
Parameter< bool > p_logInputToDataLogger = {"LogInputToDataLogger", false, this}
 When true, each engine input SharedImageSet is sent to the Data Logger.
Public Attributes inherited from ImFusion::Configurable
Signal signalParametersChanged
 Emitted whenever one of the registered Parameters' or SubPropertys' signalValueChanged signal was emitted.

Protected Member Functions

bool loadModel ()
 Load model architecture and weights from file, and return whether it was successful This method has to be called before any prediction.
void clearCache ()
 Clear the GPU cache allocated by torch.
void onModelFileChanged () override
 Function that gets called when the model file of the engine changes.
void onForceCPUChanged () override
 Function that gets called when the forceCPU flag of the engine changes.
bool useCuda () const
 Returns whether the engine and model use CUDA.
bool useMPS () const
 Returns whether the engine and model use MPS (Apple).
Protected Member Functions inherited from ImFusion::ML::Engine
virtual void init (const Properties &properties)
 Configures the engine from properties + connect signals.
void logInputToDataLogger (const DataItem &input) const
 Sends all SISBasedElement inputs to the Data Logger when p_logInputToDataLogger is enabled.
virtual void connectSignals ()
 Connect the signals like onModelFileChanged, onForceCPUChanged, ... (We do not do this automatically because children class need to call this after they register their parameters).

Protected Attributes

std::unique_ptr< TorchModulem_torchModule
bool m_hasCuda = false
bool m_cudaDeviceAvailable = false
std::optional< int > m_cudaDeviceIndex
bool m_hasMPS = false
Protected Attributes inherited from ImFusion::ML::Engine
EngineConfiguration m_config
 Configuration object.
std::string m_name = ""
 Name of the engine interface (e.g. "torch").
Protected Attributes inherited from ImFusion::Configurable
std::vector< Paramm_params
 List of all registered Parameter and SubProperty instances.

Additional Inherited Members

Static Public Member Functions inherited from ImFusion::ML::Engine
static std::vector< std::stringavailableEngines (EngineLanguage language=EngineLanguage::Any)
 List all registered inference engines for a given implementation language.
static bool isRegistered (const std::string &engineName, bool quiet=true, EngineLanguage language=EngineLanguage::Any)
 Check if an engine is registered for the selected implementation language.
static std::unique_ptr< Engine > create (const std::string &engineName, const Properties &properties, EngineLanguage language=EngineLanguage::Any)
 Factory function to create an engine for the selected implementation language.

Constructor & Destructor Documentation

◆ TorchEngine()

ImFusion::TorchEngine::TorchEngine ( const Properties & properties)
explicit

Constructor from properties Note: the creation of the correct properties is typically taken care by the ModelConfiguration object, via the method 'getEngineProperties()'.

If the some required parameters is missing, it throws. In this case no extra parameter wrt ML::Engine is needed, so everything is set up in the base class

Member Function Documentation

◆ predict()

ML::DataItem ImFusion::TorchEngine::predict ( const ML::DataItem & input)
overridevirtual

Compute predictons of all classes for multiple images.

Implements ImFusion::ML::Engine.

◆ availableProviders()

std::vector< ML::ExecutionProvider > ImFusion::TorchEngine::availableProviders ( ) const
overridevirtual

Returns which provider is supported by the engine and available on the host.

Implements ImFusion::ML::Engine.

◆ provider()

std::optional< ML::ExecutionProvider > ImFusion::TorchEngine::provider ( ) const
overridevirtual

Returns which provider is used by the engine to run the model.

Implements ImFusion::ML::Engine.

◆ onModelFileChanged()

void ImFusion::TorchEngine::onModelFileChanged ( )
overrideprotectedvirtual

Function that gets called when the model file of the engine changes.

Reimplemented from ImFusion::ML::Engine.

◆ onForceCPUChanged()

void ImFusion::TorchEngine::onForceCPUChanged ( )
overrideprotectedvirtual

Function that gets called when the forceCPU flag of the engine changes.

Reimplemented from ImFusion::ML::Engine.

Member Data Documentation

◆ p_autocast

Parameter<std::string> ImFusion::TorchEngine::p_autocast = {"Autocast", "None", this}

Configure autocast precision mode for improved performance.

Autocast automatically uses lower precision (FP16/BF16) for operations that can benefit from it while maintaining FP32 for operations that need higher precision.

Supported Values:

  • "None" (default): No autocast, uses model's native precision (typically FP32)
  • "Float16": FP16 autocast - supported on CUDA, MPS, and CPU
  • "BFloat16": BFloat16 autocast - supported on CUDA, CPU and MPS (for Torch >= 2.7)

Example YAML Configuration:

Engine:
Name: torch
ModelFile: model.pt
Autocast: BFloat16 # Enable BFloat16 autocast if the GPU supports it

The documentation for this class was generated from the following file:
  • TorchPlugin/include/ImFusion/Torch/TorchEngine.h
Search Tab / S to search, Esc to close