-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathweights.R
More file actions
55 lines (48 loc) · 1.96 KB
/
Copy pathweights.R
File metadata and controls
55 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#' Loads, and possibly downloads HF Hub models
#' @param identifier Repository id from the model
#' @param revision Revision to download from (eg tags, branches or commit hashes)
#' @importFrom hfhub hub_download WEIGHTS_NAME WEIGHTS_INDEX_NAME
#' @export
hf_state_dict <- function(identifier, revision = "main") {
# first try safetensors file
weights_path <- try(
hub_download(identifier, SAFETENSORS_NAME(), revision=revision),
silent = TRUE
)
if (!inherits(weights_path, "try-error")) {
return(safetensors::safe_load_file(weights_path))
}
err <- NULL
# try downloading the weights from the pytorch_model.bin path and save error
# if any happened
weights_path <- tryCatch({
hub_download(identifier, WEIGHTS_NAME(), revision = revision)
}, error = function(err) {
err <<- err
})
# if err is not null it probably means that the weights are sharded in the
# repository, thus we look for the index.
if (!is.null(err)) {
# we now try looking for the index - if it doesn't exist it means the repo
# doesn't contain any model or there's some connection problem.
# in this case we want to raise an error showing the two urls that we tried.
# we also prefer showing the stack trace from the first path.
index_path <- tryCatch({
hub_download(identifier, WEIGHTS_INDEX_NAME(), revision = revision)
}, error = function(e) {
cli::cli_abort(c(
x = "Error downloading weights from {.val {c(WEIGHTS_NAME(), WEIGHTS_INDEX_NAME())}}",
i = "Traceback below shows the error when trying to download {.val {WEIGHTS_NAME()}}"
), parent = err)
})
filenames <- unique(unlist(jsonlite::fromJSON(index_path)$weight_map))
weights_path <- sapply(filenames, function(fname) {
hub_download(identifier, fname, revision = revision)
})
names(weights_path) <- NULL
}
do.call("c", lapply(weights_path, torch::load_state_dict))
}
SAFETENSORS_NAME <- function() {
"model.safetensors"
}