Skip to main content

Unit HiFi-GAN

Minimal re-implementation of HiFi-GAN training on discrete speech units.

The package is available on PyPI:

pip install unit-hifigan

It is based on the "Speech Resynthesis from Discrete Disentangled Self-Supervised Representations" repository, with a clean and minimal re-implementation for both training and inference. We follow their default hyperparameters and model configurations.

Usage

Vocoder

The vocoder is available via the UnitVocoder class. You can condition it on speakers and styles (support for f0 will come later).

Load a pretrained model from a local directory or a distant HuggingFace repository like this:

from unit_hifigan import UnitVocoder

vocoder = UnitVocoder.from_pretrained("coml/hubert-phoneme-classification", revision="vocoder-base-l11")

You can also load models from the legacy implementation or from textlesslib with UnitVocoder.from_legacy_pretrained:

import requests
from unit_hifigan import UnitVocoder

url = "https://dl.fbaipublicfiles.com/textless_nlp/expresso/checkpoints/hifigan_expresso_lj_vctk_hubert_base_ls960_L9_km500/"
vocoder = UnitVocoder.from_legacy_pretrained(url + "generator.pt")
vocoder.speakers = requests.get(url + "speakers.txt").text.splitlines()
vocoder.styles = requests.get(url + "styles.txt").text.splitlines()

You can then generate audio at 16kHz with UnitVocoder.generate as below:

import torch
from unit_hifigan import UnitVocoder

units, speaker, style = torch.randint(0, 500, (1, 100)), ["speaker-0"], ["reading"]
vocoder = UnitVocoder(500, speakers=["speaker-0", "speaker-1"], styles=["reading", "crying", "laughing"])
audio = vocoder.generate(units, speaker=speaker, style=style)

Training a Unit HiFi-GAN

Data preparation

For training, you need to have manifest files for the training and validation datasets. The manifests are JSONL files with fields audio (string), units (list of integers), and optionally style (string) or speaker (string):

{"audio":"audio-1.wav","units":[24,24,173,289,289,441,487,370,370],"speaker":"spkr02"}
  • audio: full path to the audio file
  • units: discrete units from the speech encoder (for example, HuBERT layer 11 and K-means K=500)
  • speaker: name of the speaker (for speaker conditioning)
  • style: name of the style (for style conditioning)

Training

Via the CLI:

# Minimal command with default configuration
python -m unit_hifigan.train $WORKDIR --train $TRAIN_MANIFEST --val $VAL_MANIFEST --units $N_UNITS

# If you have a JSON config file
python -m unit_hifigan.train $WORKDIR --config $CONFIG

You can also use the unit_hifigan.train.train function in your Python code if you prefer. Check out unit_hifigan.train.TrainConfig for the list of configuration options.

The pipeline supports DDP by default, when run with either torchrun or Slurm. Have a look at unit_hifigan.utils.init_distributed for how distributed training is initialized.

Synthesis

Via the CLI:

python -m unit_hifigan.inference $GENERATIONS $PRETRAINED_MODEL $INFERENCE_MANIFEST

where the inference manifest has the same format as the training and validation ones.

Evaluation

Whisper ASR

python -m unit_hifigan.wer.whisper $GENERATIONS $ASR_MANIFEST $JSONL_OUTPUT --model $MODEL

where $MODEL is the name of the Whisper variant (large-v3 if not provided). The ASR manifest has the followings fields:

  • audio: relative path to the audio files from the $GENERATIONS directory.
  • transcript: ground truth transcription

Wav2vec 2.0 ASR

python -m unit_hifigan.wer.torchaudio $GENERATIONS $ASR_MANIFEST $JSONL_OUTPUT --model $MODEL

where $MODEL is the name of the torchaudio ASR pipeline to use (by default WAV2VEC2_ASR_LARGE_LV60K_960H).

Mel cepstral distortion

python -m unit_hifigan.mcd $REFERENCE $GENERATIONS $JSONL_OUTPUT

where $REFERENCE and $GENERATIONS are two directories containing audio files with matching names (for example the original dataset and the output of unit_hifigan.inference). Audio files are matched by file name, regardless of the directory structure. The MCD follows the classic SPTK recipe in pure PyTorch (see the notes in unit_hifigan.mcd.MelCepstrum): mel cepstra of order 23 with all-pass frequency warping (α=0.42 at 16kHz), computed like pysptk.sp2mc from Hamming-windowed periodograms, dropping the energy coefficient c0, with exact DTW alignment of the frames. The per-file MCDs in dB are written to $JSONL_OUTPUT.

TODO

  • Add support for F0

Acknowledgements

Metadata

Release files for unit-hifigan 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unit-hifigan 0.2.0
File Size Uploaded Provenance Actions
unit_hifigan-0.2.0.tar.gz 23.2 kB Without provenance Details for unit_hifigan-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unit-hifigan 0.2.0
File Interpreter ABI Platform Actions
unit_hifigan-0.2.0-py3-none-any.whl Python 3 none any Details for unit_hifigan-0.2.0-py3-none-any.whl

Total release size: 48.9 kB

Release files / unit_hifigan-0.2.0.tar.gz

Download URL unit_hifigan-0.2.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
70ec9e93068e040f5d65ea195d780c75a237ff9383c7694573d210ef1d736384
BLAKE2b-256 checksum
How to use checksums
4e8a8ea1d55ff03362f641cb1cf46bf43c9010f06844ee14ed8c84d17a013c12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / unit_hifigan-0.2.0-py3-none-any.whl

Download URL unit_hifigan-0.2.0-py3-none-any.whl
Size 25.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52087220f2b30a18f274dd782911e1c0297f939bf7cc2b8887a7c3c64827288a
BLAKE2b-256 checksum
How to use checksums
cc9154486e5c198b25e2d7ca7998ae00938f77ebabffa514f4b13e6222b4ff57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for unit-hifigan 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page