Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYSTEMDS-3018] Support function calls in FedAll and Heuristic planners #1666

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.sysds.hops.fedplanner;

import java.util.HashMap;
import java.util.Map;

import org.apache.sysds.common.Types;
Expand All @@ -28,6 +29,7 @@
import org.apache.sysds.hops.AggUnaryOp;
import org.apache.sysds.hops.BinaryOp;
import org.apache.sysds.hops.DataOp;
import org.apache.sysds.hops.FunctionOp;
import org.apache.sysds.hops.Hop;
import org.apache.sysds.hops.TernaryOp;
import org.apache.sysds.hops.fedplanner.FTypes.FType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,11 @@ private ArrayList<StatementBlock> rewriteDefaultStatementBlock(DMLProgram prog,
if(sbHop instanceof FunctionOp) {
String funcName = ((FunctionOp) sbHop).getFunctionName();
String funcNamespace = ((FunctionOp) sbHop).getFunctionNamespace();
Map<String, Hop> funcParamMap = FederatedPlannerUtils.getParamMap((FunctionOp) sbHop);
if ( paramMap != null && funcParamMap != null)
funcParamMap.putAll(paramMap);
paramMap = funcParamMap;
FunctionStatementBlock sbFuncBlock = prog.getFunctionDictionary(funcNamespace)
.getFunction(funcName);
rewriteStatementBlock(prog, sbFuncBlock, paramMap);

FunctionStatementBlock sbFuncBlock = prog.getFunctionDictionary(funcNamespace).getFunction(funcName);
FunctionStatement funcStatement = (FunctionStatement) sbFuncBlock.getStatement(0);

paramMap = FederatedPlannerUtils.createFunctionFedVarTable(paramMap, (FunctionOp) sbHop);
rewriteStatementBlock(prog, sbFuncBlock, paramMap);
FederatedPlannerUtils.mapFunctionOutputs((FunctionOp) sbHop, funcStatement, transientWrites);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

package org.apache.sysds.hops.fedplanner;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.sysds.common.Types.ExecType;
import org.apache.sysds.common.Types.OpOpData;
import org.apache.sysds.hops.DataOp;
import org.apache.sysds.hops.FunctionOp;
import org.apache.sysds.hops.Hop;
import org.apache.sysds.hops.fedplanner.FTypes.FType;
import org.apache.sysds.hops.ipa.FunctionCallGraph;
Expand Down Expand Up @@ -89,14 +89,14 @@ private void rRewriteStatementBlock(StatementBlock sb, Map<String, FType> fedVar
else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement)wsb.getStatement(0);
rRewriteHop(wsb.getPredicateHops(), new HashMap<>(), Collections.emptyMap());
rRewriteHop(wsb.getPredicateHops(), new HashMap<>(), new HashMap<>(), sb.getDMLProg());
for (StatementBlock csb : wstmt.getBody())
rRewriteStatementBlock(csb, fedVars);
}
else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement)isb.getStatement(0);
rRewriteHop(isb.getPredicateHops(), new HashMap<>(), Collections.emptyMap());
rRewriteHop(isb.getPredicateHops(), new HashMap<>(), new HashMap<>(), sb.getDMLProg());
for (StatementBlock csb : istmt.getIfBody())
rRewriteStatementBlock(csb, fedVars);
for (StatementBlock csb : istmt.getElseBody())
Expand All @@ -105,9 +105,9 @@ else if (sb instanceof IfStatementBlock) {
else if (sb instanceof ForStatementBlock) { //incl parfor
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fstmt = (ForStatement)fsb.getStatement(0);
rRewriteHop(fsb.getFromHops(), new HashMap<>(), Collections.emptyMap());
rRewriteHop(fsb.getToHops(), new HashMap<>(), Collections.emptyMap());
rRewriteHop(fsb.getIncrementHops(), new HashMap<>(), Collections.emptyMap());
rRewriteHop(fsb.getFromHops(), new HashMap<>(), new HashMap<>(), sb.getDMLProg());
rRewriteHop(fsb.getToHops(), new HashMap<>(), new HashMap<>(), sb.getDMLProg());
rRewriteHop(fsb.getIncrementHops(), new HashMap<>(), new HashMap<>(), sb.getDMLProg());
for (StatementBlock csb : fstmt.getBody())
rRewriteStatementBlock(csb, fedVars);
}
Expand All @@ -117,7 +117,7 @@ else if (sb instanceof ForStatementBlock) { //incl parfor
Map<Long, FType> fedHops = new HashMap<>();
if( sb.getHops() != null )
for( Hop c : sb.getHops() )
rRewriteHop(c, fedHops, fedVars);
rRewriteHop(c, fedHops, fedVars, sb.getDMLProg());

//TODO handle function calls

Expand All @@ -129,19 +129,31 @@ else if (sb instanceof ForStatementBlock) { //incl parfor
}
}

private void rRewriteHop(Hop hop, Map<Long, FType> memo, Map<String, FType> fedVars) {
private void rRewriteHop(Hop hop, Map<Long, FType> memo, Map<String, FType> fedVars, DMLProgram program) {
if( memo.containsKey(hop.getHopID()) )
return; //already processed

//process children first
for( Hop c : hop.getInput() )
rRewriteHop(c, memo, fedVars);
rRewriteHop(c, memo, fedVars, program);

//handle specific operators (except transient writes)
if( HopRewriteUtils.isData(hop, OpOpData.FEDERATED) )
if(hop instanceof FunctionOp) {
String funcName = ((FunctionOp) hop).getFunctionName();
String funcNamespace = ((FunctionOp) hop).getFunctionNamespace();
FunctionStatementBlock sbFuncBlock = program.getFunctionDictionary(funcNamespace).getFunction(funcName);
FunctionStatement funcStatement = (FunctionStatement) sbFuncBlock.getStatement(0);

Map<String, FType> funcFedVars = createFunctionFedVarTable((FunctionOp) hop, memo);
rRewriteStatementBlock(sbFuncBlock, funcFedVars);
mapFunctionOutputs((FunctionOp) hop, funcStatement, funcFedVars, fedVars);
}
else if( HopRewriteUtils.isData(hop, OpOpData.FEDERATED) )
memo.put(hop.getHopID(), deriveFType((DataOp)hop));
else if( HopRewriteUtils.isData(hop, OpOpData.TRANSIENTREAD) )
memo.put(hop.getHopID(), fedVars.get(hop.getName()));
else if( HopRewriteUtils.isData(hop, OpOpData.TRANSIENTWRITE) )
fedVars.put(hop.getName(), memo.get(hop.getHopID()));
else if( allowsFederated(hop, memo) ) {
hop.setForcedExecType(ExecType.FED);
memo.put(hop.getHopID(), getFederatedOut(hop, memo));
Expand All @@ -151,4 +163,23 @@ else if( allowsFederated(hop, memo) ) {
else // memoization as processed, but not federated
memo.put(hop.getHopID(), null);
}

// TODO: Reduce code duplication. See `createFunctionFedVarTable` and `mapFunctionOutputs` in
// `FederationPlannerUtils.java`
static private Map<String, FType> createFunctionFedVarTable(FunctionOp hop, Map<Long, FType> memo) {
Map<String, Hop> funcParamMap = FederatedPlannerUtils.getParamMap(hop);
Map<String, FType> funcFedVars = new HashMap<>();
funcParamMap.forEach((key, value) -> {
funcFedVars.put(key, memo.get(value.getHopID()));
});
return funcFedVars;
}

private void mapFunctionOutputs(FunctionOp sbHop, FunctionStatement funcStatement,
Map<String, FType> funcFedVars, Map<String, FType> callFedVars) {
for(int i = 0; i < sbHop.getOutputVariableNames().length; ++i) {
FType outputFType = funcFedVars.get(funcStatement.getOutputParams().get(i).getName());
callFedVars.put(sbHop.getOutputVariableNames()[i], outputFType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,19 @@ public static Map<String,Hop> getParamMap(FunctionOp funcOp){
* @param funcStatement The <code>FunctionStatement</code> of the called function
* @param transientWrites map of transient writes
*/
public static void mapFunctionOutputs(FunctionOp sbHop, FunctionStatement funcStatement, Map<String,Hop> transientWrites) {
for (int i = 0; i < sbHop.getOutputVariableNames().length; ++i) {
public static void mapFunctionOutputs(FunctionOp sbHop, FunctionStatement funcStatement,
Map<String, Hop> transientWrites) {
for(int i = 0; i < sbHop.getOutputVariableNames().length; ++i) {
Hop outputWrite = transientWrites.get(funcStatement.getOutputParams().get(i).getName());
transientWrites.put(sbHop.getOutputVariableNames()[i], outputWrite);
}
}

public static Map<String, Hop> createFunctionFedVarTable(Map<String, Hop> paramMap, FunctionOp sbHop) {
Map<String, Hop> funcParamMap = FederatedPlannerUtils.getParamMap(sbHop);
if(paramMap != null)
funcParamMap.putAll(paramMap);
paramMap = funcParamMap;
return paramMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void runL2SVMCostBasedTest(){
}

@Test
@Ignore
public void runL2SVMFunctionFOUTTest(){
String[] expectedHeavyHitters = new String[]{ "fed_fedinit", "fed_ba+*", "fed_tak+*", "fed_+*",
"fed_max", "fed_1-*", "fed_tsmm", "fed_>"};
Expand All @@ -90,7 +89,6 @@ public void runL2SVMFunctionFOUTTest(){
}

@Test
@Ignore
public void runL2SVMFunctionHeuristicTest(){
String[] expectedHeavyHitters = new String[]{ "fed_fedinit", "fed_ba+*"};
setTestConf("SystemDS-config-heuristic.xml");
Expand Down