Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use transformers with float16 #537

Open
chaowenguo1 opened this issue Mar 4, 2025 · 0 comments
Open

how to use transformers with float16 #537

chaowenguo1 opened this issue Mar 4, 2025 · 0 comments

Comments

@chaowenguo1
Copy link

chaowenguo1 commented Mar 4, 2025

import transformers, torch, builtins, numpy

processor = transformers.AutoProcessor.from_pretrained(' facebook/musicgen-stereo-melody-large', torch_dtype=torch.float16)
model = transformers.MusicgenMelodyForConditionalGeneration.from_pretrained('facebook/musicgen-stereo-melody-large ,torch_dtype=torch.float16).to('cuda')

result = []
for _ in builtins.range(2):
    inputs = processor(audio=result[-1] if result else None, sampling_rate=model.config.audio_encoder.sampling_rate, text='A grand and majestic symphony with soaring strings, powerful brass, and dynamic orchestration. Inspired by Beethoven and Tchaikovsky, featuring dramatic crescendos, delicate woodwind passages, and a triumphant finale. The mood is epic, emotional, and timeless', padding=True, return_tensors='pt').to('cuda')
    audio_values = model.generate(**inputs, max_new_tokens=1000)
    result += audio_values[0, 0].cpu().numpy(),

from IPython.display import Audio
Audio(numpy.concatenate(result), rate=model.config.audio_encoder.sampling_rate)

i always get

<ipython-input-12-348220656bb8> in <cell line: 0>()
      7 for _ in builtins.range(2):
      8     inputs = processor(audio=torch.from_numpy(result[-1]).to(dtype=torch.float32) if result else None, sampling_rate=model.config.audio_encoder.sampling_rate, text='A grand and majestic symphony with soaring strings, powerful brass, and dynamic orchestration. Inspired by Beethoven and Tchaikovsky, featuring dramatic crescendos, delicate woodwind passages, and a triumphant finale. The mood is epic, emotional, and timeless', padding=True, return_tensors='pt').to('cuda')
----> 9     audio_values = model.generate(**inputs, max_new_tokens=1000)
     10     result += audio_values[0, 0].cpu().numpy(),
     11 

5 frames
/usr/local/lib/python3.11/dist-packages/torch/nn/modules/linear.py in forward(self, input)
    123 
    124     def forward(self, input: Tensor) -> Tensor:
--> 125         return F.linear(input, self.weight, self.bias)
    126 
    127     def extra_repr(self) -> str:

RuntimeError: mat1 and mat2 must have the same dtype, but got Float and Half```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant