Beispiel #1
0
def create_nncf_conv2d(module):
    assert module.__class__.__name__ == nn.Conv2d.__name__

    nncf_conv = NNCFConv2d(module.in_channels, module.out_channels,
                           module.kernel_size, module.stride, module.padding,
                           module.dilation, module.groups,
                           hasattr(module, 'bias'))
    dict_update(nncf_conv.__dict__, module.__dict__)
    return nncf_conv
Beispiel #2
0
def test_can_infer_magnitude_sparse_conv(weight_importance, threshold,
                                         ref_output):
    nncf_module = NNCFConv2d(1, 1, 2)
    sparse_model = TestModel(nncf_module, weight_importance)
    sparsifier = sparse_model.sparsifier
    fill_conv_weight(nncf_module, 9)
    fill_bias(nncf_module, 0)
    if threshold is not None:
        sparsifier.threshold = threshold
    act_output = sparse_model(torch.ones([1, 1, 2, 2]))
    assert act_output.item() == ref_output
Beispiel #3
0
def test_assert_broadcastable_mask_and_weight_shape():
    nncf_module = NNCFConv2d(1, 2, 2)
    fill_conv_weight(nncf_module, 1)
    fill_bias(nncf_module, 1)

    mask = torch.zeros(10)

    with pytest.raises(RuntimeError):
        inplace_apply_filter_binary_mask(mask, nncf_module.weight.data)

    with pytest.raises(RuntimeError):
        apply_filter_binary_mask(mask, nncf_module.weight.data)
Beispiel #4
0
    def test_inplace_apply_filter_binary_mask(mask, reference_weight,
                                              reference_bias):
        """
        Test that inplace_apply_filter_binary_mask changes the input weight and returns valid result.
        """
        nncf_module = NNCFConv2d(1, 2, 2)
        fill_conv_weight(nncf_module, 1)
        fill_bias(nncf_module, 1)

        result_weight = inplace_apply_filter_binary_mask(
            mask, nncf_module.weight.data)
        assert torch.allclose(result_weight, reference_weight)
        assert torch.allclose(nncf_module.weight, reference_weight)

        result_bias = inplace_apply_filter_binary_mask(mask,
                                                       nncf_module.bias.data)
        assert torch.allclose(result_bias, reference_bias)
        assert torch.allclose(nncf_module.bias, reference_bias)
Beispiel #5
0
    def test_apply_filter_binary_mask(mask, reference_weight, reference_bias):
        """
        Test that apply_filter_binary_mask not changes the input weight and returns valid result.
        """
        nncf_module = NNCFConv2d(1, 2, 2)
        fill_conv_weight(nncf_module, 1)
        fill_bias(nncf_module, 1)

        original_weight = nncf_module.weight.data.detach().clone()
        original_bias = nncf_module.bias.data.detach().clone()

        result = apply_filter_binary_mask(mask, nncf_module.weight.data)
        assert torch.allclose(nncf_module.weight, original_weight)
        assert torch.allclose(result, reference_weight)

        result_bias = apply_filter_binary_mask(mask, nncf_module.bias.data)
        assert torch.allclose(result_bias, reference_bias)
        assert torch.allclose(nncf_module.bias, original_bias)
Beispiel #6
0
def test_can_infer_magnitude_pruned_conv(weights_val, bias_val):
    """
    Check that NNCFConv2d with FilterPruningBlock as pre ops working exactly the same as
    normal nn.Conv2d.
    :param weights_val: value for filling weights
    :param bias_val: value for filling biases
    """
    nncf_module = NNCFConv2d(1, 1, 2)
    pytorch_module = nn.Conv2d(1, 1, 2)

    sparse_model = TestFilterPruningBlockModel(nncf_module)

    fill_conv_weight(nncf_module, weights_val)
    fill_bias(nncf_module, bias_val)

    fill_conv_weight(pytorch_module, weights_val)
    fill_bias(pytorch_module, bias_val)

    act_output = sparse_model(torch.ones([1, 1, 2, 2]))
    ref_output = pytorch_module(torch.ones([1, 1, 2, 2]))
    assert act_output.item() == ref_output