Skip to content

Fix Qwen3 Embedding Float16 DType #663

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions backends/candle/src/models/qwen3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,13 @@ impl Qwen3Model {
let causal_mask = Tensor::from_slice(&mask, (seq_len, seq_len), device)?;
let causal_mask = causal_mask.expand(&[bs, dim, seq_len, seq_len])?;

let min_value = match self.dtype {
DType::F32 => f32::MIN,
_ => -65504.0, // f16 minimum value
};

let negatives =
Tensor::full(f32::MIN, attention_bias.shape(), device)?.to_dtype(self.dtype)?;
Tensor::full(min_value, attention_bias.shape(), device)?.to_dtype(self.dtype)?;
let zeros = Tensor::zeros_like(&attention_bias)?.to_dtype(self.dtype)?;

let causal_mask = causal_mask
Expand Down Expand Up @@ -514,7 +519,7 @@ impl Qwen3Model {

let attention_bias = if masking {
let attention_bias =
Tensor::from_vec(attention_bias, (batch_size, 1, 1, max_length), &self.device)?;
Tensor::from_vec(attention_bias, (batch_size, 1, 1, max_length), &self.device)?.to_dtype(self.dtype)?;
// Broadcast once instead of at every layer
let attention_bias = attention_bias
.broadcast_as((batch_size, self.num_attention_heads, max_length, max_length))?
Expand Down