Ejemplo n.º 1
0
def _test_quantitatively(sdfg):
    graph = sdfg.nodes()[0]
    A = np.random.rand(N.get()).astype(np.float64)
    B = np.random.rand(N.get()).astype(np.float64)
    C1 = np.random.rand(N.get()).astype(np.float64)
    C2 = np.random.rand(N.get()).astype(np.float64)
    D1 = np.random.rand(N.get()).astype(np.float64)
    D2 = np.random.rand(N.get()).astype(np.float64)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C1, D=D1, N=N)
    del csdfg

    subgraph = SubgraphView(graph, [node for node in graph.nodes()])

    me = MultiExpansion(subgraph)
    assert me.can_be_applied(sdfg, subgraph) == True
    me.apply(sdfg)

    sf = SubgraphFusion(subgraph)
    assert sf.can_be_applied(sdfg, subgraph) == True
    sf.apply(sdfg)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C2, D=D2, N=N)

    assert np.allclose(C1, C2)
    assert np.allclose(D1, D2)
Ejemplo n.º 2
0
def test_quantitatively(sdfg):
    graph = sdfg.nodes()[0]
    A = np.random.rand(N.get()).astype(np.float64)
    B = np.random.rand(N.get()).astype(np.float64)
    C1 = np.random.rand(N.get()).astype(np.float64)
    C2 = np.random.rand(N.get()).astype(np.float64)
    D1 = np.random.rand(N.get()).astype(np.float64)
    D2 = np.random.rand(N.get()).astype(np.float64)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C1, D=D1, N=N)

    subgraph = SubgraphView(graph, [node for node in graph.nodes()])
    expansion = MultiExpansion()
    fusion = SubgraphFusion()
    assert expansion.match(sdfg, subgraph) == True
    expansion.apply(sdfg, subgraph)
    assert fusion.match(sdfg, subgraph) == True
    fusion.apply(sdfg, subgraph)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C2, D=D2, N=N)

    assert np.allclose(C1, C2)
    assert np.allclose(D1, D2)
Ejemplo n.º 3
0
def expand_maps(sdfg: dace.SDFG,
                graph: dace.SDFGState,
                subgraph: Union[SubgraphView, List[SubgraphView]] = None,
                **kwargs):

    subgraph = graph if not subgraph else subgraph
    if not isinstance(subgraph, list):
        subgraph = [subgraph]

    trafo_expansion = MultiExpansion(subgraph[0])
    for (property, val) in kwargs.items():
        setattr(trafo_expansion, property, val)

    for sg in subgraph:
        map_entries = helpers.get_outermost_scope_maps(sdfg, graph, sg)
        trafo_expansion.expand(sdfg, graph, map_entries)
Ejemplo n.º 4
0
    def can_be_applied(self, sdfg: SDFG, subgraph: SubgraphView) -> bool:
        graph = subgraph.graph
        if self.allow_expansion == True:
            subgraph_fusion = SubgraphFusion()
            subgraph_fusion.setup_match(subgraph)
            if subgraph_fusion.can_be_applied(sdfg, subgraph):
                # try w/o copy first
                return True

            expansion = MultiExpansion()
            expansion.setup_match(subgraph)
            expansion.permutation_only = not self.expansion_split
            if expansion.can_be_applied(sdfg, subgraph):
                # deepcopy
                graph_indices = [
                    i for (i, n) in enumerate(graph.nodes()) if n in subgraph
                ]
                sdfg_copy = copy.deepcopy(sdfg)
                graph_copy = sdfg_copy.nodes()[sdfg.nodes().index(graph)]
                subgraph_copy = SubgraphView(
                    graph_copy, [graph_copy.nodes()[i] for i in graph_indices])
                expansion.sdfg_id = sdfg_copy.sdfg_id

                ##sdfg_copy.apply_transformations(MultiExpansion, states=[graph])
                #expansion = MultiExpansion()
                #expansion.setup_match(subgraph_copy)
                expansion.apply(sdfg_copy)

                subgraph_fusion = SubgraphFusion()
                subgraph_fusion.setup_match(subgraph_copy)
                if subgraph_fusion.can_be_applied(sdfg_copy, subgraph_copy):
                    return True

                stencil_tiling = StencilTiling()
                stencil_tiling.setup_match(subgraph_copy)
                if self.allow_tiling and stencil_tiling.can_be_applied(
                        sdfg_copy, subgraph_copy):
                    return True

        else:
            subgraph_fusion = SubgraphFusion()
            subgraph_fusion.setup_match(subgraph)
            if subgraph_fusion.can_be_applied(sdfg, subgraph):
                return True

        if self.allow_tiling == True:
            stencil_tiling = StencilTiling()
            stencil_tiling.setup_match(subgraph)
            if stencil_tiling.can_be_applied(sdfg, subgraph):
                return True

        return False
