concrete.ml.pytest.torch_models.md

arrow-up-right

module concrete.ml.pytest.torch_models

Torch modules for our pytests.


arrow-up-right

class SimpleNet

Fake torch model used to generate some onnx.

arrow-up-right

method __init__

__init__()None

arrow-up-right

method forward

forward(inputs)

Forward function.

Arguments:

  • inputs: the inputs of the model.

Returns:

  • torch.Tensor: the result of the computation


arrow-up-right

class FCSmall

Torch model for the tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class FC

Torch model for the tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class CNN

Torch CNN model for the tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class CNNMaxPool

Torch CNN model for the tests with a max pool.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class CNNOther

Torch CNN model for the tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class CNNInvalid

Torch CNN model for the tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class CNNGrouped

Torch CNN model with grouped convolution for compile torch tests.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class NetWithLoops

Torch model, where we reuse some elements in a loop.

Torch model, where we reuse some elements in a loop in the forward and don't expect the user to define these elements in a particular order.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class MultiInputNN

Torch model to test multiple inputs forward.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the first input of the NN

  • y: the second input of the NN

Returns: the output of the NN


arrow-up-right

class MultiInputNNConfigurable

Torch model to test multiple inputs forward.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the first input of the NN

  • y: the second input of the NN

Returns: the output of the NN


arrow-up-right

class MultiInputNNDifferentSize

Torch model to test multiple inputs with different shape in the forward pass.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: The first input of the NN.

  • y: The second input of the NN.

Returns: The output of the NN.


arrow-up-right

class BranchingModule

Torch model with some branching and skip connections.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class BranchingGemmModule

Torch model with some branching and skip connections.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class UnivariateModule

Torch model that calls univariate and shape functions of torch.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class StepActivationModule

Torch model implements a step function that needs Greater, Cast and Where.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass with a quantizer built into the computation graph.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class NetWithConcatUnsqueeze

Torch model to test the concat and unsqueeze operators.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class MultiOpOnSingleInputConvNN

Network that applies two quantized operations on a single input.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class FCSeq

Torch model that should generate MatMul->Add ONNX patterns.

This network generates additions with a constant scalar

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class FCSeqAddBiasVec

Torch model that should generate MatMul->Add ONNX patterns.

This network tests the addition with a constant vector

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class TinyCNN

A very small CNN.

arrow-up-right

method __init__

Create the tiny CNN with two conv layers.

Args:

  • n_classes: number of classes

  • act: the activation


arrow-up-right

method forward

Forward the two layers with the chosen activation function.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class TinyQATCNN

A very small QAT CNN to classify the sklearn digits data-set.

This class also allows pruning to a maximum of 10 active neurons, which should help keep the accumulator bit-width low.

arrow-up-right

method __init__

Construct the CNN with a configurable number of classes.

Args:

  • n_classes (int): number of outputs of the neural net

  • n_bits (int): number of weight and activation bits for quantization

  • n_active (int): number of active (non-zero weight) neurons to keep

  • signed (bool): whether quantized integer values are signed

  • narrow (bool): whether the range of quantized integer values is narrow/symmetric

  • power_of_two_scaling (bool): whether to use power-of-two scaling quantizers which allows to test the round PBS optimization when the scales are power-of-two


arrow-up-right

method forward

Run inference on the tiny CNN, apply the decision layer on the reshaped conv output.

Args:

  • x: the input to the NN

Returns: the output of the NN


arrow-up-right

method toggle_pruning

Enable or remove pruning.

Args:

  • enable: if we enable the pruning or not


arrow-up-right

class SimpleQAT

Torch model implements a step function that needs Greater, Cast and Where.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass with a quantizer built into the computation graph.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class QATTestModule

Torch model that implements a simple non-uniform quantizer.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass with a quantizer built into the computation graph.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class SingleMixNet

Torch model that with a single conv layer that produces the output, e.g., a blur filter.

arrow-up-right

method __init__


arrow-up-right

method forward

Execute the single convolution.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class DoubleQuantQATMixNet

Torch model that with two different quantizers on the input.

Used to test that it keeps the input TLU.

arrow-up-right

method __init__


arrow-up-right

method forward

Execute the single convolution.

Args:

  • x: the input of the NN

Returns: the output of the NN


arrow-up-right

class TorchSum

Torch model to test the ReduceSum ONNX operator in a leveled circuit.

arrow-up-right

method __init__

Initialize the module.

Args:

  • dim (Tuple[int]): The axis along which the sum should be executed

  • keepdim (bool): If the output should keep the same dimension as the input or not


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model

Returns:

  • torch_sum (torch.tensor): The sum of the input's tensor elements along the given axis


arrow-up-right

class TorchSumMod

Torch model to test the ReduceSum ONNX operator in a circuit containing a PBS.

arrow-up-right

method __init__

Initialize the module.

Args:

  • dim (Tuple[int]): The axis along which the sum should be executed

  • keepdim (bool): If the output should keep the same dimension as the input or not


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model

Returns:

  • torch_sum (torch.tensor): The sum of the input's tensor elements along the given axis


arrow-up-right

class NetWithConstantsFoldedBeforeOps

Torch QAT model that does not quantize the inputs.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model

Returns:

  • torch.tensor: Output of the network


arrow-up-right

class ShapeOperationsNet

Torch QAT model that reshapes the input.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model

Returns:

  • torch.tensor: Output of the network


arrow-up-right

class PaddingNet

Torch QAT model that applies various padding patterns.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model

Returns:

  • torch.tensor: Output of the network


arrow-up-right

class QuantCustomModel

A small quantized network with Brevitas, trained on make_classification.

arrow-up-right

method __init__

Quantized Torch Model with Brevitas.

Args:

  • input_shape (int): Input size

  • output_shape (int): Output size

  • hidden_shape (int): Hidden size

  • n_bits (int): Bit of quantization

  • weight_quant (brevitas.quant): Quantization protocol of weights

  • act_quant (brevitas.quant): Quantization protocol of activations.

  • bias_quant (brevitas.quant): Quantizer for the linear layer bias


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model.

Returns:

  • torch.tensor: Output of the network.


arrow-up-right

class TorchCustomModel

A small network with Brevitas, trained on make_classification.

arrow-up-right

method __init__

Torch Model.

Args:

  • input_shape (int): Input size

  • output_shape (int): Output size

  • hidden_shape (int): Hidden size


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model.

Returns:

  • torch.tensor: Output of the network.


arrow-up-right

class ConcatFancyIndexing

Concat with fancy indexing.

arrow-up-right

method __init__

Torch Model.

Args:

  • input_shape (int): Input size

  • output_shape (int): Output size

  • hidden_shape (int): Hidden size

  • n_bits (int): Number of bits

  • n_blocks (int): Number of blocks


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model.

Returns:

  • torch.tensor: Output of the network.


arrow-up-right

class PartialQATModel

A model with a QAT Module.

arrow-up-right

method __init__


arrow-up-right

method forward

Forward pass.

Args:

  • x (torch.tensor): The input of the model.

Returns:

  • torch.tensor: Output of the network.

Last updated

Was this helpful?