From b1f1a14198be30c84836aaab5516326ec82bffde Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 16 Sep 2025 17:55:23 +0300 Subject: [PATCH 1/2] go: allow set a use_gpu option in Go bindings --- bindings/go/context_params.go | 24 ++++++++++++++++++++++++ bindings/go/whisper.go | 9 +++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 bindings/go/context_params.go diff --git a/bindings/go/context_params.go b/bindings/go/context_params.go new file mode 100644 index 00000000000..924d077028a --- /dev/null +++ b/bindings/go/context_params.go @@ -0,0 +1,24 @@ +package whisper + +type ( + contextParamsOption interface{ apply(*ContextParams) } + contextParamsOptionFunc func(*ContextParams) +) + +func (fn contextParamsOptionFunc) apply(to *ContextParams) { + fn(to) +} + +func WithUseGPU(v bool) contextParamsOption { + return contextParamsOptionFunc(func(p *ContextParams) { + p.SetUseGPU(v) + }) +} + +func (p *ContextParams) UseGPU() bool { + return bool(p.use_gpu) +} + +func (p *ContextParams) SetUseGPU(v bool) { + p.use_gpu = toBool(v) +} diff --git a/bindings/go/whisper.go b/bindings/go/whisper.go index 3ef73414d90..dc076d3e251 100644 --- a/bindings/go/whisper.go +++ b/bindings/go/whisper.go @@ -71,6 +71,7 @@ type ( TokenData C.struct_whisper_token_data SamplingStrategy C.enum_whisper_sampling_strategy Params C.struct_whisper_full_params + ContextParams C.struct_whisper_context_params ) /////////////////////////////////////////////////////////////////////////////// @@ -101,10 +102,14 @@ var ( // Allocates all memory needed for the model and loads the model from the given file. // Returns NULL on failure. -func Whisper_init(path string) *Context { +func Whisper_init(path string, options ...contextParamsOption) *Context { cPath := C.CString(path) defer C.free(unsafe.Pointer(cPath)) - if ctx := C.whisper_init_from_file_with_params(cPath, C.whisper_context_default_params()); ctx != nil { + params := ContextParams(C.whisper_context_default_params()) + for _, o := range options { + o.apply(¶ms) + } + if ctx := C.whisper_init_from_file_with_params(cPath, (C.struct_whisper_context_params)(params)); ctx != nil { return (*Context)(ctx) } else { return nil From 7c74def7c9ea671f61523adfcb0af624d5dfdfda Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 16 Sep 2025 22:51:42 +0300 Subject: [PATCH 2/2] go: allow set a flash_attn option in Go binding, better sugar apis --- bindings/go/context_params.go | 23 ++++++++-------------- bindings/go/pkg/whisper/model.go | 20 ++++++++++--------- bindings/go/pkg/whisper/model_option.go | 26 +++++++++++++++++++++++++ bindings/go/whisper.go | 14 ++++++++----- 4 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 bindings/go/pkg/whisper/model_option.go diff --git a/bindings/go/context_params.go b/bindings/go/context_params.go index 924d077028a..cfc6dbbf9f2 100644 --- a/bindings/go/context_params.go +++ b/bindings/go/context_params.go @@ -1,20 +1,5 @@ package whisper -type ( - contextParamsOption interface{ apply(*ContextParams) } - contextParamsOptionFunc func(*ContextParams) -) - -func (fn contextParamsOptionFunc) apply(to *ContextParams) { - fn(to) -} - -func WithUseGPU(v bool) contextParamsOption { - return contextParamsOptionFunc(func(p *ContextParams) { - p.SetUseGPU(v) - }) -} - func (p *ContextParams) UseGPU() bool { return bool(p.use_gpu) } @@ -22,3 +7,11 @@ func (p *ContextParams) UseGPU() bool { func (p *ContextParams) SetUseGPU(v bool) { p.use_gpu = toBool(v) } + +func (p *ContextParams) UseFlashAttention() bool { + return bool(p.flash_attn) +} + +func (p *ContextParams) SetUseFlashAttention(v bool) { + p.flash_attn = toBool(v) +} diff --git a/bindings/go/pkg/whisper/model.go b/bindings/go/pkg/whisper/model.go index 68a150223c7..7de2ff69e97 100644 --- a/bindings/go/pkg/whisper/model.go +++ b/bindings/go/pkg/whisper/model.go @@ -23,19 +23,21 @@ var _ Model = (*model)(nil) /////////////////////////////////////////////////////////////////////////////// // LIFECYCLE -func New(path string) (Model, error) { - model := new(model) +func New(path string, options ...modelOption) (Model, error) { if _, err := os.Stat(path); err != nil { return nil, err - } else if ctx := whisper.Whisper_init(path); ctx == nil { - return nil, ErrUnableToLoadModel - } else { - model.ctx = ctx - model.path = path } - // Return success - return model, nil + params := whisper.DefaultContextParams() + for _, option := range options { + option.apply(¶ms) + } + + if ctx := whisper.Whisper_init_with_params(path, params); ctx != nil { + return &model{path, ctx}, nil + } + + return nil, ErrUnableToLoadModel } func (model *model) Close() error { diff --git a/bindings/go/pkg/whisper/model_option.go b/bindings/go/pkg/whisper/model_option.go new file mode 100644 index 00000000000..2e630545d61 --- /dev/null +++ b/bindings/go/pkg/whisper/model_option.go @@ -0,0 +1,26 @@ +package whisper + +import whisper "github.com/ggerganov/whisper.cpp/bindings/go" + +type ContextParams = whisper.ContextParams + +type ( + modelOption interface{ apply(*ContextParams) } + modelOptionFunc func(*ContextParams) +) + +func (fn modelOptionFunc) apply(to *ContextParams) { + fn(to) +} + +func WithUseGPU(v bool) modelOption { + return modelOptionFunc(func(p *ContextParams) { + p.SetUseGPU(v) + }) +} + +func WithUseFlashAttention(v bool) modelOption { + return modelOptionFunc(func(p *ContextParams) { + p.SetUseFlashAttention(v) + }) +} diff --git a/bindings/go/whisper.go b/bindings/go/whisper.go index dc076d3e251..8081f553051 100644 --- a/bindings/go/whisper.go +++ b/bindings/go/whisper.go @@ -102,13 +102,13 @@ var ( // Allocates all memory needed for the model and loads the model from the given file. // Returns NULL on failure. -func Whisper_init(path string, options ...contextParamsOption) *Context { +func Whisper_init(path string) *Context { + return Whisper_init_with_params(path, DefaultContextParams()) +} + +func Whisper_init_with_params(path string, params ContextParams) *Context { cPath := C.CString(path) defer C.free(unsafe.Pointer(cPath)) - params := ContextParams(C.whisper_context_default_params()) - for _, o := range options { - o.apply(¶ms) - } if ctx := C.whisper_init_from_file_with_params(cPath, (C.struct_whisper_context_params)(params)); ctx != nil { return (*Context)(ctx) } else { @@ -116,6 +116,10 @@ func Whisper_init(path string, options ...contextParamsOption) *Context { } } +func DefaultContextParams() ContextParams { + return ContextParams(C.whisper_context_default_params()) +} + // Frees all memory allocated by the model. func (ctx *Context) Whisper_free() { C.whisper_free((*C.struct_whisper_context)(ctx))