|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Salesforce.com, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * * Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.salesforce.op.local |
| 32 | + |
| 33 | +import com.ibm.aardpfark.spark.ml.SparkSupport |
| 34 | +import com.opendatagroup.hadrian.jvmcompiler.PFAEngine |
| 35 | +import com.salesforce.op.OpWorkflowModel |
| 36 | +import com.salesforce.op.stages.sparkwrappers.generic.SparkWrapperParams |
| 37 | +import com.salesforce.op.stages.{OPStage, OpTransformer} |
| 38 | +import org.apache.spark.ml.SparkMLSharedParamConstants._ |
| 39 | +import org.apache.spark.ml.Transformer |
| 40 | +import org.apache.spark.ml.linalg.Vector |
| 41 | +import org.apache.spark.ml.param.ParamMap |
| 42 | +import org.json4s._ |
| 43 | +import org.json4s.native.JsonMethods._ |
| 44 | +import org.json4s.native.Serialization |
| 45 | + |
| 46 | +import scala.collection.mutable |
| 47 | + |
| 48 | +/** |
| 49 | + * Enrichment for [[OpWorkflowModel]] to allow local scoring functionality |
| 50 | + */ |
| 51 | +trait OpWorkflowModelLocal { |
| 52 | + |
| 53 | + /** |
| 54 | + * Enrichment for [[OpWorkflowModel]] to allow local scoring functionality |
| 55 | + * |
| 56 | + * @param model [[OpWorkflowModel]] |
| 57 | + */ |
| 58 | + implicit class RichOpWorkflowModel(model: OpWorkflowModel) { |
| 59 | + |
| 60 | + private implicit val formats = DefaultFormats |
| 61 | + |
| 62 | + /** |
| 63 | + * Internal PFA model representation |
| 64 | + * |
| 65 | + * @param inputs mode inputs mappings |
| 66 | + * @param output output mapping |
| 67 | + * @param engine PFA engine |
| 68 | + */ |
| 69 | + private case class PFAModel |
| 70 | + ( |
| 71 | + inputs: Map[String, String], |
| 72 | + output: (String, String), |
| 73 | + engine: PFAEngine[AnyRef, AnyRef] |
| 74 | + ) |
| 75 | + |
| 76 | + /** |
| 77 | + * Internal OP model representation |
| 78 | + * |
| 79 | + * @param output output name |
| 80 | + * @param model model instance |
| 81 | + */ |
| 82 | + private case class OPModel(output: String, model: OPStage with OpTransformer) |
| 83 | + |
| 84 | + /** |
| 85 | + * Prepares a score function for local scoring |
| 86 | + * |
| 87 | + * @return score function for local scoring |
| 88 | + */ |
| 89 | + def scoreFunction: ScoreFunction = { |
| 90 | + // Prepare the stages for scoring |
| 91 | + val stagesWithIndex = model.stages.zipWithIndex |
| 92 | + // Collect all OP stages |
| 93 | + val opStages = stagesWithIndex.collect { case (s: OpTransformer, i) => OPModel(s.getOutputFeatureName, s) -> i } |
| 94 | + // Collect all Spark wrapped stages |
| 95 | + val sparkStages = stagesWithIndex.filterNot(_._1.isInstanceOf[OpTransformer]).collect { |
| 96 | + case (s: OPStage with SparkWrapperParams[_], i) if s.getSparkMlStage().isDefined => |
| 97 | + (s, s.getSparkMlStage().get.asInstanceOf[Transformer].copy(ParamMap.empty), i) |
| 98 | + } |
| 99 | + // Convert Spark wrapped stages into PFA models |
| 100 | + val pfaStages = sparkStages.map { case (opStage, sparkStage, i) => toPFAModel(opStage, sparkStage) -> i } |
| 101 | + // Combine all stages and apply the original order |
| 102 | + val allStages = (opStages ++ pfaStages).sortBy(_._2).map(_._1) |
| 103 | + val resultFeatures = model.getResultFeatures().map(_.name).toSet |
| 104 | + |
| 105 | + // Score Function |
| 106 | + input: Map[String, Any] => { |
| 107 | + val inputMap = mutable.Map.empty ++= input |
| 108 | + val transformedRow = allStages.foldLeft(inputMap) { |
| 109 | + // For OP Models we simply call transform |
| 110 | + case (row, OPModel(output, stage)) => |
| 111 | + row += output -> stage.transformKeyValue(row.apply) |
| 112 | + |
| 113 | + // For PFA Models we execute PFA engine action with json in/out |
| 114 | + case (row, PFAModel(inputs, (out, outCol), engine)) => |
| 115 | + val inJson = rowToJson(row, inputs) |
| 116 | + val engineIn = engine.jsonInput(inJson) |
| 117 | + val engineOut = engine.action(engineIn) |
| 118 | + val resMap = parse(engineOut.toString).extract[Map[String, Any]] |
| 119 | + row += out -> resMap(outCol) |
| 120 | + } |
| 121 | + transformedRow.filterKeys(resultFeatures.contains).toMap |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Convert Spark wrapped staged into PFA Models |
| 127 | + */ |
| 128 | + private def toPFAModel(opStage: OPStage with SparkWrapperParams[_], sparkStage: Transformer): PFAModel = { |
| 129 | + // Update input/output params for Spark stages to default ones |
| 130 | + val inParam = sparkStage.getParam(inputCol.name) |
| 131 | + val outParam = sparkStage.getParam(outputCol.name) |
| 132 | + val inputs = opStage.getInputFeatures().map(_.name).map { |
| 133 | + case n if sparkStage.get(inParam).contains(n) => n -> inputCol.name |
| 134 | + case n if sparkStage.get(outParam).contains(n) => n -> outputCol.name |
| 135 | + case n => n -> n |
| 136 | + }.toMap |
| 137 | + val output = opStage.getOutputFeatureName |
| 138 | + sparkStage.set(inParam, inputCol.name).set(outParam, outputCol.name) |
| 139 | + val pfaJson = SparkSupport.toPFA(sparkStage, pretty = true) |
| 140 | + val pfaEngine = PFAEngine.fromJson(pfaJson).head |
| 141 | + PFAModel(inputs, (output, outputCol.name), pfaEngine) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Convert row of Spark values into a json convertible Map |
| 146 | + * See [[FeatureTypeSparkConverter.toSpark]] for all possible values - we invert them here |
| 147 | + */ |
| 148 | + private def rowToJson(row: mutable.Map[String, Any], inputs: Map[String, String]): String = { |
| 149 | + val in = inputs.map { case (k, v) => (v, row.get(k)) }.mapValues { |
| 150 | + case Some(v: Vector) => v.toArray |
| 151 | + case Some(v: mutable.WrappedArray[_]) => v.toArray(v.elemTag) |
| 152 | + case Some(v: Map[_, _]) => v.mapValues { |
| 153 | + case v: mutable.WrappedArray[_] => v.toArray(v.elemTag) |
| 154 | + case x => x |
| 155 | + } |
| 156 | + case None | Some(null) => null |
| 157 | + case Some(v) => v |
| 158 | + } |
| 159 | + Serialization.write(in) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments