NaNs may be produced due to numerical instability when calculating hmm_hidden_state_prob.
In the post below, I had a similar chat with you. My guess is that the function hmm_hidden_state_prob may need to employ log_sum_exp to bypass the numerical instability issue. I also encountered NaNs when using hmm_hidden_state_prob and the problem was gone when custom filtered probabilities was used, which employed log_sum_exp.