예제 #1
0
def test_can_infer_magnitude_sparse_linear(weight_importance, threshold,
                                           ref_output):
    nncf_module = NNCFLinear(4, 1)
    sparse_model = TestModel(nncf_module, weight_importance)
    sparsifier = sparse_model.sparsifier
    fill_linear_weight(nncf_module, 9)
    fill_bias(nncf_module, 0)
    if threshold is not None:
        sparsifier.threshold = threshold
    act_output = sparse_model(torch.ones([1, 4]))
    assert act_output.item() == ref_output
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)
예제 #3
0
def test_can_infer_magnitude_sparse_conv(weight_importance, threshold,
                                         ref_output):
    nncf_module = NNCFConv2d(1, 1, 2)
    sparse_model = TestModel(nncf_module)
    sparsifier = sparse_model.sparsifier
    fill_conv_weight(nncf_module, 9)
    fill_bias(nncf_module, 0)

    if threshold is not None:
        sparsifier.binary_mask = calc_magnitude_binary_mask(
            sparse_model.layer.weight, weight_importance, threshold)

    act_output = sparse_model(torch.ones([1, 1, 2, 2]))
    assert act_output.item() == ref_output
    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)
    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)
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