Skip to content

Commit

Permalink
#34 Enable Metal and avoid clashing of definition of error with R.h
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijffels committed Feb 5, 2024
1 parent 20e44f2 commit 2fdc52f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Makevars
- Added detection of AVX512F for adding compilation flags to PKG_CFLAGS/PKG_CPPFLAGS
- Enable Metal
- Enable Metal for speeding up transcriptions on the GPU on Mac
- Enable compiling with OpenBLAS to speed up the transcriptions
- Add whisper_languages to get a data.frame of all languages the Whisper model can handle
- whisper_download_model
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ To find out which hardware acceleration options your hardware supports, you can

- Common settings to set for `WHISPER_CFLAGS` are `-mavx -mavx2 -mfma -mf16c` and extra possible flags `-msse3` and `mssse3`
- E.g. on my local Windows Intel machine I could set `-mavx -mavx2 -mfma -mf16c`
- For Mac users you can set `Sys.setenv(WHISPER_ACCELERATE = "1")` if your computer has the Accelerate framework
- For Mac users you can speed up transcriptions by setting before installation of audio.whisper
- `Sys.setenv(WHISPER_ACCELERATE = "1")` if your computer has the Accelerate framework
- `Sys.setenv(WHISPER_METAL = "1")` if your computer has a GPU based on Metal
- On my older local Ubuntu machine there were no optimisation possibilities. Your mileage may vary.
- If you have OpenBLAS installed, you can considerably speed up transcription by installing the R package with `Sys.setenv(WHISPER_OPENBLAS = "1")`
- If you need extra settings in `PKG_CPPFLAGS` (`CXXFLAGS`), you can e.g. use `Sys.setenv(WHISPER_CPPFLAGS = "-mcpu=native")` before installing the package
Expand Down
6 changes: 5 additions & 1 deletion src/whisper_cpp/ggml-metal.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifndef R_NO_REMAP
#define R_NO_REMAP 1
#endif
#import "R.h"
#import "ggml-metal.h"

#import "ggml-backend-impl.h"
Expand Down Expand Up @@ -183,7 +187,7 @@ @implementation GGMLMetalClass


static void ggml_metal_default_log_callback(enum ggml_log_level level, const char * msg, void * user_data) {
fprintf(stderr, "%s", msg);
Rprintf("%s", msg);

UNUSED(level);
UNUSED(user_data);
Expand Down
6 changes: 6 additions & 0 deletions tools/dev/cran-compliance.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ x <- gsub(x, pattern = "fputs(whisper_bench_memcpy_str(n_threads), stderr)", rep
x <- gsub(x, pattern = "fputs(text, stderr);", replacement = "Rcpp::Rcout << text;", fixed = TRUE)
x <- gsub(x, pattern = "rand()", replacement = "((int) floor(R::runif(0, 32767)))", fixed = TRUE)
writeLines(x, "src/whisper_cpp/common.cpp")

## Make sure Metal works - otherwise
x <- readLines("src/whisper_cpp/ggml-metal.m")
x <- c('#ifndef R_NO_REMAP', '#define R_NO_REMAP 1', '#endif', '#import "R.h"', x)
x <- gsub(x, pattern = "fprintf(stderr, ", replacement = "Rprintf(", fixed = TRUE)
writeLines(x, "src/whisper_cpp/ggml-metal.m")

0 comments on commit 2fdc52f

Please sign in to comment.