Implementation:Facebookresearch Audiocraft GeneralUtils
| Knowledge Sources | |
|---|---|
| Domains | Utilities, Training |
| Last Updated | 2026-02-14 01:00 GMT |
Overview
Concrete tool for widely-used helper functions including tensor operations, sampling strategies, distributed training utilities, and hashing provided by the AudioCraft library.
Description
The utils module provides general-purpose utility functions used across AudioCraft. Key functions include sample_top_k and sample_top_p for nucleus/top-k sampling during generation, get_pool_executor for parallel processing, length_to_mask for creating attention masks from sequence lengths, hash_trick for feature hashing, and various tensor manipulation helpers. It also includes flashy_audiocraft_export for exporting training artifacts.
Usage
Import specific functions from this module when you need sampling utilities for token generation, mask creation for variable-length sequences, or parallel execution helpers.
Code Reference
Source Location
- Repository: Facebookresearch_Audiocraft
- File: audiocraft/utils/utils.py
- Lines: 1-326
Signature
def sample_top_k(probs: torch.Tensor, k: int) -> torch.Tensor:
"""Sample from the top-k most probable tokens."""
def sample_top_p(probs: torch.Tensor, p: float) -> torch.Tensor:
"""Sample using nucleus (top-p) sampling."""
def length_to_mask(lengths: torch.Tensor, max_len: tp.Optional[int] = None) -> torch.Tensor:
"""Create a boolean mask from sequence lengths."""
def hash_trick(module_name: str, seed: int, max_val: int) -> int:
"""Feature hashing for weight sharing across conditioners."""
def get_pool_executor(num_workers: int, mp_context=None):
"""Get a ProcessPoolExecutor or DummyPoolExecutor."""
Import
from audiocraft.utils.utils import sample_top_k, sample_top_p, length_to_mask, hash_trick
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| probs | torch.Tensor | Yes | Probability distribution [B, card] (for sampling functions) |
| k | int | Yes | Top-k parameter |
| p | float | Yes | Top-p (nucleus) threshold |
| lengths | torch.Tensor | Yes | Sequence lengths [B] (for mask creation) |
Outputs
| Name | Type | Description |
|---|---|---|
| sampled_token | torch.Tensor | Sampled token indices [B, 1] |
| mask | torch.Tensor | Boolean mask [B, T] |
Usage Examples
from audiocraft.utils.utils import sample_top_k, sample_top_p, length_to_mask
import torch
# Top-k sampling
probs = torch.softmax(logits, dim=-1) # [B, vocab_size]
tokens = sample_top_k(probs, k=250)
# Top-p sampling
tokens = sample_top_p(probs, p=0.9)
# Create attention mask from lengths
lengths = torch.tensor([100, 80, 120])
mask = length_to_mask(lengths, max_len=120) # [3, 120]