Skip to content

crown

Compute CROWN, CROWN-IBP and alpha-CROWN bounds through auto_LiRPA.

bound_alpha_crown(model, ptb, data, target, n_classes=10, bound_upper=False, reuse_input=False, alpha_iterations=20)

Compute classification-margin bounds by optimising CROWN relaxation slopes.

Alpha-CROWN tightens linear relaxations through gradient-based slope optimisation. The computation enables the required gradients even inside no_grad, without accumulating parameter gradients. Caller modes, gradient flags and bound options are restored after the computation.

Source code in CTRAIN/bound/crown.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def bound_alpha_crown(model, ptb, data, target, n_classes=10,
                      bound_upper=False, reuse_input=False, alpha_iterations=20):
    """Compute classification-margin bounds by optimising CROWN relaxation slopes.

    Alpha-CROWN tightens linear relaxations through gradient-based slope
    optimisation. The computation enables the required gradients even inside
    no_grad, without accumulating parameter gradients. Caller modes, gradient
    flags and bound options are restored after the computation.
    """
    if type(alpha_iterations) is not int or alpha_iterations <= 0:
        raise ValueError('alpha_iterations must be a positive integer')
    options = copy.deepcopy(model.bound_opts)
    try:
        with preserve_model_state(model), torch.enable_grad():
            model.eval()
            for parameter in model.parameters():
                parameter.requires_grad_(False)
            model.set_bound_opts({'optimize_bound_args': {'iteration': alpha_iterations}})
            bounded = BoundedTensor(data, ptb=ptb)
            return model.compute_bounds(
                x=None if reuse_input else (bounded,), IBP=False,
                method='CROWN-Optimized', C=construct_c(data, target, n_classes),
                bound_upper=bound_upper)
    finally:
        model.set_bound_opts(options)

bound_crown(model, ptb, data, target, n_classes=10, bound_upper=False, reuse_input=False)

Compute classification-margin bounds using CROWN linear relaxations.

CROWN uses linear relaxations throughout the network to bound each true-class logit minus its competing logits over the supplied input region.

Parameters:

Name Type Description Default
model BoundedModule

The neural network model for which bounds are to be computed.

required
ptb PerturbationLpNorm

The perturbation object defining the perturbation set.

required
data Tensor

The input data tensor.

required
target Tensor

The target labels tensor.

required
n_classes int

The number of classes for classification.

10
bound_upper bool

Whether to compute the upper bound.

False
reuse_input bool

Whether to reuse the input data from previous bounding operation.

False

Returns:

Type Description
tuple

Lower and optional upper bounds on the requested outputs or margins. The upper entry is None when upper bounds are not computed.

Source code in CTRAIN/bound/crown.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def bound_crown(model, ptb, data, target, n_classes=10, bound_upper=False, reuse_input=False):
    """Compute classification-margin bounds using CROWN linear relaxations.

    CROWN uses linear relaxations throughout the network to bound each true-class
    logit minus its competing logits over the supplied input region.

    Args:
        model (auto_LiRPA.BoundedModule): The neural network model for which bounds are to
            be computed.
        ptb (auto_LiRPA.PerturbationLpNorm): The perturbation object defining the
            perturbation set.
        data (Tensor): The input data tensor.
        target (Tensor): The target labels tensor.
        n_classes (int, optional): The number of classes for classification.
        bound_upper (bool, optional): Whether to compute the upper bound.
        reuse_input (bool, optional): Whether to reuse the input data from previous bounding
            operation.

    Returns:
        (tuple): Lower and optional upper bounds on the requested outputs or
            margins. The upper entry is None when upper bounds are not computed.
    """
    data = BoundedTensor(data, ptb=ptb)
    c = construct_c(data, target, n_classes)
    if reuse_input:
        bound_input = None
    else:
        bound_input = (data,)
    lb, ub = model.compute_bounds(x=bound_input, IBP=False, method="CROWN", C=c, bound_upper=bound_upper)
    return lb, ub

bound_crown_ibp(model, ptb, data, target, n_classes=10, bound_upper=False, reuse_input=False, loss_fusion=False)

Compute CROWN margin bounds using interval bounds at intermediate layers.

CROWN-IBP combines inexpensive IBP intermediate bounds with a backward CROWN relaxation. Loss fusion instead bounds the supplied classifier-and-loss graph.

Parameters:

Name Type Description Default
model BoundedModule

The neural network model for which bounds are to be computed.

required
ptb PerturbationLpNorm

The perturbation object defining the perturbation set.

required
data Tensor

The input data tensor.

required
target Tensor

The target labels tensor.

required
n_classes int

The number of classes for classification.

10
bound_upper bool

Whether to compute the upper bound.

False
reuse_input bool

Whether to reuse the input data from previous bounding operation.

False
loss_fusion bool

Whether to use loss fusion.

False

Returns:

Type Description
tuple

Lower and optional upper bounds on the requested outputs or margins. The upper entry is None when upper bounds are not computed.

Source code in CTRAIN/bound/crown.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def bound_crown_ibp(model, ptb, data, target, n_classes=10, bound_upper=False, reuse_input=False, loss_fusion=False):
    """Compute CROWN margin bounds using interval bounds at intermediate layers.

    CROWN-IBP combines inexpensive IBP intermediate bounds with a backward CROWN
    relaxation. Loss fusion instead bounds the supplied classifier-and-loss graph.

    Args:
        model (auto_LiRPA.BoundedModule): The neural network model for which bounds are to
            be computed.
        ptb (auto_LiRPA.PerturbationLpNorm): The perturbation object defining the
            perturbation set.
        data (Tensor): The input data tensor.
        target (Tensor): The target labels tensor.
        n_classes (int, optional): The number of classes for classification.
        bound_upper (bool, optional): Whether to compute the upper bound.
        reuse_input (bool, optional): Whether to reuse the input data from previous bounding
            operation.
        loss_fusion (bool, optional): Whether to use loss fusion.

    Returns:
        (tuple): Lower and optional upper bounds on the requested outputs or
            margins. The upper entry is None when upper bounds are not computed.
    """
    data = BoundedTensor(data, ptb=ptb)
    c = construct_c(data, target, n_classes) if not loss_fusion else None
    if reuse_input:
        bound_input = None
    elif loss_fusion:
        bound_input = (data, target)
    else:
        bound_input = (data,)
    lb, ub = model.compute_bounds(x=bound_input, IBP=False, method="CROWN-IBP", C=c, bound_upper=bound_upper)
    return lb, ub