Skip to content

Commit

Permalink
Add conversion tool for HF safetensors to gemma.cpp for PaliGemma.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 725132758
  • Loading branch information
danielkeysers authored and copybara-github committed Feb 12, 2025
1 parent c495b25 commit b917a79
Show file tree
Hide file tree
Showing 3 changed files with 457 additions and 3 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,26 @@ and not a pre-trained model (any model with a `-pt` suffix).

**How do I convert my fine-tune to a `.sbs` compressed model file?**

See compression/convert_weights.py to convert a pytorch checkpint. (The code may
need updates to work with Gemma-2 models.)
For PaliGemma (1 and 2) checkpoints, you can use
python/convert_from_safetensors.py to convert from safetensors format (tested
with building via bazel). For an adapter model, you will likely need to call
merge_and_unload() to convert the adapter model to a single-file format before
converting it.

Here is how to use it using a bazel build of the compression library assuming
locally installed (venv) torch, numpy, safetensors, absl-py, etc.:

```sh
bazel build //compression/python:compression
BAZEL_OUTPUT_DIR="${PWD}/bazel-bin/compression"
python3 -c "import site; print(site.getsitepackages())"
# Use your sites-packages file here:
ln -s $BAZEL_OUTPUT_DIR [...]/site-packages/compression
python3 python/convert_from_safetensors.py --load_path [...].safetensors.index.json
```

See also compression/convert_weights.py for a slightly older option to convert a
pytorch checkpoint. (The code may need updates to work with Gemma-2 models.)

**What are some easy ways to make the model run faster?**

Expand Down
2 changes: 1 addition & 1 deletion python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ py_binary(
deps = [
":gemma",
"@python_deps//absl_py",
# placeholder forabsl/flags
# placeholder for absl/flags
"@compression_deps//numpy",
],
)
Loading

0 comments on commit b917a79

Please sign in to comment.