def test_fixed_point_graph(seed, graph_ref, graph_act):
    # TODO: adaptive delta case (how to test it...)

    from graph_converter_test_utils import structure_tester, value_tester, print_params

    # Random number
    np.random.seed(seed)
    rng = np.random.RandomState(seed)

    # Graph
    x_data = rng.randn(4, 3, 32, 32)
    x = nn.Variable.from_numpy_array(x_data)
    y_tgt = graph_act(x)

    # Convert weights/activations
    args_fpq = {
        "sign_w": True,
        "n_w": 8,
        "delta_w": 2e-4,
        "quantize_w": True,
        "sign_b": True,
        "n_b": 8,
        "delta_b": 2e-4,
        "quantize_b": True
    }
    name = "fixed-point-graph"
    converter_w = GC.FixedPointWeightConverter(args_fpq=args_fpq, name=name)

    name = "fixed-point-graph"
    args_fpq = {"n": 8, "sign": False, "delta": 2e-4}
    converter_a = GC.FixedPointActivationConverter(args_fpq=args_fpq,
                                                   name=name)

    # Convert sequentially
    converter = GC.SequentialConverter([converter_w, converter_a])
    y_act = converter.convert(y_tgt, [x])

    # Ref Graph
    name = "fixed-point-graph-ref"
    y_ref = graph_ref(x, name=name)

    # Test
    structure_tester(y_ref, y_act)
示例#2
0
def test_fixed_point_activation(seed, graph_ref, graph_act):
    from graph_converter_test_utils import structure_tester, value_tester, print_params

    # Random number
    np.random.seed(seed)
    rng = np.random.RandomState(seed)

    # Graph
    x_data = rng.randn(4, 3, 32, 32)
    x = nn.Variable.from_numpy_array(x_data)
    y_tgt = graph_act(x)

    # Convert
    args_fpq = {"n": 8, "sign": False, "delta": 2e-4}
    name = "fixed-point-activation-graph"
    converter = GC.FixedPointActivationConverter(args_fpq=args_fpq, name=name)
    y_act = converter.convert(y_tgt, [x])

    # Ref Graph
    name = "fixed-point-activation-graph-ref"
    y_ref = graph_ref(x, name=name)

    # Test
    structure_tester(y_ref, y_act)
    return pred


x = nn.Variable.from_numpy_array(np.random.rand(4, 3, 28, 28))
y = LeNet(x, test=True)
create_graph(y, width=300)

# ## BatchNormalizationLinearConverter
converter = GC.BatchNormalizationLinearConverter(name="bn-linear-lenet")
z = converter.convert(y, [x])
create_graph(z, width=300)
# ## BatchNormalizationFoldedConverter
converter = GC.BatchNormalizationFoldedConverter(name="bn-folded-lenet")
z = converter.convert(y, [x])
create_graph(z, width=300)
# ## FixedPointWeightConverter
converter = GC.FixedPointWeightConverter(name="fixed-point-weight-lenet")
z = converter.convert(y, [x])
create_graph(z, width=300)
# ## FixedPointActivationConverter
converter = GC.FixedPointActivationConverter(
    name="fixed-point-activation-lenet")
z = converter.convert(y, [x])
create_graph(z, width=300)
# -
converter_w = GC.FixedPointWeightConverter(name="fixed-point-lenet")
converter_a = GC.FixedPointActivationConverter(name="fixed-point-lenet")
converter = GC.SequentialConverter([converter_w, converter_a])
z = converter.convert(y, [x])
create_graph(z, width=300)