Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Last active July 17, 2024 13:35
Show Gist options
  • Save vjcitn/4bd8978e61779ecda7f74708708609c4 to your computer and use it in GitHub Desktop.
Save vjcitn/4bd8978e61779ecda7f74708708609c4 to your computer and use it in GitHub Desktop.
look at 'safetensors' component of bert (use after usebert.R succeeds)
# probe R user cache file system for bert resources
explore_bert = function() {
if (!requireNamespace("tibble")) stop("please install tibble to use this function")
res = dir(tools::R_user_dir("../huggingface/hub/models--google-bert--bert-base-uncased/snapshots/", "cache"), full=TRUE, recursive=TRUE)
if (length(res)<1) stop("not finding huggingface/hub or bert components in cache")
safetpath = grep("model.safetensors", res, value=TRUE)
st = try(reticulate::import("safetensors"))
if (inherits(st, "try-error")) stop("can't import safetensors with reticulate")
oo = st$safe_open(filename=safetpath, framework="pt")
kk = oo$keys()
allt = lapply(kk, function(x) oo$get_tensor(x)$shape$numel())
tibble::as_tibble(data.frame(keys=kk, size=unlist(allt)))
}
print(explore_bert())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment