ruby : Follow source tree change (#2580)

* Follow whisper.cpp source tree change

* Update whispercpp.gemspec

* Follow whisper.cpp log level change

* Fix paths in GitHub workflow for Ruby bindings

* Use GitHub workflow setting for dependency definition

* Use ternary operator
This commit is contained in:
KITAITI Makoto 2024-11-22 00:04:29 +09:00 committed by GitHub
parent 37c88027e1
commit 8c6a9b8bb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 116 additions and 153 deletions

View File

@ -3,61 +3,41 @@ on:
push: push:
paths: paths:
- bindings/ruby/** - bindings/ruby/**
- src/whisper.cpp - src/**/*.c
- include/whisper.h - src/**/*.cpp
- ggml/src/ggml.c - src/**/*.h
- ggml/src/ggml-impl.h - src/**/*.m
- ggml/src/ggml-aarch64.h - src/**/*.metal
- ggml/src/ggml-aarch64.c - include/**/*.c
- ggml/src/ggml-alloc.c - include/**/*.cpp
- ggml/src/ggml-backend-impl.h - include/**/*.h
- ggml/src/ggml-backend.cpp - include/**/*.m
- ggml/src/ggml-common.h - include/**/*.metal
- ggml/src/ggml-quants.h - ggml/**/*.c
- ggml/src/ggml-quants.c - ggml/**/*.cpp
- ggml/src/ggml-cpu-impl.h - ggml/**/*.h
- ggml/src/ggml-metal.m - ggml/**/*.m
- ggml/src/ggml-metal.metal - ggml/**/*.metal
- ggml/src/ggml-blas.cpp
- ggml/include/ggml.h
- ggml/include/ggml-alloc.h
- ggml/include/ggml-backend.h
- ggml/include/ggml-cuda.h
- ggml/include/ggml-kompute.h
- ggml/include/ggml-metal.h
- ggml/include/ggml-sycl.h
- ggml/include/ggml-vulkan.h
- ggml/include/ggml-blas.h
- scripts/get-flags.mk - scripts/get-flags.mk
- examples/dr_wav.h - examples/dr_wav.h
pull_request: pull_request:
paths: paths:
- bindings/ruby/** - bindings/ruby/**
- src/whisper.cpp - src/**/*.c
- include/whisper.h - src/**/*.cpp
- ggml/src/ggml.c - src/**/*.h
- ggml/src/ggml-impl.h - src/**/*.m
- ggml/src/ggml-aarch64.h - src/**/*.metal
- ggml/src/ggml-aarch64.c - include/**/*.c
- ggml/src/ggml-alloc.c - include/**/*.cpp
- ggml/src/ggml-backend-impl.h - include/**/*.h
- ggml/src/ggml-backend.cpp - include/**/*.m
- ggml/src/ggml-common.h - include/**/*.metal
- ggml/src/ggml-quants.h - ggml/**/*.c
- ggml/src/ggml-quants.c - ggml/**/*.cpp
- ggml/src/ggml-cpu-impl.h - ggml/**/*.h
- ggml/src/ggml-metal.m - ggml/**/*.m
- ggml/src/ggml-metal.metal - ggml/**/*.metal
- ggml/src/ggml-blas.cpp
- ggml/include/ggml.h
- ggml/include/ggml-alloc.h
- ggml/include/ggml-backend.h
- ggml/include/ggml-cuda.h
- ggml/include/ggml-kompute.h
- ggml/include/ggml-metal.h
- ggml/include/ggml-sycl.h
- ggml/include/ggml-vulkan.h
- ggml/include/ggml-blas.h
- scripts/get-flags.mk - scripts/get-flags.mk
- examples/dr_wav.h - examples/dr_wav.h

View File

@ -1,20 +1,22 @@
require 'rake/clean' require 'rake/clean'
require "bundler/gem_tasks" require "bundler/gem_tasks"
require "pathname"
require "yaml"
require "rake/testtask" require "rake/testtask"
require_relative "extsources"
extsources = YAML.load_file("extsources.yaml")
SOURCES = FileList[] SOURCES = FileList[]
extsources.each do |src|
EXTSOURCES.each do |src|
basename = src.pathmap("%f") basename = src.pathmap("%f")
dest = basename == "LICENSE" ? basename : basename.pathmap("ext/%f") dest = basename == "LICENSE" ? basename : src.pathmap("%{../..,ext}p")
dir = dest.pathmap("%d")
file src file src
file dest => src do |t| directory dir
file dest => [src, dir] do |t|
cp t.source, t.name cp t.source, t.name
end end
SOURCES.include dest SOURCES.include dest
end end
CLEAN.include SOURCES CLEAN.include SOURCES
CLEAN.include FileList[ CLEAN.include FileList[
"ext/*.o", "ext/*.o",

View File

@ -1,35 +1,14 @@
Makefile Makefile
ggml.c
ggml.h
ggml-alloc.c
ggml-alloc.h
ggml-aarch64.c
ggml-aarch64.h
ggml-backend.cpp
ggml-backend-impl.h
ggml-backend.c
ggml-backend.h
ggml-common.h
ggml-cpu-impl.h
ggml-metal.m
ggml-metal.metal
ggml-metal-embed.metal
ggml-blas.cpp
ggml-cuda.h
ggml-impl.h
ggml-kompute.h
ggml-metal.h
ggml-opencl.h
ggml-quants.c
ggml-quants.h
ggml-sycl.h
ggml-vulkan.h
ggml-blas.h
get-flags.mk
whisper.cpp
whisper.h
dr_wav.h
depend
whisper.bundle
whisper.so whisper.so
whisper.bundle
whisper.dll whisper.dll
depend
scripts/get-flags.mk
*.o
*.c
*.cpp
*.h
*.m
*.metal
!ruby_whisper.cpp
!ruby_whisper.h

9
bindings/ruby/ext/cpu.mk Normal file
View File

@ -0,0 +1,9 @@
ggml/src/ggml-cpu/ggml-cpu-cpp.o: \
ggml/src/ggml-cpu/ggml-cpu.cpp \
ggml/include/ggml-backend.h \
ggml/include/ggml.h \
ggml/include/ggml-alloc.h \
ggml/src/ggml-backend-impl.h \
ggml/include/ggml-cpu.h \
ggml/src/ggml-impl.h
$(CXX) $(CXXFLAGS) -c $< -o $@

View File

@ -35,7 +35,7 @@ if $GGML_METAL
$GGML_METAL_EMBED_LIBRARY = true $GGML_METAL_EMBED_LIBRARY = true
end end
$MK_CPPFLAGS = '' $MK_CPPFLAGS = '-Iggml/include -Iggml/src -Iinclude -Isrc -Iexamples'
$MK_CFLAGS = '-std=c11 -fPIC' $MK_CFLAGS = '-std=c11 -fPIC'
$MK_CXXFLAGS = '-std=c++11 -fPIC' $MK_CXXFLAGS = '-std=c++11 -fPIC'
$MK_NVCCFLAGS = '-std=c++11' $MK_NVCCFLAGS = '-std=c++11'
@ -123,11 +123,11 @@ end
unless ENV['GGML_NO_ACCELERATE'] unless ENV['GGML_NO_ACCELERATE']
if $UNAME_S == 'Darwin' if $UNAME_S == 'Darwin'
$MK_CPPFLAGS << ' -DGGML_USE_ACCELERATE -DGGML_USE_BLAS' $MK_CPPFLAGS << ' -DGGML_USE_ACCELERATE -DGGML_USE_BLAS -DGGML_BLAS_USE_ACCELERATE'
$MK_CPPFLAGS << ' -DACCELERATE_NEW_LAPACK' $MK_CPPFLAGS << ' -DACCELERATE_NEW_LAPACK'
$MK_CPPFLAGS << ' -DACCELERATE_LAPACK_ILP64' $MK_CPPFLAGS << ' -DACCELERATE_LAPACK_ILP64'
$MK_LDFLAGS << ' -framework Accelerate' $MK_LDFLAGS << ' -framework Accelerate'
$OBJ_GGML << 'ggml-blas.o' $OBJ_GGML << 'ggml/src/ggml-blas/ggml-blas.o'
end end
end end
@ -135,20 +135,20 @@ if ENV['GGML_OPENBLAS']
$MK_CPPFLAGS << " -DGGML_USE_BLAS #{`pkg-config --cflags-only-I openblas`.chomp}" $MK_CPPFLAGS << " -DGGML_USE_BLAS #{`pkg-config --cflags-only-I openblas`.chomp}"
$MK_CFLAGS << " #{`pkg-config --cflags-only-other openblas)`.chomp}" $MK_CFLAGS << " #{`pkg-config --cflags-only-other openblas)`.chomp}"
$MK_LDFLAGS << " #{`pkg-config --libs openblas`}" $MK_LDFLAGS << " #{`pkg-config --libs openblas`}"
$OBJ_GGML << 'ggml-blas.o' $OBJ_GGML << 'ggml/src/ggml-blas/ggml-blas.o'
end end
if ENV['GGML_OPENBLAS64'] if ENV['GGML_OPENBLAS64']
$MK_CPPFLAGS << " -DGGML_USE_BLAS #{`pkg-config --cflags-only-I openblas64`.chomp}" $MK_CPPFLAGS << " -DGGML_USE_BLAS #{`pkg-config --cflags-only-I openblas64`.chomp}"
$MK_CFLAGS << " #{`pkg-config --cflags-only-other openblas64)`.chomp}" $MK_CFLAGS << " #{`pkg-config --cflags-only-other openblas64)`.chomp}"
$MK_LDFLAGS << " #{`pkg-config --libs openblas64`}" $MK_LDFLAGS << " #{`pkg-config --libs openblas64`}"
$OBJ_GGML << 'ggml-blas.o' $OBJ_GGML << 'ggml/src/ggml-blas/ggml-blas.o'
end end
if $GGML_METAL if $GGML_METAL
$MK_CPPFLAGS << ' -DGGML_USE_METAL' $MK_CPPFLAGS << ' -DGGML_USE_METAL'
$MK_LDFLAGS << ' -framework Foundation -framework Metal -framework MetalKit' $MK_LDFLAGS << ' -framework Foundation -framework Metal -framework MetalKit'
$OBJ_GGML << 'ggml-metal.o' $OBJ_GGML << 'ggml/src/ggml-metal/ggml-metal.o'
if ENV['GGML_METAL_NDEBUG'] if ENV['GGML_METAL_NDEBUG']
$MK_CPPFLAGS << ' -DGGML_METAL_NDEBUG' $MK_CPPFLAGS << ' -DGGML_METAL_NDEBUG'
@ -156,20 +156,26 @@ if $GGML_METAL
if $GGML_METAL_EMBED_LIBRARY if $GGML_METAL_EMBED_LIBRARY
$MK_CPPFLAGS << ' -DGGML_METAL_EMBED_LIBRARY' $MK_CPPFLAGS << ' -DGGML_METAL_EMBED_LIBRARY'
$OBJ_GGML << 'ggml-metal-embed.o' $OBJ_GGML << 'ggml/src/ggml-metal/ggml-metal-embed.o'
end end
end end
$OBJ_GGML << $OBJ_GGML <<
'ggml.o' << 'ggml/src/ggml.o' <<
'ggml-cpu.o' << 'ggml/src/ggml-aarch64.o' <<
'ggml-alloc.o' << 'ggml/src/ggml-alloc.o' <<
'ggml-backend.o' << 'ggml/src/ggml-backend.o' <<
'ggml-quants.o' << 'ggml/src/ggml-backend-reg.o' <<
'ggml-aarch64.o' 'ggml/src/ggml-opt.o' <<
'ggml/src/ggml-quants.o' <<
'ggml/src/ggml-threading.o' <<
'ggml/src/ggml-cpu/ggml-cpu.o' <<
'ggml/src/ggml-cpu/ggml-cpu-cpp.o' <<
'ggml/src/ggml-cpu/ggml-cpu-aarch64.o' <<
'ggml/src/ggml-cpu/ggml-cpu-quants.o'
$OBJ_WHISPER << $OBJ_WHISPER <<
'whisper.o' 'src/whisper.o'
$objs = $OBJ_GGML + $OBJ_WHISPER + $OBJ_COMMON + $OBJ_SDL $objs = $OBJ_GGML + $OBJ_WHISPER + $OBJ_COMMON + $OBJ_SDL
$objs << "ruby_whisper.o" $objs << "ruby_whisper.o"
@ -184,9 +190,12 @@ $LDFLAGS = "#{$MK_LDFLAGS} #{$LDFLAGS}"
create_makefile('whisper') create_makefile('whisper')
File.open 'Makefile', 'a' do |file| File.open 'Makefile', 'a' do |file|
file.puts 'include get-flags.mk' file.puts 'include scripts/get-flags.mk'
file.puts 'include cpu.mk'
if $GGML_METAL if $GGML_METAL
file.puts 'include metal.mk'
if $GGML_METAL_EMBED_LIBRARY if $GGML_METAL_EMBED_LIBRARY
file.puts 'include metal-embed.mk' file.puts 'include metal-embed.mk'
end end

View File

@ -1,14 +1,17 @@
ggml-metal-embed.o: \ ggml/src/ggml-metal/ggml-metal-embed.o: \
ggml-metal.metal \ ggml/src/ggml-metal/ggml-metal.metal \
ggml-common.h ggml/src/ggml-metal/ggml-metal-impl.h \
ggml/src/ggml-common.h
@echo "Embedding Metal library" @echo "Embedding Metal library"
@sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < ggml-metal.metal > ggml-metal-embed.metal @sed -e '/__embed_ggml-common.h__/r ggml/src/ggml-common.h' -e '/__embed_ggml-common.h__/d' < ggml/src/ggml-metal/ggml-metal.metal > ggml/src/ggml-metal/ggml-metal-embed.metal.tmp
$(eval TEMP_ASSEMBLY=$(shell mktemp)) @sed -e '/#include "ggml-metal-impl.h"/r ggml/src/ggml-metal/ggml-metal-impl.h' -e '/#include "ggml-metal-impl.h"/d' < ggml/src/ggml-metal/ggml-metal-embed.metal.tmp > ggml/src/ggml-metal/ggml-metal-embed.metal
@echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY) $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
@echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY) @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY) @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@echo ".incbin \"ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY) @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY) @echo ".incbin \"ggml/src/ggml-metal/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY) @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@$(AS) $(TEMP_ASSEMBLY) -o $@ @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
@rm -f ${TEMP_ASSEMBLY} $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
@rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
@rmdir ${TEMP_ASSEMBLY}

