Fix eval_vam.py: update paths to checkpoint/ dirs, fix VAMConfig attn heads, remove open_thinking kwarg
Browse files- Fix checkpoint path: ./out/{weight}_{size}.pth -> checkpoint/omni-o/{weight}.pth
- Fix encoder paths: ./model/ -> checkpoint/{sensevoice,siglip,mimi}
- Fix VAMConfig: add num_attention_heads=8, num_key_value_heads=4 for hidden=768
- Remove open_thinking kwarg from apply_chat_template and generate
- Fix tokenizer(data).data['input_ids'] -> tokenizer(data)['input_ids']
- Guard pydub import (not always installed)
- Handle MoE model naming (avoid double _moe suffix)
- scripts/eval_vam.py +50 -31
scripts/eval_vam.py
CHANGED
|
@@ -13,44 +13,57 @@ warnings.filterwarnings('ignore')
|
|
| 13 |
|
| 14 |
|
| 15 |
def init_model(args):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
log_model_params(model)
|
| 35 |
-
if model.audio_encoder is not None
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
from transformers import MimiModel
|
| 38 |
-
model.mimi_model = MimiModel.from_pretrained(
|
|
|
|
| 39 |
return model.half().eval().to(args.device), tokenizer
|
| 40 |
|
| 41 |
|
| 42 |
def eval_sample(model, tokenizer, args, idx, prompt, audio_inputs, output_name, pixel_values=None, history=None, audio_lens=None, ref_codes=None, spk_emb=None):
|
| 43 |
import soundfile as sf
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
messages = (history or []) + [{"role": "user", "content": prompt}]
|
| 46 |
-
inputs_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True
|
| 47 |
-
x = torch.tensor(tokenizer(inputs_text)
|
| 48 |
|
| 49 |
audio_frames = []
|
| 50 |
with torch.no_grad():
|
| 51 |
res_y = model.generate(x, tokenizer.eos_token_id, max_new_tokens=args.max_new_tokens,
|
| 52 |
temperature=args.temperature, top_p=args.top_p, stream=True,
|
| 53 |
-
return_audio_codes=True,
|
| 54 |
audio_inputs=audio_inputs, audio_lens=audio_lens, pixel_values=pixel_values,
|
| 55 |
ref_codes=ref_codes, spk_emb=spk_emb)
|
| 56 |
print('📒 [Thinker]: ', end='', flush=True)
|
|
@@ -79,8 +92,11 @@ def eval_sample(model, tokenizer, args, idx, prompt, audio_inputs, output_name,
|
|
| 79 |
output_path = os.path.join(args.output_dir, output_name)
|
| 80 |
wav_path = output_path.rsplit('.', 1)[0] + '.wav'
|
| 81 |
sf.write(wav_path, audio.squeeze().float().cpu().numpy(), 24000)
|
| 82 |
-
AudioSegment
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
print(f'| Audio decoded to: {output_path}')
|
| 85 |
except Exception as e:
|
| 86 |
print(f'⚠️ 保存音频失败: {str(e)}')
|
|
@@ -89,10 +105,13 @@ def eval_sample(model, tokenizer, args, idx, prompt, audio_inputs, output_name,
|
|
| 89 |
|
| 90 |
|
| 91 |
def main():
|
| 92 |
-
parser = argparse.ArgumentParser(description="
|
| 93 |
-
parser.add_argument('--load_from', default='
|
| 94 |
-
parser.add_argument('--
|
| 95 |
-
parser.add_argument('--
|
|
|
|
|
|
|
|
|
|
| 96 |
parser.add_argument('--hidden_size', default=768, type=int, help="隐藏层维度")
|
| 97 |
parser.add_argument('--num_hidden_layers', default=8, type=int, help="隐藏层数量")
|
| 98 |
parser.add_argument('--use_moe', default=0, type=int, choices=[0, 1], help="是否使用MoE架构")
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def init_model(args):
|
| 16 |
+
ckpt_dir = args.load_from
|
| 17 |
+
# check for checkpoint/omni-o/omni-o.pth style paths
|
| 18 |
+
weight_name = args.weight
|
| 19 |
+
if not weight_name.endswith('.pth'):
|
| 20 |
+
if args.use_moe and not weight_name.endswith('_moe'):
|
| 21 |
+
weight_name = f'{weight_name}_moe.pth'
|
| 22 |
+
else:
|
| 23 |
+
weight_name = f'{weight_name}.pth'
|
| 24 |
+
ckp = os.path.join(ckpt_dir, weight_name)
|
| 25 |
+
|
| 26 |
+
config = VAMConfig(
|
| 27 |
+
hidden_size=args.hidden_size,
|
| 28 |
+
num_hidden_layers=args.num_hidden_layers,
|
| 29 |
+
num_attention_heads=args.hidden_size // 96,
|
| 30 |
+
num_key_value_heads=args.hidden_size // 192,
|
| 31 |
+
use_moe=bool(args.use_moe)
|
| 32 |
+
)
|
| 33 |
+
model = VAM(config, audio_encoder_path=args.sensevoice_dir, vision_model_path=args.siglip_dir)
|
| 34 |
+
state = torch.load(ckp, map_location=args.device, weights_only=True)
|
| 35 |
+
missing, unexpected = model.load_state_dict(state, strict=False)
|
| 36 |
+
if missing:
|
| 37 |
+
print(f' Missing keys (expected for encoders): {len(missing)}')
|
| 38 |
+
if unexpected:
|
| 39 |
+
print(f' Unexpected keys: {len(unexpected)}')
|
| 40 |
+
|
| 41 |
log_model_params(model)
|
| 42 |
+
if model.audio_encoder is not None and hasattr(model.audio_encoder, 'to'):
|
| 43 |
+
model.audio_encoder.to(args.device)
|
| 44 |
+
if model.vision_encoder is not None and hasattr(model.vision_encoder, 'to'):
|
| 45 |
+
model.vision_encoder.to(args.device)
|
| 46 |
from transformers import MimiModel
|
| 47 |
+
model.mimi_model = MimiModel.from_pretrained(args.mimi_dir).eval()
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_dir)
|
| 49 |
return model.half().eval().to(args.device), tokenizer
|
| 50 |
|
| 51 |
|
| 52 |
def eval_sample(model, tokenizer, args, idx, prompt, audio_inputs, output_name, pixel_values=None, history=None, audio_lens=None, ref_codes=None, spk_emb=None):
|
| 53 |
import soundfile as sf
|
| 54 |
+
try:
|
| 55 |
+
from pydub import AudioSegment
|
| 56 |
+
except ImportError:
|
| 57 |
+
AudioSegment = None
|
| 58 |
messages = (history or []) + [{"role": "user", "content": prompt}]
|
| 59 |
+
inputs_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 60 |
+
x = torch.tensor(tokenizer(inputs_text)['input_ids'], dtype=torch.long, device=args.device)[None, ...]
|
| 61 |
|
| 62 |
audio_frames = []
|
| 63 |
with torch.no_grad():
|
| 64 |
res_y = model.generate(x, tokenizer.eos_token_id, max_new_tokens=args.max_new_tokens,
|
| 65 |
temperature=args.temperature, top_p=args.top_p, stream=True,
|
| 66 |
+
return_audio_codes=True,
|
| 67 |
audio_inputs=audio_inputs, audio_lens=audio_lens, pixel_values=pixel_values,
|
| 68 |
ref_codes=ref_codes, spk_emb=spk_emb)
|
| 69 |
print('📒 [Thinker]: ', end='', flush=True)
|
|
|
|
| 92 |
output_path = os.path.join(args.output_dir, output_name)
|
| 93 |
wav_path = output_path.rsplit('.', 1)[0] + '.wav'
|
| 94 |
sf.write(wav_path, audio.squeeze().float().cpu().numpy(), 24000)
|
| 95 |
+
if AudioSegment is not None:
|
| 96 |
+
AudioSegment.from_wav(wav_path).export(output_path, format='mp3', bitrate='64k')
|
| 97 |
+
os.remove(wav_path)
|
| 98 |
+
else:
|
| 99 |
+
print(f'pydub not installed, keeping .wav: {wav_path}')
|
| 100 |
print(f'| Audio decoded to: {output_path}')
|
| 101 |
except Exception as e:
|
| 102 |
print(f'⚠️ 保存音频失败: {str(e)}')
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def main():
|
| 108 |
+
parser = argparse.ArgumentParser(description="Omni-O Chat")
|
| 109 |
+
parser.add_argument('--load_from', default='checkpoint/omni-o', type=str, help="模型权重目录(包含omni-o.pth)")
|
| 110 |
+
parser.add_argument('--weight', default='omni-o', type=str, help="权重文件名(不含.pth后缀)")
|
| 111 |
+
parser.add_argument('--tokenizer_dir', default='checkpoint/omni/native_hf', type=str, help="tokenizer目录")
|
| 112 |
+
parser.add_argument('--sensevoice_dir', default='checkpoint/sensevoice', type=str, help="SenseVoice目录")
|
| 113 |
+
parser.add_argument('--siglip_dir', default='checkpoint/siglip', type=str, help="SigLIP目录")
|
| 114 |
+
parser.add_argument('--mimi_dir', default='checkpoint/mimi', type=str, help="Mimi目录")
|
| 115 |
parser.add_argument('--hidden_size', default=768, type=int, help="隐藏层维度")
|
| 116 |
parser.add_argument('--num_hidden_layers', default=8, type=int, help="隐藏层数量")
|
| 117 |
parser.add_argument('--use_moe', default=0, type=int, choices=[0, 1], help="是否使用MoE架构")
|