title: How to load a Spark model for efficient predictions
body: <p>When I build a Spark model and call it, the predictions take tens of ms to return.
  However, when I save that same model, then load it, the predictions take much longer. Is there some sort of cache I should be using?</p>
  
  <p><code>model.cache()</code> after loading does not work, as the model is not an RDD.</p>
  
  <p>This works great:</p>
  
  <pre><code>from pyspark.mllib.recommendation import ALS
  from pyspark import SparkContext
  import time
  
  sc = SparkContext()
  
  # Some example data
  r = [(1, 1, 1.0),
      (1, 2, 2.0),
      (2, 1, 2.0)]
  ratings = sc.parallelize(r)
  model = ALS.trainImplicit(ratings, 1, seed=10)
  
  # Call model and time it
  now = time.time()
  for t in range(10):
      model.predict(2, 2)
  
  elapsed = (time.time() - now)*1000/(t+1)
  
  print "Average time for model call: {:.2f}ms".format(elapsed)
  
  model.save(sc, 'my_spark_model')
  </code></pre>
  
  <p>Output: <code>Average time for model call: 71.18ms</code></p>
  
  <p>If I run the following, the predictions take much more time:</p>
  
  <pre><code>from pyspark.mllib.recommendation import MatrixFactorizationModel
  from pyspark import SparkContext
  import time
  
  sc = SparkContext()
  
  model_path = "my_spark_model"
  model = MatrixFactorizationModel.load(sc, model_path)
  
  # Call model and time it
  now = time.time()
  for t in range(10):
      model.predict(2, 2)
  
  elapsed = (time.time() - now)*1000/(t+1)
  
  print "Average time for loaded model call: {:.2f}ms".format(elapsed)
  </code></pre>
  
  <p>The output: <code>Average time for loaded model call: 180.34ms</code></p>
  
  <p>For BIG models, I'm seeing prediction times over 10 seconds for a single call after loading a saved model.</p>