Ejemplo n.º 5
0
def test_p1():

    N.set(20)
    M.set(30)
    O.set(50)
    P.set(40)
    Q.set(42)
    R.set(25)

    sdfg = program.to_sdfg()
    sdfg.apply_strict_transformations()
    state = sdfg.nodes()[0]

    A = np.random.rand(N.get()).astype(np.float64)
    B = np.random.rand(M.get()).astype(np.float64)
    C = np.random.rand(O.get()).astype(np.float64)
    D = np.random.rand(M.get()).astype(np.float64)
    E = np.random.rand(N.get()).astype(np.float64)
    F = np.random.rand(P.get()).astype(np.float64)
    G = np.random.rand(M.get()).astype(np.float64)
    H = np.random.rand(P.get()).astype(np.float64)
    I = np.random.rand(N.get()).astype(np.float64)
    J = np.random.rand(R.get()).astype(np.float64)
    X = np.random.rand(N.get()).astype(np.float64)
    Y = np.random.rand(M.get()).astype(np.float64)
    Z = np.random.rand(P.get()).astype(np.float64)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C, D=D, E=E, F=F, G=G, H=H, I=I, J=J, X=X, Y=Y, Z=Z,\
          N=N, M=M, O=O, P=P, R=R,Q=Q)
    del csdfg

    subgraph = SubgraphView(state, [node for node in state.nodes()])
    expansion = MultiExpansion(subgraph)
    fusion = SubgraphFusion(subgraph)

    assert MultiExpansion.can_be_applied(sdfg, subgraph)
    expansion.apply(sdfg)

    assert SubgraphFusion.can_be_applied(sdfg, subgraph)
    fusion.apply(sdfg)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C, D=D, E=E, F=F, G=G, H=H, I=I, J=J, X=X, Y=Y, Z=Z,\
          N=N, M=M, O=O, P=P, R=R,Q=Q)
    print("PASS")
Ejemplo n.º 6
0
def test_warp_softmax(vector_length=1):
    # Get SDFG
    sdfg = softmax_fwd.to_sdfg(strict=True)

    # Apply transformations
    sdfg.apply_transformations_repeated(ReduceExpansion)
    MultiExpansion.apply_to(sdfg, sdfg.node(0).nodes())
    SubgraphFusion.apply_to(sdfg, sdfg.node(0).nodes())
    sdfg.expand_library_nodes()
    sdfg.apply_strict_transformations()
    sdfg.apply_transformations_repeated([TrivialMapElimination, MapFusion])
    sdfg.apply_transformations(GPUTransformSDFG)
    sdfg.apply_transformations(WarpTiling)
    sdfg.apply_transformations_repeated([HoistState, InlineSDFG, StateFusion],
                                        strict=True)
    sdfg.apply_transformations_repeated([TrivialMapElimination, MapFusion])
    if vector_length != 1:
        sdfg.apply_transformations_repeated(
            Vectorization,
            dict(vector_len=vector_length,
                 preamble=False,
                 postamble=False,
                 strided_map=False))
    sdfg.specialize(dict(dn1=2, dn2=16, dn3=128, dr=128))

    # Check validity
    sdfg.validate()
    assert sdfg.number_of_nodes() == 1
    state = sdfg.node(0)
    assert len([
        c for c in state.scope_children()[None]
        if isinstance(c, dace.nodes.MapEntry)
    ]) == 1

    # Check correctness
    inp = np.random.rand(2, 16, 128, 128).astype(np.float32)
    out = np.random.rand(2, 16, 128, 128).astype(np.float32)
    reg_out = softmax(inp)

    sdfg(inp=inp, out=out)

    assert np.allclose(out, reg_out, rtol=1e-4, atol=1e-6)
