Unverified Commit fa7776fd authored by Michael Yang's avatar Michael Yang Committed by GitHub
Browse files

gpt-oss (#11672)



* bf16

* tests

* gpt-oss

* enable gptoss for engine

* rough estimate

* convert to mxfp4

* handle safetensors U8

* clamp glu/linear

* update tokenizer

* MXFP4 support

This implements the Open Compute Microscaling (MX) FP4 format
as a tensor type with backend implementations focusing
on mulmat and mulmatid on CPU, CUDA, and Metal.

* Unit tests for MXFP4 support

This exercises various operations and shapes on both CPU and GPU (if detected
on the system)

* cuda graph

* unit test adjustments

* cuda: optimize memory access

Read 4 bytes at a time (8 elements) when performing mul_mat_vec_mxfp4

* mac: fix crash on old macos versions

cblas_sgemm is only supported on v13.3 and up, however bf16 is
only supported on v14+ so we were falling back to ggml-blas and
crashing on bf16 tensors.  Checking for the function being null
seems to be the simplest way to condittionally avoid registering the
backend.

* server: Minimum context length for gptoss

This model requires a minimum context length of 8192 to function
effectively. Users can set higher values through all normal mechanisms
but lower values will be silently reset.

* ggml: Multiply by numParallel for gptoss sliding window

When computing the graph size estimate, the context size is already
multiplied by numParallel so estimates reflect that. However, since
sliding window models use a smaller, fixed context size, they need
to manually take numParallel into account.

* gpt-oss integration

includes harmony parser and thinking levels, etc.

* fix sync

* fix tests

* fix lint

---------
Co-authored-by: default avatarDaniel Hiltgen <daniel@ollama.com>
Co-authored-by: default avatarJesse Gross <jesse@ollama.com>
Co-authored-by: default avatarDevon Rifkin <drifkin@drifkin.net>
parent 0d38b665
...@@ -15,3 +15,26 @@ func (m *Linear) Forward(ctx ml.Context, t ml.Tensor) ml.Tensor { ...@@ -15,3 +15,26 @@ func (m *Linear) Forward(ctx ml.Context, t ml.Tensor) ml.Tensor {
return t return t
} }
type LinearBatch struct {
Weight ml.Tensor `gguf:"weight"`
Bias ml.Tensor `gguf:"bias"`
}
func (m *LinearBatch) Forward(ctx ml.Context, t, indices ml.Tensor) ml.Tensor {
t = m.Weight.MulmatID(ctx, t, indices)
if m.Bias != nil {
var bias ml.Tensor
if len(indices.Shape()) > 1 {
// FIXME: Rows does not support 2D indices for a 2D input tensor so reshape indices to 1D.
bias = m.Bias.Rows(ctx, indices.Contiguous(ctx, indices.Dim(0)*indices.Dim(1))).
Duplicate(ctx).
Reshape(ctx, m.Bias.Dim(0), indices.Dim(0), indices.Dim(1))
} else {
bias = m.Bias.Rows(ctx, indices)
}
t = t.Add(ctx, bias)
}
return t
}
...@@ -4,9 +4,15 @@ import "github.com/ollama/ollama/ml" ...@@ -4,9 +4,15 @@ import "github.com/ollama/ollama/ml"
// Options contains optional parameters for RoPE function // Options contains optional parameters for RoPE function
type Options struct { type Options struct {
OriginalContextLength int
Type int Type int
Factors ml.Tensor Factors ml.Tensor
OriginalContextLength int
// YaRN options
ExtrapolationFactor,
AttentionFactor,
BetaFast,
BetaSlow float32
} }
// WithOriginalContextLength sets a custom context length // WithOriginalContextLength sets a custom context length
...@@ -31,3 +37,15 @@ func WithFactors(factors ml.Tensor) func(*Options) { ...@@ -31,3 +37,15 @@ func WithFactors(factors ml.Tensor) func(*Options) {
} }
} }
} }
func WithExtrapolationFactor(extrapolationFactor float32) func(*Options) {
return func(opts *Options) {
opts.ExtrapolationFactor = extrapolationFactor
}
}
func WithAttentionFactor(attentionFactor float32) func(*Options) {
return func(opts *Options) {
opts.AttentionFactor = attentionFactor
}
}
...@@ -22,7 +22,7 @@ var _ TextProcessor = (*BytePairEncoding)(nil) ...@@ -22,7 +22,7 @@ var _ TextProcessor = (*BytePairEncoding)(nil)
func NewBytePairEncoding(pre string, vocab *Vocabulary) BytePairEncoding { func NewBytePairEncoding(pre string, vocab *Vocabulary) BytePairEncoding {
return BytePairEncoding{ return BytePairEncoding{
pre: regexp2.MustCompile(pre, regexp2.Unicode|regexp2.RE2), pre: regexp2.MustCompile(pre, regexp2.None),
vocab: vocab, vocab: vocab,
} }
} }
......
This diff is collapsed.
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
_ "github.com/ollama/ollama/model/models/gemma2" _ "github.com/ollama/ollama/model/models/gemma2"
_ "github.com/ollama/ollama/model/models/gemma3" _ "github.com/ollama/ollama/model/models/gemma3"
_ "github.com/ollama/ollama/model/models/gemma3n" _ "github.com/ollama/ollama/model/models/gemma3n"
_ "github.com/ollama/ollama/model/models/gptoss"
_ "github.com/ollama/ollama/model/models/llama" _ "github.com/ollama/ollama/model/models/llama"
_ "github.com/ollama/ollama/model/models/llama4" _ "github.com/ollama/ollama/model/models/llama4"
_ "github.com/ollama/ollama/model/models/mistral3" _ "github.com/ollama/ollama/model/models/mistral3"
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -111,7 +111,8 @@ func (m *Model) Capabilities() []model.Capability { ...@@ -111,7 +111,8 @@ func (m *Model) Capabilities() []model.Capability {
// Check for thinking capability // Check for thinking capability
openingTag, closingTag := thinking.InferTags(m.Template.Template) openingTag, closingTag := thinking.InferTags(m.Template.Template)
if openingTag != "" && closingTag != "" { hasTags := openingTag != "" && closingTag != ""
if hasTags || m.Config.ModelFamily == "gptoss" {
capabilities = append(capabilities, model.CapabilityThinking) capabilities = append(capabilities, model.CapabilityThinking)
} }
......
This diff is collapsed.
...@@ -209,7 +209,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -209,7 +209,7 @@ func TestChatPrompt(t *testing.T) {
model := tt.model model := tt.model
opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}} opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
think := false think := false
prompt, images, err := chatPrompt(t.Context(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil, &think) prompt, images, err := chatPrompt(t.Context(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil, &api.ThinkValue{Value: think})
if tt.error == nil && err != nil { if tt.error == nil && err != nil {
t.Fatal(err) t.Fatal(err)
} else if tt.error != nil && err != tt.error { } else if tt.error != nil && err != tt.error {
......
This diff is collapsed.
...@@ -150,7 +150,7 @@ func TestGenerateChat(t *testing.T) { ...@@ -150,7 +150,7 @@ func TestGenerateChat(t *testing.T) {
Messages: []api.Message{ Messages: []api.Message{
{Role: "user", Content: "Hello!"}, {Role: "user", Content: "Hello!"},
}, },
Think: &think, Think: &api.ThinkValue{Value: think},
}) })
if w.Code != http.StatusBadRequest { if w.Code != http.StatusBadRequest {
......
This diff is collapsed.
This diff is collapsed.
...@@ -26,6 +26,10 @@ type Parser struct { ...@@ -26,6 +26,10 @@ type Parser struct {
n int n int
} }
func (p *Parser) GetBuffer() []byte {
return p.buffer
}
// NewParser creates a new tool call parser from a model's chat // NewParser creates a new tool call parser from a model's chat
// template and a list of provided tools. // template and a list of provided tools.
func NewParser(tmpl *template.Template, tools []api.Tool) *Parser { func NewParser(tmpl *template.Template, tools []api.Tool) *Parser {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment