Batched `eps` results in size mismatch for `LINF_BALL` input specs
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.
lb = self.input_spec.center - self.input_spec.eps
ub = self.input_spec.center + self.input_spec.eps
_lb = seeds.original_tensor.to(self.device) - self.input_spec.eps
_ub = seeds.original_tensor.to(self.device) + self.input_spec.eps
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.
Could please give more details on how to reproduce this issue, e.g, on model, input, and eps?
@guanqin-123 Added some details, please check if it is suitable.
@guanqin-123 Added some details, please check if it is suitable.
ok, as discussed, you can do it now.
@hanyuone can you have a check if issue is resolved in this pr #72 : guanqin-123:fix-issue71-batched-eps
Fixed with the patch in #72.