Ejemplo n.º 7
0
    def apply(self, sdfg):
        subgraph = self.subgraph_view(sdfg)
        graph = subgraph.graph
        scope_dict = graph.scope_dict()
        map_entries = helpers.get_outermost_scope_maps(sdfg, graph, subgraph,
                                                       scope_dict)
        first_entry = next(iter(map_entries))

        if self.allow_expansion:
            expansion = MultiExpansion()
            expansion.setup_match(subgraph, self.sdfg_id, self.state_id)
            expansion.permutation_only = not self.expansion_split
            if expansion.can_be_applied(sdfg, subgraph):
                expansion.apply(sdfg)

        sf = SubgraphFusion()
        sf.setup_match(subgraph, self.sdfg_id, self.state_id)
        if sf.can_be_applied(sdfg, self.subgraph_view(sdfg)):
            # set SubgraphFusion properties
            sf.debug = self.debug
            sf.transient_allocation = self.transient_allocation
            sf.schedule_innermaps = self.schedule_innermaps
            sf.apply(sdfg)
            self._global_map_entry = sf._global_map_entry
            return

        elif self.allow_tiling == True:
            st = StencilTiling()
            st.setup_match(subgraph, self.sdfg_id, self.state_id)
            if st.can_be_applied(sdfg, self.subgraph_view(sdfg)):
                # set StencilTiling properties
                st.debug = self.debug
                st.unroll_loops = self.stencil_unroll_loops
                st.strides = self.stencil_strides
                st.apply(sdfg)
                # StencilTiling: update nodes
                new_entries = st._outer_entries
                subgraph = helpers.subgraph_from_maps(sdfg, graph, new_entries)
                sf = SubgraphFusion()
                sf.setup_match(subgraph, self.sdfg_id, self.state_id)
                # set SubgraphFusion properties
                sf.debug = self.debug
                sf.transient_allocation = self.transient_allocation
                sf.schedule_innermaps = self.schedule_innermaps

                sf.apply(sdfg)
                self._global_map_entry = sf._global_map_entry
                return

        warnings.warn("CompositeFusion::Apply did not perform as expected")
Ejemplo n.º 8
0
def test_p1():

    N.set(20)
    M.set(30)
    O.set(50)
    P.set(40)
    Q.set(42)
    R.set(25)

    sdfg = subgraph_fusion_parallel.to_sdfg()
    sdfg.simplify()
    state = sdfg.nodes()[0]

    A = np.random.rand(N.get()).astype(np.float64)
    B = np.random.rand(M.get()).astype(np.float64)
    C = np.random.rand(O.get()).astype(np.float64)
    D = np.random.rand(M.get()).astype(np.float64)
    E = np.random.rand(N.get()).astype(np.float64)
    F = np.random.rand(P.get()).astype(np.float64)
    G = np.random.rand(M.get()).astype(np.float64)
    H = np.random.rand(P.get()).astype(np.float64)
    I = np.random.rand(N.get()).astype(np.float64)
    J = np.random.rand(R.get()).astype(np.float64)
    X = np.random.rand(N.get()).astype(np.float64)
    Y = np.random.rand(M.get()).astype(np.float64)
    Z = np.random.rand(P.get()).astype(np.float64)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C, D=D, E=E, F=F, G=G, H=H, I=I, J=J, X=X, Y=Y, Z=Z,\
          N=N, M=M, O=O, P=P, R=R,Q=Q)
    del csdfg

    subgraph = SubgraphView(state, [node for node in state.nodes()])
    expansion = MultiExpansion()
    expansion.setup_match(subgraph)
    fusion = SubgraphFusion()
    fusion.setup_match(subgraph)

    me = MultiExpansion()
    me.setup_match(subgraph)
    assert me.can_be_applied(sdfg, subgraph)
    me.apply(sdfg)

    sf = SubgraphFusion()
    sf.setup_match(subgraph)
    assert sf.can_be_applied(sdfg, subgraph)
    sf.apply(sdfg)

    csdfg = sdfg.compile()
    csdfg(A=A, B=B, C=C, D=D, E=E, F=F, G=G, H=H, I=I, J=J, X=X, Y=Y, Z=Z,\
          N=N, M=M, O=O, P=P, R=R,Q=Q)
    print("PASS")