
com.johnsnowlabs.nlp.annotators.seq2seq.OLMoTransformer.scala Maven / Gradle / Ivy
/*
* Copyright 2017-2024 John Snow Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.johnsnowlabs.nlp.annotators.seq2seq
import com.johnsnowlabs.ml.ai.util.Generation.GenerationConfig
import com.johnsnowlabs.ml.ai.OLMo
import com.johnsnowlabs.ml.onnx.OnnxWrapper.DecoderWrappers
import com.johnsnowlabs.ml.onnx.{OnnxWrapper, ReadOnnxModel, WriteOnnxModel}
import com.johnsnowlabs.ml.util.LoadExternalModel.{
loadJsonStringAsset,
loadSentencePieceAsset,
loadTextAsset,
modelSanityCheck,
notSupportedEngineError
}
import com.johnsnowlabs.ml.util.ONNX
import com.johnsnowlabs.nlp.AnnotatorType.DOCUMENT
import com.johnsnowlabs.nlp._
import com.johnsnowlabs.ml.tensorflow.sentencepiece.{
ReadSentencePieceModel,
SentencePieceWrapper,
WriteSentencePieceModel
}
import com.johnsnowlabs.nlp.serialization.MapFeature
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.ml.param._
import org.apache.spark.ml.util.Identifiable
import org.apache.spark.sql.SparkSession
import com.johnsnowlabs.nlp.serialization.{MapFeature, StructFeature}
import org.json4s._
import org.json4s.jackson.JsonMethods._
/** OLMo: Open Language Models
*
* OLMo is a series of Open Language Models designed to enable the science of language models.
* The OLMo models are trained on the Dolma dataset.
*
* Pretrained models can be loaded with `pretrained` of the companion object:
* {{{
* val OLMo = OLMoTransformer.pretrained()
* .setInputCols("document")
* .setOutputCol("generation")
* }}}
* The default model is `"olmo_1b_int4"`, if no name is provided. For available pretrained models
* please see the [[https://sparknlp.org/models?q=OLMo Models Hub]].
*
* For extended examples of usage, see
* [[https://github.com/JohnSnowLabs/spark-nlp/blob/master/src/test/scala/com/johnsnowlabs/nlp/annotators/seq2seq/OLMoTestSpec.scala OLMoTestSpec]].
*
* '''References:'''
* - [[https://allenai.org/olmo OLMo Project Page.]]
* - [[https://github.com/allenai/OLMo OLMo GitHub Repository.]]
* - [[https://arxiv.org/pdf/2402.00838.pdf OLMo: Accelerating the Science of Language Models]]
*
* '''Paper Abstract:'''
*
* ''Language models (LMs) have become ubiquitous in both NLP research and in commercial product
* offerings. As their commercial importance has surged, the most powerful models have become
* closed off, gated behind proprietary interfaces, with important details of their training
* data, architectures, and development undisclosed. Given the importance of these details in
* scientifically studying these models, including their biases and potential risks, we believe
* it is essential for the research community to have access to powerful, truly open LMs. To this
* end, this technical report details the first release of OLMo, a state-of-the-art, truly Open
* Language Model and its framework to build and study the science of language modeling. Unlike
* most prior efforts that have only released model weights and inference code, we release OLMo
* and the whole framework, including training data and training and evaluation code. We hope
* this release will empower and strengthen the open research community and inspire a new wave of
* innovation.''
*
* '''Note:'''
*
* This is a very computationally expensive module especially on larger sequence. The use of an
* accelerator such as GPU is recommended.
*
* ==Example==
* {{{
* import spark.implicits._
* import com.johnsnowlabs.nlp.base.DocumentAssembler
* import com.johnsnowlabs.nlp.annotators.seq2seq.OLMoTransformer
* import org.apache.spark.ml.Pipeline
*
* val documentAssembler = new DocumentAssembler()
* .setInputCol("text")
* .setOutputCol("documents")
*
* val OLMo = OLMoTransformer.pretrained("olmo_1b_int4")
* .setInputCols(Array("documents"))
* .setMinOutputLength(10)
* .setMaxOutputLength(50)
* .setDoSample(false)
* .setTopK(50)
* .setNoRepeatNgramSize(3)
* .setOutputCol("generation")
*
* val pipeline = new Pipeline().setStages(Array(documentAssembler, OLMo))
*
* val data = Seq(
* "My name is Leonardo."
* ).toDF("text")
* val result = pipeline.fit(data).transform(data)
*
* results.select("generation.result").show(truncate = false)
* +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |result |
* +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |[ My name is Leonardo . I am a student of the University of California, Berkeley. I am interested in the field of Artificial Intelligence and its applications in the real world. I have a strong |
* | passion for learning and am always looking for ways to improve my knowledge and skills] |
* +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* }}}
*
* @param uid
* required uid for storing annotator to disk
* @groupname anno Annotator types
* @groupdesc anno
* Required input and expected output annotator types
* @groupname Ungrouped Members
* @groupname param Parameters
* @groupname setParam Parameter setters
* @groupname getParam Parameter getters
* @groupname Ungrouped Members
* @groupprio param 1
* @groupprio anno 2
* @groupprio Ungrouped 3
* @groupprio setParam 4
* @groupprio getParam 5
* @groupdesc param
* A list of (hyper-)parameter keys this annotator can take. Users can set and get the
* parameter values through setters and getters, respectively.
*/
class OLMoTransformer(override val uid: String)
extends AnnotatorModel[OLMoTransformer]
with HasBatchedAnnotate[OLMoTransformer]
with ParamsAndFeaturesWritable
with WriteOnnxModel
with HasGeneratorProperties
with HasEngine {
def this() = this(Identifiable.randomUID("OLMoTRANSFORMER"))
/** Input annotator type : DOCUMENT
*
* @group param
*/
override val inputAnnotatorTypes: Array[AnnotatorType] = Array(DOCUMENT)
/** Output annotator type : DOCUMENT
*
* @group param
*/
override val outputAnnotatorType: String = DOCUMENT
/** @group setParam */
def setRandomSeed(value: Int): OLMoTransformer.this.type = {
if (randomSeed.isEmpty) {
this.randomSeed = Some(value)
}
this
}
/** A list of token ids which are ignored in the decoder's output (Default: `Array()`)
*
* @group param
*/
var ignoreTokenIds = new IntArrayParam(
this,
"ignoreTokenIds",
"A list of token ids which are ignored in the decoder's output")
/** @group setParam */
def setIgnoreTokenIds(tokenIds: Array[Int]): OLMoTransformer.this.type = {
set(ignoreTokenIds, tokenIds)
}
/** @group getParam */
def getIgnoreTokenIds: Array[Int] = $(ignoreTokenIds)
/** Vocabulary used to encode the words to ids with bpeTokenizer.encode
*
* @group param
*/
val vocabulary: MapFeature[String, Int] = new MapFeature(this, "vocabulary").setProtected()
/** @group setParam */
def setVocabulary(value: Map[String, Int]): this.type = set(vocabulary, value)
/** Holding merges.txt coming from RoBERTa model
*
* @group param
*/
val merges: MapFeature[(String, String), Int] = new MapFeature(this, "merges").setProtected()
/** @group setParam */
def setMerges(value: Map[(String, String), Int]): this.type = set(merges, value)
private var _model: Option[Broadcast[OLMo]] = None
val generationConfig: StructFeature[GenerationConfig] =
new StructFeature(this, "generationConfig").setProtected()
def setGenerationConfig(value: GenerationConfig): this.type =
set(generationConfig, value)
def getGenerationConfig: GenerationConfig = $$(generationConfig)
/** @group setParam */
def setModelIfNotSet(spark: SparkSession, onnxWrappers: DecoderWrappers): this.type = {
if (_model.isEmpty) {
_model = Some(
spark.sparkContext.broadcast(
new OLMo(
onnxWrappers,
$$(merges),
$$(vocabulary),
generationConfig = getGenerationConfig)))
}
this
}
/** @group getParam */
def getModelIfNotSet: OLMo = _model.get.value
setDefault(
minOutputLength -> 0,
maxOutputLength -> 20,
doSample -> false,
temperature -> 0.6,
topK -> 50,
topP -> 0.9,
repetitionPenalty -> 1.0,
noRepeatNgramSize -> 3,
ignoreTokenIds -> Array(),
batchSize -> 1,
beamSize -> 1,
maxInputLength -> 4096)
/** takes a document and annotations and produces new annotations of this annotator's annotation
* type
*
* @param batchedAnnotations
* Annotations that correspond to inputAnnotationCols generated by previous annotators if any
* @return
* any number of annotations processed for every input annotation. Not necessary one to one
* relationship
*/
override def batchAnnotate(batchedAnnotations: Seq[Array[Annotation]]): Seq[Seq[Annotation]] = {
val allAnnotations = batchedAnnotations
.filter(_.nonEmpty)
.zipWithIndex
.flatMap { case (annotations, i) =>
annotations.filter(_.result.nonEmpty).map(x => (x, i))
}
val processedAnnotations = if (allAnnotations.nonEmpty) {
this.getModelIfNotSet.predict(
sentences = allAnnotations.map(_._1),
batchSize = $(batchSize),
minOutputLength = $(minOutputLength),
maxOutputLength = $(maxOutputLength),
doSample = $(doSample),
temperature = $(temperature),
topK = $(topK),
topP = $(topP),
repetitionPenalty = $(repetitionPenalty),
noRepeatNgramSize = $(noRepeatNgramSize),
randomSeed = this.randomSeed,
ignoreTokenIds = $(ignoreTokenIds),
beamSize = $(beamSize),
maxInputLength = $(maxInputLength))
} else {
Seq()
}
Seq(processedAnnotations)
}
override def onWrite(path: String, spark: SparkSession): Unit = {
super.onWrite(path, spark)
getEngine match {
case ONNX.name =>
val wrappers = getModelIfNotSet.onnxWrappers
writeOnnxModels(
path,
spark,
Seq((wrappers.decoder, "decoder_model.onnx")),
OLMoTransformer.suffix)
}
}
}
trait ReadablePretrainedOLMoTransformerModel
extends ParamsAndFeaturesReadable[OLMoTransformer]
with HasPretrained[OLMoTransformer] {
override val defaultModelName: Some[String] = Some("olmo_1b_int4")
/** Java compliant-overrides */
override def pretrained(): OLMoTransformer = super.pretrained()
override def pretrained(name: String): OLMoTransformer = super.pretrained(name)
override def pretrained(name: String, lang: String): OLMoTransformer =
super.pretrained(name, lang)
override def pretrained(name: String, lang: String, remoteLoc: String): OLMoTransformer =
super.pretrained(name, lang, remoteLoc)
}
trait ReadOLMoTransformerDLModel extends ReadOnnxModel {
this: ParamsAndFeaturesReadable[OLMoTransformer] =>
override val onnxFile: String = "decoder_model.onnx"
val suffix: String = "_olmo"
def readModel(instance: OLMoTransformer, path: String, spark: SparkSession): Unit = {
instance.getEngine match {
case ONNX.name =>
val wrapper =
readOnnxModel(
path,
spark,
suffix,
zipped = true,
useBundle = false,
modelName = Some("decoder_model.onnx"),
dataFilePostfix = Some(".onnx_data"))
val onnxWrappers =
DecoderWrappers(decoder = wrapper)
instance.setModelIfNotSet(spark, onnxWrappers)
case _ =>
throw new Exception(notSupportedEngineError)
}
}
addReader(readModel)
def loadSavedModel(modelPath: String, spark: SparkSession): OLMoTransformer = {
implicit val formats: DefaultFormats.type = DefaultFormats // for json4
val (localModelPath, detectedEngine) =
modelSanityCheck(modelPath, isDecoder = true)
val modelConfig: JValue =
parse(loadJsonStringAsset(localModelPath, "config.json"))
val beginSuppressTokens: Array[Int] =
(modelConfig \ "begin_suppress_tokens").extract[Array[Int]]
val suppressTokenIds: Array[Int] =
(modelConfig \ "suppress_tokens").extract[Array[Int]]
val forcedDecoderIds: Array[(Int, Int)] =
(modelConfig \ "forced_decoder_ids").extract[Array[Array[Int]]].map {
case idxWithTokenId: Array[Int] if idxWithTokenId.length == 2 =>
(idxWithTokenId(0), idxWithTokenId(1))
case _ =>
throw new Exception(
"Could not extract forced_decoder_ids. Should be a list of tuples with 2 entries.")
}
def arrayOrNone[T](array: Array[T]): Option[Array[T]] =
if (array.nonEmpty) Some(array) else None
var bosTokenId = -1
try {
bosTokenId = (modelConfig \ "bos_token_id").extract[Int]
} catch {
case _: Exception =>
println("Could not extract bos_token_id from config.json, assigning default value -1")
}
val eosTokenId = (modelConfig \ "eos_token_id").extract[Int]
val padTokenId = (modelConfig \ "eos_token_id").extract[Int]
val vocabSize = (modelConfig \ "vocab_size").extract[Int]
val vocabs = loadTextAsset(localModelPath, "vocab.txt").zipWithIndex.toMap
val bytePairs = loadTextAsset(localModelPath, "merges.txt")
.map(_.split(" "))
.filter(w => w.length == 2)
.map { case Array(c1, c2) => (c1, c2) }
.zipWithIndex
.toMap
val annotatorModel = new OLMoTransformer()
.setGenerationConfig(
GenerationConfig(
bosTokenId,
padTokenId,
eosTokenId,
vocabSize,
arrayOrNone(beginSuppressTokens),
arrayOrNone(suppressTokenIds),
arrayOrNone(forcedDecoderIds)))
.setVocabulary(vocabs)
.setMerges(bytePairs)
annotatorModel.set(annotatorModel.engine, detectedEngine)
detectedEngine match {
case ONNX.name =>
val onnxWrapperDecoder =
OnnxWrapper.read(
spark,
localModelPath,
zipped = false,
useBundle = true,
modelName = "decoder_model",
dataFileSuffix = Some(".onnx_data"),
onnxFileSuffix = Some(suffix))
val onnxWrappers = DecoderWrappers(onnxWrapperDecoder)
annotatorModel
.setModelIfNotSet(spark, onnxWrappers)
case _ =>
throw new Exception(notSupportedEngineError)
}
annotatorModel
}
}
object OLMoTransformer
extends ReadablePretrainedOLMoTransformerModel
with ReadOLMoTransformerDLModel
© 2015 - 2025 Weber Informatics LLC | Privacy Policy