mleap icon indicating copy to clipboard operation
mleap copied to clipboard

OneHotEncoder does not serialize if meta data is missing from DataFrame

Open reflog opened this issue 9 years ago • 17 comments

Hi! I'm trying to use mleap-pyspark:

import mleap.pyspark
from mleap.pyspark.spark_support import SimpleSparkSerializer
model.serializeToBundle( "jar:file:/tmp/pysparklr.zip", dataset=test_model)

and the resulting ZIP file looks like this:

<─ /tmp/pysparklr.zip#uzip/root ─────────────────
│'n                                              
│/..                                             
│/Bucketizer_46cf8e4ebed70d3332d0.node           
│/Bucketizer_48dcb1b53992160f3822.node           
│/LinearRegression_4d6185c2e480b21990be.node     
│/MinMaxScaler_4b99b614a7c0d0aa7695.node         
│/OneHotEncoder_425fa08282a386439839.node        
│/OneHotEncoder_42a78b6ab211dd2dd127.node        
│/OneHotEncoder_43449ed2fc2300bc5663.node        
│/OneHotEncoder_43cf8e3711aba38cd281.node        
│/OneHotEncoder_43dcb205b2f6fb04f91e.node        
│/OneHotEncoder_453cb84de080d6deed33.node        
│/OneHotEncoder_45d39ec3561a9a81c512.node        
│/OneHotEncoder_4648a83a58352a13f7c4.node        
│/OneHotEncoder_46af8a56e8f7348b7d82.node        
│/OneHotEncoder_4be5875451f4ce2754cc.node        
│/OneHotEncoder_4c6da30961c990b9b207.node        
│/OneHotEncoder_4cee8923b7658cd30c59.node        
│/OneHotEncoder_4df6a6a32fb79fba831d.node        
│/OneHotEncoder_4f85bbf6c8707f7a7560.node        
│/PolynomialExpansion_4b0e955c0cb66c94097c.node  
│/StringIndexer_49fdbb5d6b5b7df454e8.node        
│/StringIndexer_4f3da329ccaf7d55738c.node        
│/VectorAssembler_4cac83e941ced21754e7.node      
│/VectorAssembler_4f95906e884daeb48887.node      

Nothing on the root level, only data inside the root folder. So when trying to upload to mleap-server - I get a: [ERROR] [09/29/2017 15:09:47.957] [MleapServing-akka.actor.default-dispatcher-2] [akka.actor.ActorSystemImpl(MleapServing)] Error during processing of request: 'bundle.json'. Completing with 500 Internal Server Error response.

Any suggestions on how to fix this?

Thanks!

P.S. I'm working with 0.8.0 SNAPSHOT

reflog avatar Sep 29 '17 15:09 reflog

@reflog There was probably an error during serialization. I just review the PySpark code, and we aren't unwrapping a try that should be unwrapped in order to escalate the error up to PySpark. I'll fix this issue so that the error escalates and update this issue with the PR.

hollinwilkins avatar Oct 01 '17 18:10 hollinwilkins

Here is the PR if you would like to track its progress. Would be great if you could build MLeap from this branch, use the snapshot, and report back the underlying error:

https://github.com/combust/mleap/pull/281

hollinwilkins avatar Oct 01 '17 18:10 hollinwilkins

@hollinwilkins - I will try to do that, thanks. However, I've also rewrote my piece of code in Scala and performed the serialization with:

  val sbc = SparkBundleContext().withDataset(result)
  for(bf <- managed(BundleFile("jar:file:/tmp/simple-spark-pipeline.zip"))) {
    pipelineModel.writeBundle.save(bf)(sbc).get
  }

and the resulting ZIP file had the same structure (again, no exception was thrown during serialization). Is there some logging I can turn on or some other way to understand what's failing?

Thanks!

reflog avatar Oct 02 '17 05:10 reflog

@reflog what happens if you try to be specific about the format type?

pipeline.writeBundle.format(SerializationFormat.Json).save(bf)(sbc).get

this is my code in Scala and it works

	def exportPipeline(pipeline: PipelineModel, path: String = "/tmp/model.zip"): Unit = {

		val sbc = SparkBundleContext()
		for(bf <- managed(BundleFile(s"jar:file:$path"))) {
			pipeline.writeBundle.format(SerializationFormat.Json).save(bf)(sbc).get
		}
	}

rgeos avatar Oct 02 '17 06:10 rgeos