View File

@ -0,0 +1,6 @@
ggml/src/ggml-metal/ggml-metal.o: \
ggml/src/ggml-metal/ggml-metal.m \
ggml/src/ggml-metal/ggml-metal-impl.h \
ggml/include/ggml-metal.h \
ggml/include/ggml.h
$(CC) $(CFLAGS) -c $< -o $@

View File

@ -0,0 +1,6 @@
require "yaml"
sources = `git ls-files -z ../..`.split("\x0")
paths = YAML.load_file("../../.github/workflows/bindings-ruby.yml")[true]["push"]["paths"]
paths.delete "bindings/ruby/**"
EXTSOURCES = (Dir.glob(paths, base: "../..").collect {|path| "../../#{path}"} << "../../LICENSE") & sources

View File

@ -1,31 +0,0 @@
---
- ../../src/whisper.cpp
- ../../include/whisper.h
- ../../ggml/src/ggml.c
- ../../ggml/src/ggml-cpu.c
- ../../ggml/src/ggml-impl.h
- ../../ggml/src/ggml-aarch64.h
- ../../ggml/src/ggml-aarch64.c
- ../../ggml/src/ggml-alloc.c
- ../../ggml/src/ggml-backend-impl.h
- ../../ggml/src/ggml-backend.cpp
- ../../ggml/src/ggml-common.h
- ../../ggml/src/ggml-quants.h
- ../../ggml/src/ggml-quants.c
- ../../ggml/src/ggml-cpu-impl.h
- ../../ggml/src/ggml-metal.m
- ../../ggml/src/ggml-metal.metal
- ../../ggml/src/ggml-blas.cpp
- ../../ggml/include/ggml.h
- ../../ggml/include/ggml-alloc.h
- ../../ggml/include/ggml-backend.h
- ../../ggml/include/ggml-cpu.h
- ../../ggml/include/ggml-cuda.h
- ../../ggml/include/ggml-kompute.h
- ../../ggml/include/ggml-metal.h
- ../../ggml/include/ggml-sycl.h
- ../../ggml/include/ggml-vulkan.h
- ../../ggml/include/ggml-blas.h
- ../../scripts/get-flags.mk
- ../../examples/dr_wav.h
- ../../LICENSE

