I would like to use PySpark and the library sentence-transformers to speed up the embedding of a corpus of documents.
From what I understand I can't just call :
embedd_text = udf(lambda x: model.encode(x).tolist(), ArrayType(ArrayType(FloatType())))
Because the model can't be pickled and be passed to the workers.
That's why I put the code that way:
class text_embedder:
@property
def model(self):
return SentenceTransformer('dangvantuan/sentence-camembert-large')
def embedd_text(self, x):
return self.model.encode(x).tolist()
def return_embedd_text_column(self, df):
embedd_text_udf = udf(self.embedd_text, returnType=ArrayType(ArrayType(FloatType())))
return df.withColumn("text_embedded", embedd_text_udf(data.text))
That way, the model should be created within the UDF call and distributed to the workers.
The problem is now I'm facing TypeError: cannot pickle '_thread.RLock' object and I don't know where the problem comes from.
My question is: is it possible to do what I try to do, if yes, is it a good idea to do it like that performance-wise.
Thanks in advance for your help