kehanlu commited on
Commit
d9c9969
·
verified ·
1 Parent(s): 9d45136

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. code/inference.py +9 -3
code/inference.py CHANGED
@@ -148,11 +148,17 @@ class SpeechLLMForInference:
148
  # SpeechLLM.encode_audio casts features to the encoder's own dtype
149
  batch["input_features"] = batch["input_features"].to(self.device)
150
 
 
 
 
 
151
  # passing inputs_embeds means `generate` returns only the new tokens --
152
  # there is no prompt prefix to slice off
153
- generated = self.model.generate(
154
- batch, self.tokenizer, max_new_tokens=max_new_tokens,
155
- do_sample=do_sample, **generation_kwargs)
 
 
156
  return [t.strip() for t in
157
  self.tokenizer.batch_decode(generated, skip_special_tokens=True)]
158
 
 
148
  # SpeechLLM.encode_audio casts features to the encoder's own dtype
149
  batch["input_features"] = batch["input_features"].to(self.device)
150
 
151
+ # The trainable tensors (connector, LoRA) stay in float32 while the frozen encoder and
152
+ # LLM run in `dtype`, exactly as in training -- so the forward pass has to happen under
153
+ # autocast, or layer_norm sees a float32 weight and a float16 activation and raises
154
+ # "expected scalar type Half but found Float".
155
  # passing inputs_embeds means `generate` returns only the new tokens --
156
  # there is no prompt prefix to slice off
157
+ with torch.autocast(self.device.split(":")[0], dtype=self.dtype,
158
+ enabled=not self.device.startswith("cpu")):
159
+ generated = self.model.generate(
160
+ batch, self.tokenizer, max_new_tokens=max_new_tokens,
161
+ do_sample=do_sample, **generation_kwargs)
162
  return [t.strip() for t in
163
  self.tokenizer.batch_decode(generated, skip_special_tokens=True)]
164