rope.go 1.28 KB
Newer Older
1
2
3
4
5
6
package rope

import "github.com/ollama/ollama/ml"

// Options contains optional parameters for RoPE function
type Options struct {
Michael Yang's avatar
Michael Yang committed
7
8
	Type    int
	Factors ml.Tensor
Michael Yang's avatar
Michael Yang committed
9
10

	// YaRN options
Michael Yang's avatar
Michael Yang committed
11
12
13
14
15
16
17
	YaRN struct {
		OriginalContextLength int
		ExtrapolationFactor,
		AttentionFactor,
		BetaFast,
		BetaSlow float32
	}
18

Michael Yang's avatar
Michael Yang committed
19
20
21
	// MRoPE options
	MRoPE struct {
		Sections []int
22
23
24
	}
}

25
// WithTypeNeoX sets RoPE type to NeoX
26
27
28
29
30
31
32
33
34
35
36
37
38
39
func WithTypeNeoX() func(*Options) {
	return func(opts *Options) {
		opts.Type = 2
	}
}

// WithFactors sets custom rope factors
func WithFactors(factors ml.Tensor) func(*Options) {
	return func(opts *Options) {
		if factors != nil {
			opts.Factors = factors
		}
	}
}
Michael Yang's avatar
Michael Yang committed
40

Michael Yang's avatar
Michael Yang committed
41
42
43
44
45
46
47
// WithOriginalContextLength sets a custom context length
func WithOriginalContextLength(n int) func(*Options) {
	return func(opts *Options) {
		opts.YaRN.OriginalContextLength = n
	}
}

Michael Yang's avatar
Michael Yang committed
48
49
func WithExtrapolationFactor(extrapolationFactor float32) func(*Options) {
	return func(opts *Options) {
Michael Yang's avatar
Michael Yang committed
50
		opts.YaRN.ExtrapolationFactor = extrapolationFactor
Michael Yang's avatar
Michael Yang committed
51
52
53
54
55
	}
}

func WithAttentionFactor(attentionFactor float32) func(*Options) {
	return func(opts *Options) {
Michael Yang's avatar
Michael Yang committed
56
57
58
59
60
61
62
63
		opts.YaRN.AttentionFactor = attentionFactor
	}
}

func WithMRoPESections(sections []int) func(*Options) {
	return func(opts *Options) {
		opts.Type |= 1 << 3
		opts.MRoPE.Sections = sections
Michael Yang's avatar
Michael Yang committed
64
65
	}
}