Google AI Overview Fix code
I was trying on using ComfyUI for some music generation on M1 macbook Air. The nodes gives error when the process ran after a while.
[code]RuntimeError: probability tensor contains either `inf`, `nan` or element < 0I asked LLM such as Gemini and Grok, their suggestions can't solve it. They said M1 architecture isn't the same as CUDA, they need [strong] for the computation. Therefore, running on macOS requires some conversion.
They suggested these methods:
Force ComfyUI to using on fp32, since [strong] isn't good at support bfloat16
Reduce temperature value below 0.2 for less steep of the gradient (Preset value is 0.85)
Use CPU instead of mps
I tried all and still no luck. Then I visited the code, I find value of temperature matters.
[code] if temperature > 0: cfg_logits = cfg_logits / temperature next_token = torch.multinomial(torch.softmax(cfg_logits, dim=-1), num_samples=1, generator=generator).squeeze(1) else: next_token = torch.argmax(cfg_logits, dim=-1)So, I tried to set temperature value to 0. It runs! And I can focus one it's the problem of [inlineCode]torch.multinomial(...).
Then I just put these few line and the error together to Google Search.
Their Overview AI bring me to new code:
[code] if temperature > 1e-6: # 1. Clean logits safe_logits = torch.nan_to_num(cfg_logits, nan=0.0, posinf=1e4, neginf=-1e4) # 2. Use float32 for softmax stability probs = torch.softmax(safe_logits / temperature, dim=-1) # 3. Multinomial next_token = torch.multinomial(probs, num_samples=1, generator=generator).squeeze(1) else: next_token = torch.argmax(cfg_logits, dim=-1)It works well and solved by problem! The code is going to normalize the probability and consider if the value of NaN generated from the last layer.
Perhaps, giving also to lines of code to LLM is required. It will save time to solely giving error and the platform in use to LLM.