|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.wayang.ml; |
| 20 | + |
| 21 | +import org.apache.wayang.core.api.WayangContext; |
| 22 | +import org.apache.wayang.core.api.exception.WayangException; |
| 23 | +import org.apache.logging.log4j.Level; |
| 24 | +import org.apache.wayang.core.api.Configuration; |
| 25 | +import org.apache.wayang.core.api.Job; |
| 26 | +import org.apache.wayang.core.plan.wayangplan.WayangPlan; |
| 27 | +import org.apache.wayang.core.plan.executionplan.ExecutionPlan; |
| 28 | +import org.apache.wayang.core.optimizer.DefaultOptimizationContext; |
| 29 | +import org.apache.wayang.core.optimizer.OptimizationContext; |
| 30 | +import org.apache.wayang.core.util.ReflectionUtils; |
| 31 | +import org.apache.wayang.ml.costs.PairwiseCost; |
| 32 | +import org.apache.wayang.ml.encoding.OneHotMappings; |
| 33 | +import org.apache.wayang.ml.encoding.OrtTensorEncoder; |
| 34 | +import org.apache.wayang.ml.encoding.TreeEncoder; |
| 35 | +import org.apache.wayang.ml.encoding.TreeNode; |
| 36 | +import org.apache.wayang.ml.util.EnumerationStrategy; |
| 37 | +import org.apache.wayang.ml.util.Logging; |
| 38 | +import org.apache.wayang.core.util.Tuple; |
| 39 | +import org.apache.logging.log4j.Level; |
| 40 | + |
| 41 | +import java.io.IOException; |
| 42 | +import java.io.BufferedWriter; |
| 43 | +import java.io.FileWriter; |
| 44 | +import java.time.Instant; |
| 45 | +import java.time.Duration; |
| 46 | + |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.Optional; |
| 49 | +import java.util.Collection; |
| 50 | + |
| 51 | +/** |
| 52 | + * This is the entry point for users to work with Wayang ML. |
| 53 | + */ |
| 54 | +public class MLContext extends WayangContext { |
| 55 | + |
| 56 | + private OrtMLModel model; |
| 57 | + |
| 58 | + private EnumerationStrategy enumerationStrategy = EnumerationStrategy.NONE; |
| 59 | + |
| 60 | + public MLContext() { |
| 61 | + super(); |
| 62 | + } |
| 63 | + |
| 64 | + public MLContext(Configuration configuration) { |
| 65 | + super(configuration); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Execute a plan. |
| 70 | + * |
| 71 | + * @param wayangPlan the plan to execute |
| 72 | + * @param udfJars JARs that declare the code for the UDFs |
| 73 | + * @see ReflectionUtils#getDeclaringJar(Class) |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public void execute(WayangPlan wayangPlan, String... udfJars) { |
| 77 | + this.setLogLevel(Level.ERROR); |
| 78 | + Job wayangJob = this.createJob("", wayangPlan, udfJars); |
| 79 | + OneHotMappings.setOptimizationContext(wayangJob.getOptimizationContext()); |
| 80 | + |
| 81 | + Configuration config = this.getConfiguration(); |
| 82 | + Configuration jobConfig = wayangJob.getConfiguration(); |
| 83 | + |
| 84 | + wayangJob.execute(); |
| 85 | + |
| 86 | + if (config.getBooleanProperty("wayang.ml.experience.enabled")) { |
| 87 | + String original; |
| 88 | + |
| 89 | + Optional<String> originalOption = config.getOptionalStringProperty("wayang.ml.experience.original"); |
| 90 | + if (originalOption.isPresent()) { |
| 91 | + original = originalOption.get(); |
| 92 | + } else { |
| 93 | + original = TreeEncoder.encode(wayangPlan).toString(); |
| 94 | + } |
| 95 | + |
| 96 | + String withChoices; |
| 97 | + |
| 98 | + Optional<String> choicesOption = config.getOptionalStringProperty("wayang.ml.experience.with-platforms"); |
| 99 | + if (choicesOption.isPresent()) { |
| 100 | + withChoices = choicesOption.get(); |
| 101 | + } else { |
| 102 | + withChoices = jobConfig.getStringProperty("wayang.ml.experience.with-platforms"); |
| 103 | + } |
| 104 | + |
| 105 | + long execTime = jobConfig.getLongProperty("wayang.ml.experience.exec-time"); |
| 106 | + |
| 107 | + this.logExperience(original, withChoices, execTime); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void executeVAE(WayangPlan wayangPlan, String ...udfJars) { |
| 112 | + this.setLogLevel(Level.ERROR); |
| 113 | + try { |
| 114 | + Job job = this.createJob("", wayangPlan, udfJars); |
| 115 | + Configuration jobConfig = job.getConfiguration(); |
| 116 | + //job.prepareWayangPlan(); |
| 117 | + job.estimateKeyFigures(); |
| 118 | + OneHotMappings.setOptimizationContext(job.getOptimizationContext()); |
| 119 | + OneHotMappings.encodeIds = true; |
| 120 | + |
| 121 | + // Log Encoding time |
| 122 | + Instant start = Instant.now(); |
| 123 | + TreeNode wayangNode = TreeEncoder.encode(wayangPlan); |
| 124 | + |
| 125 | + Instant end = Instant.now(); |
| 126 | + long execTime = Duration.between(start, end).toMillis(); |
| 127 | + Logging.writeToFile( |
| 128 | + String.format("Encoding: %d", execTime), |
| 129 | + this.getConfiguration().getStringProperty("wayang.ml.optimizations.file") |
| 130 | + ); |
| 131 | + OrtMLModel model = OrtMLModel.getInstance(job.getConfiguration()); |
| 132 | + // Log inference time |
| 133 | + start = Instant.now(); |
| 134 | + Tuple<WayangPlan, TreeNode> resultTuple = model.runVAE(wayangPlan, wayangNode); |
| 135 | + end = Instant.now(); |
| 136 | + execTime = Duration.between(start, end).toMillis(); |
| 137 | + |
| 138 | + WayangPlan platformPlan = resultTuple.field0; |
| 139 | + |
| 140 | + this.getConfiguration().setProperty( |
| 141 | + "wayang.ml.experience.original", |
| 142 | + wayangNode.toStringEncoding() |
| 143 | + ); |
| 144 | + |
| 145 | + this.getConfiguration().setProperty( |
| 146 | + "wayang.ml.experience.with-platforms", |
| 147 | + resultTuple.field1.toString() |
| 148 | + ); |
| 149 | + |
| 150 | + this.execute(platformPlan, udfJars); |
| 151 | + } catch (Exception e) { |
| 152 | + e.printStackTrace(); |
| 153 | + throw new WayangException("Executing WayangPlan with VAE model failed"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public ExecutionPlan buildWithVAE(WayangPlan wayangPlan, String ...udfJars) { |
| 158 | + try { |
| 159 | + Job job = this.createJob("", wayangPlan, udfJars); |
| 160 | + job.estimateKeyFigures(); |
| 161 | + OneHotMappings.setOptimizationContext(job.getOptimizationContext()); |
| 162 | + OneHotMappings.encodeIds = true; |
| 163 | + |
| 164 | + TreeNode wayangNode = TreeEncoder.encode(wayangPlan); |
| 165 | + OrtMLModel model = OrtMLModel.getInstance(job.getConfiguration()); |
| 166 | + Tuple<WayangPlan, TreeNode> resultTuple = model.runVAE(wayangPlan, wayangNode); |
| 167 | + WayangPlan platformPlan = resultTuple.field0; |
| 168 | + |
| 169 | + return this.buildInitialExecutionPlan("", platformPlan, udfJars); |
| 170 | + } catch (Exception e) { |
| 171 | + e.printStackTrace(); |
| 172 | + throw new WayangException("Executing WayangPlan with VAE model failed"); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public void setModel(OrtMLModel model) { |
| 177 | + this.model = model; |
| 178 | + } |
| 179 | + |
| 180 | + private void logExperience(String original, String withChoices, long execTime) { |
| 181 | + if (!this.getConfiguration().getBooleanProperty("wayang.ml.experience.enabled")) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + String content = String.format("%s:%s:%d", original, withChoices, execTime); |
| 186 | + Logging.writeToFile( |
| 187 | + content, |
| 188 | + this.getConfiguration().getStringProperty("wayang.ml.experience.file") |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments