Skip to content
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
34 changes: 34 additions & 0 deletions wayang-plugins/wayang-ml/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>wayang-ml</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1715926807014</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
157 changes: 157 additions & 0 deletions wayang-plugins/wayang-ml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-plugins</artifactId>
<version>0.7.1</version>
</parent>

<artifactId>wayang-ml</artifactId>
<version>0.7.1</version>

<properties>
<java-module-name>org.apache.wayang.extensions.ml</java-module-name>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-api-sql</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime</artifactId>
<version>1.21.1</version>
</dependency>
<!--dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime_gpu</artifactId>
<version>1.18.0</version>
</dependency-->
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-core</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-basic</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-java</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-spark</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-flink</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-giraph</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-generic-jdbc</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-benchmark</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.wayang</groupId>
<artifactId>wayang-api-python</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.12</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.12</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.16.3</version>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-core</artifactId>
<version>${calcite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-linq4j</artifactId>
<version>${calcite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-file</artifactId>
<version>${calcite.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* 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.wayang.ml;

import org.apache.wayang.core.api.WayangContext;
import org.apache.wayang.core.api.exception.WayangException;
import org.apache.logging.log4j.Level;
import org.apache.wayang.core.api.Configuration;
import org.apache.wayang.core.api.Job;
import org.apache.wayang.core.plan.wayangplan.WayangPlan;
import org.apache.wayang.core.plan.executionplan.ExecutionPlan;
import org.apache.wayang.core.optimizer.DefaultOptimizationContext;
import org.apache.wayang.core.optimizer.OptimizationContext;
import org.apache.wayang.core.util.ReflectionUtils;
import org.apache.wayang.ml.costs.PairwiseCost;
import org.apache.wayang.ml.encoding.OneHotMappings;
import org.apache.wayang.ml.encoding.OrtTensorEncoder;
import org.apache.wayang.ml.encoding.TreeEncoder;
import org.apache.wayang.ml.encoding.TreeNode;
import org.apache.wayang.ml.util.EnumerationStrategy;
import org.apache.wayang.ml.util.Logging;
import org.apache.wayang.core.util.Tuple;
import org.apache.logging.log4j.Level;

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.time.Instant;
import java.time.Duration;

import java.util.ArrayList;
import java.util.Optional;
import java.util.Collection;

/**
* This is the entry point for users to work with Wayang ML.
*/
public class MLContext extends WayangContext {

private OrtMLModel model;

private EnumerationStrategy enumerationStrategy = EnumerationStrategy.NONE;

public MLContext() {
super();
}

public MLContext(Configuration configuration) {
super(configuration);
}

/**
* Execute a plan.
*
* @param wayangPlan the plan to execute
* @param udfJars JARs that declare the code for the UDFs
* @see ReflectionUtils#getDeclaringJar(Class)
*/
@Override
public void execute(WayangPlan wayangPlan, String... udfJars) {
this.setLogLevel(Level.ERROR);
Job wayangJob = this.createJob("", wayangPlan, udfJars);
OneHotMappings.setOptimizationContext(wayangJob.getOptimizationContext());

Configuration config = this.getConfiguration();
Configuration jobConfig = wayangJob.getConfiguration();

wayangJob.execute();

if (config.getBooleanProperty("wayang.ml.experience.enabled")) {
String original;

Optional<String> originalOption = config.getOptionalStringProperty("wayang.ml.experience.original");
if (originalOption.isPresent()) {
original = originalOption.get();
} else {
original = TreeEncoder.encode(wayangPlan).toString();
}

String withChoices;

Optional<String> choicesOption = config.getOptionalStringProperty("wayang.ml.experience.with-platforms");
if (choicesOption.isPresent()) {
withChoices = choicesOption.get();
} else {
withChoices = jobConfig.getStringProperty("wayang.ml.experience.with-platforms");
}

long execTime = jobConfig.getLongProperty("wayang.ml.experience.exec-time");

this.logExperience(original, withChoices, execTime);
}
}

public void executeVAE(WayangPlan wayangPlan, String ...udfJars) {
this.setLogLevel(Level.ERROR);
try {
Job job = this.createJob("", wayangPlan, udfJars);
Configuration jobConfig = job.getConfiguration();
//job.prepareWayangPlan();
job.estimateKeyFigures();
OneHotMappings.setOptimizationContext(job.getOptimizationContext());
OneHotMappings.encodeIds = true;

// Log Encoding time
Instant start = Instant.now();
TreeNode wayangNode = TreeEncoder.encode(wayangPlan);

Instant end = Instant.now();
long execTime = Duration.between(start, end).toMillis();
Logging.writeToFile(
String.format("Encoding: %d", execTime),
this.getConfiguration().getStringProperty("wayang.ml.optimizations.file")
);
OrtMLModel model = OrtMLModel.getInstance(job.getConfiguration());
// Log inference time
start = Instant.now();
Tuple<WayangPlan, TreeNode> resultTuple = model.runVAE(wayangPlan, wayangNode);
end = Instant.now();
execTime = Duration.between(start, end).toMillis();

WayangPlan platformPlan = resultTuple.field0;

this.getConfiguration().setProperty(
"wayang.ml.experience.original",
wayangNode.toStringEncoding()
);

this.getConfiguration().setProperty(
"wayang.ml.experience.with-platforms",
resultTuple.field1.toString()
);

this.execute(platformPlan, udfJars);
} catch (Exception e) {
e.printStackTrace();
throw new WayangException("Executing WayangPlan with VAE model failed");
}
}

public ExecutionPlan buildWithVAE(WayangPlan wayangPlan, String ...udfJars) {
try {
Job job = this.createJob("", wayangPlan, udfJars);
job.estimateKeyFigures();
OneHotMappings.setOptimizationContext(job.getOptimizationContext());
OneHotMappings.encodeIds = true;

TreeNode wayangNode = TreeEncoder.encode(wayangPlan);
OrtMLModel model = OrtMLModel.getInstance(job.getConfiguration());
Tuple<WayangPlan, TreeNode> resultTuple = model.runVAE(wayangPlan, wayangNode);
WayangPlan platformPlan = resultTuple.field0;

return this.buildInitialExecutionPlan("", platformPlan, udfJars);
} catch (Exception e) {
e.printStackTrace();
throw new WayangException("Executing WayangPlan with VAE model failed");
}
}

public void setModel(OrtMLModel model) {
this.model = model;
}

private void logExperience(String original, String withChoices, long execTime) {
if (!this.getConfiguration().getBooleanProperty("wayang.ml.experience.enabled")) {
return;
}

String content = String.format("%s:%s:%d", original, withChoices, execTime);
Logging.writeToFile(
content,
this.getConfiguration().getStringProperty("wayang.ml.experience.file")
);
}
}
Loading
Loading