-
Notifications
You must be signed in to change notification settings - Fork 717
/
LLAMA2TestSpec.scala
67 lines (57 loc) · 2.31 KB
/
LLAMA2TestSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* 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.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.{SlowTest, FastTest}
import com.johnsnowlabs.util.Benchmark
import org.apache.spark.ml.Pipeline
import org.scalatest.flatspec.AnyFlatSpec
class LLAMA2TestSpec extends AnyFlatSpec {
"llama-7b" should "should handle temperature=0 correctly and not crash when predicting more than 1 element with doSample=True" taggedAs SlowTest in {
// Even tough the Paper states temperature in interval [0,1), using temperature=0 will result in division by 0 error.
// Also DoSample=True may result in infinities being generated and distFiltered.length==0 which results in exception if we don't return 0 instead internally.
val testData = ResourceHelper.spark
.createDataFrame(Seq(
(1, "PG&E stated it scheduled the blackouts in response to forecasts for high winds ")))
.toDF("id", "text")
.repartition(1)
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("documents")
val bart = LLAMA2Transformer
.pretrained()
.setInputCols(Array("documents"))
.setDoSample(true)
.setMaxOutputLength(50)
.setOutputCol("generation")
.setBeamSize(2)
val pipeline = new Pipeline()
.setStages(Array(documentAssembler, bart))
val pipelineModel = pipeline.fit(testData)
pipelineModel
.transform(testData)
.show(truncate = false)
pipelineModel
.transform(testData)
.show(truncate = false)
pipelineModel.stages.last
.asInstanceOf[LLAMA2Transformer]
.write
.overwrite()
.save("/tmp/llama-7b-4bit-model")
}
}