def test_load_from_store_skip_if_unecessary():
    def g(a, b):
        value = a + b
        return value

    desc = xun.describe(g)

    @xun.function_ast
    def reference_source():
        value = a + b
        return value

    # Dummy dependency
    @xun.function()
    def dummy():
        pass
    known_functions = {'f': dummy}

    code = (xun.functions.FunctionDecomposition(desc)
        .apply(xun.functions.separate_constants)
        .apply(xun.functions.sort_constants)
        .apply(xun.functions.copy_only_constants, known_functions)
        .apply(xun.functions.load_from_store, known_functions))

    generated = [*code.load_from_store, *code.body]
    reference = reference_source.body[0].body

    for a, b in zip(generated, reference):
        if not compare_ast(a, b):
            raise ValueError('\n{} != \n{}'.format(ast.dump(a), ast.dump(b)))
Exemple #2
0
def test_load_from_store_skip_if_unecessary():
    def g(a, b):
        value = a + b
        return value

    desc = xun.describe(g)

    @xun.function_ast
    def reference_source():
        value = a + b
        return value

    # Dummy dependency
    @xun.function()
    def dummy():
        pass

    known_functions = {'f': dummy}

    code = (xun.functions.FunctionDecomposition(desc).apply(
        xun.functions.separate_constants).apply(
            xun.functions.sort_constants).apply(
                xun.functions.copy_only_constants,
                known_functions).apply(xun.functions.load_from_store,
                                       known_functions))

    generated = [*code.load_from_store, *code.body]
    reference = reference_source.body[0].body

    ok, diff = check_ast_equals(generated, reference)
    assert ok, diff
Exemple #3
0
def test_load_from_store_transformation():
    def g():
        with ...:
            a = f()
            b = f(a)
            c = f(b)
        value = a + c
        return value

    desc = xun.describe(g)

    @xun.function_ast
    def reference_source():
        def _xun_load_constants():
            from copy import deepcopy  # noqa: F401
            from xun.functions import CallNode as _xun_CallNode
            from xun.functions.store import StoreAccessor as _xun_StoreAccessor
            _xun_store_accessor = _xun_StoreAccessor(_xun_store)
            a = _xun_CallNode('f', 'K9ZuxDD5x6atLkNd')
            b = _xun_CallNode('f', 'K9ZuxDD5x6atLkNd', a)
            c = _xun_CallNode('f', 'K9ZuxDD5x6atLkNd', b)
            return (
                _xun_store_accessor.load_result(
                    _xun_CallNode('f', 'K9ZuxDD5x6atLkNd')),
                _xun_store_accessor.load_result(
                    _xun_CallNode('f', 'K9ZuxDD5x6atLkNd', b)),
            )

        a, c = _xun_load_constants()
        value = a + c
        return value

    # Dummy dependency
    @xun.function()
    def dummy():
        pass

    known_functions = {'f': dummy}

    code = (xun.functions.FunctionDecomposition(desc).apply(
        xun.functions.separate_constants).apply(
            xun.functions.sort_constants).apply(
                xun.functions.copy_only_constants,
                known_functions).apply(xun.functions.load_from_store,
                                       known_functions))

    generated = [*code.load_from_store, *code.body]
    reference = reference_source.body[0].body

    ok, diff = check_ast_equals(generated, reference)
    assert ok, diff
Exemple #4
0
def test_structured_unpacking_transformation():
    def g():
        with ...:
            a, b, ((x, y, z), (𝛂, β)), c, d = f()
            something = h(x, y, z)
        return a * b * x * y * z * 𝛂 * β * c * d + something

    desc = xun.describe(g)

    # Dummy dependency
    @xun.function()
    def dummy():
        pass

    known_functions = {'f': dummy, 'h': dummy}

    code = (xun.functions.FunctionDecomposition(desc).apply(
        xun.functions.separate_constants).apply(
            xun.functions.sort_constants).apply(
                xun.functions.copy_only_constants,
                known_functions).apply(xun.functions.load_from_store,
                                       known_functions))

    @xun.function_ast
    def reference_source():
        def _xun_load_constants():
            from copy import deepcopy  # noqa: F401
            from xun.functions import CallNode as _xun_CallNode
            from xun.functions.store import StoreAccessor as _xun_StoreAccessor
            _xun_store_accessor = _xun_StoreAccessor(_xun_store)
            a, b, ((x, y, z),
                   (𝛂, β)), c, d = _xun_CallNode('f',
                                                 'K9ZuxDD5x6atLkNd').unpack(
                                                     (2, ((3, ), (2, )), 2))
            something = _xun_CallNode('h', 'K9ZuxDD5x6atLkNd', x, y, z)
            return (
                _xun_store_accessor.load_result(
                    _xun_CallNode('f', 'K9ZuxDD5x6atLkNd')),
                _xun_store_accessor.load_result(
                    _xun_CallNode('h', 'K9ZuxDD5x6atLkNd', x, y, z)),
            )

        (a, b, ((x, y, z), (𝛂, β)), c, d), something = _xun_load_constants()
        return a * b * x * y * z * 𝛂 * β * c * d + something

    generated = [*code.load_from_store, *code.body]
    reference = reference_source.body[0].body

    ok, diff = check_ast_equals(generated, reference)
    assert ok, diff
def test_load_from_store_transformation():
    def g():
        with ...:
            a = f()
            b = f(a)
            c = f(b)
        value = a + c
        return value

    desc = xun.describe(g)

    @xun.function_ast
    def reference_source():
        def _xun_load_constants():
            from copy import deepcopy  # noqa: F401
            from xun.functions import CallNode as _xun_CallNode
            from xun.functions.store import StoreAccessor as _xun_StoreAccessor
            _xun_store_accessor = _xun_StoreAccessor(_xun_store)
            a = _xun_CallNode('f')
            b = _xun_CallNode('f', a)
            c = _xun_CallNode('f', b)
            return (
                _xun_store_accessor.load_result(_xun_CallNode('f')),
                _xun_store_accessor.load_result(_xun_CallNode('f', b)),
            )
        a, c = _xun_load_constants()
        value = a + c
        return value

    # Dummy dependency
    @xun.function()
    def dummy():
        pass
    known_functions = {'f': dummy}

    code = (xun.functions.FunctionDecomposition(desc)
        .apply(xun.functions.separate_constants)
        .apply(xun.functions.sort_constants)
        .apply(xun.functions.copy_only_constants, known_functions)
        .apply(xun.functions.load_from_store, known_functions))

    generated = [*code.load_from_store, *code.body]
    reference = reference_source.body[0].body

    for a, b in zip(generated, reference):
        if not compare_ast(a, b):
            raise ValueError('\n{} != \n{}'.format(ast.dump(a), ast.dump(b)))