options.go 1.47 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
// Package rope provides options for RoPE
2
3
4
5
6
7
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
8
9
	Type    int
	Factors ml.Tensor
Michael Yang's avatar
Michael Yang committed
10
11

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

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

26
// WithTypeNeoX sets RoPE type to NeoX
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
41

Michael Yang's avatar
Michael Yang committed
42
43
44
45
46
47
48
// 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
49
50
func WithExtrapolationFactor(extrapolationFactor float32) func(*Options) {
	return func(opts *Options) {
Michael Yang's avatar
Michael Yang committed
51
		opts.YaRN.ExtrapolationFactor = extrapolationFactor
Michael Yang's avatar
Michael Yang committed
52
53
54
55
56
	}
}

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

Daniel Hiltgen's avatar
Daniel Hiltgen committed
61
func WithMRoPE(sections []int) func(*Options) {
Michael Yang's avatar
Michael Yang committed
62
63
64
	return func(opts *Options) {
		opts.Type |= 1 << 3
		opts.MRoPE.Sections = sections
Michael Yang's avatar
Michael Yang committed
65
66
	}
}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
67
68
69
70
71
72
73

func WithInterleaveMRoPE(sections []int) func(*Options) {
	return func(opts *Options) {
		opts.Type |= 1<<3 | 1<<5
		opts.MRoPE.Sections = sections
	}
}