@reflog Try this instead, I think there is still one more error to unwrap:

val sbc = SparkBundleContext().withDataset(result)
  (for(bf <- managed(BundleFile("jar:file:/tmp/simple-spark-pipeline.zip"))) yield {
    pipelineModel.writeBundle.save(bf)(sbc).get
  }).tried.get

hollinwilkins avatar Oct 02 '17 06:10 hollinwilkins

ok, managed to get the exception!

Exception in thread "main" java.lang.RuntimeException: unsupported attribute for field arrival_day
	at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$.sizeForField(OneHotEncoderOp.scala:34)
	at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.store(OneHotEncoderOp.scala:50)
	at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.store(OneHotEncoderOp.scala:40)
	at ml.combust.bundle.serializer.ModelSerializer$$anonfun$write$1.apply(ModelSerializer.scala:88)
	at ml.combust.bundle.serializer.ModelSerializer$$anonfun$write$1.apply(ModelSerializer.scala:84)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.ModelSerializer.write(ModelSerializer.scala:84)
	at ml.combust.bundle.serializer.NodeSerializer$$anonfun$write$1.apply(NodeSerializer.scala:86)
	at ml.combust.bundle.serializer.NodeSerializer$$anonfun$write$1.apply(NodeSerializer.scala:82)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.NodeSerializer.write(NodeSerializer.scala:82)
	at ml.combust.bundle.serializer.GraphSerializer$$anonfun$writeNode$1.apply(GraphSerializer.scala:34)
	at ml.combust.bundle.serializer.GraphSerializer$$anonfun$writeNode$1.apply(GraphSerializer.scala:30)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.GraphSerializer.writeNode(GraphSerializer.scala:30)
	at ml.combust.bundle.serializer.GraphSerializer$$anonfun$write$2.apply(GraphSerializer.scala:21)
	at ml.combust.bundle.serializer.GraphSerializer$$anonfun$write$2.apply(GraphSerializer.scala:21)
	at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:57)
	at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:66)
	at scala.collection.mutable.WrappedArray.foldLeft(WrappedArray.scala:35)
	at ml.combust.bundle.serializer.GraphSerializer.write(GraphSerializer.scala:20)
	at org.apache.spark.ml.bundle.ops.PipelineOp$$anon$1.store(PipelineOp.scala:21)
	at org.apache.spark.ml.bundle.ops.PipelineOp$$anon$1.store(PipelineOp.scala:14)
	at ml.combust.bundle.serializer.ModelSerializer$$anonfun$write$1.apply(ModelSerializer.scala:88)
	at ml.combust.bundle.serializer.ModelSerializer$$anonfun$write$1.apply(ModelSerializer.scala:84)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.ModelSerializer.write(ModelSerializer.scala:84)
	at ml.combust.bundle.serializer.NodeSerializer$$anonfun$write$1.apply(NodeSerializer.scala:86)
	at ml.combust.bundle.serializer.NodeSerializer$$anonfun$write$1.apply(NodeSerializer.scala:82)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.NodeSerializer.write(NodeSerializer.scala:82)
	at ml.combust.bundle.serializer.BundleSerializer$$anonfun$write$1.apply(BundleSerializer.scala:34)
	at ml.combust.bundle.serializer.BundleSerializer$$anonfun$write$1.apply(BundleSerializer.scala:29)
	at scala.util.Try$.apply(Try.scala:192)
	at ml.combust.bundle.serializer.BundleSerializer.write(BundleSerializer.scala:29)
	at ml.combust.bundle.BundleWriter.save(BundleWriter.scala:27)
	at SparkWordCount$$anonfun$exportPipeline$2.apply(SparkWordCount.scala:53)
	at SparkWordCount$$anonfun$exportPipeline$2.apply(SparkWordCount.scala:52)
	at resource.AbstractManagedResource$$anonfun$5.apply(AbstractManagedResource.scala:88)
	at scala.util.control.Exception$Catch$$anonfun$either$1.apply(Exception.scala:125)
	at scala.util.control.Exception$Catch$$anonfun$either$1.apply(Exception.scala:125)
	at scala.util.control.Exception$Catch.apply(Exception.scala:103)
	at scala.util.control.Exception$Catch.either(Exception.scala:125)
	at resource.AbstractManagedResource.acquireFor(AbstractManagedResource.scala:88)
	at resource.ManagedResourceOperations$class.apply(ManagedResourceOperations.scala:26)
	at resource.AbstractManagedResource.apply(AbstractManagedResource.scala:50)
	at resource.ManagedResourceOperations$class.acquireAndGet(ManagedResourceOperations.scala:25)
	at resource.AbstractManagedResource.acquireAndGet(AbstractManagedResource.scala:50)
	at resource.ManagedResourceOperations$class.foreach(ManagedResourceOperations.scala:53)
	at resource.AbstractManagedResource.foreach(AbstractManagedResource.scala:50)
	at SparkWordCount$.exportPipeline(SparkWordCount.scala:52)
	at SparkWordCount$.delayedEndpoint$SparkWordCount$1(SparkWordCount.scala:64)
	at SparkWordCount$delayedInit$body.apply(SparkWordCount.scala:11)
	at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.collection.immutable.List.foreach(List.scala:381)
	at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
	at scala.App$class.main(App.scala:76)

