Skip to content

Commit c4e7e46

Browse files
committed
[SYSTEMDS-3842] Improve test coverage of API components: DMLScript & DMLOptions
Closes #2241.
1 parent 2e19d94 commit c4e7e46

File tree

9 files changed

+716
-41
lines changed

9 files changed

+716
-41
lines changed

src/main/java/org/apache/sysds/api/DMLOptions.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,34 +141,32 @@ public static DMLOptions parseCLArguments(String[] args)
141141
String lineageTypes[] = line.getOptionValues("lineage");
142142
if (lineageTypes != null) {
143143
for (String lineageType : lineageTypes) {
144-
if (lineageType != null){
145-
if (lineageType.equalsIgnoreCase("dedup"))
146-
dmlOptions.lineage_dedup = lineageType.equalsIgnoreCase("dedup");
147-
else if (lineageType.equalsIgnoreCase("reuse_full")
148-
|| lineageType.equalsIgnoreCase("reuse"))
149-
dmlOptions.linReuseType = ReuseCacheType.REUSE_FULL;
150-
else if (lineageType.equalsIgnoreCase("reuse_partial"))
151-
dmlOptions.linReuseType = ReuseCacheType.REUSE_PARTIAL;
152-
else if (lineageType.equalsIgnoreCase("reuse_multilevel"))
153-
dmlOptions.linReuseType = ReuseCacheType.REUSE_MULTILEVEL;
154-
else if (lineageType.equalsIgnoreCase("reuse_hybrid"))
155-
dmlOptions.linReuseType = ReuseCacheType.REUSE_HYBRID;
156-
else if (lineageType.equalsIgnoreCase("none"))
157-
dmlOptions.linReuseType = ReuseCacheType.NONE;
158-
else if (lineageType.equalsIgnoreCase("policy_lru"))
159-
dmlOptions.linCachePolicy = LineageCachePolicy.LRU;
160-
else if (lineageType.equalsIgnoreCase("policy_costnsize"))
161-
dmlOptions.linCachePolicy = LineageCachePolicy.COSTNSIZE;
162-
else if (lineageType.equalsIgnoreCase("policy_dagheight"))
163-
dmlOptions.linCachePolicy = LineageCachePolicy.DAGHEIGHT;
164-
else if (lineageType.equalsIgnoreCase("estimate"))
165-
dmlOptions.lineage_estimate = lineageType.equalsIgnoreCase("estimate");
166-
else if (lineageType.equalsIgnoreCase("debugger"))
167-
dmlOptions.lineage_debugger = lineageType.equalsIgnoreCase("debugger");
168-
else
169-
throw new org.apache.commons.cli.ParseException(
170-
"Invalid argument specified for -lineage option: " + lineageType);
171-
}
144+
if (lineageType.equalsIgnoreCase("dedup"))
145+
dmlOptions.lineage_dedup = lineageType.equalsIgnoreCase("dedup");
146+
else if (lineageType.equalsIgnoreCase("reuse_full")
147+
|| lineageType.equalsIgnoreCase("reuse"))
148+
dmlOptions.linReuseType = ReuseCacheType.REUSE_FULL;
149+
else if (lineageType.equalsIgnoreCase("reuse_partial"))
150+
dmlOptions.linReuseType = ReuseCacheType.REUSE_PARTIAL;
151+
else if (lineageType.equalsIgnoreCase("reuse_multilevel"))
152+
dmlOptions.linReuseType = ReuseCacheType.REUSE_MULTILEVEL;
153+
else if (lineageType.equalsIgnoreCase("reuse_hybrid"))
154+
dmlOptions.linReuseType = ReuseCacheType.REUSE_HYBRID;
155+
else if (lineageType.equalsIgnoreCase("none"))
156+
dmlOptions.linReuseType = ReuseCacheType.NONE;
157+
else if (lineageType.equalsIgnoreCase("policy_lru"))
158+
dmlOptions.linCachePolicy = LineageCachePolicy.LRU;
159+
else if (lineageType.equalsIgnoreCase("policy_costnsize"))
160+
dmlOptions.linCachePolicy = LineageCachePolicy.COSTNSIZE;
161+
else if (lineageType.equalsIgnoreCase("policy_dagheight"))
162+
dmlOptions.linCachePolicy = LineageCachePolicy.DAGHEIGHT;
163+
else if (lineageType.equalsIgnoreCase("estimate"))
164+
dmlOptions.lineage_estimate = true;
165+
else if (lineageType.equalsIgnoreCase("debugger"))
166+
dmlOptions.lineage_debugger = true;
167+
else
168+
throw new org.apache.commons.cli.ParseException(
169+
"Invalid argument specified for -lineage option: " + lineageType);
172170
}
173171
}
174172
}
@@ -186,13 +184,11 @@ else if (lineageType.equalsIgnoreCase("debugger"))
186184
}
187185
if (line.hasOption("exec")){
188186
String execMode = line.getOptionValue("exec");
189-
if (execMode != null){
190-
if (execMode.equalsIgnoreCase("singlenode")) dmlOptions.execMode = ExecMode.SINGLE_NODE;
191-
else if (execMode.equalsIgnoreCase("hybrid")) dmlOptions.execMode = ExecMode.HYBRID;
192-
else if (execMode.equalsIgnoreCase("spark")) dmlOptions.execMode = ExecMode.SPARK;
193-
else throw new org.apache.commons.cli.ParseException("Invalid argument specified for -exec option, must be one of [hadoop, singlenode, hybrid, HYBRID, spark]");
194-
}
195-
}
187+
if (execMode.equalsIgnoreCase("singlenode")) dmlOptions.execMode = ExecMode.SINGLE_NODE;
188+
else if (execMode.equalsIgnoreCase("hybrid")) dmlOptions.execMode = ExecMode.HYBRID;
189+
else if (execMode.equalsIgnoreCase("spark")) dmlOptions.execMode = ExecMode.SPARK;
190+
else throw new org.apache.commons.cli.ParseException("Invalid argument specified for -exec option, must be one of [hadoop, singlenode, hybrid, HYBRID, spark]");
191+
}
196192
if (line.hasOption("explain")) {
197193
dmlOptions.explainType = ExplainType.RUNTIME;
198194
String explainType = line.getOptionValue("explain");
@@ -222,7 +218,7 @@ else if (lineageType.equalsIgnoreCase("debugger"))
222218
dmlOptions.statsNGrams = line.hasOption("ngrams");
223219
if (dmlOptions.statsNGrams){
224220
String[] nGramArgs = line.getOptionValues("ngrams");
225-
if (nGramArgs.length >= 2) {
221+
if (nGramArgs != null && nGramArgs.length >= 2) {
226222
try {
227223
String[] nGramSizeSplit = nGramArgs[0].split(",");
228224
dmlOptions.statsNGramSizes = new int[nGramSizeSplit.length];
@@ -273,11 +269,17 @@ else if (lineageType.equalsIgnoreCase("debugger"))
273269

274270
if (line.hasOption("fedMonitoring")) {
275271
dmlOptions.fedMonitoring= true;
276-
dmlOptions.fedMonitoringPort = Integer.parseInt(line.getOptionValue("fedMonitoring"));
272+
String port = line.getOptionValue("fedMonitoring");
273+
if(port != null)
274+
dmlOptions.fedMonitoringPort = Integer.parseInt(port);
275+
else
276+
throw new org.apache.commons.cli.ParseException("No port [integer] specified for -fedMonitoring option");
277277
}
278278

279279
if (line.hasOption("fedMonitoringAddress")) {
280280
dmlOptions.fedMonitoringAddress = line.getOptionValue("fedMonitoringAddress");
281+
if(dmlOptions.fedMonitoringAddress == null)
282+
throw new org.apache.commons.cli.ParseException("No address [String] specified for -fedMonitoringAddress option");
281283
}
282284

283285
if (line.hasOption("f")){
@@ -326,7 +328,7 @@ else if (lineageType.equalsIgnoreCase("debugger"))
326328
OptimizerUtils.FEDERATED_COMPILATION = true;
327329
dmlOptions.federatedCompilation = true;
328330
String[] fedCompSpecs = line.getOptionValues("federatedCompilation");
329-
if ( fedCompSpecs != null && fedCompSpecs.length > 0 ){
331+
if (fedCompSpecs != null){
330332
for ( String spec : fedCompSpecs ){
331333
String[] specPair = spec.split("=");
332334
if (specPair.length != 2){
@@ -370,8 +372,8 @@ private static Options createCLIOptions() {
370372
.withDescription("monitors and reports summary execution statistics; heavy hitter <count> is 10 unless overridden; default off")
371373
.hasOptionalArg().create("stats");
372374
Option ngramsOpt = OptionBuilder//.withArgName("ngrams")
373-
.withDescription("monitors and reports the most occurring n-grams; -ngrams <comma separated n's> <topK>")
374-
.hasOptionalArgs(2).create("ngrams");
375+
.withDescription("monitors and reports the most occurring n-grams; -ngrams <comma separated n's> <topK> <boolean lineage>")
376+
.hasOptionalArgs(3).create("ngrams");
375377
Option fedStatsOpt = OptionBuilder.withArgName("count")
376378
.withDescription("monitors and reports summary execution statistics of federated workers; heavy hitter <count> is 10 unless overridden; default off")
377379
.hasOptionalArg().create("fedStats");

src/main/java/org/apache/sysds/hops/codegen/SpoofCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static void loadNativeCodeGenerator(GeneratorAPI generator) {
223223
else {
224224
local_tmp = System.getProperty("user.dir") + "/src/main".replace("/", File.separator);
225225
}
226-
226+
// TODO: Code is unreachable here
227227
if(generator == GeneratorAPI.CUDA) {
228228
// init GPUs with jCuda to avoid double initialization problems
229229
GPUContextPool.initializeGPU();

src/test/java/org/apache/sysds/test/component/misc/CLIOptionsParserTest.java

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@
2424
import org.apache.commons.cli.AlreadySelectedException;
2525
import org.apache.commons.cli.MissingOptionException;
2626
import org.apache.commons.cli.ParseException;
27+
import org.apache.sysds.hops.OptimizerUtils;
28+
import org.apache.sysds.runtime.instructions.fed.FEDInstruction;
29+
import org.apache.sysds.runtime.lineage.LineageCacheConfig;
2730
import org.junit.Assert;
2831
import org.junit.Test;
2932
import org.apache.sysds.api.DMLOptions;
3033
import org.apache.sysds.common.Types.ExecMode;
3134
import org.apache.sysds.runtime.lineage.LineageCacheConfig.ReuseCacheType;
3235
import org.apache.sysds.utils.Explain;
3336

37+
import static org.apache.sysds.api.DMLOptions.parseCLArguments;
38+
3439
@net.jcip.annotations.NotThreadSafe
3540
public class CLIOptionsParserTest {
3641

@@ -162,6 +167,7 @@ public void testLineageReuseP() throws Exception {
162167
Assert.assertEquals(ReuseCacheType.REUSE_PARTIAL, o.linReuseType);
163168
Assert.assertEquals(false, o.lineage_dedup);
164169
}
170+
165171
@Test
166172
public void testLineageReuseH() throws Exception {
167173
String cl = "systemds -f test.dml -lineage reuse_hybrid";
@@ -450,4 +456,193 @@ public void testNVArgs4() throws Exception {
450456
Map<String, String> m = o.argVals;
451457
Assert.assertEquals("'def'", m.get("$abc"));
452458
}
459+
460+
@Test
461+
public void parseCLArgumentsLineageDAGHEIGHTTest() throws ParseException {
462+
String[] args = new String[]{"-f", "test", "-lineage", "policy_dagheight"};
463+
DMLOptions opts = parseCLArguments(args);
464+
Assert.assertTrue(opts.lineage && opts.linCachePolicy == LineageCacheConfig.LineageCachePolicy.DAGHEIGHT);
465+
}
466+
467+
@Test
468+
public void parseCLIArgumentsLineageEstimateTest() throws ParseException {
469+
String[] args = new String[]{"-f", "test", "-lineage", "estimate"};
470+
DMLOptions opts = parseCLArguments(args);
471+
Assert.assertTrue(opts.lineage && opts.lineage_estimate);
472+
}
473+
474+
@Test
475+
public void parseCLArgumentsGPUTest() throws ParseException {
476+
String[] args = new String[]{"-f", "test", "-gpu",};
477+
DMLOptions opts = parseCLArguments(args);
478+
Assert.assertTrue(opts.gpu);
479+
}
480+
481+
@Test
482+
public void parseCLArgumentsInvalidExplainTest() throws ParseException {
483+
String[] args = new String[]{"-f", "test","-explain","XYZ"};
484+
try {
485+
parseCLArguments(args);
486+
} catch (ParseException e) {
487+
assert e.getMessage().equals("Invalid argument specified for -hops option, must be one of [hops, runtime, recompile_hops, recompile_runtime, codegen, codegen_recompile]");
488+
}
489+
}
490+
491+
@Test
492+
public void parseCLArgumentsExplainCodegenRecompileTest() throws ParseException {
493+
String[] args = new String[]{"-f", "test","-explain","codegen_recompile"};
494+
DMLOptions opts = parseCLArguments(args);
495+
Assert.assertEquals(opts.explainType, Explain.ExplainType.CODEGEN_RECOMPILE);
496+
}
497+
498+
@Test
499+
public void parseCLArgumentsNGramsTest1() throws ParseException {
500+
String[] args = new String[]{"-f", "test", "-ngrams",};
501+
DMLOptions opts = parseCLArguments(args);
502+
Assert.assertTrue(opts.statsNGrams);
503+
}
504+
505+
@Test
506+
public void parseCLArgumentsNGramsTest2() throws ParseException {
507+
String[] args = new String[]{"-f", "test", "-ngrams","1"};
508+
DMLOptions opts = parseCLArguments(args);
509+
Assert.assertTrue(opts.statsNGrams);
510+
}
511+
512+
@Test
513+
public void parseCLArgumentsNGramsTest3() throws ParseException {
514+
String[] args = new String[]{"-f", "test", "-ngrams","1","1","FALSE"};
515+
DMLOptions opts = parseCLArguments(args);
516+
Assert.assertTrue(opts.statsNGrams);
517+
Assert.assertEquals(opts.statsNGramSizes[0], 1);
518+
Assert.assertEquals(opts.statsTopKNGrams, 1);
519+
Assert.assertFalse(opts.statsNGramsUseLineage);
520+
}
521+
522+
@Test
523+
public void parseCLArgumentsNGramsTest4() throws ParseException {
524+
String[] args = new String[]{"-f", "test", "-ngrams","1,3","1"};
525+
DMLOptions opts = parseCLArguments(args);
526+
Assert.assertTrue(opts.statsNGrams);
527+
Assert.assertEquals(opts.statsNGramSizes[0], 1);
528+
Assert.assertEquals(opts.statsNGramSizes[1], 3);
529+
Assert.assertEquals(opts.statsTopKNGrams, 1);
530+
Assert.assertTrue(opts.statsNGramsUseLineage);
531+
}
532+
533+
@Test
534+
public void parseCLArgumentsNGramsTest5() throws ParseException {
535+
String[] args = new String[]{"-f", "test","-ngrams","1,2","b"};
536+
try {
537+
parseCLArguments(args);
538+
} catch (ParseException e) {
539+
assert e.getMessage().equals("Invalid argument specified for -ngrams option, must be a valid integer");
540+
}
541+
}
542+
543+
@Test
544+
public void parseCLArgumentsFEDStatsTest1() throws ParseException {
545+
String[] args = new String[]{"-f", "test", "-fedStats",};
546+
DMLOptions opts = parseCLArguments(args);
547+
Assert.assertTrue(opts.fedStats);
548+
}
549+
550+
@Test
551+
public void parseCLArgumentsFEDStatsTest2() throws ParseException {
552+
String[] args = new String[]{"-f", "test", "-fedStats", "21"};
553+
DMLOptions opts = parseCLArguments(args);
554+
Assert.assertTrue(opts.fedStats);
555+
Assert.assertEquals(21, opts.fedStatsCount);
556+
}
557+
558+
@Test
559+
public void parseCLArgumentsFEDStatsTest3() {
560+
String[] args = new String[]{"-f", "test", "-fedStats", "xyz"};
561+
try {
562+
parseCLArguments(args);
563+
} catch (ParseException e) {
564+
assert e.getMessage().equals("Invalid argument specified for -fedStats option, must be a valid integer");
565+
}
566+
}
567+
568+
@Test
569+
public void parseCLArgumentsFEDMonitoringTest1() {
570+
String[] args = new String[]{"-fedMonitoring"};
571+
try {
572+
parseCLArguments(args);
573+
} catch (ParseException e) {
574+
assert e.getMessage().equals("No port [integer] specified for -fedMonitoring option");
575+
}
576+
}
577+
578+
@Test
579+
public void parseCLArgumentsFEDMonitoringTest2() {
580+
String[] args = new String[]{"-fedMonitoring","21", "-fedMonitoringAddress"};
581+
try {
582+
parseCLArguments(args);
583+
} catch (ParseException e) {
584+
assert e.getMessage().equals("No address [String] specified for -fedMonitoringAddress option");
585+
}
586+
}
587+
588+
@Test
589+
public void parseCLArgumentsFEDMonitoringTest3() throws ParseException {
590+
String[] args = new String[]{"-fedMonitoring", "21"};
591+
DMLOptions opts = parseCLArguments(args);
592+
Assert.assertTrue(opts.fedMonitoring);
593+
Assert.assertEquals(21, opts.fedMonitoringPort);
594+
}
595+
596+
@Test
597+
public void parseCLArgumentsFEDMonitoringTest4() throws ParseException {
598+
String[] args = new String[]{"-fedMonitoring", "21", "-fedMonitoringAddress", "xyz"};
599+
DMLOptions opts = parseCLArguments(args);
600+
Assert.assertTrue(opts.fedMonitoring);
601+
Assert.assertEquals(21, opts.fedMonitoringPort);
602+
Assert.assertEquals("xyz", opts.fedMonitoringAddress);
603+
}
604+
605+
@Test
606+
public void parseCLArgumentsFEDCompilationTest1() throws ParseException {
607+
String[] args = new String[]{"-f", "test", "-federatedCompilation"};
608+
DMLOptions opts = parseCLArguments(args);
609+
Assert.assertTrue(opts.federatedCompilation);
610+
}
611+
612+
@Test
613+
public void parseCLArgumentsFEDCompilationTest2() throws ParseException {
614+
String[] args = new String[]{"-f", "test", "-federatedCompilation", "1=NONE"};
615+
DMLOptions opts = parseCLArguments(args);
616+
Assert.assertTrue(opts.federatedCompilation);
617+
Assert.assertEquals(OptimizerUtils.FEDERATED_SPECS.get(1), FEDInstruction.FederatedOutput.NONE);
618+
}
619+
620+
@Test
621+
public void parseCLArgumentsFEDCompilationTest3() {
622+
String[] args = new String[]{"-f","test", "-federatedCompilation","1=n=n"};
623+
try {
624+
parseCLArguments(args);
625+
throw new AssertionError("Test should have resulted in Exception");
626+
} catch (ParseException e){
627+
Assert.assertEquals("Invalid argument specified for -federatedCompilation option, must be a list of space separated K=V pairs, where K is a line number of the DML script and V is a federated output value",e.getMessage());
628+
}
629+
}
630+
631+
@Test
632+
public void parseCLArgumentsFEDNoRuntimeConversionTest() throws ParseException {
633+
String[] args = new String[]{"-f", "test", "-noFedRuntimeConversion"};
634+
DMLOptions opts = parseCLArguments(args);
635+
Assert.assertTrue(opts.noFedRuntimeConversion);
636+
}
637+
638+
@Test
639+
public void testDMLOptionToString() throws ParseException {
640+
String cl = "systemds -f test.dml -exec spark";
641+
String[] args = cl.split(" ");
642+
DMLOptions o = DMLOptions.parseCLArguments(args);
643+
String oString = o.toString();
644+
Assert.assertTrue(oString.contains("script='null'"));
645+
Assert.assertTrue(oString.contains("filePath='test.dml'"));
646+
Assert.assertTrue(oString.contains("execMode=SPARK"));
647+
}
453648
}

0 commit comments

Comments
 (0)