ACT icon indicating copy to clipboard operation
ACT copied to clipboard

Batched `eps` results in size mismatch for `LINF_BALL` input specs

Open hanyuone opened this issue 6 months ago • 5 comments

Summary

When batching LINF_BALL models, the center and eps tensors are joined to form tensors of dimensions (B, C, W, H) and (B,) respectively. However, because of broadcast semantics, lines such as https://github.com/SVF-tools/ACT/blob/main/act/pipeline/fuzzing/mutations.py#L495 will now result in a RuntimeError:

RuntimeError: The size of tensor a (224) must match the size of tensor b (24) at non-singleton dimension 3

since dimension H of center gets matched with dimension B of eps.

Location

The bug is in three locations in act/pipeline/fuzzing/mutations.py, all to do with directly interacting with eps to create lower/upper bounds, or deltas.

Lines 495-496:

lb = self.input_spec.center - self.input_spec.eps
ub = self.input_spec.center + self.input_spec.eps

Lines 592-593:

_lb = seeds.original_tensor.to(self.device) - self.input_spec.eps
_ub = seeds.original_tensor.to(self.device) + self.input_spec.eps

Line 686:

delta = torch.clamp(delta, -eps, eps)

Reproduction

This issue can be reproduced when testing with any batched LINF_BALL model. Below is an example tested with TorchVision specs, using CIFAR100 and resnet50:

if __name__ == "__main__":
    creator = TorchVisionSpecCreator(config_name="torchvision_classification")
    spec_results = creator.create_specs_for_data_model_pairs(
        dataset_names=["cifar100"],
        model_names=["resnet50"],
        validate_shapes=True)

    models = synthesize_models_from_specs(spec_results)
    linfball_models = {k: v for (k, v) in models.items() if k[2] == InKind.LINF_BALL}

    config_path = Path(ACT_ROOT) / "act/pipeline/fuzzing/config.yaml"
    config = FuzzingConfig.from_yaml(config_path=config_path)

    # Iterate over wrapped models for fuzzing results
    for idx, item in enumerate(linfball_models.items()):
        model: VerifiableModel = item[1] # type: ignore

        input_layer: InputLayer = model.input_layer
        labeled_input = input_layer.labeled_input

        images = labeled_input.tensor.to(DEVICE)
        labels = labeled_input.label.to(DEVICE)

        batch_size = images.shape[0]

        initial_seeds = [
            LabeledInputTensor(tensor=images[i:i+1], label=labels[i:i+1])
            for i in range(batch_size)
        ]

        fuzzer = ACTFuzzer(
            wrapped_model=model,
            initial_seeds=initial_seeds,
            config=config
        )

Unsure if this can be replicated with VNNLib, as by default it generates specs which are of input kind BOX or LINF_POLY.

hanyuone avatar May 25 '26 06:05 hanyuone

Could please give more details on how to reproduce this issue, e.g, on model, input, and eps?

guanqin-123 avatar May 25 '26 08:05 guanqin-123

@guanqin-123 Added some details, please check if it is suitable.

hanyuone avatar May 26 '26 02:05 hanyuone

@guanqin-123 Added some details, please check if it is suitable.

ok, as discussed, you can do it now.

guanqin-123 avatar May 26 '26 05:05 guanqin-123

@hanyuone can you have a check if issue is resolved in this pr #72 : guanqin-123:fix-issue71-batched-eps

guanqin-123 avatar May 29 '26 01:05 guanqin-123

Fixed with the patch in #72.

hanyuone avatar May 29 '26 05:05 hanyuone