style: cargo fmt --workspace (whitespace/wrapping only, no semantic change)
Whole-workspace rustfmt pass picked up while iterating on Mamba GPU backward work. Verified formatting-only via diff sampling; no logic changed. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -7,10 +7,10 @@
|
||||
//! Reference: "HiFi-GAN: Generative Adversarial Networks for Efficient and High Fidelity
|
||||
//! Speech Synthesis" (Kong et al., 2020)
|
||||
|
||||
use super::Vocoder;
|
||||
use crate::error::{Result, TtsError};
|
||||
use rtx_tensor::{Device, Tensor};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::error::{Result, TtsError};
|
||||
use super::Vocoder;
|
||||
|
||||
/// Configuration for HiFi-GAN vocoder
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -37,11 +37,7 @@ impl Default for HiFiGANConfig {
|
||||
upsample_rates: vec![8, 8, 2, 2],
|
||||
upsample_kernel_sizes: vec![16, 16, 4, 4],
|
||||
resblock_kernel_sizes: vec![3, 7, 11],
|
||||
resblock_dilation_sizes: vec![
|
||||
vec![1, 3, 5],
|
||||
vec![1, 3, 5],
|
||||
vec![1, 3, 5],
|
||||
],
|
||||
resblock_dilation_sizes: vec![vec![1, 3, 5], vec![1, 3, 5], vec![1, 3, 5]],
|
||||
initial_channel: 512,
|
||||
mel_channels: 80,
|
||||
sample_rate: 22050,
|
||||
@@ -53,29 +49,39 @@ impl HiFiGANConfig {
|
||||
/// Validate configuration
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.upsample_rates.is_empty() {
|
||||
return Err(TtsError::InvalidConfig("Upsample rates cannot be empty".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Upsample rates cannot be empty".to_string(),
|
||||
));
|
||||
}
|
||||
if self.upsample_rates.len() != self.upsample_kernel_sizes.len() {
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Upsample rates and kernel sizes must have same length".to_string()
|
||||
"Upsample rates and kernel sizes must have same length".to_string(),
|
||||
));
|
||||
}
|
||||
if self.resblock_kernel_sizes.is_empty() {
|
||||
return Err(TtsError::InvalidConfig("Resblock kernel sizes cannot be empty".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Resblock kernel sizes cannot be empty".to_string(),
|
||||
));
|
||||
}
|
||||
if self.resblock_kernel_sizes.len() != self.resblock_dilation_sizes.len() {
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Resblock kernel sizes and dilation sizes must have same length".to_string()
|
||||
"Resblock kernel sizes and dilation sizes must have same length".to_string(),
|
||||
));
|
||||
}
|
||||
if self.initial_channel == 0 {
|
||||
return Err(TtsError::InvalidConfig("Initial channel must be > 0".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Initial channel must be > 0".to_string(),
|
||||
));
|
||||
}
|
||||
if self.mel_channels == 0 {
|
||||
return Err(TtsError::InvalidConfig("Mel channels must be > 0".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Mel channels must be > 0".to_string(),
|
||||
));
|
||||
}
|
||||
if self.sample_rate == 0 {
|
||||
return Err(TtsError::InvalidConfig("Sample rate must be > 0".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Sample rate must be > 0".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -90,9 +96,16 @@ struct ResBlock1 {
|
||||
}
|
||||
|
||||
impl ResBlock1 {
|
||||
fn new(_channels: usize, kernel_size: usize, dilations: Vec<usize>, device: &Device) -> Result<Self> {
|
||||
fn new(
|
||||
_channels: usize,
|
||||
kernel_size: usize,
|
||||
dilations: Vec<usize>,
|
||||
device: &Device,
|
||||
) -> Result<Self> {
|
||||
if dilations.is_empty() {
|
||||
return Err(TtsError::InvalidConfig("Dilations cannot be empty".to_string()));
|
||||
return Err(TtsError::InvalidConfig(
|
||||
"Dilations cannot be empty".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
@@ -113,14 +126,20 @@ impl ResBlock1 {
|
||||
let residual = self.apply_conv_block(&output, channels, dilation)?;
|
||||
|
||||
// Residual connection
|
||||
output = output.add(&residual)
|
||||
output = output
|
||||
.add(&residual)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn apply_conv_block(&self, input: &Tensor, _channels: usize, _dilation: usize) -> Result<Tensor> {
|
||||
fn apply_conv_block(
|
||||
&self,
|
||||
input: &Tensor,
|
||||
_channels: usize,
|
||||
_dilation: usize,
|
||||
) -> Result<Tensor> {
|
||||
// Simplified convolution block
|
||||
// In a full implementation, this would use proper Conv1d with LeakyReLU
|
||||
|
||||
@@ -129,10 +148,12 @@ impl ResBlock1 {
|
||||
let dims = input.dims();
|
||||
|
||||
// Apply LeakyReLU-like transformation (y = max(0.2x, x))
|
||||
let data = input.to_cpu()
|
||||
let data = input
|
||||
.to_cpu()
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
|
||||
let transformed: Vec<f32> = data.iter()
|
||||
let transformed: Vec<f32> = data
|
||||
.iter()
|
||||
.map(|&x| if x > 0.0 { x } else { 0.2 * x })
|
||||
.collect();
|
||||
|
||||
@@ -166,7 +187,7 @@ impl HiFiGANGenerator {
|
||||
let dims = mel.dims();
|
||||
if dims.len() != 2 {
|
||||
return Err(TtsError::InvalidInput(
|
||||
"Mel spectrogram must be 2D [mel_channels, time_frames]".to_string()
|
||||
"Mel spectrogram must be 2D [mel_channels, time_frames]".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -178,7 +199,8 @@ impl HiFiGANGenerator {
|
||||
}
|
||||
|
||||
// Reshape to [1, mel_channels, time_frames] for convolution
|
||||
let mut x = mel.unsqueeze(0)
|
||||
let mut x = mel
|
||||
.unsqueeze(0)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
|
||||
// Pre-convolution: expand mel channels to initial_channel
|
||||
@@ -201,9 +223,11 @@ impl HiFiGANGenerator {
|
||||
x = self.post_conv(&x)?;
|
||||
|
||||
// Remove batch dimension and return [samples]
|
||||
x = x.squeeze(Some(0))
|
||||
x = x
|
||||
.squeeze(Some(0))
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
x = x.squeeze(Some(0))
|
||||
x = x
|
||||
.squeeze(Some(0))
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
|
||||
Ok(x)
|
||||
@@ -224,12 +248,18 @@ impl HiFiGANGenerator {
|
||||
Tensor::from_data(
|
||||
data,
|
||||
vec![batch_size, self.config.initial_channel, time_frames],
|
||||
&self.device
|
||||
&self.device,
|
||||
)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))
|
||||
}
|
||||
|
||||
fn upsample_block(&self, input: &Tensor, channels: usize, rate: usize, _idx: usize) -> Result<Tensor> {
|
||||
fn upsample_block(
|
||||
&self,
|
||||
input: &Tensor,
|
||||
channels: usize,
|
||||
rate: usize,
|
||||
_idx: usize,
|
||||
) -> Result<Tensor> {
|
||||
// Transposed convolution for upsampling
|
||||
// In full implementation: ConvTranspose1d with proper upsampling
|
||||
|
||||
@@ -246,7 +276,7 @@ impl HiFiGANGenerator {
|
||||
Tensor::from_data(
|
||||
data,
|
||||
vec![batch_size, out_channels, upsampled_frames],
|
||||
&self.device
|
||||
&self.device,
|
||||
)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))
|
||||
}
|
||||
@@ -269,13 +299,15 @@ impl HiFiGANGenerator {
|
||||
|
||||
let mut result = outputs[0].clone();
|
||||
for output in outputs.iter().skip(1) {
|
||||
result = result.add(output)
|
||||
result = result
|
||||
.add(output)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
}
|
||||
|
||||
// Divide by number of outputs for averaging
|
||||
let scale = 1.0 / outputs.len() as f32;
|
||||
result.mul_scalar(scale)
|
||||
result
|
||||
.mul_scalar(scale)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))
|
||||
}
|
||||
|
||||
@@ -289,15 +321,12 @@ impl HiFiGANGenerator {
|
||||
|
||||
// Create output waveform
|
||||
let data = vec![0.0f32; batch_size * time_frames];
|
||||
let mut output = Tensor::from_data(
|
||||
data,
|
||||
vec![batch_size, 1, time_frames],
|
||||
&self.device
|
||||
)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
let mut output = Tensor::from_data(data, vec![batch_size, 1, time_frames], &self.device)
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
|
||||
// Apply tanh activation for audio range [-1, 1]
|
||||
output = output.tanh()
|
||||
output = output
|
||||
.tanh()
|
||||
.map_err(|e| TtsError::TensorError(e.to_string()))?;
|
||||
|
||||
Ok(output)
|
||||
@@ -330,10 +359,7 @@ impl HiFiGAN {
|
||||
pub fn new(config: HiFiGANConfig, device: &Device) -> Result<Self> {
|
||||
let generator = HiFiGANGenerator::new(config.clone(), device)?;
|
||||
|
||||
Ok(Self {
|
||||
generator,
|
||||
config,
|
||||
})
|
||||
Ok(Self { generator, config })
|
||||
}
|
||||
|
||||
/// Get the generator
|
||||
|
||||
Reference in New Issue
Block a user