-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYSTEMDS-3660] GPU cache eviction operator and related rewrite
This patch introduces a new operator, _evict, to clean up the free pointer cached in the lineage cache. A shift in the allocation pattern leads to large eviction overhead and memory fragmentation. To address that, we speculatively clear a fraction of the free pointers. Currently, we place a _evict before every mini-batch processing.
- Loading branch information
1 parent
4b0133f
commit 33149f8
Showing
12 changed files
with
953 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/org/apache/sysds/lops/rewrite/RewriteAddGPUEvictLop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.sysds.lops.rewrite; | ||
|
||
import org.apache.sysds.api.DMLScript; | ||
import org.apache.sysds.common.Types; | ||
import org.apache.sysds.conf.ConfigurationManager; | ||
import org.apache.sysds.hops.Hop; | ||
import org.apache.sysds.hops.LiteralOp; | ||
import org.apache.sysds.hops.UnaryOp; | ||
import org.apache.sysds.lops.BinaryScalar; | ||
import org.apache.sysds.lops.Data; | ||
import org.apache.sysds.lops.Lop; | ||
import org.apache.sysds.lops.OperatorOrderingUtils; | ||
import org.apache.sysds.lops.RightIndex; | ||
import org.apache.sysds.lops.UnaryCP; | ||
import org.apache.sysds.parser.ForStatement; | ||
import org.apache.sysds.parser.ForStatementBlock; | ||
import org.apache.sysds.parser.StatementBlock; | ||
import org.apache.sysds.parser.VariableSet; | ||
import org.apache.sysds.runtime.lineage.LineageCacheConfig; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RewriteAddGPUEvictLop extends LopRewriteRule | ||
{ | ||
@Override | ||
public List<StatementBlock> rewriteLOPinStatementBlock(StatementBlock sb) { | ||
// TODO: Move this as a Statement block rewrite | ||
if (!ConfigurationManager.isAutoEvictionEnabled()) | ||
return List.of(sb); | ||
|
||
if (sb == null || !(sb instanceof ForStatementBlock) | ||
|| !DMLScript.USE_ACCELERATOR || LineageCacheConfig.ReuseCacheType.isNone()) | ||
return List.of(sb); | ||
|
||
// Collect the LOPs | ||
StatementBlock csb = ((ForStatement) sb.getStatement(0)).getBody().get(0); | ||
ArrayList<Lop> lops = OperatorOrderingUtils.getLopList(csb); | ||
|
||
// Check if this loop is for mini-batch processing | ||
boolean isMiniBatch = findMiniBatchSlicing(lops); | ||
|
||
// Insert statement block with _evict instruction before the loop | ||
ArrayList<StatementBlock> ret = new ArrayList<>(); | ||
if (isMiniBatch) { | ||
int evictFrac = 100; | ||
StatementBlock sb0 = new StatementBlock(); | ||
sb0.setDMLProg(sb.getDMLProg()); | ||
sb0.setParseInfo(sb); | ||
sb0.setLiveIn(new VariableSet()); | ||
sb0.setLiveOut(new VariableSet()); | ||
// Create both lops and hops (hops for recompilation) | ||
// TODO: Add another input for the backend (GPU/CPU/Spark) | ||
ArrayList<Lop> newlops = new ArrayList<>(); | ||
ArrayList<Hop> newhops = new ArrayList<>(); | ||
Lop fr = Data.createLiteralLop(Types.ValueType.INT64, Integer.toString(evictFrac)); | ||
fr.getOutputParameters().setDimensions(0, 0, 0, -1); | ||
UnaryCP evict = new UnaryCP(fr, Types.OpOp1._EVICT, fr.getDataType(), fr.getValueType(), Types.ExecType.CP); | ||
Hop in = new LiteralOp(evictFrac); | ||
Hop evictHop = new UnaryOp("tmp", Types.DataType.SCALAR, Types.ValueType.INT64, Types.OpOp1._EVICT, in); | ||
newlops.add(evict); | ||
newhops.add(evictHop); | ||
sb0.setLops(newlops); | ||
sb0.setHops(newhops); | ||
ret.add(sb0); | ||
} | ||
ret.add(sb); | ||
|
||
return ret; | ||
} | ||
|
||
@Override | ||
public List<StatementBlock> rewriteLOPinStatementBlocks(List<StatementBlock> sbs) { | ||
return sbs; | ||
} | ||
|
||
// To verify mini-batch processing, match the below pattern | ||
// beg = ((i-1) * batch_size) %% N + 1; | ||
// end = min(N, beg+batch_size-1); | ||
// X_batch = X[beg:end]; | ||
private boolean findMiniBatchSlicing(ArrayList<Lop> lops) { | ||
for (Lop l : lops) { | ||
if (l instanceof RightIndex) { | ||
ArrayList<Lop> inputs = l.getInputs(); | ||
if (inputs.get(0) instanceof Data && ((Data) inputs.get(0)).isTransientRead() | ||
&& inputs.get(0).getInputs().size() == 0 //input1 is the dataset | ||
&& inputs.get(1) instanceof BinaryScalar //input2 is beg | ||
&& ((BinaryScalar) inputs.get(1)).getOperationType() == Types.OpOp2.PLUS | ||
&& inputs.get(2) instanceof BinaryScalar //input3 is end | ||
&& ((BinaryScalar) inputs.get(2)).getOperationType() == Types.OpOp2.MIN) | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/apache/sysds/runtime/instructions/cp/EvictCPInstruction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.sysds.runtime.instructions.cp; | ||
|
||
import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; | ||
import org.apache.sysds.runtime.instructions.InstructionUtils; | ||
import org.apache.sysds.runtime.lineage.LineageGPUCacheEviction; | ||
import org.apache.sysds.runtime.matrix.operators.Operator; | ||
|
||
public class EvictCPInstruction extends UnaryCPInstruction | ||
{ | ||
private EvictCPInstruction(Operator op, CPOperand in, CPOperand out, String opcode, String istr) { | ||
super(CPType.EvictLineageCache, op, in, out, opcode, istr); | ||
} | ||
|
||
public static EvictCPInstruction parseInstruction(String str) { | ||
InstructionUtils.checkNumFields(str, 3); | ||
String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); | ||
String opcode = parts[0]; | ||
CPOperand in = new CPOperand(parts[1]); | ||
CPOperand out = new CPOperand(parts[2]); | ||
return new EvictCPInstruction(null, in, out, opcode, str); | ||
} | ||
|
||
@Override | ||
public void processInstruction(ExecutionContext ec) { | ||
// Evict fraction of cached objects | ||
ScalarObject fr = ec.getScalarInput(input1); | ||
double evictFrac = ((double) fr.getLongValue()) / 100; | ||
LineageGPUCacheEviction.removeAllEntries(evictFrac); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.