Skip to main content

Bidirectional converter between GGUF and HuggingFace formats with weight verification

Project description

FanFu - GGUF/HuggingFace Bidirectional Converter

Bidirectional converter between GGUF and HuggingFace formats with weight verification.

FanFu (反复) -- Back and forth, roundtrip.

Features

  • GGUF to HuggingFace -- Convert GGUF files to HuggingFace safetensors format with automatic dequantization (Q8_0, Q4_0, F16, F32).
  • HuggingFace to GGUF -- Convert HuggingFace model directories to GGUF format with optional quantization.
  • Weight Verification -- Compare weights between GGUF and HF models with configurable tolerance, reporting matched/mismatched tensors.
  • Tokenizer Extraction -- Automatically extract tokenizer from GGUF files and generate HF-compatible tokenizer files (tokenizer.json, tokenizer_config.json, special_tokens_map.json).
  • CLI & Python API -- Use from the command line or import as a library.

Requirements

  • Python 3.10+
  • gguf, safetensors, torch, numpy, rich

Installation

From PyPI (Recommended)

pip install fanfu

From Source

git clone https://github.com/CodeOfMe/FanFu.git
cd FanFu
pip install -e .

Usage

CLI

# Convert GGUF to HuggingFace
fanfu gguf-to-hf model.gguf -o hf_model/
fanfu gguf-to-hf model.gguf -o hf_model/ -t f16
fanfu gguf-to-hf model.gguf -o hf_model/ --no-tokenizer

# Convert HuggingFace to GGUF
fanfu hf-to-gguf hf_model/ -o model.gguf
fanfu hf-to-gguf hf_model/ -o model.gguf -t q8_0
fanfu hf-to-gguf hf_model/ -o model.gguf -t f16

# Compare weights between GGUF and HF
fanfu compare model.gguf hf_model/
fanfu compare model.gguf hf_model/ --tolerance 0.1 -o results.json

# Show version
fanfu --version

Python API

from fanfu import convert_gguf_to_hf, convert_hf_to_gguf, compare_weights

# GGUF -> HF
result = convert_gguf_to_hf("model.gguf", "hf_model/", outtype="f32")
print(result.data)  # {"output_dir": "hf_model/", "tensors": 536, "skipped": 0}

# HF -> GGUF
result = convert_hf_to_gguf("hf_model/", "model.gguf", outtype="q8_0")
print(result.data)  # {"output_path": "model.gguf", "tensors": 535}

# Compare weights
result = compare_weights("model.gguf", "hf_model/", tolerance=0.5)
print(f"Accuracy: {result.data['accuracy']:.1f}%")
print(f"Matched: {result.data['matched']}, Mismatched: {result.data['mismatched']}")

Project Structure

FanFu/
├── pyproject.toml              # Package metadata & build config
├── MANIFEST.in                 # Source distribution manifest
├── LICENSE                     # GPL-3.0-or-later
├── README.md                   # English documentation
├── README_CN.md                # Chinese documentation
├── fanfu/
│   ├── __init__.py             # Package version & public exports
│   ├── __main__.py             # python -m fanfu entry
│   ├── cli.py                  # CLI entry point with subcommands
│   ├── api.py                  # Public Python API
│   ├── constants.py            # App constants & architecture mappings
│   ├── errors.py               # Custom exception classes
│   ├── gguf_to_hf.py           # GGUF -> HuggingFace converter
│   ├── hf_to_gguf.py           # HuggingFace -> GGUF converter
│   └── compare.py              # Weight comparison & verification
├── tests/
│   ├── __init__.py
│   └── test_core.py            # Comprehensive test suite
└── publish.py                  # PyPI publish helper script

Testing

# Run full test suite
python -m pytest tests/ -v

# Run with coverage
python -m pytest tests/ --cov=fanfu --cov-report=term-missing

Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
python -m pytest tests/ -v

Publishing

# Build only
python publish.py

# Build + upload to TestPyPI
python publish.py test

# Build + upload to PyPI
python publish.py release

License

GPL-3.0-or-later. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fanfu-0.1.1.tar.gz (33.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fanfu-0.1.1-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file fanfu-0.1.1.tar.gz.

File metadata

  • Download URL: fanfu-0.1.1.tar.gz
  • Upload date:
  • Size: 33.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for fanfu-0.1.1.tar.gz
Algorithm Hash digest
SHA256 99d13597f336c35da23a96c9aac2920a7af63b9cfd054e64d2c8f93ca368d793
MD5 ade95d3b42f89a1bdb6c8cde03961aa5
BLAKE2b-256 8adf372ad1192853cb579028edf9a251ee5f129818653c17a5c138713fee8cbb

See more details on using hashes here.

File details

Details for the file fanfu-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fanfu-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for fanfu-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9860db0106d088120460813fbc77647faa9f2ceb0246bf34154f0550cd5791a7
MD5 0fd36b93af4a303be5d369e10e68494a
BLAKE2b-256 803135c739a0989a12a6e654cc23b4ac4b3f4d033438c37f980cac7af31cdd3d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page