|
| 1 | +# Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved. |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from brevitas.fx import symbolic_trace |
| 7 | +from brevitas.graph.channel_splitting import _clean_regions |
| 8 | +from brevitas.graph.channel_splitting import _split |
| 9 | +from brevitas.graph.equalize import _extract_regions |
| 10 | +from brevitas.graph.fixed_point import MergeBatchNorm |
| 11 | + |
| 12 | +from .equalization_fixtures import * |
| 13 | + |
| 14 | +no_split_models = ( |
| 15 | + 'mul_model', |
| 16 | + 'bnconv_model', |
| 17 | + 'convdepthconv_model', |
| 18 | + 'linearmha_model', |
| 19 | + 'layernormmha_model', |
| 20 | + 'convgroupconv_model', |
| 21 | + 'vit_b_32', |
| 22 | + 'shufflenet_v2_x0_5', |
| 23 | + 'googlenet', |
| 24 | + 'inception_v3') |
| 25 | + |
| 26 | +SPLIT_RATIO = 0.1 |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize('split_input', [False, True]) |
| 30 | +def test_toymodels(toy_model, split_input, request): |
| 31 | + test_id = request.node.callspec.id |
| 32 | + |
| 33 | + torch.manual_seed(SEED) |
| 34 | + |
| 35 | + model_class = toy_model |
| 36 | + model = model_class() |
| 37 | + if 'mha' in test_id: |
| 38 | + inp = torch.randn(IN_SIZE_LINEAR) |
| 39 | + else: |
| 40 | + inp = torch.randn(IN_SIZE_CONV) |
| 41 | + |
| 42 | + model.eval() |
| 43 | + expected_out = model(inp) |
| 44 | + |
| 45 | + model = symbolic_trace(model) |
| 46 | + # merge BN before applying channel splitting |
| 47 | + model = MergeBatchNorm().apply(model) |
| 48 | + |
| 49 | + # save model's state dict to check if channel splitting was done or not |
| 50 | + old_state_dict = model.state_dict() |
| 51 | + |
| 52 | + regions = _extract_regions(model) |
| 53 | + regions = _clean_regions(regions) |
| 54 | + if model_class in no_split_models: |
| 55 | + assert len(regions) == 0 |
| 56 | + else: |
| 57 | + model = _split(model, regions, split_ratio=SPLIT_RATIO, split_input=split_input) |
| 58 | + |
| 59 | + out = model(inp) |
| 60 | + assert torch.allclose(expected_out, out, atol=ATOL) |
| 61 | + |
| 62 | + modified_sources = {source for region in regions for source in region.srcs_names} |
| 63 | + # avoiding checking the same module multiple times |
| 64 | + modified_sinks = { |
| 65 | + sink for region in regions for sink in region.sinks_names} - modified_sources |
| 66 | + for module in modified_sources: |
| 67 | + if 'mha' in module: |
| 68 | + module += '.out_proj' |
| 69 | + weight_name = module + '.weight' |
| 70 | + assert not torch.equal(old_state_dict[weight_name], model.state_dict()[weight_name]) |
| 71 | + bias_name = module + '.bias' |
| 72 | + # not all modules have bias and they only differ when splitting output channels |
| 73 | + if bias_name in old_state_dict.keys() and not split_input: |
| 74 | + assert not torch.equal(old_state_dict[bias_name], model.state_dict()[bias_name]) |
| 75 | + for module in modified_sinks: |
| 76 | + weight_name = module + '.weight' |
| 77 | + assert not torch.equal(old_state_dict[weight_name], model.state_dict()[weight_name]) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize('split_input', [False, True]) |
| 81 | +def test_torchvision_models(model_coverage: tuple, split_input: bool, request): |
| 82 | + model_class = request.node.callspec.id.split('-')[0] |
| 83 | + |
| 84 | + model, coverage = model_coverage |
| 85 | + |
| 86 | + torch.manual_seed(SEED) |
| 87 | + inp = torch.randn(IN_SIZE_CONV) |
| 88 | + |
| 89 | + model.eval() |
| 90 | + expected_out = model(inp) |
| 91 | + |
| 92 | + model = symbolic_trace(model) |
| 93 | + # merge BN before applying channel splitting |
| 94 | + model = MergeBatchNorm().apply(model) |
| 95 | + |
| 96 | + old_state_dict = model.state_dict() |
| 97 | + |
| 98 | + regions = _extract_regions(model) |
| 99 | + regions = _clean_regions(regions) |
| 100 | + if model_class in no_split_models: |
| 101 | + assert len(regions) == 0 |
| 102 | + else: |
| 103 | + model = _split(model, regions, split_ratio=SPLIT_RATIO, split_input=split_input) |
| 104 | + |
| 105 | + out = model(inp) |
| 106 | + assert torch.allclose(expected_out, out, atol=ATOL) |
| 107 | + |
| 108 | + modified_sources = {source for region in regions for source in region.srcs_names} |
| 109 | + # avoiding checking the same module multiple times |
| 110 | + modified_sinks = { |
| 111 | + sink for region in regions for sink in region.sinks_names} - modified_sources |
| 112 | + for module in modified_sources: |
| 113 | + weight_name = module + '.weight' |
| 114 | + assert not torch.equal(old_state_dict[weight_name], model.state_dict()[weight_name]) |
| 115 | + bias_name = module + '.bias' |
| 116 | + # not all modules have bias and they only differ when splitting output channels |
| 117 | + if bias_name in old_state_dict.keys() and not split_input: |
| 118 | + assert not torch.equal(old_state_dict[bias_name], model.state_dict()[bias_name]) |
| 119 | + for module in modified_sinks: |
| 120 | + weight_name = module + '.weight' |
| 121 | + assert not torch.equal(old_state_dict[weight_name], model.state_dict()[weight_name]) |
0 commit comments