Implementation:Facebookresearch Audiocraft Export pretrained compression model
Overview
Export_pretrained_compression_model packages a compression model (EnCodec or DAC) for distribution alongside a language model. It handles both pretrained model references and custom-trained model files.
Implements
Principle:Facebookresearch_Audiocraft_Pretrained_Compression_Export
API Signature
def export_pretrained_compression_model(
pretrained_encodec: str,
out_file: Union[Path, str]
) -> None
Source: audiocraft/utils/export.py, lines 36-58
Import:
from audiocraft.utils.export import export_pretrained_compression_model
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pretrained_encodec |
str |
required | Either a pretrained model identifier (e.g., 'facebook/encodec_32khz') or a filesystem path to a custom-trained, already-exported compression model file
|
out_file |
Union[Path, str] |
required | Destination path for the compression model export (typically compression_state_dict.bin)
|
Return Value
Returns None. The export file is written to out_file as a side effect.
Dependencies
torch-- for loading and saving checkpoint filesaudiocraft.__version__-- for embedding the version tag
How It Works
from audiocraft.utils.export import export_pretrained_compression_model
# Export using a standard pretrained model reference
export_pretrained_compression_model(
pretrained_encodec='facebook/encodec_32khz',
out_file='/path/to/exports/my_model/compression_state_dict.bin'
)
# Export using a custom-trained compression model
export_pretrained_compression_model(
pretrained_encodec='/path/to/custom_encodec/checkpoint_exported.th',
out_file='/path/to/exports/my_model/compression_state_dict.bin'
)
The implementation:
def export_pretrained_compression_model(pretrained_encodec, out_file):
if Path(pretrained_encodec).exists():
pkg = torch.load(pretrained_encodec)
assert 'best_state' in pkg
assert 'xp.cfg' in pkg
assert 'version' in pkg
assert 'exported' in pkg
else:
pkg = {
'pretrained': pretrained_encodec,
'exported': True,
'version': __version__,
}
Path(out_file).parent.mkdir(exist_ok=True, parents=True)
torch.save(pkg, out_file)
Output Format
The output depends on the input type:
For pretrained model references:
| Key | Type | Description |
|---|---|---|
pretrained |
str |
The model identifier string (e.g., 'facebook/encodec_32khz')
|
exported |
bool |
Always True
|
version |
str |
AudioCraft version string |
For custom-trained models:
| Key | Type | Description |
|---|---|---|
best_state |
dict |
Full model state dictionary |
xp.cfg |
str |
YAML-serialized model configuration |
version |
str |
AudioCraft version string |
exported |
bool |
Always True
|
Loading Behavior
When the exported compression model is loaded by load_compression_model() in audiocraft/models/loaders.py, the presence of the pretrained key triggers a call to CompressionModel.get_pretrained(), which fetches the actual model weights from HuggingFace Hub. This deferred loading strategy keeps the export file small while ensuring the correct model is used at inference time.