Class FunctionalGraph
Represents a model graph built with the Functional API.
Inherited Members
Namespace: Unity.InferenceEngine
Assembly: Unity.InferenceEngine.dll
Syntax
[MovedFrom("Unity.Sentis")]
public class FunctionalGraph
Remarks
FunctionalGraph defines a computation graph by adding inputs, applying Functional operations to FunctionalTensor values, and declaring outputs. Use AddInput(DataType, DynamicTensorShape, string) or AddInputs(Model) to define graph inputs, then chain operations from the Functional class. Call AddOutput(FunctionalTensor, string) or AddOutputs(params FunctionalTensor[]) to declare which tensors the graph produces, then Compile() to build an optimized runtime Model.
You can create new models from scratch, wrap or extend existing models with Forward(Model, params FunctionalTensor[]), or build preprocessing and postprocessing pipelines.
Additional resources
Examples
Create a functional graph from scratch. Build a graph with inputs, operations, and outputs, then compile to a model.
var graph = new FunctionalGraph();
var x = graph.AddInput<float>(new TensorShape(6), "input_x");
var y = graph.AddInput<float>(new TensorShape(6), "input_y");
var prod = x * y;
var reduce = Functional.ReduceSum(prod, dim: 0, keepdim: false);
graph.AddOutput(reduce, "output");
Model model = graph.Compile();
// Modify an existing model.
// Declare a functional graph.
var graph = new FunctionalGraph();
// Get the input functional tensor from the graph with input data type and shape matching that of the original model input.
var RGB = graph.AddInput(sourceModel, 0);
// Apply f(x) = x^(1/2.2) element-wise to transform from RGB to sRGB.
var sRGB = Functional.Pow(RGB, Functional.Constant(1 / 2.2f));
// Apply f(x) = x * 2 - 1 element-wise to transform values from the range [0, 1] to the range [-1, 1].
var sRGB_normalised = sRGB * 2 - 1;
// Apply the forward method of the source model to the transformed functional input and add the outputs to the graph.
var outputs = Functional.Forward(sourceModel, sRGB_normalised);
graph.AddOutputs(outputs);
// Compile the graph to return the final model.
m_RuntimeModel = graph.Compile();
Methods
AddInput(DataType, DynamicTensorShape, string)
Appends an input to the graph with the specified data type and dynamic shape.
Declaration
public FunctionalTensor AddInput(DataType dataType, DynamicTensorShape shape, string name = null)
Parameters
| Type | Name | Description |
|---|---|---|
| DataType | dataType | The data type of the input. |
| DynamicTensorShape | shape | The dynamic shape of the input. |
| string | name | The name of the input. If null, defaults to |
Returns
| Type | Description |
|---|---|
| FunctionalTensor | The functional tensor input. |
Remarks
Use this overload when the input has dynamic dimensions (for example, variable batch size or sequence length).
Examples
Add an input with dynamic shape for variable batch or sequence length.
var shape = DynamicTensorShape.DynamicOfRank(2);
var input = graph.AddInput(DataType.Float, shape, "input");
AddInput(DataType, TensorShape, string)
Appends an input to the graph with the specified data type and static shape.
Declaration
public FunctionalTensor AddInput(DataType dataType, TensorShape shape, string name = null)
Parameters
| Type | Name | Description |
|---|---|---|
| DataType | dataType | The data type of the input. |
| TensorShape | shape | The static shape of the input. |
| string | name | The name of the input. If null, defaults to |
Returns
| Type | Description |
|---|---|
| FunctionalTensor | The functional tensor input. |
Remarks
Use this overload when all dimensions of the input are known at compile time.
Examples
Add an input with static shape.
var input = graph.AddInput(DataType.Float, new TensorShape(1, 3, 224, 224), "image");
AddInput(Model, int, string)
Appends an input to the graph that matches a specific input of an existing model.
Declaration
public FunctionalTensor AddInput(Model model, int index, string name = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Model | model | The model whose input to match. |
| int | index | The index of the input in the model. |
| string | name | The name for this input. If null, uses the name from the model. |
Returns
| Type | Description |
|---|---|
| FunctionalTensor | The functional tensor input. |
Remarks
Use this overload when extending or wrapping an existing model. The new input has the same data type and shape as the model input at the given index. Pass the resulting tensor(s) to Forward(Model, params FunctionalTensor[]) to run the model.
Examples
Add an input matching a model input, then run the model and add its output.
var input = graph.AddInput(sourceModel, 0);
var outputs = Functional.Forward(sourceModel, new[] { input });
graph.AddOutput(outputs[0]);
AddInput<T>(DynamicTensorShape, string)
Appends an input to the graph with type inferred from T and a dynamic shape.
Declaration
public FunctionalTensor AddInput<T>(DynamicTensorShape shape, string name = null) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| DynamicTensorShape | shape | The dynamic shape of the input. |
| string | name | The name of the input. If null, defaults to |
Returns
| Type | Description |
|---|---|
| FunctionalTensor | The functional tensor input. |
Type Parameters
| Name | Description |
|---|---|
| T | The element type of the input (for example, |
Remarks
The data type is derived from the generic parameter (for example, float for AddInput<float>). Use when you prefer type inference over explicit DataType.
Examples
Add an input with type inferred from the generic parameter.
var input = graph.AddInput<float>(DynamicTensorShape.DynamicOfRank(1));
AddInput<T>(TensorShape, string)
Appends an input to the graph with type inferred from T and a static shape.
Declaration
public FunctionalTensor AddInput<T>(TensorShape shape, string name = null) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| TensorShape | shape | The static shape of the input. |
| string | name | The name of the input. If null, defaults to |
Returns
| Type | Description |
|---|---|
| FunctionalTensor | The functional tensor input. |
Type Parameters
| Name | Description |
|---|---|
| T | The element type of the input (for example, |
Remarks
The data type is derived from the generic parameter (for example, float for AddInput<float>). Use when you prefer type inference over explicit DataType.
Examples
Add inputs with type inferred from the generic parameter.
var x = graph.AddInput<float>(new TensorShape(6), "input_x");
var y = graph.AddInput<int>(new TensorShape(3), "indices");
AddInputs(Model)
Appends inputs to the graph that match all inputs of an existing model.
Declaration
public FunctionalTensor[] AddInputs(Model model)
Parameters
| Type | Name | Description |
|---|---|---|
| Model | model | The model whose inputs to match. |
Returns
| Type | Description |
|---|---|
| FunctionalTensor[] | The array of functional tensor inputs, one per model input. |
Remarks
Use this overload when wrapping or extending a model. Each input matches the corresponding model input by index. Pass the returned array to Forward(Model, params FunctionalTensor[]) to run the model.
Examples
Add inputs matching all model inputs, run the model, and add a postprocessed output.
var inputs = graph.AddInputs(sourceModel);
var softmax = Functional.Softmax(outputs[0]);
graph.AddOutput(softmax);
AddOutput(FunctionalTensor, string)
Appends an output to the graph from a functional tensor.
Declaration
public void AddOutput(FunctionalTensor output, string name = null)
Parameters
| Type | Name | Description |
|---|---|---|
| FunctionalTensor | output | The functional tensor to use as an output. |
| string | name | The name for the output. If null, defaults to |
Remarks
The output tensor must be derived from inputs or constants in the graph. If the tensor comes from an input or constant, it is cloned before being added. You must add at least one output before calling Compile().
Examples
Add an output from a functional tensor.
var x = graph.AddInput<float>(new TensorShape(6));
var result = Functional.Relu(x);
graph.AddOutput(result, "output");
AddOutputs(params FunctionalTensor[])
Appends multiple outputs to the graph from the given functional tensors.
Declaration
public void AddOutputs(params FunctionalTensor[] outputs)
Parameters
| Type | Name | Description |
|---|---|---|
| FunctionalTensor[] | outputs | The functional tensors to use as outputs. |
Remarks
Equivalent to calling AddOutput(FunctionalTensor, string) for each tensor in the array. Use when the graph produces multiple outputs. You must add at least one output before calling Compile()
Examples
Add multiple outputs at once.
var x = graph.AddInput<float>(new TensorShape(6));
var y = graph.AddInput<float>(new TensorShape(6));
var reduce = Functional.ReduceSum(x * y, dim: 0, keepdim: false);
var prod = x * y;
graph.AddOutputs(reduce, prod);
Compile()
Compiles a graph with the given outputs and returns an optimized runtime model.
Declaration
public Model Compile()
Returns
| Type | Description |
|---|---|
| Model | The compiled runtime model. |
Remarks
You must add at least one output to the graph using AddOutput(FunctionalTensor, string) or AddOutputs(params FunctionalTensor[]) before calling this method. The returned model is optimized and ready to run with a Worker.
Examples
Compile the graph to an optimized model.
var x = graph.AddInput<float>(new TensorShape(6));
var result = Functional.Relu(x);
graph.AddOutput(result);
Model model = graph.Compile();
Compile(params FunctionalTensor[])
Compiles a graph with the given outputs and returns an optimized runtime model.
Declaration
public Model Compile(params FunctionalTensor[] outputs)
Parameters
| Type | Name | Description |
|---|---|---|
| FunctionalTensor[] | outputs | The functional tensors to use as outputs. |
Returns
| Type | Description |
|---|---|
| Model | The compiled runtime model. |
Remarks
Adds the specified tensors as outputs and then compiles. With this overload, don't add outputs to the graph separately or AddOutputs(params FunctionalTensor[]). The returned model is optimized and ready to run with a Worker. Throws an exception if outputs were already added to the graph.
Examples
Compile with inline outputs (no prior AddOutput call).
var x = graph.AddInput<float>(new TensorShape(6));
var y = graph.AddInput<float>(new TensorShape(6));
var result = x * y;
Model model = graph.Compile(result);
Exceptions
| Type | Condition |
|---|---|
| AssertionException | Thrown when outputs have already been added to the graph. |