This repository contains the Ascend NPU adaptation of hubert-base-960h-itw-deepfake, a fine-tuned HuBERT model for audio deepfake detection.
HubertForSequenceClassificationbona-fide (0), spoof (1)| Component | Version |
|---|---|
| Python | >= 3.9 |
| PyTorch | >= 2.0.0 |
| torch-npu | >= 2.9.0 |
| transformers | >= 4.38.0 |
| Ascend Driver | 25.5.2 or compatible |
Hardware: Ascend 910 NPU (tested on Atlas 800 A2)
Device Mapping
torch.device("cuda") to torch.device("npu") for Ascend hardware.Model Loading
transformers.AutoConfig and HubertForSequenceClassification for standard loading.torch_npu.No Architecture Changes
transformers architecture; no weight remapping or operator replacement was required.# 1. Clone this repository
git clone <repo-url>
cd hubert-npu-adapted
# 2. Install dependencies
pip install -r requirements.txt
# 3. Run inference on NPU
python inference_npu.py --model_path ./hubert-base-960h-itw-deepfake
# 4. Run accuracy verification (CPU vs NPU)
python verify_accuracy.pyWe compared the model outputs on CPU and Ascend NPU using randomly initialized weights (structure validation) with multiple random seeds.
| Metric | Value |
|---|---|
| Max Absolute Error | 2.42e-04 |
| Mean Absolute Error | 1.79e-04 |
| Max Relative Error | 2.81e-03 |
| Mean Relative Error | 2.03e-03 |
| Cosine Similarity | 0.999997 |
| Seed | Max Abs Error | Cosine Similarity |
|---|---|---|
| 42 | 2.72e-04 | 0.99999832 |
| 123 | 2.62e-04 | 0.99999812 |
| 456 | 2.33e-04 | 0.99999839 |
| 789 | 2.82e-04 | 0.99999817 |
| 2024 | 2.13e-04 | 0.99999869 |
Conclusion: The CPU and NPU outputs are numerically consistent. The absolute error is within the expected floating-point precision tolerance (< 1e-3). The model is verified for Ascend NPU deployment.
import torch
import torch_npu
import numpy as np
from transformers import AutoConfig, Wav2Vec2FeatureExtractor, HubertForSequenceClassification
# Use NPU
device = torch.device("npu" if torch.npu.is_available() else "cpu")
model_path = "./hubert-base-960h-itw-deepfake"
config = AutoConfig.from_pretrained(model_path)
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_path)
model = HubertForSequenceClassification.from_pretrained(model_path, config=config).to(device)
model.eval()
# Dummy audio (replace with real waveform)
audio = np.random.randn(16000).astype(np.float32) * 0.1
inputs = feature_extractor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
input_values = inputs.input_values.to(device)
with torch.no_grad():
logits = model(input_values).logits
probs = torch.softmax(logits, dim=-1)
print(probs) # [[bona-fide_prob, spoof_prob]]audio_batch = [np.random.randn(16000).astype(np.float32) for _ in range(4)]
inputs = feature_extractor(audio_batch, sampling_rate=16000, return_tensors="pt", padding=True)
input_values = inputs.input_values.to(device)
with torch.no_grad():
logits = model(input_values).logits
probs = torch.softmax(logits, dim=-1)| File | Description |
|---|---|
inference_npu.py | NPU inference script with automatic device selection |
verify_accuracy.py | CPU vs NPU accuracy comparison script |
accuracy_report.json | Detailed accuracy metrics JSON |
requirements.txt | Python dependencies |
Apache-2.0