I've tried debugging it a little bit, but it seems the debugger stops on OneHotEncoderOp.scala:34 for every categorical feature, but throws an exception only for "arrival_day". There's nothing special about that column, it's an Int and it's already working fine in the regression...

Any other suggestions on what I could try?

reflog avatar Oct 02 '17 06:10 reflog

@reflog You've uncovered a bug with MLeap! It seems you aren't creating that column using a feature extractor like StringIndexer, but maybe simply including it to start. Spark will actually analyze the data to figure out the size of the vector in this case, but MLeap relies solely on the field meta data when serializing. The meta data doesn't exist if the feature wasn't produced by certain transformers.

A quick fix for this while we fix the underlying issue is to run your field through a StringIndexer first. This will produce the needed meta data for serialization.

hollinwilkins avatar Oct 02 '17 13:10 hollinwilkins

The thing is - arrival_day is already an Int column, so passing it through StringIndexer is not an option. (it's a category column already, identifying a week day, from 0..6). Right now, it's passed this way: arrival_day => OneHotEncoder => VectorAssembler => LinearRegression

reflog avatar Oct 02 '17 15:10 reflog

You can run it through a string indexer. Pretty much anything can go into one. What it will do is add the necessary meta data to the field so that the one hot encoder can be serialized for the output field of the string indexer.

hollinwilkins avatar Oct 02 '17 15:10 hollinwilkins

So you suggest to change the pipeline to: arrival_day => StringIndexer => OneHotEncoder => VectorAssembler => LinearRegression ?

reflog avatar Oct 02 '17 15:10 reflog

Yup, until we resolve this issue you found.

hollinwilkins avatar Oct 02 '17 15:10 hollinwilkins

@reflog Did the string indexer fix your issue for now?

hollinwilkins avatar Oct 05 '17 16:10 hollinwilkins

Yep, sorry for forgetting to confirm. Added StringIndexer, exported the bundle and confirmed the pipeline using the mlserve. Worked like a treat! 👍

reflog avatar Oct 06 '17 07:10 reflog

Closing this issue.

hollinwilkins avatar Mar 16 '18 14:03 hollinwilkins

@hollinwilkins Do you have solved the bug?I meet the same exception:"unsupported attribute for field **"

liaosimin avatar Aug 08 '18 02:08 liaosimin

Is there a similar issue with IndexToString? When the dataset that I pass in to serialzeToBundle doesn't have an output column, the serialization fails, complaining that the field does not exist.

val testIndexToString = new IndexToString()
testIndexToString.setInputCol("id")
testIndexToString.setOutputCol("label")
testIndexToString.setLabels(labels)
val sss = new ml.combust.mleap.spark.SimpleSparkSerializer()
val inputEvent = Seq(
  (0,"asdf"),
  (1,"asdf2")
).toDF("id","extracolumn")
sss.serializeToBundle(testIndexToString, "jar:file:" + outputFile.toString(), inputEvent)

Fails with java.lang.IllegalArgumentException: Field "label" does not exist. If I change the column name extracolumn to label, and run the code again, it works.

rucarden avatar May 08 '19 18:05 rucarden

I get the same error using pyspark 3.0.2 and mleap 0.18.1.

Py4JJavaError: An error occurred while calling o99817.serializeToBundle. : java.lang.RuntimeException: unsupported attribute for field loan_termidximputed at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$.sizeForField(OneHotEncoderOp.scala:31) at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.$anonfun$store$2(OneHotEncoderOp.scala:47) at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.$anonfun$store$2$adapted(OneHotEncoderOp.scala:47) at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:238) at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36) at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33) at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:198) at scala.collection.TraversableLike.map(TraversableLike.scala:238) at scala.collection.TraversableLike.map$(TraversableLike.scala:231) at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:198) at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.store(OneHotEncoderOp.scala:47) at org.apache.spark.ml.bundle.ops.feature.OneHotEncoderOp$$anon$1.store(OneHotEncoderOp.scala:37) at ml.combust.bundle.serializer.ModelSerializer.$anonfun$write$1(ModelSerializer.scala:87) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.ModelSerializer.write(ModelSerializer.scala:83) at ml.combust.bundle.serializer.NodeSerializer.$anonfun$write$1(NodeSerializer.scala:85) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.NodeSerializer.write(NodeSerializer.scala:81) at ml.combust.bundle.serializer.GraphSerializer.$anonfun$writeNode$1(GraphSerializer.scala:34) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.GraphSerializer.writeNode(GraphSerializer.scala:30) at ml.combust.bundle.serializer.GraphSerializer.$anonfun$write$2(GraphSerializer.scala:21) at scala.collection.IndexedSeqOptimized.foldLeft(IndexedSeqOptimized.scala:60) at scala.collection.IndexedSeqOptimized.foldLeft$(IndexedSeqOptimized.scala:68) at scala.collection.mutable.WrappedArray.foldLeft(WrappedArray.scala:38) at ml.combust.bundle.serializer.GraphSerializer.write(GraphSerializer.scala:21) at org.apache.spark.ml.bundle.ops.PipelineOp$$anon$1.store(PipelineOp.scala:21) at org.apache.spark.ml.bundle.ops.PipelineOp$$anon$1.store(PipelineOp.scala:14) at ml.combust.bundle.serializer.ModelSerializer.$anonfun$write$1(ModelSerializer.scala:87) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.ModelSerializer.write(ModelSerializer.scala:83) at ml.combust.bundle.serializer.NodeSerializer.$anonfun$write$1(NodeSerializer.scala:85) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.NodeSerializer.write(NodeSerializer.scala:81) at ml.combust.bundle.serializer.BundleSerializer.$anonfun$write$1(BundleSerializer.scala:34) at scala.util.Try$.apply(Try.scala:213) at ml.combust.bundle.serializer.BundleSerializer.write(BundleSerializer.scala:29) at ml.combust.bundle.BundleWriter.save(BundleWriter.scala:34) at ml.combust.mleap.spark.SimpleSparkSerializer.$anonfun$serializeToBundleWithFormat$4(SimpleSparkSerializer.scala:26) at resource.AbstractManagedResource.$anonfun$acquireFor$1(AbstractManagedResource.scala:88) at scala.util.control.Exception$Catch.$anonfun$either$1(Exception.scala:252) at scala.util.control.Exception$Catch.apply(Exception.scala:228) at scala.util.control.Exception$Catch.either(Exception.scala:252) at resource.AbstractManagedResource.acquireFor(AbstractManagedResource.scala:88) at resource.ManagedResourceOperations.apply(ManagedResourceOperations.scala:26) at resource.ManagedResourceOperations.apply$(ManagedResourceOperations.scala:26) at resource.AbstractManagedResource.apply(AbstractManagedResource.scala:50) at resource.DeferredExtractableManagedResource.$anonfun$tried$1(AbstractManagedResource.scala:33) at scala.util.Try$.apply(Try.scala:213) at resource.DeferredExtractableManagedResource.tried(AbstractManagedResource.scala:33) at ml.combust.mleap.spark.SimpleSparkSerializer.serializeToBundleWithFormat(SimpleSparkSerializer.scala:25) at ml.combust.mleap.spark.SimpleSparkSerializer.serializeToBundle(SimpleSparkSerializer.scala:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244) at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357) at py4j.Gateway.invoke(Gateway.java:282) at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) at py4j.commands.CallCommand.execute(CallCommand.java:79) at py4j.GatewayConnection.run(GatewayConnection.java:238) at java.lang.Thread.run(Thread.java:748)

But this time I have the following pipeline:

numerical_imputer --> categorical_indexer --> categorical_imputer --> target_indexer --> one_hot_encoder --> realtime_vector_assembler --> realtime_scaler --> features_vector_assembler --> best_rfor

with categorical_indexer is StringIndexer.

Any insights?

inardini avatar Oct 03 '21 16:10 inardini