Closed adamramey closed 1 year ago
Same problem here. Anyone got suggestions for a fix?
Thanks for a great package @OscarKjell
Unfortunately, I'm unable to test this but I've attempted to add support for this. If you are able to install text
via Github (as opposed to CRAN) then you can pull the latest code (I've attempted to push a quick fix) and try running
textEmbed(...., device='mps', ...)
Note that MPS acceleration is available on MacOS 12.3+.
Also, I think you will need PyTorch v1.12 and maybe even >=1.13. We haven't tested text
on either of these versions as of writing this.
Thank you @sjgiorgi , will do
Thanks a lot for this @sjgiorgi! Zhoujun will soon make a pull request with an update that works! I think you will have to set device = "mps:0" in textEmbed().
One can try with device = "mps" in textEmbed(). It still works.
Yay, I'm happy to confirm this works for me (M1 MacBook Pro, {text}
0.9.99.9)
> # Example text
> texts <- c("I feel great!")
>
> # Defaults
> embeddings <- textEmbed(texts,
+ device = "mps:0")
Completed layers output for (variable: 1/1, duration: 6.218717 secs).
Completed layers aggregation for word_type_embeddings.
Completed layers aggregation (variable 1/1, duration: 0.063519 secs).
Completed layers aggregation (variable 1/1, duration: 0.057727 secs).
MPS_for_MacM1+_available: True
Using mps!
@MattCowgill Thanks for reporting back!
@moomoofarm1 @OscarKjell Have we confirmed that we are seeing increased speed when using this?
If anything @sjgiorgi I see a slight slowdown with device = "mps:0"
Please see reprex with timings below
library(text)
#> [0;34mThis is text (version 0.9.99.9).
#> [0m[0;32mText is new and still rapidly improving.
#>
#> Newer versions may have improved functions and updated defaults to reflect current understandings of the state-of-the-art.
#> Please send us feedback based on your experience.[0m[0;35m
#>
#> Please note that defaults has changed in the textEmbed-functions since last version; see help(textEmbed) or www.r-text.org for more details.[0m
textrpp_initialize()
#> [0;32m
#> Successfully initialized text required python packages.
#> [0m
#> [0;34mPython options:
#> type = "textrpp_condaenv",
#> name = "textrpp_condaenv".[0m
texts <- c("I feel great!",
"I don't feel so good",
"Water is wet")
bench::mark(textEmbed(texts,
device = "mps:0"),
textEmbed(texts,
device = "cpu"),
min_iterations = 3,
max_iterations = 3,
check = FALSE)
#> [0;32mCompleted layers output for (variable: 1/1, duration: 8.813812 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.610342 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.444646 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 6.690455 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.307038 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.340210 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 6.106852 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.284889 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.255946 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 6.581084 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.232711 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.291940 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 6.734517 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.290402 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.328100 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 5.629423 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.225370 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.227123 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 5.711532 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.226697 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.227989 secs).
#> [0m[0;32mCompleted layers output for (variable: 1/1, duration: 6.046991 secs).
#> [0m[0;32mCompleted layers aggregation for word_type_embeddings.
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.542720 secs).
#> [0m[0;34mCompleted layers aggregation (variable 1/1, duration: 0.239926 secs).
#> [0m
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_a…¹ gc/se…²
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:b> <dbl>
#> 1 textEmbed(texts, device = "mps:0") 7.37s 7.82s 0.129 43.1MB 0.516
#> 2 textEmbed(texts, device = "cpu") 6.78s 6.86s 0.142 36MB 0.613
#> # … with abbreviated variable names ¹mem_alloc, ²`gc/sec`
Created on 2023-04-18 with reprex v2.0.2
This is with torch 1.13.0 FYI
I have not been able to see improvements in time using mps:1 – but the good news is that @moomoofarm1 has improved a sorting function that reduces the time by about half when using textEmbed()
or textEmbedRawLayers()
with text data of more than 200 rows of the example data.
We have also added the sort
parameter in textEmbedRawLayers()
, which is set to FALSE return results in lists (rather than tidy format), which reduces the time a lot.
# Example data
texts <- bind_rows(Language_based_assessment_data_8[1],
Language_based_assessment_data_8[1],
Language_based_assessment_data_8[1],
Language_based_assessment_data_8[1],
Language_based_assessment_data_8[1])
texts
t1 <- Sys.time()
emb_old <- textEmbedRawLayers_OLD(texts)
t2 <- Sys.time()
t_old <- t2-t1
t3 <- Sys.time()
emb_mps <- text::textEmbedRawLayers(texts)
t4 <- Sys.time()
t_new <- t4 - t3
t5 <- Sys.time()
emb_fsort_mps <- textEmbedRawLayers(texts, sort = FALSE, device = "mps:1")
t6 <- Sys.time()
t_fsort_mps <- t6 - t5
t7 <- Sys.time()
emb_fsort <- textEmbedRawLayers(texts, sort = FALSE)
t8 <- Sys.time()
t_fsort <- t8 - t7
# 200 rows of text t_old Time difference of 2.868509 mins
t_new Time difference of 1.200108 mins
t_fsort_mps Time difference of 56.20257 secs
t_fsort Time difference of 43.55411 secs
# 600 rows of text
t_old Time difference of 6.698215 mins t_new Time difference of 2.765697 mins t_fsort_mps Time difference of 1.481872 mins t_fsort Time difference of 1.153369 mins
# 1200 rows of text
t_old Time difference of 14.84135 mins t_new Time difference of 6.070847 mins t_fsort_mps Time difference of 1.947607 mins t_fsort Time difference of 2.087477 mins
Hello - I've managed to get the package up and running on my M1 Mac but GPU support isn't working for some reason. I get the error "Unable to use CUDA (GPU), using CPU"
Any idea how we can set this up properly?