View File

@ -107,7 +107,7 @@ class TestWhisper < TestBase
assert logs.length > 30 assert logs.length > 30
logs.each do |log| logs.each do |log|
assert_equal Whisper::LOG_LEVEL_INFO, log[0] assert_include [Whisper::LOG_LEVEL_DEBUG, Whisper::LOG_LEVEL_INFO, Whisper::LOG_LEVEL_WARN], log[0]
assert_same user_data, log[2] assert_same user_data, log[2]
end end
end end

View File

@ -1,4 +1,4 @@
require "yaml" require_relative "extsources"
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "whispercpp" s.name = "whispercpp"
@ -10,24 +10,24 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ['LICENSE', 'README.md'] s.extra_rdoc_files = ['LICENSE', 'README.md']
s.files = `git ls-files . -z`.split("\x0") + s.files = `git ls-files . -z`.split("\x0") +
YAML.load_file("extsources.yaml").collect {|file| EXTSOURCES.collect {|file|
basename = File.basename(file) basename = File.basename(file)
if s.extra_rdoc_files.include?(basename) if s.extra_rdoc_files.include?(basename)
basename basename
else else
File.join("ext", basename) file.sub("../..", "ext")
end end
} }
s.summary = %q{Ruby whisper.cpp bindings} s.summary = %q{Ruby whisper.cpp bindings}
s.test_files = ["tests/test_whisper.rb"] s.test_files = s.files.select {|file| file.start_with? "tests/"}
s.extensions << 'ext/extconf.rb' s.extensions << 'ext/extconf.rb'
#### Documentation and testing. #### Documentation and testing.
s.homepage = 'https://github.com/ggerganov/whisper.cpp' s.homepage = 'https://github.com/ggerganov/whisper.cpp'
s.rdoc_options = ['--main', '../../README.md'] s.rdoc_options = ['--main', 'README.md']
s.platform = Gem::Platform::RUBY s.platform = Gem::Platform::RUBY