File size: 401,500 Bytes
c6a64b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
from __future__ import annotations


import io
import json
import os
import re
import sys
import threading
import traceback
from pathlib import Path
from typing import AbstractSet, Any, Dict, List, Optional, Sequence, Tuple, Union
import numpy as np
import torch
from transformers.utils import logging as hf_logging
import math
import random
import warnings
from dataclasses import dataclass

try:
    import librosa
except Exception:
    librosa = None
try:
    import resampy
except Exception:
    resampy = None


def _resample_if_needed(wav: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
    if orig_sr == target_sr:
        return wav.astype(np.float32, copy=False)
    if resampy is not None:
        return resampy.resample(wav.astype(np.float32), orig_sr, target_sr)
    if librosa is not None:
        return librosa.resample(
            y=wav.astype(np.float32), orig_sr=orig_sr, target_sr=target_sr
        )
    warnings.warn(
        "No resampler available; treating audio as target_sr without resampling. Install resampy or librosa.",
        RuntimeWarning,
    )
    return wav.astype(np.float32, copy=False)


class QWEN3VoxDataset:

    def __init__(
        self,
        dataset: Any,
        text_column: str = "text",
        audio_column: str = "audio",
        voice_prompts_column: Optional[str] = "voice_prompts",
    ) -> None:
        self.dataset = dataset
        self.text_column = text_column
        self.audio_column = audio_column
        self.voice_prompts_column = voice_prompts_column

    def __len__(self) -> int:
        return len(self.dataset)

    def __getitem__(self, idx: int) -> Dict[str, Any]:
        item = self.dataset[idx]
        data: Dict[str, Any] = {}
        data["text"] = item[self.text_column]
        data["audio"] = item[self.audio_column]
        user_provided_prompt = None
        if self.voice_prompts_column and self.voice_prompts_column in item:
            user_provided_prompt = item[self.voice_prompts_column]
        if user_provided_prompt:
            if not isinstance(user_provided_prompt, list):
                data["voice_prompts"] = [user_provided_prompt]
            else:
                data["voice_prompts"] = user_provided_prompt
        else:
            try:
                target_sr = 22050
                wav_array = _load_audio_to_24k(
                    item[self.audio_column], target_sr=target_sr
                )
                audio_len_seconds = len(wav_array) / target_sr
                min_len_sec = min(5.0, audio_len_seconds / 4.0)
                max_len_sec = min(15.0, audio_len_seconds / 2.0)
                if min_len_sec > max_len_sec:
                    min_len_sec = max_len_sec
                max_len_sec = min(max_len_sec, audio_len_seconds)
                if max_len_sec > 0.1:
                    prompt_len_sec = random.uniform(min_len_sec, max_len_sec)
                    prompt_len_samples = int(prompt_len_sec * target_sr)
                    max_start_sample = len(wav_array) - prompt_len_samples
                    start_sample = random.randint(0, max_start_sample)
                    prompt_crop = wav_array[
                        start_sample : start_sample + prompt_len_samples
                    ]
                    data["voice_prompts"] = [prompt_crop]
                else:
                    data["voice_prompts"] = None
            except Exception as e:
                warnings.warn(f"Could not create voice prompt for item {idx }: {e }")
                data["voice_prompts"] = None
        return data


def _apply_silence_with_crossfade(
    wav: np.ndarray,
    *,
    sample_rate: int,
    pre_silence_sec: float = 0.25,
    pre_crossfade_sec: float = 0.25,
    post_crossfade_sec: float = 0.25,
    post_silence_sec: float = 0.75,
) -> np.ndarray:
    wav = np.asarray(wav, dtype=np.float32).reshape(-1)
    start_sil_samples = int(round(pre_silence_sec * sample_rate))
    end_sil_samples = int(round(post_silence_sec * sample_rate))
    pre_crossfade_samples = int(round(pre_crossfade_sec * sample_rate))
    post_crossfade_samples = int(round(post_crossfade_sec * sample_rate))
    total_len = wav.shape[0]
    if total_len == 0:
        pieces: List[np.ndarray] = []
        if start_sil_samples > 0:
            pieces.append(np.zeros(start_sil_samples, dtype=np.float32))
        if end_sil_samples > 0:
            pieces.append(np.zeros(end_sil_samples, dtype=np.float32))
        return np.concatenate(pieces) if pieces else wav
    start_len = min(pre_crossfade_samples, total_len)
    remaining_after_start = max(total_len - start_len, 0)
    end_len = min(post_crossfade_samples, remaining_after_start)
    middle_end_idx = total_len - end_len
    start_segment = wav[:start_len]
    middle_segment = wav[start_len:middle_end_idx]
    end_segment = wav[middle_end_idx:]

    def _linear_fade(num_samples: int, start: float, end: float) -> np.ndarray:
        if num_samples <= 0:
            return np.zeros((0,), dtype=np.float32)
        return np.linspace(start, end, num_samples, endpoint=True, dtype=np.float32)

    start_crossfade = start_segment * _linear_fade(start_len, 0.0, 1.0)
    end_crossfade = end_segment * _linear_fade(end_segment.shape[0], 1.0, 0.0)
    pieces: List[np.ndarray] = []
    if start_sil_samples > 0:
        pieces.append(np.zeros(start_sil_samples, dtype=np.float32))
    if start_crossfade.size > 0:
        pieces.append(start_crossfade.astype(np.float32, copy=False))
    if middle_segment.size > 0:
        pieces.append(middle_segment.astype(np.float32, copy=False))
    if end_crossfade.size > 0:
        pieces.append(end_crossfade.astype(np.float32, copy=False))
    if end_sil_samples > 0:
        pieces.append(np.zeros(end_sil_samples, dtype=np.float32))
    return np.concatenate(pieces)


def _load_audio_to_24k(
    audio: Union[str, np.ndarray, torch.Tensor, Dict[str, Any]],
    *,
    target_sr: int = 22050,
    augment_with_silence: bool = False,
) -> np.ndarray:
    if isinstance(audio, np.ndarray):
        wav_out = audio.astype(np.float32)
    elif isinstance(audio, torch.Tensor):
        wav_out = audio.detach().cpu().float().numpy()
    elif isinstance(audio, str):
        if librosa is None:
            raise RuntimeError(
                "librosa is required to load audio file paths. Please pip install librosa."
            )
        wav, sr = librosa.load(audio, sr=None, mono=True)
        wav_out = _resample_if_needed(wav, int(sr), target_sr)
    elif isinstance(audio, dict) and "array" in audio and ("sampling_rate" in audio):
        arr = np.asarray(audio["array"], dtype=np.float32)
        sr = int(audio["sampling_rate"])
        wav_out = _resample_if_needed(arr, sr, target_sr)
    else:
        raise ValueError(f"Unsupported audio type: {type (audio )}")
    wav_out = np.asarray(wav_out, dtype=np.float32)
    if augment_with_silence:
        wav_out = _apply_silence_with_crossfade(wav_out, sample_rate=target_sr)
    return wav_out


@dataclass
class QWEN3VoxCollator:
    processor: Any
    max_length: Optional[int] = None
    speech_compress_ratio: int = 3200
    semantic_vae_dim: int = 128
    compute_semantics: bool = False
    debug_checks: bool = False
    text_field: str = "text"
    audio_field: str = "audio"
    voice_prompts_field: str = "voice_prompts"
    voice_prompt_drop_rate: float = 0.0

    def __call__(self, features: Sequence[Dict[str, Any]]) -> Dict[str, Any]:
        batch_size = len(features)
        sample_input_ids: List[List[int]] = []
        sample_attention_masks: List[List[int]] = []
        sample_acoustic_input_masks: List[List[bool]] = []
        sample_acoustic_loss_masks: List[List[bool]] = []
        all_speech_waveforms: List[np.ndarray] = []
        all_speech_latent_lengths: List[int] = []
        per_segment_is_target: List[bool] = []
        for ex in features:
            text: str = ex.get(self.text_field, "")
            voice_prompts: Optional[List[Union[str, np.ndarray, torch.Tensor]]] = (
                ex.get(self.voice_prompts_field)
            )
            target_audio: Union[str, np.ndarray, torch.Tensor, Dict[str, Any]] = ex.get(
                self.audio_field
            )
            _drop_rate = self.voice_prompt_drop_rate
            if _drop_rate < 0.0:
                _drop_rate = 0.0
            elif _drop_rate > 1.0:
                _drop_rate = 1.0
            proc = self.processor(
                text=[text],
                voice_samples=(
                    [voice_prompts]
                    if voice_prompts is not None and random.random() >= _drop_rate
                    else None
                ),
                padding=False,
                truncation=False,
                max_length=self.max_length,
                return_tensors="pt",
            )
            ids = proc["input_ids"][0].tolist()
            attn = proc.get("attention_mask", torch.ones_like(proc["input_ids"]))[
                0
            ].tolist()
            speech_input_mask = proc.get("speech_input_mask")
            if speech_input_mask is None:
                speech_input_mask = torch.zeros_like(
                    proc["input_ids"], dtype=torch.bool
                )
            speech_input_mask_list = speech_input_mask[0].tolist()
            wav_target = _load_audio_to_24k(
                target_audio, target_sr=22050, augment_with_silence=True
            )
            target_latent_len = None
            try:
                acoustic_tok = getattr(self.processor, "acoustic_tokenizer", None)
                if acoustic_tok is not None and hasattr(acoustic_tok, "encode"):
                    enc_out = acoustic_tok.encode(wav_target)
                    T = None
                    try:
                        if (
                            hasattr(enc_out, "shape")
                            and len(getattr(enc_out, "shape", [])) >= 1
                        ):
                            T = int(enc_out.shape[0])
                        else:
                            cand = enc_out
                            for _ in range(2):
                                if isinstance(cand, (list, tuple)) and len(cand) > 0:
                                    cand = cand[0]
                            if (
                                hasattr(cand, "shape")
                                and len(getattr(cand, "shape", [])) >= 1
                            ):
                                T = int(cand.shape[0])
                    except Exception:
                        T = None
                    if T is not None and T > 0:
                        target_latent_len = T
            except Exception:
                target_latent_len = None
            if target_latent_len is None:
                target_latent_len = max(
                    1,
                    int(math.ceil(len(wav_target) / float(self.speech_compress_ratio))),
                )
            speech_diff_id = self.processor.tokenizer.speech_diffusion_id
            target_placeholders = [speech_diff_id] * target_latent_len
            ids_extended = ids + target_placeholders
            attn_extended = attn + [1] * target_latent_len
            acoustic_input_mask = speech_input_mask_list + [True] * target_latent_len
            acoustic_loss_mask = [False] * len(speech_input_mask_list) + [
                True
            ] * target_latent_len
            speech_end_id = self.processor.tokenizer.speech_end_id
            ids_extended.append(speech_end_id)
            attn_extended.append(1)
            acoustic_input_mask.append(False)
            acoustic_loss_mask.append(False)
            eos_token_id = getattr(self.processor.tokenizer, "eos_id", None)
            if eos_token_id is None:
                eos_token_id = getattr(self.processor.tokenizer, "eos_token_id", None)
            if eos_token_id is not None and eos_token_id >= 0:
                ids_extended.append(eos_token_id)
                attn_extended.append(1)
                acoustic_input_mask.append(False)
                acoustic_loss_mask.append(False)
            if self.max_length is not None and len(ids_extended) > self.max_length:
                cut = len(ids_extended) - int(self.max_length)
                leading_non_acoustic = 0
                for v in acoustic_input_mask:
                    if v:
                        break
                    leading_non_acoustic += 1
                if cut > leading_non_acoustic:
                    raise ValueError(
                        f"--max_length={self .max_length } would truncate into acoustic tokens. Needed cut={cut }, but only {leading_non_acoustic } leading non-acoustic tokens available. Increase max_length or shorten text/voice-prompt preamble."
                    )
                ids_extended = ids_extended[cut:]
                attn_extended = attn_extended[cut:]
                acoustic_input_mask = acoustic_input_mask[cut:]
                acoustic_loss_mask = acoustic_loss_mask[cut:]
            sample_input_ids.append(ids_extended)
            sample_attention_masks.append(attn_extended)
            sample_acoustic_input_masks.append(acoustic_input_mask)
            sample_acoustic_loss_masks.append(acoustic_loss_mask)
            voice_speeches = []
            voice_latent_lengths = []
            if proc.get("speech_tensors") is not None:
                voice_np = proc["speech_tensors"].cpu().numpy()
                voice_masks = proc["speech_masks"].cpu().numpy().astype(bool)
                for seg_idx in range(voice_np.shape[0]):
                    voice_speeches.append(voice_np[seg_idx])
                    voice_latent_lengths.append(int(voice_masks[seg_idx].sum()))
            all_speech_waveforms.extend(voice_speeches)
            all_speech_latent_lengths.extend(voice_latent_lengths)
            per_segment_is_target.extend([False] * len(voice_speeches))
            all_speech_waveforms.append(wav_target)
            all_speech_latent_lengths.append(target_latent_len)
            per_segment_is_target.append(True)
        max_seq_len = max((len(x) for x in sample_input_ids))
        padded_input_ids = []
        padded_attention_masks = []
        padded_acoustic_input_masks = []
        padded_acoustic_loss_masks = []
        tok = self.processor.tokenizer
        pad_token_id = getattr(tok, "pad_token_id", None)
        if pad_token_id is None or pad_token_id < 0:
            pad_token_id = getattr(tok, "eos_token_id", None)
            if pad_token_id is None or pad_token_id < 0:
                raise ValueError(
                    "Tokenizer has no pad_token_id or eos_token_id; please set one or pass a valid pad id."
                )
        for ids, attn, ain_mask, aloss_mask in zip(
            sample_input_ids,
            sample_attention_masks,
            sample_acoustic_input_masks,
            sample_acoustic_loss_masks,
        ):
            pad_len = max_seq_len - len(ids)
            padded_input_ids.append(ids + [pad_token_id] * pad_len)
            padded_attention_masks.append(attn + [0] * pad_len)
            padded_acoustic_input_masks.append(ain_mask + [False] * pad_len)
            padded_acoustic_loss_masks.append(aloss_mask + [False] * pad_len)
        input_ids_tensor = torch.tensor(padded_input_ids, dtype=torch.long)
        attention_mask_tensor = torch.tensor(padded_attention_masks, dtype=torch.long)
        acoustic_input_mask_tensor = torch.tensor(
            padded_acoustic_input_masks, dtype=torch.bool
        )
        acoustic_loss_mask_tensor = torch.tensor(
            padded_acoustic_loss_masks, dtype=torch.bool
        )
        if all_speech_waveforms:
            max_wave_len = max((w.shape[0] for w in all_speech_waveforms))
            padded_speeches = np.zeros(
                (len(all_speech_waveforms), max_wave_len), dtype=np.float32
            )
            for i, w in enumerate(all_speech_waveforms):
                L = w.shape[0]
                padded_speeches[i, :L] = w
            max_latent_len = (
                max(all_speech_latent_lengths) if all_speech_latent_lengths else 1
            )
            speech_masks_np = np.zeros(
                (len(all_speech_waveforms), max_latent_len), dtype=np.bool_
            )
            for i, L_lat in enumerate(all_speech_latent_lengths):
                speech_masks_np[i, :L_lat] = True
            speech_tensors_tensor = torch.tensor(padded_speeches, dtype=torch.float32)
            speech_masks_tensor = torch.tensor(speech_masks_np, dtype=torch.bool)
            speeches_loss_input_np = np.zeros_like(speech_masks_np, dtype=np.bool_)
            for i, is_target in enumerate(per_segment_is_target):
                if is_target:
                    speeches_loss_input_np[i] = speech_masks_np[i]
            speeches_loss_input_tensor = torch.tensor(
                speeches_loss_input_np, dtype=torch.bool
            )
            if (
                self.compute_semantics
                and hasattr(self.processor, "semantic_tokenizer")
                and (self.processor.semantic_tokenizer is not None)
            ):
                sem_feats: List[np.ndarray] = []
                for w in all_speech_waveforms:
                    try:
                        sem = self.processor.semantic_tokenizer.encode(w)
                        sem = np.asarray(sem, dtype=np.float32)
                    except Exception:
                        sem = np.zeros((0, self.semantic_vae_dim), dtype=np.float32)
                    if sem.ndim != 2:
                        raise RuntimeError(
                            f"Semantic tokenizer returned unexpected shape {sem .shape }. Expect [T, D]."
                        )
                    L = sem.shape[0]
                    D = sem.shape[1]
                    if D != self.semantic_vae_dim:
                        if D < self.semantic_vae_dim:
                            pad_d = np.zeros(
                                (L, self.semantic_vae_dim - D), dtype=np.float32
                            )
                            sem = np.concatenate([sem, pad_d], axis=1)
                        else:
                            sem = sem[:, : self.semantic_vae_dim]
                    if L < max_latent_len:
                        pad = np.zeros(
                            (max_latent_len - L, self.semantic_vae_dim),
                            dtype=np.float32,
                        )
                        sem = np.concatenate([sem, pad], axis=0)
                    elif L > max_latent_len:
                        sem = sem[:max_latent_len]
                    sem_feats.append(sem.astype(np.float32))
                speech_semantic_tensors = torch.tensor(
                    np.stack(sem_feats, axis=0), dtype=torch.float32
                )
            else:
                raise RuntimeError(
                    "Semantic features are required but could not be computed. Ensure processor.semantic_tokenizer is available or precompute and provide features."
                )
        else:
            speech_tensors_tensor = None
            speech_masks_tensor = None
            speeches_loss_input_tensor = None
            speech_semantic_tensors = None
        if self.debug_checks:
            assert (input_ids_tensor >= 0).all(), "input_ids contains negative indices"
            if speech_tensors_tensor is not None:
                assert (
                    speech_tensors_tensor.dim() == 2
                ), "Expected speech_tensors 2D [segments, samples]"
        return {
            "input_ids": input_ids_tensor,
            "attention_mask": attention_mask_tensor,
            "speech_tensors": speech_tensors_tensor,
            "speech_masks": speech_masks_tensor,
            "speech_semantic_tensors": speech_semantic_tensors,
            "acoustic_input_mask": acoustic_input_mask_tensor,
            "acoustic_loss_mask": acoustic_loss_mask_tensor,
            "speeches_loss_input": speeches_loss_input_tensor,
        }


' QWEN3Vox_AcousticTokenizer model configuration'
from typing import Dict, List, Optional, Tuple
from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging
from transformers.models.qwen2.configuration_qwen2 import Qwen2Config

logger = logging.get_logger(__name__)


class QWEN3VoxAcousticTokenizerConfig(PretrainedConfig):
    model_type = 'vibevoice_acoustic_tokenizer'

    def __init__(
        self,
        channels: int = 1,
        corpus_normalize: float = 0.0,
        causal: bool = True,
        vae_dim: int = 64,
        fix_std: float = 0.5,
        std_dist_type: str = "gaussian",
        mixer_layer: str = "depthwise_conv",
        conv_norm: str = "none",
        pad_mode: str = "constant",
        disable_last_norm: bool = True,
        layernorm: str = "RMSNorm",
        layernorm_eps: float = 1e-05,
        layernorm_elementwise_affine: bool = True,
        conv_bias: bool = True,
        layer_scale_init_value: float = 1e-06,
        weight_init_value: float = 0.01,
        encoder_n_filters: int = 32,
        encoder_ratios: Optional[List[int]] = [8, 5, 5, 4, 2, 2],
        encoder_depths: str = "3-3-3-3-3-3-8",
        decoder_n_filters: int = 32,
        decoder_ratios: Optional[List[int]] = None,
        decoder_depths: Optional[str] = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.channels = channels
        self.corpus_normalize = corpus_normalize
        self.causal = causal
        self.vae_dim = vae_dim
        self.fix_std = fix_std
        self.std_dist_type = std_dist_type
        self.conv_norm = conv_norm
        self.pad_mode = pad_mode
        self.layernorm_eps = layernorm_eps
        self.disable_last_norm = disable_last_norm
        self.layernorm = layernorm
        self.layernorm_elementwise_affine = layernorm_elementwise_affine
        self.conv_bias = conv_bias
        self.layer_scale_init_value = layer_scale_init_value
        self.weight_init_value = weight_init_value
        self.mixer_layer = mixer_layer
        self.encoder_n_filters = encoder_n_filters
        self.encoder_ratios = encoder_ratios
        self.encoder_depths = encoder_depths
        self.decoder_ratios = (
            decoder_ratios if decoder_ratios is not None else encoder_ratios
        )
        self.decoder_n_filters = decoder_n_filters
        self.decoder_depths = decoder_depths


class QWEN3VoxSemanticTokenizerConfig(PretrainedConfig):
    model_type = 'vibevoice_semantic_tokenizer'

    def __init__(
        self,
        channels: int = 1,
        corpus_normalize: float = 0.0,
        causal: bool = True,
        vae_dim: int = 64,
        fix_std: float = 0,
        std_dist_type: str = "none",
        mixer_layer: str = "depthwise_conv",
        conv_norm: str = "none",
        pad_mode: str = "constant",
        disable_last_norm: bool = True,
        layernorm: str = "RMSNorm",
        layernorm_eps: float = 1e-05,
        layernorm_elementwise_affine: bool = True,
        conv_bias: bool = True,
        layer_scale_init_value: float = 1e-06,
        weight_init_value: float = 0.01,
        encoder_n_filters: int = 32,
        encoder_ratios: Optional[List[int]] = [8, 5, 5, 4, 2, 2],
        encoder_depths: str = "3-3-3-3-3-3-8",
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.channels = channels
        self.corpus_normalize = corpus_normalize
        self.causal = causal
        self.vae_dim = vae_dim
        self.fix_std = fix_std
        self.std_dist_type = std_dist_type
        self.conv_norm = conv_norm
        self.pad_mode = pad_mode
        self.layernorm_eps = layernorm_eps
        self.disable_last_norm = disable_last_norm
        self.layernorm = layernorm
        self.layernorm_elementwise_affine = layernorm_elementwise_affine
        self.conv_bias = conv_bias
        self.layer_scale_init_value = layer_scale_init_value
        self.weight_init_value = weight_init_value
        self.mixer_layer = mixer_layer
        self.encoder_n_filters = encoder_n_filters
        self.encoder_ratios = encoder_ratios
        self.encoder_depths = encoder_depths


class QWEN3VoxDiffusionHeadConfig(PretrainedConfig):
    model_type = 'vibevoice_diffusion_head'

    def __init__(
        self,
        hidden_size=768,
        head_layers=4,
        head_ffn_ratio=3.0,
        rms_norm_eps=1e-05,
        latent_size=64,
        speech_vae_dim=None,
        prediction_type="v_prediction",
        diffusion_type="ddpm",
        ddpm_num_steps=1000,
        ddpm_num_inference_steps=30,
        ddpm_beta_schedule="cosine",
        ddpm_batch_mul=4,
        **kwargs,
    ):
        self.hidden_size = hidden_size
        self.head_layers = head_layers
        self.head_ffn_ratio = head_ffn_ratio
        self.rms_norm_eps = rms_norm_eps
        self.latent_size = latent_size
        self.speech_vae_dim = speech_vae_dim
        self.prediction_type = prediction_type
        self.diffusion_type = diffusion_type
        self.ddpm_num_steps = ddpm_num_steps
        self.ddpm_num_inference_steps = ddpm_num_inference_steps
        self.ddpm_beta_schedule = ddpm_beta_schedule
        self.ddpm_batch_mul = ddpm_batch_mul
        super().__init__(**kwargs)


class QWEN3VoxConfig(PretrainedConfig):
    model_type = 'vibevoice'
    is_composition = True
    sub_configs = {
        "acoustic_tokenizer_config": QWEN3VoxAcousticTokenizerConfig,
        "semantic_tokenizer_config": QWEN3VoxSemanticTokenizerConfig,
        "decoder_config": Qwen2Config,
        "diffusion_head_config": QWEN3VoxDiffusionHeadConfig,
    }
    base_model_tp_plan = {
        "layers.*.self_attn.q_proj": "colwise",
        "layers.*.self_attn.k_proj": "colwise",
        "layers.*.self_attn.v_proj": "colwise",
        "layers.*.self_attn.o_proj": "rowwise",
        "layers.*.mlp.gate_proj": "colwise",
        "layers.*.mlp.up_proj": "colwise",
        "layers.*.mlp.down_proj": "rowwise",
    }

    def __init__(
        self,
        acoustic_tokenizer_config=None,
        semantic_tokenizer_config=None,
        decoder_config=None,
        diffusion_head_config=None,
        **kwargs,
    ):
        kwargs["_attn_implementation_autoset"] = False
        if acoustic_tokenizer_config is None:
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ]()
        elif isinstance(acoustic_tokenizer_config, dict):
            acoustic_tokenizer_config["model_type"] = 'vibevoice_acoustic_tokenizer'
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ](**acoustic_tokenizer_config)
        elif isinstance(acoustic_tokenizer_config, QWEN3VoxAcousticTokenizerConfig):
            self.acoustic_tokenizer_config = acoustic_tokenizer_config
        if semantic_tokenizer_config is None:
            self.semantic_tokenizer_config = self.sub_configs[
                "semantic_tokenizer_config"
            ]()
        elif isinstance(semantic_tokenizer_config, dict):
            semantic_tokenizer_config["model_type"] = 'vibevoice_semantic_tokenizer'
            self.semantic_tokenizer_config = self.sub_configs[
                "semantic_tokenizer_config"
            ](**semantic_tokenizer_config)
        elif isinstance(semantic_tokenizer_config, QWEN3VoxSemanticTokenizerConfig):
            self.semantic_tokenizer_config = semantic_tokenizer_config
        if decoder_config is None:
            self.decoder_config = self.sub_configs["decoder_config"]()
        elif isinstance(decoder_config, dict):
            if decoder_config.get("model_type", "") == "qwen2":
                self.decoder_config = Qwen2Config(**decoder_config)
            else:
                raise ValueError(
                    f"Unsupported decoder model type: {decoder_config .get ('model_type','')}"
                )
        elif isinstance(decoder_config, (Qwen2Config,)):
            self.decoder_config = decoder_config
        if diffusion_head_config is None:
            self.diffusion_head_config = self.sub_configs["diffusion_head_config"]()
        elif isinstance(diffusion_head_config, dict):
            diffusion_head_config["model_type"] = 'vibevoice_diffusion_head'
            self.diffusion_head_config = self.sub_configs["diffusion_head_config"](
                **diffusion_head_config
            )
        elif isinstance(diffusion_head_config, QWEN3VoxDiffusionHeadConfig):
            self.diffusion_head_config = diffusion_head_config
        self.acoustic_vae_dim = getattr(self.acoustic_tokenizer_config, "vae_dim", 64)
        self.semantic_vae_dim = getattr(self.semantic_tokenizer_config, "vae_dim", 128)
        super().__init__(**kwargs)


class QWEN3VoxASRConfig(PretrainedConfig):
    model_type = 'vibevoice'
    is_composition = True
    sub_configs = {
        "acoustic_tokenizer_config": QWEN3VoxAcousticTokenizerConfig,
        "semantic_tokenizer_config": QWEN3VoxSemanticTokenizerConfig,
        "decoder_config": Qwen2Config,
    }
    base_model_tp_plan = {
        "layers.*.self_attn.q_proj": "colwise",
        "layers.*.self_attn.k_proj": "colwise",
        "layers.*.self_attn.v_proj": "colwise",
        "layers.*.self_attn.o_proj": "rowwise",
        "layers.*.mlp.gate_proj": "colwise",
        "layers.*.mlp.up_proj": "colwise",
        "layers.*.mlp.down_proj": "rowwise",
    }

    def __init__(
        self,
        acoustic_tokenizer_config=None,
        semantic_tokenizer_config=None,
        decoder_config=None,
        **kwargs,
    ):
        kwargs["_attn_implementation_autoset"] = False
        if acoustic_tokenizer_config is None:
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ]()
        elif isinstance(acoustic_tokenizer_config, dict):
            acoustic_tokenizer_config["model_type"] = 'vibevoice_acoustic_tokenizer'
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ](**acoustic_tokenizer_config)
        elif isinstance(acoustic_tokenizer_config, QWEN3VoxAcousticTokenizerConfig):
            self.acoustic_tokenizer_config = acoustic_tokenizer_config
        if semantic_tokenizer_config is None:
            self.semantic_tokenizer_config = self.sub_configs[
                "semantic_tokenizer_config"
            ]()
        elif isinstance(semantic_tokenizer_config, dict):
            semantic_tokenizer_config["model_type"] = 'vibevoice_semantic_tokenizer'
            self.semantic_tokenizer_config = self.sub_configs[
                "semantic_tokenizer_config"
            ](**semantic_tokenizer_config)
        elif isinstance(semantic_tokenizer_config, QWEN3VoxSemanticTokenizerConfig):
            self.semantic_tokenizer_config = semantic_tokenizer_config
        if decoder_config is None:
            self.decoder_config = self.sub_configs["decoder_config"]()
        elif isinstance(decoder_config, dict):
            if decoder_config.get("model_type", "") == "qwen2":
                self.decoder_config = Qwen2Config(**decoder_config)
            else:
                raise ValueError(
                    f"Unsupported decoder model type: {decoder_config .get ('model_type','')}"
                )
        elif isinstance(decoder_config, Qwen2Config):
            self.decoder_config = decoder_config
        self.acoustic_vae_dim = getattr(self.acoustic_tokenizer_config, "vae_dim", 64)
        self.semantic_vae_dim = getattr(self.semantic_tokenizer_config, "vae_dim", 128)
        super().__init__(**kwargs)

    def get_text_config(self, decoder: bool = False):
        return self.decoder_config

    @property
    def vocab_size(self):
        return self.decoder_config.vocab_size

    @property
    def num_attention_heads(self):
        return self.decoder_config.num_attention_heads

    @property
    def num_key_value_heads(self):
        return self.decoder_config.num_key_value_heads

    @property
    def hidden_size(self):
        return self.decoder_config.hidden_size

    @property
    def num_hidden_layers(self):
        return self.decoder_config.num_hidden_layers

    @property
    def head_dim(self):
        return getattr(
            self.decoder_config,
            "head_dim",
            self.hidden_size // self.num_attention_heads,
        )


__all__ = [
    'QWEN3VoxAcousticTokenizerConfig',
    'QWEN3VoxSemanticTokenizerConfig',
    'QWEN3VoxDiffusionHeadConfig',
    'QWEN3VoxConfig',
    'QWEN3VoxASRConfig',
]
import torch
import asyncio
from queue import Queue
from typing import TYPE_CHECKING, Optional
from transformers.generation import BaseStreamer


class AudioStreamer(BaseStreamer):

    def __init__(
        self,
        batch_size: int,
        stop_signal: Optional[any] = None,
        timeout: Optional[float] = None,
    ):
        self.batch_size = batch_size
        self.stop_signal = stop_signal
        self.timeout = timeout
        self.audio_queues = [Queue() for _ in range(batch_size)]
        self.finished_flags = [False for _ in range(batch_size)]
        self.sample_indices_map = {}

    def put(self, audio_chunks: torch.Tensor, sample_indices: torch.Tensor):
        for i, sample_idx in enumerate(sample_indices):
            idx = sample_idx.item()
            if idx < self.batch_size and (not self.finished_flags[idx]):
                audio_chunk = audio_chunks[i].detach().cpu()
                self.audio_queues[idx].put(audio_chunk, timeout=self.timeout)

    def end(self, sample_indices: Optional[torch.Tensor] = None):
        if sample_indices is None:
            for idx in range(self.batch_size):
                if not self.finished_flags[idx]:
                    self.audio_queues[idx].put(self.stop_signal, timeout=self.timeout)
                    self.finished_flags[idx] = True
        else:
            for sample_idx in sample_indices:
                idx = sample_idx.item() if torch.is_tensor(sample_idx) else sample_idx
                if idx < self.batch_size and (not self.finished_flags[idx]):
                    self.audio_queues[idx].put(self.stop_signal, timeout=self.timeout)
                    self.finished_flags[idx] = True

    def __iter__(self):
        return AudioBatchIterator(self)

    def get_stream(self, sample_idx: int):
        if sample_idx >= self.batch_size:
            raise ValueError(
                f"Sample index {sample_idx } exceeds batch size {self .batch_size }"
            )
        return AudioSampleIterator(self, sample_idx)


class AudioSampleIterator:

    def __init__(self, streamer: AudioStreamer, sample_idx: int):
        self.streamer = streamer
        self.sample_idx = sample_idx

    def __iter__(self):
        return self

    def __next__(self):
        value = self.streamer.audio_queues[self.sample_idx].get(
            timeout=self.streamer.timeout
        )
        if value == self.streamer.stop_signal:
            raise StopIteration()
        return value


class AudioBatchIterator:

    def __init__(self, streamer: AudioStreamer):
        self.streamer = streamer
        self.active_samples = set(range(streamer.batch_size))

    def __iter__(self):
        return self

    def __next__(self):
        if not self.active_samples:
            raise StopIteration()
        batch_chunks = {}
        samples_to_remove = set()
        for idx in self.active_samples:
            try:
                value = self.streamer.audio_queues[idx].get(block=False)
                if value == self.streamer.stop_signal:
                    samples_to_remove.add(idx)
                else:
                    batch_chunks[idx] = value
            except:
                pass
        self.active_samples -= samples_to_remove
        if batch_chunks:
            return batch_chunks
        elif self.active_samples:
            import time

            time.sleep(0.01)
            return self.__next__()
        else:
            raise StopIteration()


class AsyncAudioStreamer(AudioStreamer):

    def __init__(
        self,
        batch_size: int,
        stop_signal: Optional[any] = None,
        timeout: Optional[float] = None,
    ):
        super().__init__(batch_size, stop_signal, timeout)
        self.audio_queues = [asyncio.Queue() for _ in range(batch_size)]
        self.loop = asyncio.get_running_loop()

    def put(self, audio_chunks: torch.Tensor, sample_indices: torch.Tensor):
        for i, sample_idx in enumerate(sample_indices):
            idx = sample_idx.item()
            if idx < self.batch_size and (not self.finished_flags[idx]):
                audio_chunk = audio_chunks[i].detach().cpu()
                self.loop.call_soon_threadsafe(
                    self.audio_queues[idx].put_nowait, audio_chunk
                )

    def end(self, sample_indices: Optional[torch.Tensor] = None):
        if sample_indices is None:
            indices_to_end = range(self.batch_size)
        else:
            indices_to_end = [
                s.item() if torch.is_tensor(s) else s for s in sample_indices
            ]
        for idx in indices_to_end:
            if idx < self.batch_size and (not self.finished_flags[idx]):
                self.loop.call_soon_threadsafe(
                    self.audio_queues[idx].put_nowait, self.stop_signal
                )
                self.finished_flags[idx] = True

    async def get_stream(self, sample_idx: int):
        if sample_idx >= self.batch_size:
            raise ValueError(
                f"Sample index {sample_idx } exceeds batch size {self .batch_size }"
            )
        while True:
            value = await self.audio_queues[sample_idx].get()
            if value == self.stop_signal:
                break
            yield value

    def __aiter__(self):
        return AsyncAudioBatchIterator(self)


class AsyncAudioBatchIterator:

    def __init__(self, streamer: AsyncAudioStreamer):
        self.streamer = streamer
        self.active_samples = set(range(streamer.batch_size))

    def __aiter__(self):
        return self

    async def __anext__(self):
        if not self.active_samples:
            raise StopAsyncIteration()
        batch_chunks = {}
        samples_to_remove = set()
        tasks = {
            idx: asyncio.create_task(self._get_chunk(idx))
            for idx in self.active_samples
        }
        done, pending = await asyncio.wait(
            tasks.values(),
            return_when=asyncio.FIRST_COMPLETED,
            timeout=self.streamer.timeout,
        )
        for task in pending:
            task.cancel()
        for idx, task in tasks.items():
            if task in done:
                try:
                    value = await task
                    if value == self.streamer.stop_signal:
                        samples_to_remove.add(idx)
                    else:
                        batch_chunks[idx] = value
                except asyncio.CancelledError:
                    pass
        self.active_samples -= samples_to_remove
        if batch_chunks:
            return batch_chunks
        elif self.active_samples:
            return await self.__anext__()
        else:
            raise StopAsyncIteration()

    async def _get_chunk(self, idx):
        return await self.streamer.audio_queues[idx].get()


'Tokenization classes for QWEN3Vox.'
from typing import List, Optional, Union
from transformers.utils import logging
from transformers.models.qwen2.tokenization_qwen2 import Qwen2Tokenizer
from transformers.models.qwen2.tokenization_qwen2_fast import Qwen2TokenizerFast

logger = logging.get_logger(__name__)


class QWEN3VoxTextTokenizer(Qwen2Tokenizer):
    model_input_names = ["input_ids", "attention_mask"]

    def __init__(
        self,
        vocab_file,
        merges_file,
        errors="replace",
        unk_token="<|endoftext|>",
        bos_token=None,
        eos_token="<|endoftext|>",
        pad_token="<|endoftext|>",
        add_prefix_space=False,
        add_special_tokens=True,
        **kwargs,
    ):
        super().__init__(
            vocab_file=vocab_file,
            merges_file=merges_file,
            errors=errors,
            unk_token=unk_token,
            bos_token=bos_token,
            eos_token=eos_token,
            pad_token=pad_token,
            add_prefix_space=add_prefix_space,
            add_special_tokens=add_special_tokens,
            **kwargs,
        )
        self._add_q3_sp_tok()

    def _add_q3_sp_tok(self):
        special_tokens = {
            "additional_special_tokens": [
                "<|vision_start|>",
                "<|vision_end|>",
                "<|vision_pad|>",
            ]
        }
        num_added = self.add_special_tokens(special_tokens)
        self._speech_start_id = self.convert_tokens_to_ids("<|vision_start|>")
        self._speech_end_id = self.convert_tokens_to_ids("<|vision_end|>")
        self._speech_diffusion_id = self.convert_tokens_to_ids("<|vision_pad|>")
        self._eos_id = self.convert_tokens_to_ids("<|endoftext|>")
        return num_added

    @property
    def eos_id(self) -> int:
        return self._eos_id

    @property
    def speech_start_id(self) -> int:
        return self._speech_start_id

    @property
    def speech_end_id(self) -> int:
        return self._speech_end_id

    @property
    def speech_diffusion_id(self) -> int:
        return self._speech_diffusion_id

    @property
    def pad_id(self) -> int:
        return -100


class QWEN3VoxTextTokenizerFast(Qwen2TokenizerFast):
    model_input_names = ["input_ids", "attention_mask"]

    def __init__(
        self,
        vocab_file=None,
        merges_file=None,
        tokenizer_file=None,
        unk_token="<|endoftext|>",
        bos_token=None,
        eos_token="<|endoftext|>",
        pad_token="<|endoftext|>",
        add_prefix_space=False,
        **kwargs,
    ):
        super().__init__(
            vocab_file=vocab_file,
            merges_file=merges_file,
            tokenizer_file=tokenizer_file,
            unk_token=unk_token,
            bos_token=bos_token,
            eos_token=eos_token,
            pad_token=pad_token,
            add_prefix_space=add_prefix_space,
            **kwargs,
        )
        self._add_q3_sp_tok()

    def _add_q3_sp_tok(self):
        special_tokens = {
            "additional_special_tokens": [
                "<|vision_start|>",
                "<|vision_end|>",
                "<|vision_pad|>",
            ]
        }
        num_added = self.add_special_tokens(special_tokens)
        self._speech_start_id = self.convert_tokens_to_ids("<|vision_start|>")
        self._speech_end_id = self.convert_tokens_to_ids("<|vision_end|>")
        self._speech_diffusion_id = self.convert_tokens_to_ids("<|vision_pad|>")
        self._eos_id = self.eos_token_id
        self._pad_id = self.convert_tokens_to_ids("<|image_pad|>")
        return num_added

    @property
    def eos_id(self) -> int:
        return self._eos_id

    @property
    def speech_start_id(self) -> int:
        return self._speech_start_id

    @property
    def speech_end_id(self) -> int:
        return self._speech_end_id

    @property
    def speech_diffusion_id(self) -> int:
        return self._speech_diffusion_id

    @property
    def pad_id(self) -> int:
        return self._pad_id


QWEN3VoxASRTextTokenizerFast = QWEN3VoxTextTokenizerFast

__all__ = [
    'QWEN3VoxTextTokenizer',
    'QWEN3VoxTextTokenizerFast',
]
"Utilities for loading fine-tuned LoRA adapters and connector weights."
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
import torch
import torch.nn as nn
from transformers.utils import logging

logger = logging.get_logger(__name__)


@dataclass
class _LoadReport:
    language_model: bool = False
    diffusion_head_lora: bool = False
    diffusion_head_full: bool = False
    acoustic_connector: bool = False
    semantic_connector: bool = False
    adapter_root: Optional[Path] = None


class _DiffusionHeadForwardShim(nn.Module):

    def __init__(self, base: nn.Module):
        super().__init__()
        self.base = base

    def forward(self, *args, **kwargs):
        if len(args) >= 3:
            noisy_images, timesteps, condition = args[:3]
        else:
            noisy_images = kwargs.get("noisy_images")
            timesteps = kwargs.get("timesteps")
            condition = kwargs.get("condition")
        return self.base(noisy_images, timesteps, condition)


def _resolve_adapter_root(checkpoint_path: Path) -> Path:
    if checkpoint_path.is_file():
        checkpoint_path = checkpoint_path.parent
    if (checkpoint_path / "lora").exists():
        return checkpoint_path / "lora"
    return checkpoint_path


def _load_connector(
    module: Optional[nn.Module], path: Path, device: torch.device
) -> bool:
    if module is None or not path.exists():
        return False
    state_dict = torch.load(path, map_location=device)
    missing, unexpected = module.load_state_dict(state_dict, strict=False)
    if missing:
        logger.warning(f"Connector load missing keys: {missing }")
    if unexpected:
        logger.warning(f"Connector load unexpected keys: {unexpected }")
    module.to(device)
    return True


def _load_diffusion_head(
    model, adapter_root: Path, device: torch.device, report: _LoadReport
) -> None:
    diff_dir = adapter_root / "diffusion_head"
    adapter_config = diff_dir / "adapter_config.json"
    adapter_model = diff_dir / "adapter_model.bin"
    adapter_model_safetensors = diff_dir / "adapter_model.safetensors"
    try:
        from peft import PeftModel
    except ImportError as exc:
        raise RuntimeError(
            "peft is required to load diffusion head adapters but is not installed"
        ) from exc
    if adapter_config.exists() and (
        adapter_model.exists() or adapter_model_safetensors.exists()
    ):
        logger.warning(
            f"Skipping diffusion-head LoRA at {diff_dir }; "
            "PeftModel.from_pretrained is not allowed in miner.py (use full weights .bin)."
        )
        return
    full_path = diff_dir / "diffusion_head_full.bin"
    if not full_path.exists():
        full_path = adapter_root / "diffusion_head_full.bin"
    if full_path.exists():
        logger.info(f"Loading full diffusion head weights from {full_path }")
        state_dict = torch.load(full_path, map_location=device)
        missing, unexpected = model.model.prediction_head.load_state_dict(
            state_dict, strict=False
        )
        if missing:
            logger.warning(f"Diffusion head load missing keys: {missing }")
        if unexpected:
            logger.warning(f"Diffusion head load unexpected keys: {unexpected }")
        model.model.prediction_head.to(device)
        report.diffusion_head_full = True


def _load_language_model(
    model, adapter_root: Path, device: torch.device, report: _LoadReport
) -> None:
    config_file = adapter_root / "adapter_config.json"
    bin_file = adapter_root / "adapter_model.bin"
    safe_tensors_file = adapter_root / "adapter_model.safetensors"
    if not (config_file.exists() and (bin_file.exists() or safe_tensors_file.exists())):
        return
    try:
        from peft import PeftConfig, PeftModel, TaskType
    except ImportError as exc:
        raise RuntimeError(
            "peft is required to load language model adapters but is not installed"
        ) from exc
    logger.warning(
        f"Skipping language-model LoRA at {adapter_root }; "
        "PeftModel.from_pretrained is not allowed in miner.py (use full weights .bin)."
    )


def load_lora_assets(
    model, checkpoint_dir: str, device: Optional[torch.device] = None
) -> _LoadReport:
    adapter_root = _resolve_adapter_root(Path(checkpoint_dir))
    if not adapter_root.exists():
        raise FileNotFoundError(f"Adapter directory not found: {adapter_root }")
    inferred_device = device or next(model.parameters()).device
    report = _LoadReport(adapter_root=adapter_root)
    _load_language_model(model, adapter_root, inferred_device, report)
    _load_diffusion_head(model, adapter_root, inferred_device, report)
    ac_path = adapter_root / "acoustic_connector" / "pytorch_model.bin"
    if _load_connector(
        getattr(model.model, "acoustic_connector", None), ac_path, inferred_device
    ):
        report.acoustic_connector = True
    se_path = adapter_root / "semantic_connector" / "pytorch_model.bin"
    if _load_connector(
        getattr(model.model, "semantic_connector", None), se_path, inferred_device
    ):
        report.semantic_connector = True
    if not any(report.__dict__.values()):
        logger.warning(
            "No adapter assets were loaded. Ensure the checkpoint directory is correct and contains LoRA weights."
        )
    return report


import math
from typing import List, Optional, Tuple, Union
import numpy as np
import torch
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.utils import deprecate
from diffusers.utils.torch_utils import randn_tensor
from diffusers.schedulers.scheduling_utils import (
    KarrasDiffusionSchedulers,
    SchedulerMixin,
    SchedulerOutput,
)


def betas_for_alpha_bar(
    num_diffusion_timesteps, max_beta=0.999, alpha_transform_type="cosine"
):
    if alpha_transform_type == "cosine":

        def alpha_bar_fn(t):
            return math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2

    elif alpha_transform_type == "exp":

        def alpha_bar_fn(t):
            return math.exp(t * -12.0)

    elif alpha_transform_type == "cauchy":

        def alpha_bar_fn(t, gamma=1, mu=3):
            snr = mu + gamma * math.tan(math.pi * (0.5 - t) * 0.9)
            return 1 - 1 / (math.exp(snr) + 1.1)

    elif alpha_transform_type == "laplace":

        def alpha_bar_fn(t, mu=0, b=1):
            snr = mu - b * math.copysign(1, 0.5 - t) * math.log(
                1 - 2 * abs(t - 0.5) * 0.98
            )
            return 1 - 1 / (math.exp(snr) + 1.02)

    else:
        raise ValueError(f"Unsupported alpha_transform_type: {alpha_transform_type }")
    betas = []
    for i in range(num_diffusion_timesteps):
        t1 = i / num_diffusion_timesteps
        t2 = (i + 1) / num_diffusion_timesteps
        betas.append(min(1 - alpha_bar_fn(t2) / alpha_bar_fn(t1), max_beta))
    return torch.tensor(betas, dtype=torch.float32)


def rescale_zero_terminal_snr(betas):
    alphas = 1.0 - betas
    alphas_cumprod = torch.cumprod(alphas, dim=0)
    alphas_bar_sqrt = alphas_cumprod.sqrt()
    alphas_bar_sqrt_0 = alphas_bar_sqrt[0].clone()
    alphas_bar_sqrt_T = alphas_bar_sqrt[-1].clone()
    alphas_bar_sqrt -= alphas_bar_sqrt_T
    alphas_bar_sqrt *= alphas_bar_sqrt_0 / (alphas_bar_sqrt_0 - alphas_bar_sqrt_T)
    alphas_bar = alphas_bar_sqrt**2
    alphas = alphas_bar[1:] / alphas_bar[:-1]
    alphas = torch.cat([alphas_bar[0:1], alphas])
    betas = 1 - alphas
    return betas


class DPMSolverMultistepScheduler(SchedulerMixin, ConfigMixin):
    _compatibles = [e.name for e in KarrasDiffusionSchedulers]
    order = 1

    @register_to_config
    def __init__(
        self,
        num_train_timesteps: int = 1000,
        beta_start: float = 0.0001,
        beta_end: float = 0.02,
        beta_schedule: str = "linear",
        trained_betas: Optional[Union[np.ndarray, List[float]]] = None,
        solver_order: int = 2,
        prediction_type: str = "epsilon",
        thresholding: bool = False,
        dynamic_thresholding_ratio: float = 0.995,
        sample_max_value: float = 1.0,
        algorithm_type: str = "dpmsolver++",
        solver_type: str = "midpoint",
        lower_order_final: bool = True,
        euler_at_final: bool = False,
        use_karras_sigmas: Optional[bool] = False,
        use_lu_lambdas: Optional[bool] = False,
        final_sigmas_type: Optional[str] = "zero",
        lambda_min_clipped: float = -float("inf"),
        variance_type: Optional[str] = None,
        timestep_spacing: str = "linspace",
        steps_offset: int = 0,
        rescale_betas_zero_snr: bool = False,
    ):
        if algorithm_type in ["dpmsolver", "sde-dpmsolver"]:
            deprecation_message = f"algorithm_type {algorithm_type } is deprecated and will be removed in a future version. Choose from `dpmsolver++` or `sde-dpmsolver++` instead"
            deprecate(
                "algorithm_types dpmsolver and sde-dpmsolver",
                "1.0.0",
                deprecation_message,
            )
        if trained_betas is not None:
            self.betas = torch.tensor(trained_betas, dtype=torch.float32)
        elif beta_schedule == "linear":
            self.betas = torch.linspace(
                beta_start, beta_end, num_train_timesteps, dtype=torch.float32
            )
        elif beta_schedule == "scaled_linear":
            self.betas = (
                torch.linspace(
                    beta_start**0.5,
                    beta_end**0.5,
                    num_train_timesteps,
                    dtype=torch.float32,
                )
                ** 2
            )
        elif beta_schedule == "squaredcos_cap_v2" or beta_schedule == "cosine":
            self.betas = betas_for_alpha_bar(
                num_train_timesteps, alpha_transform_type="cosine"
            )
        elif beta_schedule == "cauchy":
            self.betas = betas_for_alpha_bar(
                num_train_timesteps, alpha_transform_type="cauchy"
            )
        elif beta_schedule == "laplace":
            self.betas = betas_for_alpha_bar(
                num_train_timesteps, alpha_transform_type="laplace"
            )
        else:
            raise NotImplementedError(
                f"{beta_schedule } is not implemented for {self .__class__ }"
            )
        if rescale_betas_zero_snr:
            self.betas = rescale_zero_terminal_snr(self.betas)
        self.alphas = 1.0 - self.betas
        self.alphas_cumprod = torch.cumprod(self.alphas, dim=0)
        if rescale_betas_zero_snr:
            self.alphas_cumprod[-1] = 2 ** (-24)
        self.alpha_t = torch.sqrt(self.alphas_cumprod)
        self.sigma_t = torch.sqrt(1 - self.alphas_cumprod)
        self.lambda_t = torch.log(self.alpha_t) - torch.log(self.sigma_t)
        self.sigmas = ((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5
        self.init_noise_sigma = 1.0
        if algorithm_type not in [
            "dpmsolver",
            "dpmsolver++",
            "sde-dpmsolver",
            "sde-dpmsolver++",
        ]:
            if algorithm_type == "deis":
                self.register_to_config(algorithm_type="dpmsolver++")
            else:
                raise NotImplementedError(
                    f"{algorithm_type } is not implemented for {self .__class__ }"
                )
        if solver_type not in ["midpoint", "heun"]:
            if solver_type in ["logrho", "bh1", "bh2"]:
                self.register_to_config(solver_type="midpoint")
            else:
                raise NotImplementedError(
                    f"{solver_type } is not implemented for {self .__class__ }"
                )
        if (
            algorithm_type not in ["dpmsolver++", "sde-dpmsolver++"]
            and final_sigmas_type == "zero"
        ):
            raise ValueError(
                f"`final_sigmas_type` {final_sigmas_type } is not supported for `algorithm_type` {algorithm_type }. Please choose `sigma_min` instead."
            )
        self.num_inference_steps = None
        timesteps = np.linspace(
            0, num_train_timesteps - 1, num_train_timesteps, dtype=np.float32
        )[::-1].copy()
        self.timesteps = torch.from_numpy(timesteps)
        self.model_outputs = [None] * solver_order
        self.lower_order_nums = 0
        self._step_index = None
        self._begin_index = None
        self.sigmas = self.sigmas.to("cpu")

    @property
    def step_index(self):
        return self._step_index

    @property
    def begin_index(self):
        return self._begin_index

    def set_begin_index(self, begin_index: int = 0):
        self._begin_index = begin_index

    def set_timesteps(
        self,
        num_inference_steps: int = None,
        device: Union[str, torch.device] = None,
        timesteps: Optional[List[int]] = None,
    ):
        if num_inference_steps is None and timesteps is None:
            raise ValueError(
                "Must pass exactly one of `num_inference_steps` or `timesteps`."
            )
        if num_inference_steps is not None and timesteps is not None:
            raise ValueError(
                "Can only pass one of `num_inference_steps` or `custom_timesteps`."
            )
        if timesteps is not None and self.config.use_karras_sigmas:
            raise ValueError(
                "Cannot use `timesteps` with `config.use_karras_sigmas = True`"
            )
        if timesteps is not None and self.config.use_lu_lambdas:
            raise ValueError(
                "Cannot use `timesteps` with `config.use_lu_lambdas = True`"
            )
        if timesteps is not None:
            timesteps = np.array(timesteps).astype(np.int64)
        else:
            clipped_idx = torch.searchsorted(
                torch.flip(self.lambda_t, [0]), self.config.lambda_min_clipped
            )
            last_timestep = (
                (self.config.num_train_timesteps - clipped_idx).numpy().item()
            )
            if self.config.timestep_spacing == "linspace":
                timesteps = (
                    np.linspace(0, last_timestep - 1, num_inference_steps + 1)
                    .round()[::-1][:-1]
                    .copy()
                    .astype(np.int64)
                )
            elif self.config.timestep_spacing == "leading":
                step_ratio = last_timestep // (num_inference_steps + 1)
                timesteps = (
                    (np.arange(0, num_inference_steps + 1) * step_ratio)
                    .round()[::-1][:-1]
                    .copy()
                    .astype(np.int64)
                )
                timesteps += self.config.steps_offset
            elif self.config.timestep_spacing == "trailing":
                step_ratio = self.config.num_train_timesteps / num_inference_steps
                timesteps = (
                    np.arange(last_timestep, 0, -step_ratio)
                    .round()
                    .copy()
                    .astype(np.int64)
                )
                timesteps -= 1
            else:
                raise ValueError(
                    f"{self .config .timestep_spacing } is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."
                )
        sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
        log_sigmas = np.log(sigmas)
        if self.config.use_karras_sigmas:
            sigmas = np.flip(sigmas).copy()
            sigmas = self._convert_to_karras(
                in_sigmas=sigmas, num_inference_steps=num_inference_steps
            )
            timesteps = np.array(
                [self._sigma_to_t(sigma, log_sigmas) for sigma in sigmas]
            ).round()
        elif self.config.use_lu_lambdas:
            lambdas = np.flip(log_sigmas.copy())
            lambdas = self._convert_to_lu(
                in_lambdas=lambdas, num_inference_steps=num_inference_steps
            )
            sigmas = np.exp(lambdas)
            timesteps = np.array(
                [self._sigma_to_t(sigma, log_sigmas) for sigma in sigmas]
            ).round()
        else:
            sigmas = np.interp(timesteps, np.arange(0, len(sigmas)), sigmas)
        if self.config.final_sigmas_type == "sigma_min":
            sigma_last = ((1 - self.alphas_cumprod[0]) / self.alphas_cumprod[0]) ** 0.5
        elif self.config.final_sigmas_type == "zero":
            sigma_last = 0
        else:
            raise ValueError(
                f"`final_sigmas_type` must be one of 'zero', or 'sigma_min', but got {self .config .final_sigmas_type }"
            )
        sigmas = np.concatenate([sigmas, [sigma_last]]).astype(np.float32)
        self.sigmas = torch.from_numpy(sigmas)
        self.timesteps = torch.from_numpy(timesteps).to(
            device=device, dtype=torch.int64
        )
        self.num_inference_steps = len(timesteps)
        self.model_outputs = [None] * self.config.solver_order
        self.lower_order_nums = 0
        self._step_index = None
        self._begin_index = None
        self.sigmas = self.sigmas.to("cpu")

    def _threshold_sample(self, sample: torch.Tensor) -> torch.Tensor:
        dtype = sample.dtype
        batch_size, channels, *remaining_dims = sample.shape
        if dtype not in (torch.float32, torch.float64):
            sample = sample.float()
        sample = sample.reshape(batch_size, channels * np.prod(remaining_dims))
        abs_sample = sample.abs()
        s = torch.quantile(abs_sample, self.config.dynamic_thresholding_ratio, dim=1)
        s = torch.clamp(s, min=1, max=self.config.sample_max_value)
        s = s.unsqueeze(1)
        sample = torch.clamp(sample, -s, s) / s
        sample = sample.reshape(batch_size, channels, *remaining_dims)
        sample = sample.to(dtype)
        return sample

    def _sigma_to_t(self, sigma, log_sigmas):
        log_sigma = np.log(np.maximum(sigma, 1e-10))
        dists = log_sigma - log_sigmas[:, np.newaxis]
        low_idx = (
            np.cumsum(dists >= 0, axis=0)
            .argmax(axis=0)
            .clip(max=log_sigmas.shape[0] - 2)
        )
        high_idx = low_idx + 1
        low = log_sigmas[low_idx]
        high = log_sigmas[high_idx]
        w = (low - log_sigma) / (low - high)
        w = np.clip(w, 0, 1)
        t = (1 - w) * low_idx + w * high_idx
        t = t.reshape(sigma.shape)
        return t

    def _sigma_to_alpha_sigma_t(self, sigma):
        alpha_t = 1 / (sigma**2 + 1) ** 0.5
        sigma_t = sigma * alpha_t
        return (alpha_t, sigma_t)

    def _convert_to_karras(
        self, in_sigmas: torch.Tensor, num_inference_steps
    ) -> torch.Tensor:
        if hasattr(self.config, "sigma_min"):
            sigma_min = self.config.sigma_min
        else:
            sigma_min = None
        if hasattr(self.config, "sigma_max"):
            sigma_max = self.config.sigma_max
        else:
            sigma_max = None
        sigma_min = sigma_min if sigma_min is not None else in_sigmas[-1].item()
        sigma_max = sigma_max if sigma_max is not None else in_sigmas[0].item()
        rho = 7.0
        ramp = np.linspace(0, 1, num_inference_steps)
        min_inv_rho = sigma_min ** (1 / rho)
        max_inv_rho = sigma_max ** (1 / rho)
        sigmas = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
        return sigmas

    def _convert_to_lu(
        self, in_lambdas: torch.Tensor, num_inference_steps
    ) -> torch.Tensor:
        lambda_min: float = in_lambdas[-1].item()
        lambda_max: float = in_lambdas[0].item()
        rho = 1.0
        ramp = np.linspace(0, 1, num_inference_steps)
        min_inv_rho = lambda_min ** (1 / rho)
        max_inv_rho = lambda_max ** (1 / rho)
        lambdas = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
        return lambdas

    def convert_model_output(
        self, model_output: torch.Tensor, *args, sample: torch.Tensor = None, **kwargs
    ) -> torch.Tensor:
        timestep = args[0] if len(args) > 0 else kwargs.pop("timestep", None)
        if sample is None:
            if len(args) > 1:
                sample = args[1]
            else:
                raise ValueError("missing `sample` as a required keyward argument")
        if timestep is not None:
            deprecate(
                "timesteps",
                "1.0.0",
                "Passing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        if self.config.algorithm_type in ["dpmsolver++", "sde-dpmsolver++"]:
            if self.config.prediction_type == "epsilon":
                if self.config.variance_type in ["learned", "learned_range"]:
                    model_output = model_output[:, :3]
                sigma = self.sigmas[self.step_index]
                alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma)
                x0_pred = (sample - sigma_t * model_output) / alpha_t
            elif self.config.prediction_type == "sample":
                x0_pred = model_output
            elif self.config.prediction_type == "v_prediction":
                sigma = self.sigmas[self.step_index]
                alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma)
                x0_pred = alpha_t * sample - sigma_t * model_output
            else:
                raise ValueError(
                    f"prediction_type given as {self .config .prediction_type } must be one of `epsilon`, `sample`, or `v_prediction` for the DPMSolverMultistepScheduler."
                )
            if self.config.thresholding:
                x0_pred = self._threshold_sample(x0_pred)
            return x0_pred
        elif self.config.algorithm_type in ["dpmsolver", "sde-dpmsolver"]:
            if self.config.prediction_type == "epsilon":
                if self.config.variance_type in ["learned", "learned_range"]:
                    epsilon = model_output[:, :3]
                else:
                    epsilon = model_output
            elif self.config.prediction_type == "sample":
                sigma = self.sigmas[self.step_index]
                alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma)
                epsilon = (sample - alpha_t * model_output) / sigma_t
            elif self.config.prediction_type == "v_prediction":
                sigma = self.sigmas[self.step_index]
                alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma)
                epsilon = alpha_t * model_output + sigma_t * sample
            else:
                raise ValueError(
                    f"prediction_type given as {self .config .prediction_type } must be one of `epsilon`, `sample`, or `v_prediction` for the DPMSolverMultistepScheduler."
                )
            if self.config.thresholding:
                sigma = self.sigmas[self.step_index]
                alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma)
                x0_pred = (sample - sigma_t * epsilon) / alpha_t
                x0_pred = self._threshold_sample(x0_pred)
                epsilon = (sample - alpha_t * x0_pred) / sigma_t
            return epsilon

    def dpm_solver_first_order_update(
        self,
        model_output: torch.Tensor,
        *args,
        sample: torch.Tensor = None,
        noise: Optional[torch.Tensor] = None,
        **kwargs,
    ) -> torch.Tensor:
        timestep = args[0] if len(args) > 0 else kwargs.pop("timestep", None)
        prev_timestep = args[1] if len(args) > 1 else kwargs.pop("prev_timestep", None)
        if sample is None:
            if len(args) > 2:
                sample = args[2]
            else:
                raise ValueError(" missing `sample` as a required keyward argument")
        if timestep is not None:
            deprecate(
                "timesteps",
                "1.0.0",
                "Passing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        if prev_timestep is not None:
            deprecate(
                "prev_timestep",
                "1.0.0",
                "Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        sigma_t, sigma_s = (
            self.sigmas[self.step_index + 1],
            self.sigmas[self.step_index],
        )
        alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma_t)
        alpha_s, sigma_s = self._sigma_to_alpha_sigma_t(sigma_s)
        lambda_t = torch.log(alpha_t) - torch.log(sigma_t)
        lambda_s = torch.log(alpha_s) - torch.log(sigma_s)
        h = lambda_t - lambda_s
        if self.config.algorithm_type == "dpmsolver++":
            x_t = (
                sigma_t / sigma_s * sample
                - alpha_t * (torch.exp(-h) - 1.0) * model_output
            )
        elif self.config.algorithm_type == "dpmsolver":
            x_t = (
                alpha_t / alpha_s * sample
                - sigma_t * (torch.exp(h) - 1.0) * model_output
            )
        elif self.config.algorithm_type == "sde-dpmsolver++":
            assert noise is not None
            x_t = (
                sigma_t / sigma_s * torch.exp(-h) * sample
                + alpha_t * (1 - torch.exp(-2.0 * h)) * model_output
                + sigma_t * torch.sqrt(1.0 - torch.exp(-2 * h)) * noise
            )
        elif self.config.algorithm_type == "sde-dpmsolver":
            assert noise is not None
            x_t = (
                alpha_t / alpha_s * sample
                - 2.0 * (sigma_t * (torch.exp(h) - 1.0)) * model_output
                + sigma_t * torch.sqrt(torch.exp(2 * h) - 1.0) * noise
            )
        return x_t

    def multistep_dpm_solver_second_order_update(
        self,
        model_output_list: List[torch.Tensor],
        *args,
        sample: torch.Tensor = None,
        noise: Optional[torch.Tensor] = None,
        **kwargs,
    ) -> torch.Tensor:
        timestep_list = args[0] if len(args) > 0 else kwargs.pop("timestep_list", None)
        prev_timestep = args[1] if len(args) > 1 else kwargs.pop("prev_timestep", None)
        if sample is None:
            if len(args) > 2:
                sample = args[2]
            else:
                raise ValueError(" missing `sample` as a required keyward argument")
        if timestep_list is not None:
            deprecate(
                "timestep_list",
                "1.0.0",
                "Passing `timestep_list` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        if prev_timestep is not None:
            deprecate(
                "prev_timestep",
                "1.0.0",
                "Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        sigma_t, sigma_s0, sigma_s1 = (
            self.sigmas[self.step_index + 1],
            self.sigmas[self.step_index],
            self.sigmas[self.step_index - 1],
        )
        alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma_t)
        alpha_s0, sigma_s0 = self._sigma_to_alpha_sigma_t(sigma_s0)
        alpha_s1, sigma_s1 = self._sigma_to_alpha_sigma_t(sigma_s1)
        lambda_t = torch.log(alpha_t) - torch.log(sigma_t)
        lambda_s0 = torch.log(alpha_s0) - torch.log(sigma_s0)
        lambda_s1 = torch.log(alpha_s1) - torch.log(sigma_s1)
        m0, m1 = (model_output_list[-1], model_output_list[-2])
        h, h_0 = (lambda_t - lambda_s0, lambda_s0 - lambda_s1)
        r0 = h_0 / h
        D0, D1 = (m0, 1.0 / r0 * (m0 - m1))
        if self.config.algorithm_type == "dpmsolver++":
            if self.config.solver_type == "midpoint":
                x_t = (
                    sigma_t / sigma_s0 * sample
                    - alpha_t * (torch.exp(-h) - 1.0) * D0
                    - 0.5 * (alpha_t * (torch.exp(-h) - 1.0)) * D1
                )
            elif self.config.solver_type == "heun":
                x_t = (
                    sigma_t / sigma_s0 * sample
                    - alpha_t * (torch.exp(-h) - 1.0) * D0
                    + alpha_t * ((torch.exp(-h) - 1.0) / h + 1.0) * D1
                )
        elif self.config.algorithm_type == "dpmsolver":
            if self.config.solver_type == "midpoint":
                x_t = (
                    alpha_t / alpha_s0 * sample
                    - sigma_t * (torch.exp(h) - 1.0) * D0
                    - 0.5 * (sigma_t * (torch.exp(h) - 1.0)) * D1
                )
            elif self.config.solver_type == "heun":
                x_t = (
                    alpha_t / alpha_s0 * sample
                    - sigma_t * (torch.exp(h) - 1.0) * D0
                    - sigma_t * ((torch.exp(h) - 1.0) / h - 1.0) * D1
                )
        elif self.config.algorithm_type == "sde-dpmsolver++":
            assert noise is not None
            if self.config.solver_type == "midpoint":
                x_t = (
                    sigma_t / sigma_s0 * torch.exp(-h) * sample
                    + alpha_t * (1 - torch.exp(-2.0 * h)) * D0
                    + 0.5 * (alpha_t * (1 - torch.exp(-2.0 * h))) * D1
                    + sigma_t * torch.sqrt(1.0 - torch.exp(-2 * h)) * noise
                )
            elif self.config.solver_type == "heun":
                x_t = (
                    sigma_t / sigma_s0 * torch.exp(-h) * sample
                    + alpha_t * (1 - torch.exp(-2.0 * h)) * D0
                    + alpha_t * ((1.0 - torch.exp(-2.0 * h)) / (-2.0 * h) + 1.0) * D1
                    + sigma_t * torch.sqrt(1.0 - torch.exp(-2 * h)) * noise
                )
        elif self.config.algorithm_type == "sde-dpmsolver":
            assert noise is not None
            if self.config.solver_type == "midpoint":
                x_t = (
                    alpha_t / alpha_s0 * sample
                    - 2.0 * (sigma_t * (torch.exp(h) - 1.0)) * D0
                    - sigma_t * (torch.exp(h) - 1.0) * D1
                    + sigma_t * torch.sqrt(torch.exp(2 * h) - 1.0) * noise
                )
            elif self.config.solver_type == "heun":
                x_t = (
                    alpha_t / alpha_s0 * sample
                    - 2.0 * (sigma_t * (torch.exp(h) - 1.0)) * D0
                    - 2.0 * (sigma_t * ((torch.exp(h) - 1.0) / h - 1.0)) * D1
                    + sigma_t * torch.sqrt(torch.exp(2 * h) - 1.0) * noise
                )
        return x_t

    def multistep_dpm_solver_third_order_update(
        self,
        model_output_list: List[torch.Tensor],
        *args,
        sample: torch.Tensor = None,
        **kwargs,
    ) -> torch.Tensor:
        timestep_list = args[0] if len(args) > 0 else kwargs.pop("timestep_list", None)
        prev_timestep = args[1] if len(args) > 1 else kwargs.pop("prev_timestep", None)
        if sample is None:
            if len(args) > 2:
                sample = args[2]
            else:
                raise ValueError(" missing`sample` as a required keyward argument")
        if timestep_list is not None:
            deprecate(
                "timestep_list",
                "1.0.0",
                "Passing `timestep_list` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        if prev_timestep is not None:
            deprecate(
                "prev_timestep",
                "1.0.0",
                "Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`",
            )
        sigma_t, sigma_s0, sigma_s1, sigma_s2 = (
            self.sigmas[self.step_index + 1],
            self.sigmas[self.step_index],
            self.sigmas[self.step_index - 1],
            self.sigmas[self.step_index - 2],
        )
        alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma_t)
        alpha_s0, sigma_s0 = self._sigma_to_alpha_sigma_t(sigma_s0)
        alpha_s1, sigma_s1 = self._sigma_to_alpha_sigma_t(sigma_s1)
        alpha_s2, sigma_s2 = self._sigma_to_alpha_sigma_t(sigma_s2)
        lambda_t = torch.log(alpha_t) - torch.log(sigma_t)
        lambda_s0 = torch.log(alpha_s0) - torch.log(sigma_s0)
        lambda_s1 = torch.log(alpha_s1) - torch.log(sigma_s1)
        lambda_s2 = torch.log(alpha_s2) - torch.log(sigma_s2)
        m0, m1, m2 = (
            model_output_list[-1],
            model_output_list[-2],
            model_output_list[-3],
        )
        h, h_0, h_1 = (
            lambda_t - lambda_s0,
            lambda_s0 - lambda_s1,
            lambda_s1 - lambda_s2,
        )
        r0, r1 = (h_0 / h, h_1 / h)
        D0 = m0
        D1_0, D1_1 = (1.0 / r0 * (m0 - m1), 1.0 / r1 * (m1 - m2))
        D1 = D1_0 + r0 / (r0 + r1) * (D1_0 - D1_1)
        D2 = 1.0 / (r0 + r1) * (D1_0 - D1_1)
        if self.config.algorithm_type == "dpmsolver++":
            x_t = (
                sigma_t / sigma_s0 * sample
                - alpha_t * (torch.exp(-h) - 1.0) * D0
                + alpha_t * ((torch.exp(-h) - 1.0) / h + 1.0) * D1
                - alpha_t * ((torch.exp(-h) - 1.0 + h) / h**2 - 0.5) * D2
            )
        elif self.config.algorithm_type == "dpmsolver":
            x_t = (
                alpha_t / alpha_s0 * sample
                - sigma_t * (torch.exp(h) - 1.0) * D0
                - sigma_t * ((torch.exp(h) - 1.0) / h - 1.0) * D1
                - sigma_t * ((torch.exp(h) - 1.0 - h) / h**2 - 0.5) * D2
            )
        return x_t

    def index_for_timestep(self, timestep, schedule_timesteps=None):
        if schedule_timesteps is None:
            schedule_timesteps = self.timesteps
        index_candidates = (schedule_timesteps == timestep).nonzero()
        if len(index_candidates) == 0:
            step_index = len(self.timesteps) - 1
        elif len(index_candidates) > 1:
            step_index = index_candidates[1].item()
        else:
            step_index = index_candidates[0].item()
        return step_index

    def _init_step_index(self, timestep):
        if self.begin_index is None:
            if isinstance(timestep, torch.Tensor):
                timestep = timestep.to(self.timesteps.device)
            self._step_index = self.index_for_timestep(timestep)
        else:
            self._step_index = self._begin_index

    def step(
        self,
        model_output: torch.Tensor,
        timestep: int,
        sample: torch.Tensor,
        generator=None,
        variance_noise: Optional[torch.Tensor] = None,
        return_dict: bool = True,
    ) -> Union[SchedulerOutput, Tuple]:
        if self.num_inference_steps is None:
            raise ValueError(
                "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"
            )
        if self.step_index is None:
            self._init_step_index(timestep)
        lower_order_final = self.step_index == len(self.timesteps) - 1 and (
            self.config.euler_at_final
            or (self.config.lower_order_final and len(self.timesteps) < 15)
            or self.config.final_sigmas_type == "zero"
        )
        lower_order_second = (
            self.step_index == len(self.timesteps) - 2
            and self.config.lower_order_final
            and (len(self.timesteps) < 15)
        )
        model_output = self.convert_model_output(model_output, sample=sample)
        for i in range(self.config.solver_order - 1):
            self.model_outputs[i] = self.model_outputs[i + 1]
        self.model_outputs[-1] = model_output
        sample = sample.to(torch.float32)
        if (
            self.config.algorithm_type in ["sde-dpmsolver", "sde-dpmsolver++"]
            and variance_noise is None
        ):
            noise = randn_tensor(
                model_output.shape,
                generator=generator,
                device=model_output.device,
                dtype=torch.float32,
            )
        elif self.config.algorithm_type in ["sde-dpmsolver", "sde-dpmsolver++"]:
            noise = variance_noise.to(device=model_output.device, dtype=torch.float32)
        else:
            noise = None
        if (
            self.config.solver_order == 1
            or self.lower_order_nums < 1
            or lower_order_final
        ):
            prev_sample = self.dpm_solver_first_order_update(
                model_output, sample=sample, noise=noise
            )
        elif (
            self.config.solver_order == 2
            or self.lower_order_nums < 2
            or lower_order_second
        ):
            prev_sample = self.multistep_dpm_solver_second_order_update(
                self.model_outputs, sample=sample, noise=noise
            )
        else:
            prev_sample = self.multistep_dpm_solver_third_order_update(
                self.model_outputs, sample=sample
            )
        if self.lower_order_nums < self.config.solver_order:
            self.lower_order_nums += 1
        prev_sample = prev_sample.to(model_output.dtype)
        self._step_index += 1
        if not return_dict:
            return (prev_sample,)
        return SchedulerOutput(prev_sample=prev_sample)

    def add_noise(
        self,
        original_samples: torch.Tensor,
        noise: torch.Tensor,
        timesteps: torch.IntTensor,
    ) -> torch.Tensor:
        alpha_t = self.alpha_t.to(original_samples.device).to(original_samples.dtype)
        sigma_t = self.sigma_t.to(original_samples.device).to(original_samples.dtype)
        timesteps = timesteps.to(original_samples.device)
        alpha_t = alpha_t[timesteps].flatten()
        while len(alpha_t.shape) < len(original_samples.shape):
            alpha_t = alpha_t.unsqueeze(-1)
        sigma_t = sigma_t[timesteps].flatten()
        while len(sigma_t.shape) < len(original_samples.shape):
            sigma_t = sigma_t.unsqueeze(-1)
        noisy_samples = alpha_t * original_samples + sigma_t * noise
        return noisy_samples

    def get_velocity(
        self,
        original_samples: torch.Tensor,
        noise: torch.Tensor,
        timesteps: torch.IntTensor,
    ) -> torch.Tensor:
        alpha_t = self.alpha_t.to(original_samples.device).to(original_samples.dtype)
        sigma_t = self.sigma_t.to(original_samples.device).to(original_samples.dtype)
        timesteps = timesteps.to(original_samples.device)
        alpha_t = alpha_t[timesteps].flatten()
        while len(alpha_t.shape) < len(original_samples.shape):
            alpha_t = alpha_t.unsqueeze(-1)
        sigma_t = sigma_t[timesteps].flatten()
        while len(sigma_t.shape) < len(original_samples.shape):
            sigma_t = sigma_t.unsqueeze(-1)
        velocity = alpha_t * noise - sigma_t * original_samples
        return velocity

    def __len__(self):
        return self.config.num_train_timesteps


'\nProcessor class for QWEN3Vox models.\n'
import os
import json
import warnings
from typing import List, Optional, Union, Dict, Any
import numpy as np
import torch
from transformers.feature_extraction_utils import FeatureExtractionMixin
from transformers.utils import logging

logger = logging.get_logger(__name__)


class AudioNormalizer:

    def __init__(self, target_dB_FS: float = -25, eps: float = 1e-06):
        self.target_dB_FS = target_dB_FS
        self.eps = eps

    def tailor_dB_FS(self, audio: np.ndarray) -> tuple:
        rms = np.sqrt(np.mean(audio**2))
        scalar = 10 ** (self.target_dB_FS / 20) / (rms + self.eps)
        normalized_audio = audio * scalar
        return (normalized_audio, rms, scalar)

    def avoid_clipping(
        self, audio: np.ndarray, scalar: Optional[float] = None
    ) -> tuple:
        if scalar is None:
            max_val = np.max(np.abs(audio))
            if max_val > 1.0:
                scalar = max_val + self.eps
            else:
                scalar = 1.0
        return (audio / scalar, scalar)

    def __call__(self, audio: np.ndarray) -> np.ndarray:
        audio, _, _ = self.tailor_dB_FS(audio)
        audio, _ = self.avoid_clipping(audio)
        return audio


class QWEN3VoxTokenizerProcessor(FeatureExtractionMixin):
    model_input_names = ["input_features"]

    def __init__(
        self,
        sampling_rate: int = 22050,
        normalize_audio: bool = True,
        target_dB_FS: float = -25,
        eps: float = 1e-06,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.sampling_rate = sampling_rate
        self.normalize_audio = normalize_audio
        if self.normalize_audio:
            self.normalizer = AudioNormalizer(target_dB_FS=target_dB_FS, eps=eps)
        else:
            self.normalizer = None
        self.feature_extractor_dict = {
            "sampling_rate": sampling_rate,
            "normalize_audio": normalize_audio,
            "target_dB_FS": target_dB_FS,
            "eps": eps,
        }

    def _ensure_mono(self, audio: np.ndarray) -> np.ndarray:
        if len(audio.shape) == 1:
            return audio
        elif len(audio.shape) == 2:
            if audio.shape[0] == 2:
                return np.mean(audio, axis=0)
            elif audio.shape[1] == 2:
                return np.mean(audio, axis=1)
            elif audio.shape[0] == 1:
                return audio.squeeze(0)
            elif audio.shape[1] == 1:
                return audio.squeeze(1)
            else:
                raise ValueError(f"Unexpected audio shape: {audio .shape }")
        else:
            raise ValueError(f"Audio should be 1D or 2D, got shape: {audio .shape }")

    def _process_single_audio(
        self, audio: Union[np.ndarray, List[float]]
    ) -> np.ndarray:
        if not isinstance(audio, np.ndarray):
            audio = np.array(audio, dtype=np.float32)
        else:
            audio = audio.astype(np.float32)
        audio = self._ensure_mono(audio)
        if self.normalize_audio and self.normalizer is not None:
            audio = self.normalizer(audio)
        return audio

    def __call__(
        self,
        audio: Union[
            str, np.ndarray, List[float], List[np.ndarray], List[List[float]], List[str]
        ] = None,
        sampling_rate: Optional[int] = None,
        return_tensors: Optional[str] = None,
        **kwargs,
    ):
        if audio is None:
            raise ValueError("Audio input is required")
        if sampling_rate is not None and sampling_rate != self.sampling_rate:
            logger.warning(
                f"Input sampling rate ({sampling_rate }) differs from expected sampling rate ({self .sampling_rate }). Please resample your audio."
            )
        if isinstance(audio, str):
            audio = self._load_audio_from_path(audio)
            is_batched = False
        elif isinstance(audio, list):
            if len(audio) == 0:
                raise ValueError("Empty audio list provided")
            if all((isinstance(item, str) for item in audio)):
                audio = [self._load_audio_from_path(path) for path in audio]
                is_batched = True
            else:
                is_batched = isinstance(audio[0], (np.ndarray, list))
        else:
            is_batched = False
        if is_batched:
            processed_audio = [self._process_single_audio(a) for a in audio]
        else:
            processed_audio = [self._process_single_audio(audio)]
        if return_tensors == "pt":
            if len(processed_audio) == 1:
                input_features = (
                    torch.from_numpy(processed_audio[0]).unsqueeze(0).unsqueeze(1)
                )
            else:
                input_features = torch.stack(
                    [torch.from_numpy(a) for a in processed_audio]
                ).unsqueeze(1)
        elif return_tensors == "np":
            if len(processed_audio) == 1:
                input_features = processed_audio[0][np.newaxis, np.newaxis, :]
            else:
                input_features = np.stack(processed_audio)[:, np.newaxis, :]
        else:
            input_features = (
                processed_audio[0] if len(processed_audio) == 1 else processed_audio
            )
        outputs = {"audio": input_features}
        return outputs

    def _load_audio_from_path(self, audio_path: str) -> np.ndarray:
        file_ext = os.path.splitext(audio_path)[1].lower()
        if file_ext in [".wav", ".mp3", ".flac", ".m4a", ".ogg"]:
            import librosa

            audio_array, sr = librosa.load(audio_path, sr=self.sampling_rate, mono=True)
            return audio_array
        elif file_ext == ".pt":
            audio_tensor = torch.load(audio_path, map_location="cpu").squeeze()
            if isinstance(audio_tensor, torch.Tensor):
                audio_array = audio_tensor.numpy()
            else:
                audio_array = np.array(audio_tensor)
            return audio_array.astype(np.float32)
        elif file_ext == ".npy":
            audio_array = np.load(audio_path)
            return audio_array.astype(np.float32)
        else:
            raise ValueError(
                f"Unsupported file format: {file_ext }. Supported formats: .wav, .mp3, .flac, .m4a, .ogg, .pt, .npy, .npz"
            )

    def preprocess_audio(
        self,
        audio_path_or_array: Union[str, np.ndarray],
        normalize: Optional[bool] = None,
    ) -> np.ndarray:
        if isinstance(audio_path_or_array, str):
            audio_array = self._load_audio_from_path(audio_path_or_array)
        else:
            audio_array = np.array(audio_path_or_array, dtype=np.float32)
        original_normalize = self.normalize_audio
        if normalize is not None:
            self.normalize_audio = normalize
        try:
            processed = self._process_single_audio(audio_array)
        finally:
            self.normalize_audio = original_normalize
        return processed

    def to_dict(self) -> Dict[str, Any]:
        return self.feature_extractor_dict

    def save_audio(
        self,
        audio: Union[torch.Tensor, np.ndarray, List[Union[torch.Tensor, np.ndarray]]],
        output_path: str = "output.wav",
        sampling_rate: Optional[int] = None,
        normalize: bool = False,
        batch_prefix: str = "audio_",
    ):
        if sampling_rate is None:
            sampling_rate = self.sampling_rate
        try:
            import soundfile as sf
        except ImportError:
            raise ImportError(
                "soundfile is required to save audio files. Install it with: pip install soundfile"
            )
        if isinstance(audio, torch.Tensor):
            audio_np = audio.float().detach().cpu().numpy()
        elif isinstance(audio, np.ndarray):
            audio_np = audio
        elif isinstance(audio, list):
            if all((isinstance(a, torch.Tensor) for a in audio)):
                audio_np = [a.float().detach().cpu().numpy() for a in audio]
            else:
                audio_np = audio
        else:
            raise ValueError(f"Unsupported audio type: {type (audio )}")
        saved_paths = []
        if isinstance(audio_np, list):
            output_dir = output_path
            os.makedirs(output_dir, exist_ok=True)
            for i, audio_item in enumerate(audio_np):
                audio_item = self._prepare_audio_for_save(audio_item, normalize)
                file_path = os.path.join(output_dir, f"{batch_prefix }{i }.wav")
                sf.write(file_path, audio_item, sampling_rate)
                saved_paths.append(file_path)
        elif len(audio_np.shape) >= 3:
            batch_size = audio_np.shape[0]
            if batch_size > 1:
                output_dir = output_path
                os.makedirs(output_dir, exist_ok=True)
                for i in range(batch_size):
                    single_audio = audio_np[i]
                    if len(single_audio.shape) > 1:
                        if single_audio.shape[0] == 1:
                            single_audio = single_audio.squeeze(0)
                    single_audio = self._prepare_audio_for_save(single_audio, normalize)
                    file_path = os.path.join(output_dir, f"{batch_prefix }{i }.wav")
                    sf.write(file_path, single_audio, sampling_rate)
                    saved_paths.append(file_path)
            else:
                audio_item = audio_np.squeeze()
                audio_item = self._prepare_audio_for_save(audio_item, normalize)
                sf.write(output_path, audio_item, sampling_rate)
                saved_paths.append(output_path)
        else:
            audio_item = self._prepare_audio_for_save(audio_np, normalize)
            sf.write(output_path, audio_item, sampling_rate)
            saved_paths.append(output_path)
        return saved_paths

    def _prepare_audio_for_save(self, audio: np.ndarray, normalize: bool) -> np.ndarray:
        if len(audio.shape) > 1 and audio.shape[0] == 1:
            audio = audio.squeeze(0)
        if normalize:
            max_val = np.abs(audio).max()
            if max_val > 0:
                audio = audio / max_val
        return audio


__all__ = [
    'QWEN3VoxTokenizerProcessor',
    "AudioNormalizer",
]
import math
import torch


class UniformSampler:

    def __init__(self, timesteps=1000):
        self.timesteps = timesteps

    def sample(self, batch_size, device):
        return torch.randint(0, self.timesteps, (batch_size,), device=device)


class LogitNormalSampler:

    def __init__(self, timesteps=1000, m=0, s=1):
        self.timesteps = timesteps
        timesteps = torch.linspace(0, 1, timesteps)
        logit = torch.log(timesteps / (1 - timesteps))
        self.prob = torch.exp(-0.5 * (logit - m) ** 2 / s**2) / (
            s * math.sqrt(2 * math.pi)
        )

    def sample(self, batch_size, device):
        return torch.multinomial(self.prob, batch_size, replacement=True).to(device)


' QWEN3Vox Streaming model configuration'
from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging
from transformers.models.qwen2.configuration_qwen2 import Qwen2Config

logger = logging.get_logger(__name__)


class QWEN3VoxStreamingConfig(PretrainedConfig):
    model_type = 'vibevoice_streaming'
    is_composition = True
    sub_configs = {
        "acoustic_tokenizer_config": QWEN3VoxAcousticTokenizerConfig,
        "decoder_config": Qwen2Config,
        "diffusion_head_config": QWEN3VoxDiffusionHeadConfig,
    }
    base_model_tp_plan = {
        "layers.*.self_attn.q_proj": "colwise",
        "layers.*.self_attn.k_proj": "colwise",
        "layers.*.self_attn.v_proj": "colwise",
        "layers.*.self_attn.o_proj": "rowwise",
        "layers.*.mlp.gate_proj": "colwise",
        "layers.*.mlp.up_proj": "colwise",
        "layers.*.mlp.down_proj": "rowwise",
    }

    def __init__(
        self,
        acoustic_tokenizer_config=None,
        decoder_config=None,
        diffusion_head_config=None,
        tts_backbone_num_hidden_layers=20,
        **kwargs,
    ):
        kwargs["_attn_implementation_autoset"] = False
        if acoustic_tokenizer_config is None:
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ]()
        elif isinstance(acoustic_tokenizer_config, dict):
            acoustic_tokenizer_config["model_type"] = 'vibevoice_acoustic_tokenizer'
            self.acoustic_tokenizer_config = self.sub_configs[
                "acoustic_tokenizer_config"
            ](**acoustic_tokenizer_config)
        elif isinstance(acoustic_tokenizer_config, QWEN3VoxAcousticTokenizerConfig):
            self.acoustic_tokenizer_config = acoustic_tokenizer_config
        if decoder_config is None:
            self.decoder_config = self.sub_configs["decoder_config"]()
        elif isinstance(decoder_config, dict):
            if decoder_config.get("model_type", "") == "qwen2":
                self.decoder_config = Qwen2Config(**decoder_config)
            else:
                raise ValueError(
                    f"Unsupported decoder model type: {decoder_config .get ('model_type','')}"
                )
        elif isinstance(decoder_config, (Qwen2Config,)):
            self.decoder_config = decoder_config
        if diffusion_head_config is None:
            self.diffusion_head_config = self.sub_configs["diffusion_head_config"]()
        elif isinstance(diffusion_head_config, dict):
            diffusion_head_config["model_type"] = 'vibevoice_diffusion_head'
            self.diffusion_head_config = self.sub_configs["diffusion_head_config"](
                **diffusion_head_config
            )
        elif isinstance(diffusion_head_config, QWEN3VoxDiffusionHeadConfig):
            self.diffusion_head_config = diffusion_head_config
        self.acoustic_vae_dim = getattr(self.acoustic_tokenizer_config, "vae_dim", 64)
        self.tts_backbone_num_hidden_layers = tts_backbone_num_hidden_layers
        super().__init__(**kwargs)


__all__ = [
    'QWEN3VoxStreamingConfig'
]
import math
from typing import Optional, Tuple, Union
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.models.auto import AutoModel
from transformers.modeling_utils import PreTrainedModel
from transformers.activations import ACT2FN
from transformers.utils import logging

logger = logging.get_logger(__name__)


class RMSNorm(nn.Module):

    def __init__(
        self,
        dim: int,
        eps: float = 1e-06,
        elementwise_affine=True,
        memory_efficient=False,
    ):
        super().__init__()
        self.dim = dim
        self.eps = eps
        self.elementwise_affine = elementwise_affine
        if self.elementwise_affine:
            self.weight = nn.Parameter(torch.ones(dim))
        else:
            self.register_parameter("weight", None)

    def _norm(self, x):
        return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)

    def forward(self, x):
        output = self._norm(x.float()).type_as(x)
        if self.weight is not None:
            output = output * self.weight
        return output

    def extra_repr(self) -> str:
        return f"dim={self .dim }, eps={self .eps }, elementwise_affine={self .elementwise_affine }"


def modulate(x, shift, scale):
    return x * (1 + scale) + shift


class TimestepEmbedder(nn.Module):

    def __init__(self, hidden_size, frequency_embedding_size=256):
        super().__init__()
        self.mlp = nn.Sequential(
            nn.Linear(frequency_embedding_size, hidden_size, bias=False),
            ACT2FN["silu"],
            nn.Linear(hidden_size, hidden_size, bias=False),
        )
        self.frequency_embedding_size = frequency_embedding_size

    @staticmethod
    def timestep_embedding(t, dim, max_period=10000):
        half = dim // 2
        freqs = torch.exp(
            -math.log(max_period)
            * torch.arange(start=0, end=half, dtype=torch.float32)
            / half
        ).to(t.device)
        args = t[:, None].float() * freqs[None]
        embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
        if dim % 2:
            embedding = torch.cat(
                [embedding, torch.zeros_like(embedding[:, :1])], dim=-1
            )
        return embedding.to(t.dtype)

    def forward(self, t):
        t_freq = self.timestep_embedding(t, self.frequency_embedding_size)
        t_emb = self.mlp(t_freq)
        return t_emb


class FeedForwardNetwork(nn.Module):

    def __init__(self, embed_dim, ffn_dim):
        super().__init__()
        self.embed_dim = embed_dim
        self.gate_proj = nn.Linear(self.embed_dim, ffn_dim, bias=False)
        self.up_proj = nn.Linear(self.embed_dim, ffn_dim, bias=False)
        self.down_proj = nn.Linear(ffn_dim, self.embed_dim, bias=False)
        self.act_fn = ACT2FN["silu"]

    def forward(self, x):
        gate = self.gate_proj(x)
        up = self.up_proj(x)
        gate = self.act_fn(gate)
        return self.down_proj(gate * up)


class HeadLayer(nn.Module):

    def __init__(self, embed_dim, ffn_dim, cond_dim, norm_eps=1e-05):
        super().__init__()
        self.embed_dim = embed_dim
        self.cond_dim = cond_dim
        self.ffn_dim = ffn_dim
        self.ffn = FeedForwardNetwork(self.embed_dim, self.ffn_dim)
        self.norm = RMSNorm(self.embed_dim, eps=norm_eps)
        self.adaLN_modulation = nn.Sequential(
            ACT2FN["silu"], nn.Linear(cond_dim, 3 * self.embed_dim, bias=False)
        )

    def forward(self, x, c):
        shift_ffn, scale_ffn, gate_ffn = self.adaLN_modulation(c).chunk(3, dim=-1)
        x = x + gate_ffn * self.ffn(modulate(self.norm(x), shift_ffn, scale_ffn))
        return x


class FinalLayer(nn.Module):

    def __init__(self, hidden_size, output_size, cond_size, norm_eps=1e-05):
        super().__init__()
        self.norm_final = RMSNorm(hidden_size, eps=norm_eps, elementwise_affine=False)
        self.linear = nn.Linear(hidden_size, output_size, bias=False)
        self.adaLN_modulation = nn.Sequential(
            ACT2FN["silu"], nn.Linear(cond_size, 2 * hidden_size, bias=False)
        )

    def forward(self, x, c):
        shift, scale = self.adaLN_modulation(c).chunk(2, dim=-1)
        x = modulate(self.norm_final(x), shift, scale)
        x = self.linear(x)
        return x


class QWEN3VoxDiffusionHead(PreTrainedModel):
    config_class = QWEN3VoxDiffusionHeadConfig
    supports_gradient_checkpointing = True
    _supports_flash_attn_2 = True
    _supports_sdpa = True

    def __init__(self, config):
        super().__init__(config)
        self.config = config
        self.cond_dim = config.hidden_size
        latent_size = config.latent_size
        self.noisy_images_proj = nn.Linear(latent_size, config.hidden_size, bias=False)
        self.cond_proj = nn.Linear(config.hidden_size, self.cond_dim, bias=False)
        self.t_embedder = TimestepEmbedder(self.cond_dim)
        ffn_dim = int(config.hidden_size * config.head_ffn_ratio)
        self.layers = nn.ModuleList(
            [
                HeadLayer(
                    embed_dim=config.hidden_size,
                    ffn_dim=ffn_dim,
                    cond_dim=self.cond_dim,
                    norm_eps=config.rms_norm_eps,
                )
                for _ in range(config.head_layers)
            ]
        )
        self.final_layer = FinalLayer(
            hidden_size=config.hidden_size,
            output_size=latent_size,
            cond_size=self.cond_dim,
            norm_eps=config.rms_norm_eps,
        )
        self.initialize_weights()

    def initialize_weights(self):
        nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
        nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
        for layer in self.layers:
            nn.init.constant_(layer.adaLN_modulation[-1].weight, 0)
        nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0)
        nn.init.constant_(self.final_layer.linear.weight, 0)

    def forward(self, noisy_images, timesteps, condition):
        x = self.noisy_images_proj(noisy_images)
        t = self.t_embedder(timesteps)
        condition = self.cond_proj(condition)
        c = condition + t
        for layer in self.layers:
            x = layer(x, c)
        x = self.final_layer(x, c)
        return x


AutoModel.register(QWEN3VoxDiffusionHeadConfig, QWEN3VoxDiffusionHead)
__all__ = [
    'QWEN3VoxDiffusionHead'
]
import math
import typing as tp
from functools import partial
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, Union
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.models.auto import AutoModel
from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging
from transformers.modeling_utils import PreTrainedModel
from transformers.activations import ACT2FN

logger = logging.get_logger(__name__)
import os

try:
    from apex.normalization.fused_layer_norm import fused_rms_norm_affine

    APEX_AVAILABLE = True
    logger.info("APEX FusedRMSNorm is available and will be used for optimization")
    if int(os.getenv("OPTIMIZE_FOR_SPEED", "0")) == 0:
        APEX_AVAILABLE = False
        logger.warning(
            "APEX FusedRMSNorm is disabled by environment variable OPTIMIZE_FOR_SPEED=0"
        )
except ImportError:
    APEX_AVAILABLE = False
    logger.warning("APEX FusedRMSNorm not available, using native implementation")


class ConvLayerNorm(nn.LayerNorm):

    def __init__(
        self, normalized_shape: tp.Union[int, tp.List[int], torch.Size], **kwargs
    ):
        super().__init__(normalized_shape, **kwargs)

    def forward(self, x):
        x = x.transpose(1, 2)
        x = nn.functional.layer_norm(
            x.float(),
            self.normalized_shape,
            self.weight.float(),
            self.bias.float(),
            self.eps,
        ).type_as(x)
        x = x.transpose(1, 2)
        return x


class RMSNorm(nn.Module):

    def __init__(
        self, dim: int, eps: float = 1e-05, elementwise_affine=True, weight_shape=None
    ):
        super().__init__()
        self.dim = dim
        self.eps = eps
        self.elementwise_affine = elementwise_affine
        if self.elementwise_affine:
            weight_shape = (dim,) if weight_shape is None else weight_shape
            self.weight = nn.Parameter(torch.ones(weight_shape))
        else:
            self.register_parameter("weight", None)

    def _norm(self, x):
        return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)

    def forward(self, x):
        output = self._norm(x.float()).type_as(x)
        if self.weight is not None:
            output = output * self.weight
        return output

    def extra_repr(self) -> str:
        return f"dim={self .dim }, eps={self .eps }, elementwise_affine={self .elementwise_affine }"


class ConvRMSNorm(RMSNorm):

    def __init__(
        self, dim: int, eps: float = 1e-05, elementwise_affine=True, weight_shape=None
    ):
        super().__init__(dim, eps, elementwise_affine, weight_shape)

    def forward(self, x):
        x = x.transpose(1, 2)
        if not APEX_AVAILABLE or not self.elementwise_affine:
            output = self._norm(x.float()).type_as(x)
            if self.weight is not None:
                output = output * self.weight
        else:
            output = fused_rms_norm_affine(x, self.weight, self.weight.shape, self.eps)
        output = output.transpose(1, 2)
        return output


CONV_NORMALIZATIONS = frozenset(
    [
        "none",
        "weight_norm",
        "spectral_norm",
        "time_layer_norm",
        "layer_norm",
        "time_group_norm",
    ]
)


def apply_parametrization_norm(module: nn.Module, norm: str = "none") -> nn.Module:
    assert norm in CONV_NORMALIZATIONS
    if norm == "weight_norm":
        return nn.utils.weight_norm(module)
    elif norm == "spectral_norm":
        return nn.utils.spectral_norm(module)
    else:
        return module


def get_norm_module(
    module: nn.Module, causal: bool = False, norm: str = "none", **norm_kwargs
) -> nn.Module:
    assert norm in CONV_NORMALIZATIONS
    if norm == "layer_norm":
        assert isinstance(module, nn.modules.conv._ConvNd)
        return ConvLayerNorm(module.out_channels, **norm_kwargs)
    elif norm == "time_group_norm":
        if causal:
            raise ValueError("GroupNorm doesn't support causal evaluation.")
        assert isinstance(module, nn.modules.conv._ConvNd)
        return nn.GroupNorm(1, module.out_channels, **norm_kwargs)
    else:
        return nn.Identity()


def get_extra_padding_for_conv1d(
    x: torch.Tensor, kernel_size: int, stride: int, padding_total: int = 0
) -> int:
    length = x.shape[-1]
    n_frames = (length - kernel_size + padding_total) / stride + 1
    ideal_length = (math.ceil(n_frames) - 1) * stride + (kernel_size - padding_total)
    return ideal_length - length


def pad1d(
    x: torch.Tensor,
    paddings: tp.Tuple[int, int],
    mode: str = "zero",
    value: float = 0.0,
):
    length = x.shape[-1]
    padding_left, padding_right = paddings
    assert padding_left >= 0 and padding_right >= 0, (padding_left, padding_right)
    if mode == "reflect":
        max_pad = max(padding_left, padding_right)
        extra_pad = 0
        if length <= max_pad:
            extra_pad = max_pad - length + 1
            x = F.pad(x, (0, extra_pad))
        padded = F.pad(x, paddings, mode, value)
        end = padded.shape[-1] - extra_pad
        return padded[..., :end]
    else:
        return F.pad(x, paddings, mode, value)


def unpad1d(x: torch.Tensor, paddings: tp.Tuple[int, int]):
    padding_left, padding_right = paddings
    assert padding_left >= 0 and padding_right >= 0, (padding_left, padding_right)
    assert padding_left + padding_right <= x.shape[-1]
    end = x.shape[-1] - padding_right
    return x[..., padding_left:end]


class NormConv1d(nn.Module):

    def __init__(
        self,
        *args,
        causal: bool = False,
        norm: str = "none",
        norm_kwargs: tp.Dict[str, tp.Any] = {},
        **kwargs,
    ):
        super().__init__()
        self.conv = apply_parametrization_norm(nn.Conv1d(*args, **kwargs), norm)
        self.norm = get_norm_module(self.conv, causal, norm, **norm_kwargs)
        self.norm_type = norm

    def forward(self, x):
        x = self.conv(x)
        x = self.norm(x)
        return x


class NormConvTranspose1d(nn.Module):

    def __init__(
        self,
        *args,
        causal: bool = False,
        norm: str = "none",
        norm_kwargs: tp.Dict[str, tp.Any] = {},
        **kwargs,
    ):
        super().__init__()
        self.convtr = apply_parametrization_norm(
            nn.ConvTranspose1d(*args, **kwargs), norm
        )
        self.norm = get_norm_module(self.convtr, causal, norm, **norm_kwargs)
        self.norm_type = norm

    def forward(self, x):
        x = self.convtr(x)
        x = self.norm(x)
        return x


class QWEN3VoxTokenizerStreamingCache:

    def __init__(self):
        self.cache = {}

    def get(
        self, layer_id: str, sample_indices: torch.Tensor
    ) -> Optional[torch.Tensor]:
        states = []
        max_length = 0
        for idx in sample_indices.tolist():
            key = (layer_id, idx)
            if key not in self.cache:
                return None
            state = self.cache[key]
            states.append(state)
            max_length = max(max_length, state.shape[-1])
        if len(states) > 0 and states[0].dim() >= 2:
            padded_states = []
            for state in states:
                if state.shape[-1] < max_length:
                    pad_size = max_length - state.shape[-1]
                    padded_state = F.pad(state, (pad_size, 0), mode="constant", value=0)
                    padded_states.append(padded_state)
                else:
                    padded_states.append(state)
            return torch.stack(padded_states, dim=0)
        else:
            return torch.stack(states, dim=0)

    def set(self, layer_id: str, sample_indices: torch.Tensor, states: torch.Tensor):
        for i, idx in enumerate(sample_indices.tolist()):
            key = (layer_id, idx)
            self.cache[key] = states[i].detach()

    def set_to_zero(self, sample_indices: torch.Tensor):
        for key in list(self.cache.keys()):
            layer_id, sample_idx = key
            if sample_idx in sample_indices.tolist():
                cached_tensor = self.cache[key]
                self.cache[key] = torch.zeros_like(cached_tensor)

    def clear(
        self,
        layer_id: Optional[str] = None,
        sample_indices: Optional[torch.Tensor] = None,
    ):
        if layer_id is None and sample_indices is None:
            self.cache.clear()
        elif layer_id is not None and sample_indices is None:
            keys_to_remove = [k for k in self.cache.keys() if k[0] == layer_id]
            for k in keys_to_remove:
                del self.cache[k]
        elif layer_id is not None and sample_indices is not None:
            for idx in sample_indices.tolist():
                key = (layer_id, idx)
                self.cache.pop(key, None)


class SConv1d(nn.Module):

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: int,
        stride: int = 1,
        dilation: int = 1,
        groups: int = 1,
        bias: bool = True,
        causal: bool = False,
        norm: str = "none",
        norm_kwargs: tp.Dict[str, tp.Any] = {},
        pad_mode: str = "reflect",
    ):
        super().__init__()
        self.conv = NormConv1d(
            in_channels,
            out_channels,
            kernel_size,
            stride,
            dilation=dilation,
            groups=groups,
            bias=bias,
            causal=causal,
            norm=norm,
            norm_kwargs=norm_kwargs,
        )
        self.causal = causal
        self.pad_mode = pad_mode
        self.kernel_size = kernel_size
        self.dilation = dilation
        self.stride = stride
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.context_size = (kernel_size - 1) * dilation - (stride - 1)
        self.padding_total = (kernel_size - 1) * dilation - (stride - 1)
        self._layer_id = None

    @property
    def layer_id(self):
        if self._layer_id is None:
            self._layer_id = f"sconv1d_{id (self )}"
        return self._layer_id

    def forward(
        self,
        x: torch.Tensor,
        cache: Optional[QWEN3VoxTokenizerStreamingCache] = None,
        sample_indices: Optional[torch.Tensor] = None,
        use_cache: bool = False,
        debug: bool = False,
    ) -> torch.Tensor:
        B, C, T = x.shape
        if not use_cache or cache is None:
            return self._forward_non_streaming(x, debug=debug)
        assert self.causal, "Streaming mode is only supported for causal convolutions"
        assert (
            sample_indices is not None
        ), "sample_indices must be provided for streaming mode"
        assert len(sample_indices) == B, "sample_indices must match batch size"
        return self._forward_streaming(x, cache, sample_indices, debug)

    def _forward_streaming(
        self,
        x: torch.Tensor,
        cache: QWEN3VoxTokenizerStreamingCache,
        sample_indices: torch.Tensor,
        debug: bool = False,
    ) -> torch.Tensor:
        B, C, T = x.shape
        cached_states = cache.get(self.layer_id, sample_indices)
        if cached_states is None:
            if self.context_size > 0:
                cached_states = torch.zeros(
                    B, C, self.context_size, device=x.device, dtype=x.dtype
                )
                if debug:
                    print(
                        f"[DEBUG] Initialized cache with shape: {cached_states .shape }, context_size={self .context_size }"
                    )
            else:
                cached_states = torch.zeros(B, C, 0, device=x.device, dtype=x.dtype)
                if debug:
                    print(f"[DEBUG] No context needed (kernel_size=stride)")
        if cached_states.shape[2] > 0:
            input_with_context = torch.cat([cached_states, x], dim=2)
        else:
            input_with_context = x
        if debug:
            print(
                f"[DEBUG] Input shape: {x .shape }, Cache shape: {cached_states .shape }, Combined: {input_with_context .shape }"
            )
        output = self.conv(input_with_context)
        if debug:
            print(f"[DEBUG] Output shape: {output .shape }")
        if self.context_size > 0:
            total_input_length = input_with_context.shape[2]
            if total_input_length >= self.context_size:
                new_cache_start = total_input_length - self.context_size
                new_cache = input_with_context[:, :, new_cache_start:]
            else:
                new_cache = input_with_context
            if debug:
                print(f"[DEBUG] New cache shape: {new_cache .shape }")
            cache.set(self.layer_id, sample_indices, new_cache)
        return output

    def _forward_non_streaming(
        self, x: torch.Tensor, debug: bool = False
    ) -> torch.Tensor:
        B, C, T = x.shape
        kernel_size = self.kernel_size
        stride = self.stride
        dilation = self.dilation
        padding_total = self.padding_total
        extra_padding = get_extra_padding_for_conv1d(
            x, kernel_size, stride, padding_total
        )
        if debug:
            print(
                f"[DEBUG NON-STREAMING] Input shape: {x .shape }, padding_total={padding_total }, extra_padding={extra_padding }"
            )
        if self.causal:
            if self.pad_mode == "constant":
                x = pad1d(
                    x, (padding_total, extra_padding), mode=self.pad_mode, value=0
                )
            else:
                x = pad1d(x, (padding_total, extra_padding), mode=self.pad_mode)
        else:
            padding_right = padding_total // 2
            padding_left = padding_total - padding_right
            x = pad1d(
                x, (padding_left, padding_right + extra_padding), mode=self.pad_mode
            )
        if debug:
            print(f"[DEBUG NON-STREAMING] After padding: {x .shape }")
        output = self.conv(x)
        if debug:
            print(f"[DEBUG NON-STREAMING] Output shape: {output .shape }")
        return output


class SConvTranspose1d(nn.Module):

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: int,
        stride: int = 1,
        causal: bool = False,
        norm: str = "none",
        trim_right_ratio: float = 1.0,
        norm_kwargs: tp.Dict[str, tp.Any] = {},
        bias: bool = True,
    ):
        super().__init__()
        self.convtr = NormConvTranspose1d(
            in_channels,
            out_channels,
            kernel_size,
            stride,
            causal=causal,
            norm=norm,
            norm_kwargs=norm_kwargs,
            bias=bias,
        )
        self.causal = causal
        self.trim_right_ratio = trim_right_ratio
        assert (
            self.causal or self.trim_right_ratio == 1.0
        ), "`trim_right_ratio` != 1.0 only makes sense for causal convolutions"
        assert self.trim_right_ratio >= 0.0 and self.trim_right_ratio <= 1.0
        self.kernel_size = kernel_size
        self.stride = stride
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.padding_total = kernel_size - stride
        self.context_size = kernel_size - 1
        self._layer_id = None

    @property
    def layer_id(self):
        if self._layer_id is None:
            self._layer_id = f"sconvtr1d_{id (self )}"
        return self._layer_id

    def forward(
        self,
        x: torch.Tensor,
        cache: Optional[QWEN3VoxTokenizerStreamingCache] = None,
        sample_indices: Optional[torch.Tensor] = None,
        use_cache: bool = False,
        debug: bool = False,
    ) -> torch.Tensor:
        B, C, T = x.shape
        if not use_cache or cache is None:
            return self._forward_non_streaming(x, debug=debug)
        assert (
            sample_indices is not None
        ), "sample_indices must be provided for streaming mode"
        assert len(sample_indices) == B, "sample_indices must match batch size"
        return self._forward_streaming(x, cache, sample_indices, debug)

    def _forward_streaming(
        self,
        x: torch.Tensor,
        cache: QWEN3VoxTokenizerStreamingCache,
        sample_indices: torch.Tensor,
        debug: bool = False,
    ) -> torch.Tensor:
        B, C, T = x.shape
        cached_input = cache.get(self.layer_id, sample_indices)
        if cached_input is None:
            cached_input = torch.zeros(B, C, 0, device=x.device, dtype=x.dtype)
            if debug:
                print(f"[DEBUG] Initialized empty cache for transposed conv")
        full_input = torch.cat([cached_input, x], dim=2)
        if debug:
            print(
                f"[DEBUG] Input shape: {x .shape }, Cache shape: {cached_input .shape }, Combined: {full_input .shape }"
            )
        full_output = self.convtr(full_input)
        if debug:
            print(f"[DEBUG] Full transposed conv output shape: {full_output .shape }")
        if self.causal:
            padding_right = math.ceil(self.padding_total * self.trim_right_ratio)
            padding_left = self.padding_total - padding_right
        else:
            padding_right = self.padding_total // 2
            padding_left = self.padding_total - padding_right
        if padding_left + padding_right > 0:
            full_output = unpad1d(full_output, (padding_left, padding_right))
        if debug:
            print(f"[DEBUG] After unpadding: {full_output .shape }")
        if cached_input.shape[2] == 0:
            output = full_output
        else:
            expected_new_output = T * self.stride
            if full_output.shape[2] >= expected_new_output:
                output = full_output[:, :, -expected_new_output:]
            else:
                output = full_output
        if debug:
            print(f"[DEBUG] Final streaming output shape: {output .shape }")
        if full_input.shape[2] > self.context_size:
            new_cache = full_input[:, :, -self.context_size :]
        else:
            new_cache = full_input
        if debug:
            print(f"[DEBUG] New cache shape: {new_cache .shape }")
        cache.set(self.layer_id, sample_indices, new_cache)
        return output

    def _forward_non_streaming(
        self, x: torch.Tensor, debug: bool = False
    ) -> torch.Tensor:
        if debug:
            print(f"[DEBUG NON-STREAMING] Input shape: {x .shape }")
        y = self.convtr(x)
        if debug:
            print(f"[DEBUG NON-STREAMING] After transposed conv: {y .shape }")
        if self.causal:
            padding_right = math.ceil(self.padding_total * self.trim_right_ratio)
            padding_left = self.padding_total - padding_right
        else:
            padding_right = self.padding_total // 2
            padding_left = self.padding_total - padding_right
        if padding_left + padding_right > 0:
            y = unpad1d(y, (padding_left, padding_right))
        if debug:
            print(f"[DEBUG NON-STREAMING] Final output shape: {y .shape }")
        return y


class FFN(nn.Module):

    def __init__(self, embed_dim, ffn_dim, bias=False):
        super().__init__()
        self.embed_dim = embed_dim
        self.linear1 = nn.Linear(self.embed_dim, ffn_dim, bias=bias)
        self.gelu = ACT2FN["gelu"]
        self.linear2 = nn.Linear(ffn_dim, self.embed_dim, bias=bias)

    def forward(self, x):
        x = self.linear1(x)
        x = self.gelu(x)
        x = self.linear2(x)
        return x


class Convlayer(nn.Module):

    def __init__(
        self,
        in_channels,
        out_channels,
        kernel_size,
        stride=1,
        dilation=1,
        groups=1,
        bias=True,
        pad_mode="zeros",
        norm="weight_norm",
        causal=True,
    ):
        super().__init__()
        self.conv = SConv1d(
            in_channels,
            out_channels,
            kernel_size,
            stride=stride,
            dilation=dilation,
            groups=groups,
            bias=bias,
            pad_mode=pad_mode,
            norm=norm,
            causal=causal,
        )

    def forward(self, x):
        return self.conv(x)


class Block1D(nn.Module):

    def __init__(
        self,
        dim,
        kernel_size=7,
        drop_path=0.0,
        mixer_layer="conv",
        layer_scale_init_value=1e-06,
        **kwargs,
    ):
        super().__init__()
        if kwargs.get("layernorm", "LN") == "LN":
            self.norm = ConvLayerNorm(dim, eps=kwargs.get("eps", 1e-06))
            self.ffn_norm = ConvLayerNorm(dim, eps=kwargs.get("eps", 1e-06))
        elif kwargs.get("layernorm", "RMSNorm") == "RMSNorm":
            self.norm = ConvRMSNorm(dim, eps=kwargs.get("eps", 1e-06))
            self.ffn_norm = ConvRMSNorm(dim, eps=kwargs.get("eps", 1e-06))
        if mixer_layer == "conv":
            self.mixer = Convlayer(
                dim,
                dim,
                groups=kwargs.get("groups", 1),
                kernel_size=kernel_size,
                pad_mode=kwargs.get("pad_mode", "reflect"),
                norm=kwargs.get("norm", "none"),
                causal=kwargs.get("causal", True),
                bias=kwargs.get("bias", True),
            )
        elif mixer_layer == "depthwise_conv":
            self.mixer = Convlayer(
                dim,
                dim,
                groups=dim,
                kernel_size=kernel_size,
                pad_mode=kwargs.get("pad_mode", "reflect"),
                norm=kwargs.get("norm", "none"),
                causal=kwargs.get("causal", True),
                bias=kwargs.get("bias", True),
            )
        else:
            raise ValueError(f"Unsupported mixer layer: {mixer_layer }")
        self.ffn = FFN(
            dim, kwargs.get("ffn_expansion", 4) * dim, bias=kwargs.get("bias", False)
        )
        self.drop_path = (
            nn.Identity() if drop_path <= 0.0 else nn.modules.DropPath(drop_path)
        )
        if layer_scale_init_value > 0:
            self.gamma = nn.Parameter(
                layer_scale_init_value * torch.ones(dim), requires_grad=True
            )
            self.ffn_gamma = nn.Parameter(
                layer_scale_init_value * torch.ones(dim), requires_grad=True
            )
        else:
            self.gamma = None
            self.ffn_gamma = None

    def forward(self, x):
        residual = x
        x = self.norm(x)
        x = self.mixer(x)
        if self.gamma is not None:
            x = x * self.gamma.unsqueeze(-1)
        x = residual + self.drop_path(x)
        residual = x
        x = self.ffn_norm(x)
        x = x.permute(0, 2, 1)
        x = self.ffn(x)
        x = x.permute(0, 2, 1)
        if self.ffn_gamma is not None:
            x = x * self.ffn_gamma.unsqueeze(-1)
        x = residual + self.drop_path(x)
        return x


class TokenizerEncoder(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.channels = config.channels
        self.dimension = config.dimension
        self.n_filters = config.n_filters
        self.ratios = list(reversed(config.ratios))
        self.depths = config.depths
        self.n_residual_layers = getattr(config, "n_residual_layers", 1)
        self.hop_length = np.prod(self.ratios)
        self.causal = config.causal
        kernel_size = getattr(config, "kernel_size", 7)
        last_kernel_size = getattr(config, "last_kernel_size", 7)
        norm = getattr(config, "norm", "none")
        norm_params = getattr(config, "norm_params", {})
        pad_mode = getattr(config, "pad_mode", "reflect")
        bias = getattr(config, "bias", True)
        layernorm = getattr(config, "layernorm", "LN")
        layernorm_eps = getattr(config, "layernorm_eps", 1e-06)
        layernorm_elementwise_affine = getattr(
            config, "layernorm_elementwise_affine", True
        )
        drop_path_rate = getattr(config, "drop_path_rate", 0.0)
        mixer_layer = getattr(config, "mixer_layer", "conv")
        layer_scale_init_value = getattr(config, "layer_scale_init_value", 0)
        disable_last_norm = getattr(config, "disable_last_norm", False)
        if layernorm == "LN":
            norm_type = ConvLayerNorm
        elif layernorm == "RMSNorm":
            norm_type = partial(
                ConvRMSNorm, elementwise_affine=layernorm_elementwise_affine
            )
        else:
            raise ValueError(f"Unsupported norm type: {layernorm }")
        stem = nn.Sequential(
            SConv1d(
                self.channels,
                self.n_filters,
                kernel_size,
                norm=norm,
                norm_kwargs=norm_params,
                causal=self.causal,
                pad_mode=pad_mode,
                bias=bias,
            )
        )
        self.downsample_layers = nn.ModuleList()
        self.downsample_layers.append(stem)
        for i in range(len(self.ratios)):
            in_ch = self.n_filters * 2**i
            out_ch = self.n_filters * 2 ** (i + 1)
            downsample_layer = nn.Sequential(
                SConv1d(
                    in_ch,
                    out_ch,
                    kernel_size=self.ratios[i] * 2,
                    stride=self.ratios[i],
                    causal=self.causal,
                    pad_mode=pad_mode,
                    norm=norm,
                    bias=bias,
                )
            )
            self.downsample_layers.append(downsample_layer)
        layer_type = partial(
            Block1D,
            mixer_layer=mixer_layer,
            layernorm=layernorm,
            eps=layernorm_eps,
            causal=self.causal,
            pad_mode=pad_mode,
            norm=norm,
            bias=bias,
            layer_scale_init_value=layer_scale_init_value,
        )
        self.stages = nn.ModuleList()
        dp_rates = [
            x.item() for x in torch.linspace(0, drop_path_rate, sum(self.depths))
        ]
        cur = 0
        for i in range(len(self.depths)):
            in_ch = self.n_filters * 2**i
            stage = nn.Sequential(
                *[
                    layer_type(dim=in_ch, drop_path=dp_rates[cur + j])
                    for j in range(self.depths[i])
                ]
            )
            self.stages.append(stage)
            cur += self.depths[i]
        if not disable_last_norm:
            self.norm = norm_type(in_ch, eps=layernorm_eps)
        else:
            self.norm = nn.Identity()
        self.head = SConv1d(
            in_ch,
            self.dimension,
            kernel_size=last_kernel_size,
            causal=self.causal,
            pad_mode=pad_mode,
            norm=norm,
            bias=bias,
        )

    def forward_features(
        self, x, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        for i in range(len(self.depths)):
            for layer in self.downsample_layers[i]:
                if isinstance(layer, SConv1d):
                    x = layer(
                        x,
                        cache=cache,
                        sample_indices=sample_indices,
                        use_cache=use_cache,
                        debug=debug,
                    )
                else:
                    x = layer(x)
            for block in self.stages[i]:
                if (
                    hasattr(block, "mixer")
                    and hasattr(block.mixer, "conv")
                    and isinstance(block.mixer.conv, SConv1d)
                ):
                    residual = x
                    x = block.norm(x)
                    x = block.mixer.conv(
                        x,
                        cache=cache,
                        sample_indices=sample_indices,
                        use_cache=use_cache,
                        debug=debug,
                    )
                    if block.gamma is not None:
                        x = x * block.gamma.unsqueeze(-1)
                    x = residual + x
                    residual = x
                    x = block.ffn_norm(x)
                    x = x.permute(0, 2, 1)
                    x = block.ffn(x)
                    x = x.permute(0, 2, 1)
                    if block.ffn_gamma is not None:
                        x = x * block.ffn_gamma.unsqueeze(-1)
                    x = residual + x
                else:
                    x = block(x)
        return self.norm(x)

    def forward(self, x, cache=None, sample_indices=None, use_cache=False, debug=False):
        x = self.forward_features(
            x,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        x = self.head(
            x,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return x


class TokenizerDecoder(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.dimension = config.dimension
        self.channels = config.channels
        self.n_filters = config.n_filters
        self.ratios = config.ratios
        self.depths = config.depths
        self.n_residual_layers = getattr(config, "n_residual_layers", 1)
        self.hop_length = np.prod(self.ratios)
        self.causal = config.causal
        kernel_size = getattr(config, "kernel_size", 7)
        last_kernel_size = getattr(config, "last_kernel_size", 7)
        norm = getattr(config, "norm", "none")
        norm_params = getattr(config, "norm_params", {})
        pad_mode = getattr(config, "pad_mode", "reflect")
        bias = getattr(config, "bias", True)
        layernorm = getattr(config, "layernorm", "LN")
        layernorm_eps = getattr(config, "layernorm_eps", 1e-06)
        trim_right_ratio = getattr(config, "trim_right_ratio", 1.0)
        layernorm_elementwise_affine = getattr(
            config, "layernorm_elementwise_affine", True
        )
        drop_path_rate = getattr(config, "drop_path_rate", 0.0)
        mixer_layer = getattr(config, "mixer_layer", "conv")
        layer_scale_init_value = getattr(config, "layer_scale_init_value", 0)
        disable_last_norm = getattr(config, "disable_last_norm", False)
        if layernorm == "LN":
            norm_type = ConvLayerNorm
        elif layernorm == "RMSNorm":
            norm_type = partial(
                ConvRMSNorm, elementwise_affine=layernorm_elementwise_affine
            )
        else:
            raise ValueError(f"Unsupported norm type: {layernorm }")
        stem = nn.Sequential(
            SConv1d(
                self.dimension,
                self.n_filters * 2 ** (len(self.depths) - 1),
                kernel_size,
                norm=norm,
                norm_kwargs=norm_params,
                causal=self.causal,
                pad_mode=pad_mode,
                bias=bias,
            )
        )
        self.upsample_layers = nn.ModuleList()
        self.upsample_layers.append(stem)
        for i in range(len(self.ratios)):
            in_ch = self.n_filters * 2 ** (len(self.depths) - 1 - i)
            out_ch = self.n_filters * 2 ** (len(self.depths) - 1 - i - 1)
            upsample_layer = nn.Sequential(
                SConvTranspose1d(
                    in_ch,
                    out_ch,
                    kernel_size=self.ratios[i] * 2,
                    stride=self.ratios[i],
                    norm=norm,
                    norm_kwargs=norm_params,
                    bias=bias,
                    causal=self.causal,
                    trim_right_ratio=trim_right_ratio,
                )
            )
            self.upsample_layers.append(upsample_layer)
        layer_type = partial(
            Block1D,
            mixer_layer=mixer_layer,
            layernorm=layernorm,
            eps=layernorm_eps,
            causal=self.causal,
            pad_mode=pad_mode,
            norm=norm,
            bias=bias,
            layer_scale_init_value=layer_scale_init_value,
        )
        self.stages = nn.ModuleList()
        dp_rates = [
            x.item() for x in torch.linspace(0, drop_path_rate, sum(self.depths))
        ]
        cur = 0
        for i in range(len(self.depths)):
            in_ch = self.n_filters * 2 ** (len(self.depths) - 1 - i)
            stage = nn.Sequential(
                *[
                    layer_type(dim=in_ch, drop_path=dp_rates[cur + j])
                    for j in range(self.depths[i])
                ]
            )
            self.stages.append(stage)
            cur += self.depths[i]
        if not disable_last_norm:
            self.norm = norm_type(in_ch, eps=layernorm_eps)
        else:
            self.norm = nn.Identity()
        self.head = SConv1d(
            in_ch,
            self.channels,
            kernel_size=last_kernel_size,
            causal=self.causal,
            pad_mode=pad_mode,
            norm=norm,
            bias=bias,
        )

    def forward_features(
        self, x, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        for i in range(len(self.depths)):
            for layer in self.upsample_layers[i]:
                if isinstance(layer, (SConv1d, SConvTranspose1d)):
                    x = layer(
                        x,
                        cache=cache,
                        sample_indices=sample_indices,
                        use_cache=use_cache,
                        debug=debug,
                    )
                else:
                    x = layer(x)
            for block in self.stages[i]:
                if (
                    hasattr(block, "mixer")
                    and hasattr(block.mixer, "conv")
                    and isinstance(block.mixer.conv, SConv1d)
                ):
                    residual = x
                    x = block.norm(x)
                    x = block.mixer.conv(
                        x,
                        cache=cache,
                        sample_indices=sample_indices,
                        use_cache=use_cache,
                        debug=debug,
                    )
                    if block.gamma is not None:
                        x = x * block.gamma.unsqueeze(-1)
                    x = residual + x
                    residual = x
                    x = block.ffn_norm(x)
                    x = x.permute(0, 2, 1)
                    x = block.ffn(x)
                    x = x.permute(0, 2, 1)
                    if block.ffn_gamma is not None:
                        x = x * block.ffn_gamma.unsqueeze(-1)
                    x = residual + x
                else:
                    x = block(x)
        return self.norm(x)

    def forward(self, x, cache=None, sample_indices=None, use_cache=False, debug=False):
        x = self.forward_features(
            x,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        x = self.head(
            x,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return x


@dataclass
class QWEN3VoxTokenizerEncoderOutput:
    mean: torch.Tensor
    std: Optional[Union[float, torch.Tensor]] = None

    def sample(self, dist_type="fix"):
        if dist_type == "fix":
            x = self.mean + self.std * torch.randn_like(self.mean)
            return (x, self.std)
        elif dist_type == "gaussian":
            batch_size = self.mean.size(0)
            value = self.std / 0.8
            std = (
                torch.randn(batch_size, device=self.mean.device, dtype=self.mean.dtype)
                * value
            )
            while std.dim() < self.mean.dim():
                std = std.unsqueeze(-1)
            x = self.mean + std * torch.randn_like(self.mean)
            return (x, std)
        else:
            return (self.mean, self.std)

    def kl(self):
        target = torch.zeros_like(self.mean)
        return F.mse_loss(self.mean, target, reduction="none")

    def mode(self):
        return self.mean


class QWEN3VoxAcousticTokenizerModel(PreTrainedModel):
    config_class = QWEN3VoxAcousticTokenizerConfig
    base_model_prefix = 'vibevoice_acoustic_tokenizer'
    _supports_flash_attn_2 = True
    _supports_sdpa = True
    _no_split_modules = ["TokenizerEncoder", "TokenizerDecoder"]

    def __init__(self, config):
        super().__init__(config)
        self.register_buffer("fix_std", torch.tensor(config.fix_std), persistent=False)
        self.std_dist_type = getattr(config, "std_dist_type", "fix")
        if isinstance(config.encoder_depths, str):
            encoder_depths = [int(d) for d in config.encoder_depths.split("-")]
        else:
            encoder_depths = config.encoder_depths
        if config.decoder_depths is not None and isinstance(config.decoder_depths, str):
            decoder_depths = [int(d) for d in config.decoder_depths.split("-")]
        else:
            decoder_depths = list(reversed(encoder_depths))
        encoder_config = copy.deepcopy(config)
        encoder_config.dimension = config.vae_dim
        encoder_config.n_filters = config.encoder_n_filters
        encoder_config.ratios = config.encoder_ratios
        encoder_config.depths = encoder_depths
        encoder_config.norm = config.conv_norm
        encoder_config.pad_mode = config.pad_mode
        encoder_config.bias = config.conv_bias
        encoder_config.layernorm_eps = config.layernorm_eps
        encoder_config.layernorm_elementwise_affine = (
            config.layernorm_elementwise_affine
        )
        encoder_config.mixer_layer = config.mixer_layer
        encoder_config.layer_scale_init_value = config.layer_scale_init_value
        encoder_config.disable_last_norm = config.disable_last_norm
        decoder_config = copy.deepcopy(config)
        decoder_config.dimension = config.vae_dim
        decoder_config.n_filters = config.decoder_n_filters
        decoder_config.ratios = config.decoder_ratios
        decoder_config.depths = decoder_depths
        decoder_config.norm = config.conv_norm
        decoder_config.pad_mode = config.pad_mode
        decoder_config.bias = config.conv_bias
        decoder_config.layernorm_eps = config.layernorm_eps
        decoder_config.layernorm_elementwise_affine = (
            config.layernorm_elementwise_affine
        )
        decoder_config.mixer_layer = config.mixer_layer
        decoder_config.layer_scale_init_value = config.layer_scale_init_value
        decoder_config.disable_last_norm = config.disable_last_norm
        self.encoder = TokenizerEncoder(encoder_config)
        self.decoder = TokenizerDecoder(decoder_config)
        self.apply(self._init_weights)

    def _init_weights(self, module):
        if isinstance(module, nn.Linear):
            nn.init.normal_(module.weight, std=self.config.weight_init_value)
            if module.bias is not None:
                nn.init.zeros_(module.bias)
        elif isinstance(module, nn.LayerNorm):
            nn.init.ones_(module.weight)
            nn.init.zeros_(module.bias)
        elif isinstance(module, nn.Conv1d):
            nn.init.normal_(module.weight, std=self.config.weight_init_value)
            if module.bias is not None:
                nn.init.zeros_(module.bias)

    @torch.no_grad()
    def encode(
        self, audio, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        latents = self.encoder(
            audio,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return QWEN3VoxTokenizerEncoderOutput(
            mean=latents.permute(0, 2, 1), std=self.fix_std
        )

    @torch.no_grad()
    def sampling(self, encoder_output, dist_type=None):
        dist_type = dist_type or self.std_dist_type
        if dist_type == "fix":
            return encoder_output.sample(dist_type="fix")
        elif dist_type == "gaussian":
            return encoder_output.sample(dist_type="gaussian")
        else:
            raise ValueError(
                f"Unsupported dist_type: {dist_type }, expected 'fix' or 'gaussian'"
            )

    @torch.no_grad()
    def decode(
        self, latents, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        if latents.shape[1] == self.config.vae_dim:
            pass
        else:
            latents = latents.permute(0, 2, 1)
        audio = self.decoder(
            latents,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return audio

    def forward(
        self, audio, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        encoder_output = self.encode(
            audio,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        sampled_latents, _ = self.sampling(encoder_output)
        reconstructed = self.decode(
            sampled_latents,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return (reconstructed, sampled_latents)


class QWEN3VoxSemanticTokenizerModel(PreTrainedModel):
    config_class = QWEN3VoxSemanticTokenizerConfig
    base_model_prefix = 'vibevoice_semantic_tokenizer'
    _supports_flash_attn_2 = True
    _supports_sdpa = True
    _no_split_modules = ["TokenizerEncoder"]

    def __init__(self, config):
        super().__init__(config)
        if isinstance(config.encoder_depths, str):
            encoder_depths = [int(d) for d in config.encoder_depths.split("-")]
        else:
            encoder_depths = config.encoder_depths
        encoder_config = copy.deepcopy(config)
        encoder_config.dimension = config.vae_dim
        encoder_config.n_filters = config.encoder_n_filters
        encoder_config.ratios = config.encoder_ratios
        encoder_config.depths = encoder_depths
        encoder_config.norm = config.conv_norm
        encoder_config.pad_mode = config.pad_mode
        encoder_config.bias = config.conv_bias
        encoder_config.layernorm_eps = config.layernorm_eps
        encoder_config.layernorm_elementwise_affine = (
            config.layernorm_elementwise_affine
        )
        encoder_config.mixer_layer = config.mixer_layer
        encoder_config.layer_scale_init_value = config.layer_scale_init_value
        encoder_config.disable_last_norm = config.disable_last_norm
        self.encoder = TokenizerEncoder(encoder_config)
        self.apply(self._init_weights)

    def _init_weights(self, module):
        if isinstance(module, nn.Linear):
            nn.init.normal_(module.weight, std=self.config.weight_init_value)
            if module.bias is not None:
                nn.init.zeros_(module.bias)
        elif isinstance(module, nn.LayerNorm):
            nn.init.ones_(module.weight)
            nn.init.zeros_(module.bias)
        elif isinstance(module, nn.Conv1d):
            nn.init.normal_(module.weight, std=self.config.weight_init_value)
            if module.bias is not None:
                nn.init.zeros_(module.bias)

    @torch.no_grad()
    def encode(
        self, audio, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        latents = self.encoder(
            audio,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        return QWEN3VoxTokenizerEncoderOutput(mean=latents.permute(0, 2, 1))

    @torch.no_grad()
    def sampling(self, encoder_output, dist_type=None):
        return encoder_output.sample(dist_type="none")

    def forward(
        self, audio, cache=None, sample_indices=None, use_cache=False, debug=False
    ):
        encoder_output = self.encode(
            audio,
            cache=cache,
            sample_indices=sample_indices,
            use_cache=use_cache,
            debug=debug,
        )
        sampled_latents, _ = self.sampling(encoder_output, dist_type="none")
        return (None, sampled_latents)


AutoModel.register(QWEN3VoxAcousticTokenizerConfig, QWEN3VoxAcousticTokenizerModel)
AutoModel.register(QWEN3VoxSemanticTokenizerConfig, QWEN3VoxSemanticTokenizerModel)
__all__ = [
    'QWEN3VoxTokenizerStreamingCache',
    'QWEN3VoxAcousticTokenizerModel',
    'QWEN3VoxSemanticTokenizerModel',
]
'\nProcessor class for QWEN3Vox ASR models.\n'
import os
import json
import math
import warnings
from typing import List, Optional, Union, Dict, Any, Tuple
import numpy as np
import torch
from transformers.tokenization_utils_base import BatchEncoding
from transformers.utils import TensorType, logging

logger = logging.get_logger(__name__)
SYSTEM_PROMPT = "You are a helpful assistant that transcribes audio input into text output in JSON format."


class QWEN3VoxASRProcessor:

    def __init__(
        self,
        tokenizer=None,
        audio_processor=None,
        speech_tok_compress_ratio=320,
        target_sample_rate=22050,
        normalize_audio=True,
        **kwargs,
    ):
        self.tokenizer = tokenizer
        self.audio_processor = audio_processor or QWEN3VoxTokenizerProcessor(
            sampling_rate=target_sample_rate, normalize_audio=normalize_audio
        )
        self.speech_tok_compress_ratio = speech_tok_compress_ratio
        self.target_sample_rate = target_sample_rate
        self.normalize_audio = normalize_audio
        if normalize_audio:
            self.audio_normalizer = AudioNormalizer()
        else:
            self.audio_normalizer = None
        self._cache_special_tokens()

    def _cache_special_tokens(self):
        if hasattr(self.tokenizer, "speech_start_id"):
            self.speech_start_id = self.tokenizer.speech_start_id
        else:
            self.speech_start_id = self.tokenizer.convert_tokens_to_ids(
                "<|speech_start|>"
            )
        if hasattr(self.tokenizer, "speech_end_id"):
            self.speech_end_id = self.tokenizer.speech_end_id
        else:
            self.speech_end_id = self.tokenizer.convert_tokens_to_ids("<|speech_end|>")
        if hasattr(self.tokenizer, "speech_pad_id"):
            self.speech_pad_id = self.tokenizer.speech_pad_id
        else:
            self.speech_pad_id = self.tokenizer.convert_tokens_to_ids("<|speech_pad|>")
        if hasattr(self.tokenizer, "pad_id"):
            self.pad_id = self.tokenizer.pad_id
        elif hasattr(self.tokenizer, "pad_token_id"):
            self.pad_id = self.tokenizer.pad_token_id
        else:
            self.pad_id = self.tokenizer.convert_tokens_to_ids("<|endoftext|>")

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
        import json
        from transformers.utils import cached_file

        model_name = str(pretrained_model_name_or_path)
        config_path = os.path.join(
            model_name, "preprocessor_config.json"
        )
        config = {}
        if os.path.exists(config_path):
            with open(config_path, "r") as f:
                config = json.load(f)
        else:
            try:
                config_file = cached_file(
                    model_name, "preprocessor_config.json", **kwargs
                )
                with open(config_file, "r") as f:
                    config = json.load(f)
            except Exception as e:
                logger.warning(f"Could not load preprocessor_config.json: {e }")
                logger.warning("Using default configuration")
        speech_tok_compress_ratio = config.get("speech_tok_compress_ratio", 3200)
        target_sample_rate = config.get("target_sample_rate", 22050)
        normalize_audio = config.get("normalize_audio", True)
        language_model_pretrained_name = config.get(
            "language_model_pretrained_name", None
        ) or kwargs.pop("language_model_pretrained_name", None)
        if not language_model_pretrained_name:
            language_model_pretrained_name = model_name
        logger.info(f"Loading tokenizer from repo {model_name }")
        tokenizer = QWEN3VoxASRTextTokenizerFast.from_pretrained(
            model_name, **kwargs
        )
        audio_processor = QWEN3VoxTokenizerProcessor(
            sampling_rate=target_sample_rate,
            normalize_audio=normalize_audio,
            target_dB_FS=config.get("target_dB_FS", -25),
            eps=config.get("eps", 1e-06),
        )
        return cls(
            tokenizer=tokenizer,
            audio_processor=audio_processor,
            speech_tok_compress_ratio=speech_tok_compress_ratio,
            target_sample_rate=target_sample_rate,
            normalize_audio=normalize_audio,
        )

    def save_pretrained(self, save_directory: Union[str, os.PathLike], **kwargs):
        import json

        os.makedirs(save_directory, exist_ok=True)
        processor_config = {
            "processor_class": "QWEN3VoxASRProcessor",
            "speech_tok_compress_ratio": self.speech_tok_compress_ratio,
            "target_sample_rate": self.target_sample_rate,
            "normalize_audio": self.normalize_audio,
            "target_dB_FS": -25,
            "eps": 1e-06,
        }
        config_path = os.path.join(save_directory, "preprocessor_config.json")
        with open(config_path, "w") as f:
            json.dump(processor_config, f, indent=2)
        logger.info(f"Processor configuration saved in {config_path }")

    def __call__(
        self,
        audio: Optional[
            Union[
                str,
                np.ndarray,
                torch.Tensor,
                List[Union[str, np.ndarray, torch.Tensor]],
            ]
        ] = None,
        sampling_rate: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        padding: bool = True,
        max_length: Optional[int] = None,
        truncation: bool = False,
        add_generation_prompt: bool = True,
        use_streaming: bool = True,
        context_info: Optional[str] = None,
        **kwargs,
    ) -> BatchEncoding:
        if audio is None:
            raise ValueError("Audio input is required for ASR processing")
        if isinstance(audio, list):
            is_batched = True
            audio_list = audio
        else:
            is_batched = False
            audio_list = [audio]
        all_encodings = []
        for audio_input in audio_list:
            encoding = self._process_single_audio(
                audio_input,
                sampling_rate=sampling_rate,
                add_generation_prompt=add_generation_prompt,
                use_streaming=use_streaming,
                context_info=context_info,
            )
            all_encodings.append(encoding)
        batch_encoding = self._batch_encode(
            all_encodings,
            padding=padding,
            max_length=max_length,
            truncation=truncation,
            return_tensors=return_tensors,
        )
        return batch_encoding

    def _process_single_audio(
        self,
        audio: Union[str, np.ndarray, torch.Tensor],
        sampling_rate: Optional[int] = None,
        add_generation_prompt: bool = True,
        use_streaming: bool = True,
        context_info: Optional[str] = None,
    ) -> Dict[str, Any]:
        if isinstance(audio, str):
            import soundfile as sf

            audio_array, file_sr = sf.read(audio)
            if audio_array.ndim > 1:
                audio_array = audio_array.mean(axis=1)
            if file_sr != self.target_sample_rate:
                import librosa

                audio_array = librosa.resample(
                    audio_array, orig_sr=file_sr, target_sr=self.target_sample_rate
                )
        elif isinstance(audio, torch.Tensor):
            audio_array = audio.cpu().numpy()
            if audio_array.ndim > 1:
                audio_array = audio_array.squeeze()
        else:
            audio_array = np.array(audio, dtype=np.float32)
            if audio_array.ndim > 1:
                audio_array = audio_array.squeeze()
        audio_array = audio_array.astype(np.float32)
        if self.normalize_audio and self.audio_normalizer:
            audio_array = self.audio_normalizer(audio_array)
        audio_duration = len(audio_array) / self.target_sample_rate
        if use_streaming and audio_duration < 60.0:
            use_streaming = False
        vae_tok_len = math.ceil(len(audio_array) / self.speech_tok_compress_ratio)
        system_prompt_text = self.tokenizer.apply_chat_template(
            [{"role": "system", "content": SYSTEM_PROMPT}], tokenize=False
        )
        system_tokens = self.tokenizer.encode(system_prompt_text)
        sp_start_token = self.tokenizer.convert_ids_to_tokens(self.speech_start_id)
        sp_pad_token = self.tokenizer.convert_ids_to_tokens(self.speech_pad_id)
        sp_end_token = self.tokenizer.convert_ids_to_tokens(self.speech_end_id)
        show_keys = ["Start time", "End time", "Speaker ID", "Content"]
        if context_info and context_info.strip():
            user_suffix = (
                f"This is a {audio_duration :.2f} seconds audio, with extra info: {context_info .strip ()}\n\nPlease transcribe it with these keys: "
                + ", ".join(show_keys)
            )
        else:
            user_suffix = (
                f"This is a {audio_duration :.2f} seconds audio, please transcribe it with these keys: "
                + ", ".join(show_keys)
            )
        user_input_string = (
            "".join([sp_start_token] + [sp_pad_token] * vae_tok_len + [sp_end_token])
            + "\n"
            + user_suffix
        )
        user_tokens = self.tokenizer.apply_chat_template(
            [{"role": "user", "content": user_input_string}], tokenize=True
        )
        full_tokens = system_tokens + user_tokens
        acoustic_input_mask = [
            1 if token == self.speech_pad_id else 0 for token in full_tokens
        ]
        return {
            "input_ids": full_tokens,
            "acoustic_input_mask": acoustic_input_mask,
            "speech": audio_array,
            "vae_tok_len": vae_tok_len,
        }

    def _batch_encode(
        self,
        encodings: List[Dict[str, Any]],
        padding: bool = True,
        max_length: Optional[int] = None,
        truncation: bool = False,
        return_tensors: Optional[str] = None,
    ) -> BatchEncoding:
        input_ids_list = [enc["input_ids"] for enc in encodings]
        acoustic_masks_list = [enc["acoustic_input_mask"] for enc in encodings]
        speech_list = [enc["speech"] for enc in encodings]
        vae_tok_lens = [enc["vae_tok_len"] for enc in encodings]
        if padding:
            if max_length is not None:
                target_length = max_length
            else:
                target_length = max((len(ids) for ids in input_ids_list))
            padded_input_ids = []
            padded_acoustic_masks = []
            attention_masks = []
            for input_ids, acoustic_mask in zip(input_ids_list, acoustic_masks_list):
                if truncation and len(input_ids) > target_length:
                    input_ids = input_ids[:target_length]
                    acoustic_mask = acoustic_mask[:target_length]
                padding_length = target_length - len(input_ids)
                padded_ids = [self.pad_id] * padding_length + input_ids
                padded_acoustic = [0] * padding_length + acoustic_mask
                attention_mask = [0] * padding_length + [1] * len(input_ids)
                padded_input_ids.append(padded_ids)
                padded_acoustic_masks.append(padded_acoustic)
                attention_masks.append(attention_mask)
            input_ids_list = padded_input_ids
            acoustic_masks_list = padded_acoustic_masks
        else:
            attention_masks = [[1] * len(ids) for ids in input_ids_list]
        max_speech_length = max((len(s) for s in speech_list))
        padded_speeches = np.zeros(
            (len(speech_list), max_speech_length), dtype=np.float32
        )
        speech_masks = np.zeros((len(speech_list), max(vae_tok_lens)), dtype=bool)
        for i, (speech, vae_len) in enumerate(zip(speech_list, vae_tok_lens)):
            padded_speeches[i, : len(speech)] = speech
            speech_masks[i, :vae_len] = True
        batch_encoding = BatchEncoding()
        if return_tensors == "pt":
            batch_encoding["input_ids"] = torch.tensor(input_ids_list, dtype=torch.long)
            batch_encoding["attention_mask"] = torch.tensor(
                attention_masks, dtype=torch.long
            )
            batch_encoding["acoustic_input_mask"] = torch.tensor(
                acoustic_masks_list, dtype=torch.bool
            )
            batch_encoding["speech_tensors"] = torch.tensor(
                padded_speeches, dtype=torch.float32
            )
            batch_encoding["speech_masks"] = torch.tensor(
                speech_masks, dtype=torch.bool
            )
        else:
            batch_encoding["input_ids"] = (
                input_ids_list if len(input_ids_list) > 1 else input_ids_list[0]
            )
            batch_encoding["attention_mask"] = (
                attention_masks if len(attention_masks) > 1 else attention_masks[0]
            )
            batch_encoding["acoustic_input_mask"] = (
                acoustic_masks_list
                if len(acoustic_masks_list) > 1
                else acoustic_masks_list[0]
            )
            batch_encoding["speech_tensors"] = (
                padded_speeches if len(padded_speeches) > 1 else padded_speeches[0]
            )
            batch_encoding["speech_masks"] = (
                speech_masks if len(speech_masks) > 1 else speech_masks[0]
            )
        return batch_encoding

    def batch_decode(self, *args, **kwargs):
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        return self.tokenizer.decode(*args, **kwargs)

    def post_process_transcription(self, text: str) -> List[Dict[str, Any]]:
        try:
            if "```json" in text:
                json_start = text.find("```json") + 7
                json_end = text.find("```", json_start)
                json_str = text[json_start:json_end].strip()
            else:
                json_start = text.find("[")
                if json_start == -1:
                    json_start = text.find("{")
                if json_start != -1:
                    bracket_count = 0
                    json_end = json_start
                    for i in range(json_start, len(text)):
                        if text[i] in "[{":
                            bracket_count += 1
                        elif text[i] in "]}":
                            bracket_count -= 1
                            if bracket_count == 0:
                                json_end = i + 1
                                break
                    json_str = text[json_start:json_end]
                else:
                    json_str = text
            result = json.loads(json_str)
            if isinstance(result, dict):
                result = [result]
            cleaned_result = []
            for item in result:
                if isinstance(item, dict):
                    cleaned_item = {}
                    key_mapping = {
                        "Start time": "start_time",
                        "Start": "start_time",
                        "End time": "end_time",
                        "End": "end_time",
                        "Speaker ID": "speaker_id",
                        "Speaker": "speaker_id",
                        "Content": "text",
                    }
                    for key, mapped_key in key_mapping.items():
                        if key in item:
                            cleaned_item[mapped_key] = item[key]
                    if cleaned_item:
                        cleaned_result.append(cleaned_item)
            return cleaned_result
        except json.JSONDecodeError as e:
            logger.warning(f"Failed to parse JSON from transcription: {e }")
            logger.debug(f"Raw text: {text }")
            return []
        except Exception as e:
            logger.warning(f"Error post-processing transcription: {e }")
            return []

    @property
    def model_input_names(self):
        return [
            "input_ids",
            "attention_mask",
            "acoustic_input_mask",
            "speech_tensors",
            "speech_masks",
        ]


__all__ = [
    'QWEN3VoxASRProcessor'
]
import math
import warnings
from typing import List, Optional, Union, Dict, Any, Tuple
import os
import re
import numpy as np
import torch
from transformers.tokenization_utils_base import (
    BatchEncoding,
    PaddingStrategy,
    PreTokenizedInput,
    TextInput,
    TruncationStrategy,
)
from transformers.utils import TensorType, logging

logger = logging.get_logger(__name__)


class QWEN3VoxProcessor:

    def __init__(
        self,
        tokenizer=None,
        audio_processor=None,
        speech_tok_compress_ratio=3200,
        db_normalize=True,
        **kwargs,
    ):
        self.tokenizer = tokenizer
        self.audio_processor = audio_processor
        self.speech_tok_compress_ratio = speech_tok_compress_ratio
        self.db_normalize = db_normalize
        self.audio_normalizer = AudioNormalizer() if db_normalize else None
        self.system_prompt = " Transform the text provided by various speakers into speech output, utilizing the distinct voice of each respective speaker.\n"

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
        import os
        import json
        from transformers.utils import cached_file

        model_name = str(pretrained_model_name_or_path)
        config_path = os.path.join(
            model_name, "preprocessor_config.json"
        )
        config = None
        if os.path.exists(config_path):
            with open(config_path, "r") as f:
                config = json.load(f)
        else:
            try:
                config_file = cached_file(
                    model_name, "preprocessor_config.json", **kwargs
                )
                with open(config_file, "r") as f:
                    config = json.load(f)
            except Exception as e:
                logger.warning(
                    f"Could not load preprocessor_config.json from {model_name }: {e }"
                )
                logger.warning("Using default configuration")
                config = {"speech_tok_compress_ratio": 3200, "db_normalize": True}
        speech_tok_compress_ratio = config.get("speech_tok_compress_ratio", 3200)
        db_normalize = config.get("db_normalize", True)
        language_model_pretrained_name = config.get(
            "language_model_pretrained_name", None
        ) or kwargs.pop("language_model_pretrained_name", None)
        if not language_model_pretrained_name:
            language_model_pretrained_name = model_name
        logger.info(f"Loading tokenizer from repo {model_name }")
        tokenizer = QWEN3VoxTextTokenizerFast.from_pretrained(
            model_name, **kwargs
        )
        if "audio_processor" in config:
            audio_config = config["audio_processor"]
            audio_processor = QWEN3VoxTokenizerProcessor(
                sampling_rate=audio_config.get("sampling_rate", 22050),
                normalize_audio=audio_config.get("normalize_audio", True),
                target_dB_FS=audio_config.get("target_dB_FS", -25),
                eps=audio_config.get("eps", 1e-06),
            )
        else:
            audio_processor = QWEN3VoxTokenizerProcessor()
        return cls(
            tokenizer=tokenizer,
            audio_processor=audio_processor,
            speech_tok_compress_ratio=speech_tok_compress_ratio,
            db_normalize=db_normalize,
        )

    def save_pretrained(self, save_directory: Union[str, os.PathLike], **kwargs):
        import os
        import json

        os.makedirs(save_directory, exist_ok=True)
        processor_config = {
            "processor_class": "QWEN3VoxProcessor",
            "speech_tok_compress_ratio": self.speech_tok_compress_ratio,
            "db_normalize": self.db_normalize,
            "audio_processor": {
                "feature_extractor_type": "QWEN3VoxTokenizerProcessor",
                "sampling_rate": getattr(self.audio_processor, "sampling_rate", 22050),
                "normalize_audio": getattr(
                    self.audio_processor, "normalize_audio", True
                ),
                "target_dB_FS": getattr(self.audio_processor, "target_dB_FS", -25),
                "eps": getattr(self.audio_processor, "eps", 1e-06),
            },
        }
        config_path = os.path.join(save_directory, "preprocessor_config.json")
        with open(config_path, "w") as f:
            json.dump(processor_config, f, indent=2)
        logger.info(f"Processor configuration saved in {config_path }")

    def __call__(
        self,
        text: Optional[
            Union[
                str,
                List[str],
                TextInput,
                PreTokenizedInput,
                List[TextInput],
                List[PreTokenizedInput],
            ]
        ] = None,
        voice_samples: Optional[
            Union[List[Union[str, np.ndarray]], List[List[Union[str, np.ndarray]]]]
        ] = None,
        padding: Union[bool, str, PaddingStrategy] = True,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        return_attention_mask: bool = True,
        **kwargs,
    ) -> BatchEncoding:
        if isinstance(text, str) or (
            isinstance(text, list) and len(text) > 0 and (not isinstance(text[0], str))
        ):
            texts = [text]
            is_batched = False
        else:
            texts = text
            is_batched = True
        if voice_samples is not None:
            if not is_batched or isinstance(voice_samples[0], (str, np.ndarray)):
                voice_samples_list = [voice_samples]
            else:
                voice_samples_list = voice_samples
        else:
            voice_samples_list = [None] * len(texts)
        all_encodings = []
        for text_input, voice_input in zip(texts, voice_samples_list):
            encoding = self._process_single(text_input, voice_input)
            all_encodings.append(encoding)
        batch_encoding = self._batch_encode(
            all_encodings,
            padding=padding,
            truncation=truncation,
            max_length=max_length,
            return_tensors=return_tensors,
            return_attention_mask=return_attention_mask,
        )
        return batch_encoding

    def _process_single(
        self,
        text: Union[str, TextInput],
        voice_samples: Optional[List[Union[str, np.ndarray]]] = None,
    ) -> Dict[str, Any]:
        script = None
        if isinstance(text, str):
            if text.endswith(".json") and os.path.exists(text):
                script = self._convert_json_to_script(text)
            elif text.endswith(".txt") and os.path.exists(text):
                script = self._convert_text_to_script(text)
            else:
                script = text
        if script is None:
            raise ValueError(f"Could not process input text: {text }")
        parsed_lines = self._parse_script(script)
        all_speakers = list(set((speaker_id for speaker_id, _ in parsed_lines)))
        system_tokens = self.tokenizer.encode(self.system_prompt)
        if voice_samples:
            voice_tokens, voice_speech_inputs, voice_speech_masks = (
                self._create_voice_prompt(voice_samples[: len(all_speakers)])
            )
        else:
            voice_tokens, voice_speech_inputs, voice_speech_masks = ([], [], [])
        full_tokens = system_tokens + voice_tokens
        speech_input_mask = [False] * len(system_tokens) + voice_speech_masks
        full_tokens += self.tokenizer.encode(" Text input:\n", add_special_tokens=False)
        speech_input_mask += [False] * len(
            self.tokenizer.encode(" Text input:\n", add_special_tokens=False)
        )
        for speaker_id, speaker_text in parsed_lines:
            speaker_text_tokens = self.tokenizer.encode(
                f" Speaker {speaker_id }:{speaker_text }\n", add_special_tokens=False
            )
            full_tokens += speaker_text_tokens
            speech_input_mask += [False] * len(speaker_text_tokens)
        full_tokens += self.tokenizer.encode(
            " Speech output:\n", add_special_tokens=False
        ) + [self.tokenizer.speech_start_id]
        speech_input_mask += [False] * (
            len(self.tokenizer.encode(" Speech output:\n", add_special_tokens=False))
            + 1
        )
        return {
            "input_ids": full_tokens,
            "speech_inputs": voice_speech_inputs if voice_speech_inputs else None,
            "speech_input_mask": speech_input_mask,
            "parsed_script": parsed_lines,
            "all_speakers": all_speakers,
        }

    def _batch_encode(
        self,
        encodings: List[Dict[str, Any]],
        padding: Union[bool, str, PaddingStrategy] = True,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        return_attention_mask: bool = True,
    ) -> BatchEncoding:
        input_ids_list = [enc["input_ids"] for enc in encodings]
        speech_input_masks_list = [enc["speech_input_mask"] for enc in encodings]
        if isinstance(padding, bool):
            padding_strategy = (
                PaddingStrategy.LONGEST if padding else PaddingStrategy.DO_NOT_PAD
            )
        elif isinstance(padding, str):
            padding_strategy = PaddingStrategy(padding)
        else:
            padding_strategy = padding
        if padding_strategy != PaddingStrategy.DO_NOT_PAD:
            if padding_strategy == PaddingStrategy.LONGEST:
                max_len = max((len(ids) for ids in input_ids_list))
            elif (
                padding_strategy == PaddingStrategy.MAX_LENGTH
                and max_length is not None
            ):
                max_len = max_length
            else:
                max_len = max((len(ids) for ids in input_ids_list))
            padded_input_ids = []
            attention_masks = []
            padded_speech_input_masks = []
            for input_ids, speech_mask in zip(input_ids_list, speech_input_masks_list):
                if truncation and len(input_ids) > max_len:
                    input_ids = input_ids[:max_len]
                    speech_mask = speech_mask[:max_len]
                padding_length = max_len - len(input_ids)
                padded_ids = [self.tokenizer.pad_id] * padding_length + input_ids
                attention_mask = [0] * padding_length + [1] * len(input_ids)
                padded_speech_mask = [False] * padding_length + speech_mask
                padded_input_ids.append(padded_ids)
                attention_masks.append(attention_mask)
                padded_speech_input_masks.append(padded_speech_mask)
            input_ids_list = padded_input_ids
            speech_input_masks_list = padded_speech_input_masks
        else:
            attention_masks = (
                [[1] * len(ids) for ids in input_ids_list]
                if return_attention_mask
                else None
            )
        all_speech_inputs = []
        has_speech = False
        for enc in encodings:
            if enc["speech_inputs"] is not None:
                all_speech_inputs.extend(enc["speech_inputs"])
                has_speech = True
        batch_encoding = BatchEncoding()
        if return_tensors is not None:
            batch_encoding["input_ids"] = torch.tensor(input_ids_list, dtype=torch.long)
            if return_attention_mask and attention_masks is not None:
                batch_encoding["attention_mask"] = torch.tensor(
                    attention_masks, dtype=torch.long
                )
            batch_encoding["speech_input_mask"] = torch.tensor(
                speech_input_masks_list, dtype=torch.bool
            )
        else:
            batch_encoding["input_ids"] = input_ids_list
            if return_attention_mask and attention_masks is not None:
                batch_encoding["attention_mask"] = attention_masks
            batch_encoding["speech_input_mask"] = speech_input_masks_list
        if has_speech:
            speech_dict = self.prepare_speech_inputs(
                all_speech_inputs, return_tensors=return_tensors
            )
            batch_encoding["speech_tensors"] = speech_dict["padded_speeches"]
            batch_encoding["speech_masks"] = speech_dict["speech_masks"]
        else:
            batch_encoding["speech_tensors"] = None
            batch_encoding["speech_masks"] = None
        batch_encoding["parsed_scripts"] = [enc["parsed_script"] for enc in encodings]
        batch_encoding["all_speakers_list"] = [enc["all_speakers"] for enc in encodings]
        return batch_encoding

    def _create_voice_prompt(
        self, speaker_samples: List[Union[str, np.ndarray]]
    ) -> Tuple[List[int], List[np.ndarray], List[bool]]:
        vae_token_id = self.tokenizer.speech_diffusion_id
        voice_full_tokens = self.tokenizer.encode(
            " Voice input:\n", add_special_tokens=False
        )
        voice_speech_inputs = []
        voice_speech_masks = [False] * len(voice_full_tokens)
        for speaker_id, speaker_audio in enumerate(speaker_samples):
            prefix_tokens = self.tokenizer.encode(
                f" Speaker {speaker_id }:", add_special_tokens=False
            )
            if isinstance(speaker_audio, str):
                wav = self.audio_processor._load_audio_from_path(speaker_audio)
            elif isinstance(speaker_audio, dict):
                if "array" in speaker_audio:
                    wav = np.array(speaker_audio["array"], dtype=np.float32)
                elif "audio" in speaker_audio:
                    wav = np.array(speaker_audio["audio"], dtype=np.float32)
                else:
                    raise ValueError(
                        f"Dictionary audio input must have 'array' or 'audio' key, got: {speaker_audio .keys ()}"
                    )
            else:
                wav = np.array(speaker_audio, dtype=np.float32)
            if self.db_normalize and self.audio_normalizer:
                wav = self.audio_normalizer(wav)
            vae_tok_len = math.ceil(wav.shape[0] / self.speech_tok_compress_ratio)
            speaker_tokens = (
                prefix_tokens
                + [self.tokenizer.speech_start_id]
                + [vae_token_id] * vae_tok_len
                + [self.tokenizer.speech_end_id]
                + self.tokenizer.encode("\n", add_special_tokens=False)
            )
            vae_input_mask = (
                [False] * len(prefix_tokens)
                + [False]
                + [True] * vae_tok_len
                + [False]
                + [False]
            )
            voice_full_tokens.extend(speaker_tokens)
            voice_speech_masks.extend(vae_input_mask)
            voice_speech_inputs.append(wav)
        return (voice_full_tokens, voice_speech_inputs, voice_speech_masks)

    def prepare_speech_inputs(
        self,
        speech_inputs: List[np.ndarray],
        return_tensors: Optional[Union[str, TensorType]] = None,
        device: Optional[Union[str, torch.device]] = None,
        dtype: Optional[torch.dtype] = None,
    ) -> Dict[str, Any]:
        if not speech_inputs:
            return {"padded_speeches": None, "speech_masks": None}
        vae_tok_seqlens = [
            math.ceil(s.shape[0] / self.speech_tok_compress_ratio)
            for s in speech_inputs
        ]
        max_speech_length = max((s.shape[0] for s in speech_inputs))
        if speech_inputs[0].ndim == 1:
            padded_speeches = np.full(
                (len(speech_inputs), max_speech_length), fill_value=0, dtype=np.float32
            )
        else:
            padded_speeches = np.full(
                (len(speech_inputs), max_speech_length, speech_inputs[0].shape[-1]),
                fill_value=0,
                dtype=np.float32,
            )
        speech_masks = np.zeros(
            (len(speech_inputs), max(vae_tok_seqlens)), dtype=np.bool_
        )
        for i, (speech, vae_tok_length) in enumerate(
            zip(speech_inputs, vae_tok_seqlens)
        ):
            padded_speeches[i, : len(speech)] = speech
            speech_masks[i, :vae_tok_length] = True
        result = {"padded_speeches": padded_speeches, "speech_masks": speech_masks}
        if return_tensors == "pt":
            result["padded_speeches"] = torch.tensor(
                padded_speeches, device=device, dtype=dtype or torch.float32
            )
            result["speech_masks"] = torch.tensor(
                speech_masks, device=device, dtype=torch.bool
            )
        return result

    def _convert_json_to_script(self, json_file: str) -> str:
        import json

        with open(json_file, "r", encoding="utf-8") as f:
            data = json.load(f)
        if not isinstance(data, list):
            raise ValueError("JSON file must contain a list of speaker entries")
        script_lines = []
        for item in data:
            if not isinstance(item, dict):
                logger.warning(f"Skipping non-dict entry: {item }")
                continue
            speaker = item.get("speaker")
            text = item.get("text")
            if speaker is None or text is None:
                logger.warning(f"Skipping entry missing speaker or text: {item }")
                continue
            try:
                speaker_id = int(speaker)
            except (ValueError, TypeError):
                logger.warning(f"Invalid speaker ID: {speaker }, skipping entry")
                continue
            text = text.strip()
            if text:
                script_lines.append(f"Speaker {speaker_id }: {text }")
        if not script_lines:
            raise ValueError("No valid entries found in JSON file")
        return "\n".join(script_lines)

    def _convert_text_to_script(self, text_file: str) -> str:
        with open(text_file, "r", encoding="utf-8") as f:
            lines = f.readlines()
        script_lines = []
        current_speaker = 1
        for line in lines:
            line = line.strip()
            if not line:
                continue
            speaker_match = re.match(
                "^Speaker\\s+(\\d+)\\s*:\\s*(.*)$", line, re.IGNORECASE
            )
            if speaker_match:
                speaker_id = int(speaker_match.group(1))
                text = speaker_match.group(2).strip()
                if text:
                    script_lines.append(f"Speaker {speaker_id }: {text }")
            else:
                script_lines.append(f"Speaker {current_speaker }: {line }")
        if not script_lines:
            raise ValueError("No valid content found in text file")
        return "\n".join(script_lines)

    def _parse_script(self, script: str) -> List[Tuple[int, str]]:
        stripped = script.strip()
        if not stripped:
            raise ValueError(
                "No valid speaker lines found in script (empty text). "
                "If training with HuggingFace Trainer, set remove_unused_columns=False "
                "so dataset columns like `text` are not stripped before the collator."
            )
        non_empty = [ln.strip() for ln in stripped.split("\n") if ln.strip()]
        if not non_empty:
            raise ValueError("No valid speaker lines found in script")
        _speaker_line = r"^Speaker\s+(\d+)\s*:\s*(.*)$"
        if not any(re.match(_speaker_line, ln, re.IGNORECASE) for ln in non_empty):
            # JSONL / TTS-style rows: plain prompt with no "Speaker N:" lines.
            collapsed = " ".join(stripped.split())
            return [(0, " " + collapsed)]
        parsed_lines: List[Tuple[int, str]] = []
        speaker_ids: List[int] = []
        for line in non_empty:
            match = re.match(_speaker_line, line, re.IGNORECASE)
            if match:
                speaker_id = int(match.group(1))
                text = " " + match.group(2).strip()
                parsed_lines.append((speaker_id, text))
                speaker_ids.append(speaker_id)
            else:
                logger.warning(f"Could not parse line: '{line }'")
        if not parsed_lines:
            raise ValueError("No valid speaker lines found in script")
        min_speaker_id = min(speaker_ids)
        if min_speaker_id > 0:
            normalized_lines = []
            for speaker_id, text in parsed_lines:
                normalized_lines.append((speaker_id - 1, text))
            return normalized_lines
        else:
            return parsed_lines

    def _merge_inputs(
        self, text_inputs: BatchEncoding, audio_inputs: Dict
    ) -> BatchEncoding:
        merged = BatchEncoding(text_inputs)
        if "audio" in audio_inputs:
            merged["speech_inputs"] = audio_inputs["audio"]
        if "streaming" in audio_inputs:
            merged["streaming"] = audio_inputs["streaming"]
        return merged

    def batch_decode(self, *args, **kwargs):
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        audio_processor_input_names = self.audio_processor.model_input_names
        return list(
            dict.fromkeys(
                tokenizer_input_names
                + audio_processor_input_names
                + ["speech_inputs", "speech_input_mask"]
            )
        )

    def save_audio(
        self,
        audio: Union[torch.Tensor, np.ndarray, List[Union[torch.Tensor, np.ndarray]]],
        output_path: str = "output.wav",
        sampling_rate: Optional[int] = None,
        normalize: bool = False,
        batch_prefix: str = "audio_",
    ) -> str:
        return self.audio_processor.save_audio(
            audio,
            output_path=output_path,
            sampling_rate=sampling_rate,
            normalize=normalize,
            batch_prefix=batch_prefix,
        )


__all__ = [
    'QWEN3VoxProcessor'
]
'\nQWEN3Vox Streaming Processor\n\nThis processor handles input preparation for the streaming 0.5B model,\nincluding text tokenization and cached voice prompt handling.\n'
import math
import warnings
from typing import List, Optional, Union, Dict, Any, Tuple
import os
import re
import numpy as np
import torch
from transformers.tokenization_utils_base import (
    BatchEncoding,
    PaddingStrategy,
    PreTokenizedInput,
    TextInput,
    TruncationStrategy,
)
from transformers.utils import TensorType, logging

logger = logging.get_logger(__name__)


class QWEN3VoxStreamingProcessor:

    def __init__(
        self,
        tokenizer=None,
        audio_processor=None,
        speech_tok_compress_ratio=3200,
        db_normalize=True,
        **kwargs,
    ):
        self.tokenizer = tokenizer
        self.audio_processor = audio_processor
        self.speech_tok_compress_ratio = speech_tok_compress_ratio
        self.db_normalize = db_normalize
        self.audio_normalizer = AudioNormalizer() if db_normalize else None

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
        import os
        import json
        from transformers.utils import cached_file

        model_name = str(pretrained_model_name_or_path)
        config_path = os.path.join(
            model_name, "preprocessor_config.json"
        )
        config = None
        if os.path.exists(config_path):
            with open(config_path, "r") as f:
                config = json.load(f)
        else:
            try:
                config_file = cached_file(
                    model_name, "preprocessor_config.json", **kwargs
                )
                with open(config_file, "r") as f:
                    config = json.load(f)
            except Exception as e:
                logger.warning(
                    f"Could not load preprocessor_config.json from {model_name }: {e }"
                )
                logger.warning("Using default configuration")
                config = {"speech_tok_compress_ratio": 3200, "db_normalize": True}
        speech_tok_compress_ratio = config.get("speech_tok_compress_ratio", 3200)
        db_normalize = config.get("db_normalize", True)
        logger.info(f"Loading tokenizer from repo {model_name }")
        tokenizer = QWEN3VoxTextTokenizerFast.from_pretrained(
            model_name, **kwargs
        )
        if "audio_processor" in config:
            audio_config = config["audio_processor"]
            audio_processor = QWEN3VoxTokenizerProcessor(
                sampling_rate=audio_config.get("sampling_rate", 22050),
                normalize_audio=audio_config.get("normalize_audio", True),
                target_dB_FS=audio_config.get("target_dB_FS", -25),
                eps=audio_config.get("eps", 1e-06),
            )
        else:
            audio_processor = QWEN3VoxTokenizerProcessor()
        return cls(
            tokenizer=tokenizer,
            audio_processor=audio_processor,
            speech_tok_compress_ratio=speech_tok_compress_ratio,
            db_normalize=db_normalize,
        )

    def save_pretrained(self, save_directory: Union[str, os.PathLike], **kwargs):
        import os
        import json

        os.makedirs(save_directory, exist_ok=True)
        processor_config = {
            "processor_class": "QWEN3VoxStreamingProcessor",
            "speech_tok_compress_ratio": self.speech_tok_compress_ratio,
            "db_normalize": self.db_normalize,
            "audio_processor": {
                "feature_extractor_type": "QWEN3VoxTokenizerProcessor",
                "sampling_rate": getattr(self.audio_processor, "sampling_rate", 22050),
                "normalize_audio": getattr(
                    self.audio_processor, "normalize_audio", True
                ),
                "target_dB_FS": getattr(self.audio_processor, "target_dB_FS", -25),
                "eps": getattr(self.audio_processor, "eps", 1e-06),
            },
        }
        config_path = os.path.join(save_directory, "preprocessor_config.json")
        with open(config_path, "w") as f:
            json.dump(processor_config, f, indent=2)
        logger.info(f"Processor configuration saved in {config_path }")

    def __call__(self) -> BatchEncoding:
        raise NotImplementedError(
            'QWEN3VoxStreamingProcessor.__call__ is not implemented. Use process_input_with_cached_prompt for streaming inputs.'
        )

    def process_input_with_cached_prompt(
        self,
        text: Optional[str] = None,
        cached_prompt: Optional[Dict[str, Any]] = None,
        padding: Union[bool, str, PaddingStrategy] = True,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        return_attention_mask: bool = True,
        **kwargs,
    ) -> BatchEncoding:
        texts = [text]
        cached_prompts = [cached_prompt]
        is_batched = False
        all_encodings = []
        for text_input, cached_prompt_input in zip(texts, cached_prompts):
            script_tokens = self.tokenizer.encode(
                text_input.strip() + "\n", add_special_tokens=False
            )
            input_id_length = cached_prompt_input["lm"]["last_hidden_state"].size(1)
            tts_lm_input_id_length = cached_prompt_input["tts_lm"][
                "last_hidden_state"
            ].size(1)
            input_ids = [self.tokenizer.pad_id] * input_id_length
            tts_lm_input_ids = [self.tokenizer.pad_id] * tts_lm_input_id_length
            speech_input_mask = [False] * tts_lm_input_id_length
            encoding = {
                "input_ids": input_ids,
                "tts_lm_input_ids": tts_lm_input_ids,
                "tts_text_ids": script_tokens,
                "speech_inputs": None,
                "speech_input_mask": speech_input_mask,
            }
            all_encodings.append(encoding)
        batch_encoding = self._batch_encode(
            all_encodings,
            padding=padding,
            truncation=truncation,
            max_length=max_length,
            return_tensors=return_tensors,
            return_attention_mask=return_attention_mask,
        )
        return batch_encoding

    def _batch_encode(
        self,
        encodings: List[Dict[str, Any]],
        padding: Union[bool, str, PaddingStrategy] = True,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[Union[str, TensorType]] = None,
        return_attention_mask: bool = True,
    ) -> BatchEncoding:
        input_ids_list = [enc["input_ids"] for enc in encodings]
        tts_lm_input_ids_list = [enc["tts_lm_input_ids"] for enc in encodings]
        tts_text_ids_list = [enc["tts_text_ids"] for enc in encodings]
        speech_input_masks_list = [enc["speech_input_mask"] for enc in encodings]
        attention_masks = (
            [[1] * len(ids) for ids in input_ids_list]
            if return_attention_mask
            else None
        )
        tts_lm_attention_masks = (
            [[1] * len(ids) for ids in tts_lm_input_ids_list]
            if return_attention_mask
            else None
        )
        all_speech_inputs = []
        has_speech = False
        for enc in encodings:
            if enc["speech_inputs"] is not None:
                all_speech_inputs.extend(enc["speech_inputs"])
                has_speech = True
        batch_encoding = BatchEncoding()
        if return_tensors is not None:
            batch_encoding["input_ids"] = torch.tensor(input_ids_list, dtype=torch.long)
            batch_encoding["tts_lm_input_ids"] = torch.tensor(
                tts_lm_input_ids_list, dtype=torch.long
            )
            batch_encoding["tts_text_ids"] = torch.tensor(
                tts_text_ids_list, dtype=torch.long
            )
            if return_attention_mask and attention_masks is not None:
                batch_encoding["attention_mask"] = torch.tensor(
                    attention_masks, dtype=torch.long
                )
                batch_encoding["tts_lm_attention_mask"] = torch.tensor(
                    tts_lm_attention_masks, dtype=torch.long
                )
            batch_encoding["speech_input_mask"] = torch.tensor(
                speech_input_masks_list, dtype=torch.bool
            )
        else:
            batch_encoding["input_ids"] = input_ids_list
            batch_encoding["tts_lm_input_ids"] = tts_lm_input_ids_list
            batch_encoding["tts_text_ids"] = tts_text_ids_list
            if return_attention_mask and attention_masks is not None:
                batch_encoding["attention_mask"] = attention_masks
                batch_encoding["tts_lm_attention_mask"] = tts_lm_attention_masks
            batch_encoding["speech_input_mask"] = speech_input_masks_list
        if has_speech:
            speech_dict = self.prepare_speech_inputs(
                all_speech_inputs, return_tensors=return_tensors
            )
            batch_encoding["speech_tensors"] = speech_dict["padded_speeches"]
            batch_encoding["speech_masks"] = speech_dict["speech_masks"]
        else:
            batch_encoding["speech_tensors"] = None
            batch_encoding["speech_masks"] = None
        return batch_encoding

    def prepare_speech_inputs(
        self,
        speech_inputs: List[np.ndarray],
        return_tensors: Optional[Union[str, TensorType]] = None,
        device: Optional[Union[str, torch.device]] = None,
        dtype: Optional[torch.dtype] = None,
    ) -> Dict[str, Any]:
        if not speech_inputs:
            return {"padded_speeches": None, "speech_masks": None}
        vae_tok_seqlens = [
            math.ceil(s.shape[0] / self.speech_tok_compress_ratio)
            for s in speech_inputs
        ]
        max_speech_length = max((s.shape[0] for s in speech_inputs))
        if speech_inputs[0].ndim == 1:
            padded_speeches = np.full(
                (len(speech_inputs), max_speech_length), fill_value=0, dtype=np.float32
            )
        else:
            padded_speeches = np.full(
                (len(speech_inputs), max_speech_length, speech_inputs[0].shape[-1]),
                fill_value=0,
                dtype=np.float32,
            )
        speech_masks = np.zeros(
            (len(speech_inputs), max(vae_tok_seqlens)), dtype=np.bool_
        )
        for i, (speech, vae_tok_length) in enumerate(
            zip(speech_inputs, vae_tok_seqlens)
        ):
            padded_speeches[i, : len(speech)] = speech
            speech_masks[i, :vae_tok_length] = True
        result = {"padded_speeches": padded_speeches, "speech_masks": speech_masks}
        if return_tensors == "pt":
            result["padded_speeches"] = torch.tensor(
                padded_speeches, device=device, dtype=dtype or torch.float32
            )
            result["speech_masks"] = torch.tensor(
                speech_masks, device=device, dtype=torch.bool
            )
        return result

    def batch_decode(self, *args, **kwargs):
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        audio_processor_input_names = self.audio_processor.model_input_names
        return list(
            dict.fromkeys(
                tokenizer_input_names
                + audio_processor_input_names
                + ["speech_inputs", "speech_input_mask"]
            )
        )

    def save_audio(
        self,
        audio: Union[torch.Tensor, np.ndarray, List[Union[torch.Tensor, np.ndarray]]],
        output_path: str = "output.wav",
        sampling_rate: Optional[int] = None,
        normalize: bool = False,
        batch_prefix: str = "audio_",
    ) -> str:
        return self.audio_processor.save_audio(
            audio,
            output_path=output_path,
            sampling_rate=sampling_rate,
            normalize=normalize,
            batch_prefix=batch_prefix,
        )


__all__ = [
    'QWEN3VoxStreamingProcessor'
]
'\nQWEN3Vox Streaming Model Architecture (0.5B)\n\nThis module implements the streaming-optimized version of QWEN3Vox for real-time TTS.\nKey differences from the multi-speaker model:\n- No semantic tokenizer (only acoustic)\n- Split language model architecture: lower layers for text, upper layers for TTS\n- Optimized for low-latency generation\n'
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union, Callable
from tqdm import tqdm
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from transformers.models.auto import AutoModel, AutoModelForCausalLM
from transformers.activations import ACT2FN
from transformers.modeling_outputs import (
    CausalLMOutput,
    BaseModelOutputWithPast,
    ModelOutput,
)
from transformers.models.llama.modeling_llama import LlamaRMSNorm
from transformers import modeling_utils
from transformers.modeling_utils import PreTrainedModel
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
from transformers.utils import logging

logger = logging.get_logger(__name__)
if (
    not hasattr(modeling_utils, "ALL_PARALLEL_STYLES")
    or modeling_utils.ALL_PARALLEL_STYLES is None
):
    modeling_utils.ALL_PARALLEL_STYLES = ["tp", "none", "colwise", "rowwise"]


class BinaryClassifier(nn.Module):

    def __init__(self, hidden_size):
        super(BinaryClassifier, self).__init__()
        self.fc1 = nn.Linear(hidden_size, hidden_size)
        self.fc2 = nn.Linear(hidden_size, 1)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x


class SpeechConnector(nn.Module):

    def __init__(self, input_dim, output_dim):
        super().__init__()
        self.fc1 = nn.Linear(input_dim, output_dim)
        self.norm = LlamaRMSNorm(output_dim, eps=1e-06)
        self.fc2 = nn.Linear(output_dim, output_dim)

    def forward(self, features, **kwargs):
        x = self.fc1(features)
        x = self.norm(x)
        x = self.fc2(x)
        return x


class QWEN3VoxStreamingPreTrainedModel(PreTrainedModel):
    config_class = QWEN3VoxStreamingConfig
    base_model_prefix = "model"
    supports_gradient_checkpointing = True
    _skip_keys_device_placement = "past_key_values"
    _supports_cache_class = True
    _supports_flash_attn_2 = True
    _supports_sdpa = True
    _supports_quantized_cache = True
    _supports_static_cache = True
    _supports_attention_backend = True

    def _init_weights(self, module):
        if isinstance(module, QWEN3VoxDiffusionHead):
            module.initialize_weights()
            return
        if hasattr(self.config, "language_model_config") and hasattr(
            self.config.language_model_config, "initializer_range"
        ):
            std = self.config.language_model_config.initializer_range
        elif hasattr(self.config, "decoder_config") and hasattr(
            self.config.decoder_config, "initializer_range"
        ):
            std = self.config.decoder_config.initializer_range
        else:
            std = 0.02
        if isinstance(module, nn.Linear):
            module.weight.data.normal_(mean=0.0, std=std)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(module, nn.LayerNorm):
            module.weight.data.fill_(1.0)
            module.bias.data.zero_()


class QWEN3VoxStreamingModel(QWEN3VoxStreamingPreTrainedModel):

    def __init__(self, config):
        super().__init__(config)
        if hasattr(config, "torch_dtype") and config.torch_dtype is not None:
            if isinstance(config.torch_dtype, str):
                dtype = getattr(torch, config.torch_dtype)
            else:
                dtype = config.torch_dtype
        else:
            dtype = torch.float32
        lm_config = copy.deepcopy(config.decoder_config)
        lm_backbone_num_hidden_layers = (
            getattr(lm_config, "num_hidden_layers", 24)
            - config.tts_backbone_num_hidden_layers
        )
        lm_config.num_hidden_layers = lm_backbone_num_hidden_layers
        self.language_model = AutoModel.from_config(lm_config)
        self.language_model.norm = nn.Identity()
        tts_lm_config = copy.deepcopy(lm_config)
        tts_lm_config.num_hidden_layers = config.tts_backbone_num_hidden_layers
        self.tts_language_model = AutoModel.from_config(tts_lm_config)
        self.tts_input_types = nn.Embedding(
            num_embeddings=2, embedding_dim=config.decoder_config.hidden_size
        )
        self.acoustic_tokenizer = AutoModel.from_config(
            config.acoustic_tokenizer_config
        ).to(dtype)
        self.acoustic_connector = SpeechConnector(
            config.acoustic_vae_dim, lm_config.hidden_size
        ).to(dtype)
        self.register_buffer("speech_scaling_factor", torch.tensor(float("nan")))
        self.register_buffer("speech_bias_factor", torch.tensor(float("nan")))
        self.prediction_head = AutoModel.from_config(config.diffusion_head_config).to(
            dtype
        )
        self.noise_scheduler = DPMSolverMultistepScheduler(
            num_train_timesteps=config.diffusion_head_config.ddpm_num_steps,
            beta_schedule=config.diffusion_head_config.ddpm_beta_schedule,
            prediction_type=config.diffusion_head_config.prediction_type,
        )

    def get_input_embeddings(self):
        if hasattr(self.language_model, "embed_tokens"):
            return self.language_model.embed_tokens
        for name, attr in self.language_model.fullmap.items():
            if attr.orig_name == "embed_tokens.weight":
                return getattr(self.language_model, name)
        assert False, "should not arrive here"

    def set_input_embeddings(self, value):
        self.language_model.embed_tokens = value

    def set_speech_tokenizers(self, acoustic_tokenizer=None):
        self.acoustic_tokenizer = acoustic_tokenizer
        if self.acoustic_tokenizer is not None:
            self.acoustic_tokenizer.train(False)

    def forward(self, *args, **kwargs):
        raise RuntimeError(
            'QWEN3VoxStreamingModel.forward is intentionally disabled. Use `model.language_model(...)` or `model.tts_language_model(...)` instead.'
        )


AutoModel.register(QWEN3VoxStreamingConfig, QWEN3VoxStreamingModel)
__all__ = [
    'QWEN3VoxStreamingPreTrainedModel',
    'QWEN3VoxStreamingModel',
    "BinaryClassifier",
    "SpeechConnector",
]
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union, Callable
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from transformers.models.auto import AutoModel, AutoModelForCausalLM
from transformers.activations import ACT2FN
from transformers.modeling_outputs import (
    CausalLMOutput,
    BaseModelOutputWithPast,
    ModelOutput,
)
from transformers.models.llama.modeling_llama import LlamaRMSNorm
from transformers import modeling_utils
from transformers.modeling_utils import PreTrainedModel
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
from transformers.utils import logging

logger = logging.get_logger(__name__)
if (
    not hasattr(modeling_utils, "ALL_PARALLEL_STYLES")
    or modeling_utils.ALL_PARALLEL_STYLES is None
):
    modeling_utils.ALL_PARALLEL_STYLES = ["tp", "none", "colwise", "rowwise"]


@dataclass
class QWEN3VoxCausalLMOutputWithPast(ModelOutput):
    loss: Optional[torch.FloatTensor] = None
    diffusion_loss: Optional[torch.FloatTensor] = None
    speech_token_num: Optional[int] = None
    logits: torch.FloatTensor = None
    past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
    hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
    attentions: Optional[Tuple[torch.FloatTensor, ...]] = None


@dataclass
class QWEN3VoxGenerationOutput(ModelOutput):
    sequences: torch.LongTensor = None
    speech_outputs: Optional[List[torch.FloatTensor]] = None


class SpeechConnector(nn.Module):

    def __init__(self, input_dim, output_dim):
        super().__init__()
        self.fc1 = nn.Linear(input_dim, output_dim)
        self.norm = LlamaRMSNorm(output_dim, eps=1e-06)
        self.fc2 = nn.Linear(output_dim, output_dim)

    def forward(self, features, **kwargs):
        x = self.fc1(features)
        x = self.norm(x)
        x = self.fc2(x)
        return x


class QWEN3VoxPreTrainedModel(PreTrainedModel):
    config_class = QWEN3VoxConfig
    base_model_prefix = "model"
    supports_gradient_checkpointing = True
    _skip_keys_device_placement = "past_key_values"
    _supports_cache_class = True
    _supports_flash_attn_2 = True
    _supports_sdpa = True
    _supports_quantized_cache = True
    _supports_static_cache = True
    _supports_attention_backend = True

    def _init_weights(self, module):
        if isinstance(module, QWEN3VoxDiffusionHead):
            module.initialize_weights()
            return
        if hasattr(self.config, "language_model_config") and hasattr(
            self.config.language_model_config, "initializer_range"
        ):
            std = self.config.language_model_config.initializer_range
        elif hasattr(self.config, "decoder_config") and hasattr(
            self.config.decoder_config, "initializer_range"
        ):
            std = self.config.decoder_config.initializer_range
        else:
            std = 0.02
        if isinstance(module, nn.Linear):
            module.weight.data.normal_(mean=0.0, std=std)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(module, nn.LayerNorm):
            module.weight.data.fill_(1.0)
            module.bias.data.zero_()


class QWEN3VoxModel(QWEN3VoxPreTrainedModel):

    def __init__(self, config):
        super().__init__(config)
        if hasattr(config, "torch_dtype") and config.torch_dtype is not None:
            if isinstance(config.torch_dtype, str):
                dtype = getattr(torch, config.torch_dtype)
            else:
                dtype = config.torch_dtype
        else:
            dtype = torch.float32
        lm_config = config.decoder_config
        self.language_model = AutoModel.from_config(lm_config)
        self.acoustic_tokenizer = AutoModel.from_config(
            config.acoustic_tokenizer_config
        ).to(dtype)
        self.semantic_tokenizer = AutoModel.from_config(
            config.semantic_tokenizer_config
        ).to(dtype)
        self.acoustic_connector = SpeechConnector(
            config.acoustic_vae_dim, lm_config.hidden_size
        ).to(dtype)
        self.semantic_connector = SpeechConnector(
            config.semantic_vae_dim, lm_config.hidden_size
        ).to(dtype)
        self.register_buffer("speech_scaling_factor", torch.tensor(float("nan")))
        self.register_buffer("speech_bias_factor", torch.tensor(float("nan")))
        self.prediction_head = AutoModel.from_config(config.diffusion_head_config).to(
            dtype
        )
        self.noise_scheduler = DPMSolverMultistepScheduler(
            num_train_timesteps=config.diffusion_head_config.ddpm_num_steps,
            beta_schedule=config.diffusion_head_config.ddpm_beta_schedule,
            prediction_type=config.diffusion_head_config.prediction_type,
        )

    def get_input_embeddings(self):
        if hasattr(self.language_model, "embed_tokens"):
            return self.language_model.embed_tokens
        for name, attr in self.language_model.fullmap.items():
            if attr.orig_name == "embed_tokens.weight":
                return getattr(self.language_model, name)
        assert False, "should not arrive here"

    def set_input_embeddings(self, value):
        self.language_model.embed_tokens = value

    def set_speech_tokenizers(self, acoustic_tokenizer=None, semantic_tokenizer=None):
        self.acoustic_tokenizer = acoustic_tokenizer
        self.semantic_tokenizer = semantic_tokenizer
        if self.acoustic_tokenizer is not None:
            self.acoustic_tokenizer.train(False)
        if self.semantic_tokenizer is not None:
            self.semantic_tokenizer.train(False)

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        **kwargs,
    ) -> Union[Tuple, BaseModelOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        outputs = self.language_model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            inputs_embeds=inputs_embeds,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
            **kwargs,
        )
        if not return_dict:
            return outputs
        return BaseModelOutputWithPast(
            last_hidden_state=outputs.last_hidden_state,
            past_key_values=outputs.past_key_values,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )


class QWEN3VoxForConditionalGeneration(QWEN3VoxPreTrainedModel):
    _tied_weights_keys = ["lm_head.weight"]
    _tp_plan = {"lm_head": "colwise_rep"}

    def __init__(self, config):
        super().__init__(config)
        self.model = QWEN3VoxModel(config)
        self.vocab_size = config.decoder_config.vocab_size
        self.lm_head = nn.Linear(
            config.decoder_config.hidden_size, self.vocab_size, bias=False
        )
        self.post_init()

    def get_input_embeddings(self):
        return self.model.get_input_embeddings()

    def set_input_embeddings(self, value):
        self.model.set_input_embeddings(value)

    def get_output_embeddings(self):
        return self.lm_head

    def set_decoder(self, decoder):
        self.model.language_model = decoder

    def get_decoder(self):
        return self.model.language_model

    def tie_weights(self):
        if getattr(self.config.decoder_config, "tie_word_embeddings", False):
            output_embeddings = self.get_output_embeddings()
            input_embeddings = self.get_input_embeddings()
            if hasattr(input_embeddings, "weight"):
                output_embeddings.weight = input_embeddings.weight
            else:
                output_embeddings.weight = input_embeddings
            if getattr(output_embeddings, "bias", None) is not None:
                output_embeddings.bias.data = nn.functional.pad(
                    output_embeddings.bias.data,
                    (
                        0,
                        output_embeddings.weight.shape[0]
                        - output_embeddings.bias.shape[0],
                    ),
                    "constant",
                    0,
                )
            print("Tied input and output embeddings using standard assignment.")
        else:
            print("tie_word_embeddings is False, not tying weights.")

    def set_output_embeddings(self, new_embeddings):
        self.lm_head = new_embeddings

    def forward_speech_features(
        self,
        speech_tensors=None,
        speech_masks=None,
        speech_type="audio",
        return_unmask=False,
    ):
        if speech_tensors is None:
            vae_dim = self.config.acoustic_tokenizer_config.vae_dim
            audio_features = torch.zeros(1, 1, vae_dim).to(
                self.get_input_embeddings().weight
            )
            connect_features = self.model.acoustic_connector(audio_features)
            return (audio_features, connect_features)
        else:
            with torch.no_grad():
                if speech_type == "audio":
                    with torch.no_grad():
                        frames = self.model.acoustic_tokenizer.encode(
                            speech_tensors.unsqueeze(1)
                        )[0][0]
                    audio_tokens = frames.sample(
                        self.model.acoustic_tokenizer.std_dist_type
                    )[0]
                elif speech_type == "vae":
                    vae_dim = self.config.acoustic_tokenizer_config.vae_dim
                    speech_mode = speech_tensors.reshape(
                        speech_tensors.size(0), -1, vae_dim
                    )
                    batch_size = speech_mode.size(0)
                    value = self.model.acoustic_tokenizer.fix_std / 0.8
                    std = (
                        torch.randn(
                            batch_size,
                            dtype=speech_mode.dtype,
                            device=speech_mode.device,
                        )
                        * value
                    )
                    std = std.view(-1, *[1] * (speech_mode.dim() - 1))
                    audio_tokens = speech_mode + std * torch.randn(
                        speech_mode.shape
                    ).to(speech_mode)
                else:
                    raise NotImplementedError(
                        f"Speech type {speech_type } not implemented"
                    )
                if torch.isnan(self.model.speech_scaling_factor) or torch.isnan(
                    self.model.speech_bias_factor
                ):
                    scaling_factor = 1.0 / audio_tokens[speech_masks].flatten().std()
                    bias_factor = -audio_tokens[speech_masks].flatten().mean()
                    if dist.is_available() and dist.is_initialized():
                        dist.all_reduce(scaling_factor, op=dist.ReduceOp.SUM)
                        dist.all_reduce(bias_factor, op=dist.ReduceOp.SUM)
                        world_size = dist.get_world_size()
                        self.model.speech_scaling_factor.copy_(
                            scaling_factor / world_size
                        )
                        self.model.speech_bias_factor.copy_(bias_factor / world_size)
                        print(
                            f"Speech scaling factor (distributed): {self .model .speech_scaling_factor }, bias factor: {self .model .speech_bias_factor }",
                            flush=True,
                        )
                    else:
                        self.model.speech_scaling_factor.copy_(scaling_factor)
                        self.model.speech_bias_factor.copy_(bias_factor)
                        print(
                            f"Speech scaling factor (single process): {self .model .speech_scaling_factor }, bias factor: {self .model .speech_bias_factor }",
                            flush=True,
                        )
                audio_features = (
                    audio_tokens + self.model.speech_bias_factor
                ) * self.model.speech_scaling_factor
            connect_features = self.model.acoustic_connector(audio_features)
            if return_unmask:
                return (audio_features, connect_features)
            return (audio_features[speech_masks], connect_features[speech_masks])

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[List[torch.FloatTensor]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = False,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        speech_tensors: Optional[torch.FloatTensor] = None,
        speech_masks: Optional[torch.BoolTensor] = None,
        speeches_loss_input: Optional[torch.FloatTensor] = None,
        speech_semantic_tensors: Optional[torch.FloatTensor] = None,
        acoustic_input_mask: Optional[torch.BoolTensor] = None,
        acoustic_loss_mask: Optional[torch.BoolTensor] = None,
        ddpm_batch_mul: int = 1,
        **kwargs: Optional[Dict[str, Union[torch.Tensor, str]]],
    ) -> Union[Tuple, QWEN3VoxCausalLMOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        x = self.get_input_embeddings()(input_ids)
        semantic_speech_all_connect_features = self.model.semantic_connector(
            speech_semantic_tensors
        )
        if speeches_loss_input is not None:
            speech_all_features, speech_all_connect_features = (
                self.forward_speech_features(
                    speech_tensors=(
                        speech_tensors.type_as(x)
                        if speech_tensors is not None
                        else None
                    ),
                    speech_masks=speech_masks,
                    speech_type=kwargs.get("speech_type", "audio"),
                    return_unmask=True,
                )
            )
            if speech_tensors is not None:
                if semantic_speech_all_connect_features is not None:
                    x[acoustic_input_mask] = (
                        speech_all_connect_features[speech_masks]
                        + semantic_speech_all_connect_features[speech_masks]
                    )
                else:
                    x[acoustic_input_mask] = speech_all_connect_features[speech_masks]
                target_latent_mask = speeches_loss_input & speech_masks
                speech_features = speech_all_features[target_latent_mask]
                speech_connect_features = speech_all_connect_features[
                    target_latent_mask
                ]
        else:
            speech_features, speech_connect_features = self.forward_speech_features(
                speech_tensors=(
                    speech_tensors.type_as(x) if speech_tensors is not None else None
                ),
                speech_masks=speech_masks,
                speech_type=kwargs.get("speech_type", "audio"),
            )
            if speech_tensors is not None:
                x[acoustic_input_mask] = speech_connect_features
        outputs = self.model(
            input_ids=None,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            inputs_embeds=x,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=False,
            return_dict=return_dict,
            cache_position=cache_position,
        )
        hidden_states = outputs.last_hidden_state
        logits = self.lm_head(hidden_states)
        loss = None
        if labels is not None:
            pass
        diffusion_loss = None
        if speech_tensors is not None and acoustic_loss_mask.sum().item() > 0:
            condition_features = hidden_states[acoustic_loss_mask]
            speech_len, latent_size = speech_features.shape
            noise = torch.randn(
                (speech_len * ddpm_batch_mul, latent_size),
                device=hidden_states.device,
                dtype=hidden_states.dtype,
            )
            timesteps = torch.multinomial(
                torch.ones(self.config.diffusion_head_config.ddpm_num_steps),
                speech_len * ddpm_batch_mul,
                replacement=True,
            ).to(hidden_states.device)
            speech_features_repeated = speech_features.repeat_interleave(
                ddpm_batch_mul, dim=0
            )
            condition_features_repeated = condition_features.repeat_interleave(
                ddpm_batch_mul, dim=0
            )
            noisy_speech_features = self.model.noise_scheduler.add_noise(
                speech_features_repeated, noise, timesteps
            )
            model_output = self.model.prediction_head(
                noisy_speech_features, timesteps.type_as(x), condition_features_repeated
            )
            prediction_type = self.config.diffusion_head_config.prediction_type
            if prediction_type == "epsilon":
                target_for_loss = noise
            elif prediction_type == "v_prediction":
                target_for_loss = self.model.noise_scheduler.get_velocity(
                    speech_features_repeated, noise, timesteps
                )
            else:
                raise NotImplementedError(
                    f"Prediction type {prediction_type } not implemented"
                )
            diffusion_loss = F.mse_loss(
                model_output.float(), target_for_loss.float(), reduction="sum"
            )
            if latent_size > 0 and ddpm_batch_mul > 0:
                diffusion_loss = diffusion_loss / latent_size / ddpm_batch_mul
            else:
                diffusion_loss = torch.tensor(0.0, device=diffusion_loss.device)
        else:
            diffusion_loss = (
                sum((p.sum() for p in self.model.prediction_head.parameters())) * 0.0
            )
            diffusion_loss += (
                sum((p.sum() for p in self.model.acoustic_connector.parameters())) * 0.0
            )
            diffusion_loss += (
                sum((p.sum() for p in self.model.semantic_connector.parameters())) * 0.0
            )
        if not return_dict:
            output = (logits, speech_len) + outputs.to_tuple()[1:]
            return (loss, diffusion_loss) + output
        return QWEN3VoxCausalLMOutputWithPast(
            loss=loss,
            diffusion_loss=diffusion_loss,
            speech_token_num=speech_len if speech_tensors is not None else 0,
            logits=logits,
            past_key_values=outputs.past_key_values,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )


AutoModel.register(QWEN3VoxConfig, QWEN3VoxModel)
AutoModelForCausalLM.register(QWEN3VoxConfig, QWEN3VoxForConditionalGeneration)
__all__ = [
    'QWEN3VoxModel',
    'QWEN3VoxPreTrainedModel',
    'QWEN3VoxForConditionalGeneration',
    'QWEN3VoxCausalLMOutputWithPast',
    'QWEN3VoxGenerationOutput',
]
'\nQWEN3Vox Processors\n\nThis module provides processors for preparing inputs for QWEN3Vox models:\n- QWEN3VoxProcessor: For multi-speaker models (1.5B, 7B)\n- QWEN3VoxStreamingProcessor: For streaming model (0.5B)\n'
__all__ = [
    'QWEN3VoxProcessor',
    'QWEN3VoxStreamingProcessor',
    'QWEN3VoxTokenizerProcessor',
    "AudioNormalizer",
    'QWEN3VoxASRProcessor',
]
'\nQWEN3Vox Streaming Inference Model (0.5B)\n\nThis module implements the inference engine for real-time streaming TTS.\nKey features:\n- Window-based text/speech interleaving for streaming\n- Binary EOS classifier for end-of-speech detection\n- Classifier-free guidance for speech quality\n- Audio streaming support\n'
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union, Callable
from tqdm import tqdm
import torch
import torch.nn as nn
from transformers.models.auto import AutoModel, AutoModelForCausalLM
from transformers.generation import (
    GenerationMixin,
    GenerationConfig,
    LogitsProcessor,
    LogitsProcessorList,
    StoppingCriteriaList,
)
from transformers.modeling_outputs import BaseModelOutputWithPast, ModelOutput
from transformers import modeling_utils
from transformers.modeling_utils import PreTrainedModel
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
from transformers.utils import logging

logger = logging.get_logger(__name__)
if (
    not hasattr(modeling_utils, "ALL_PARALLEL_STYLES")
    or modeling_utils.ALL_PARALLEL_STYLES is None
):
    modeling_utils.ALL_PARALLEL_STYLES = ["tp", "none", "colwise", "rowwise"]
TTS_TEXT_WINDOW_SIZE = 5
TTS_SPEECH_WINDOW_SIZE = 6


def _update_model_kwargs_for_generation(
    outputs: ModelOutput, model_kwargs: Dict[str, Any], num_new_tokens: int = 1
) -> Dict[str, Any]:
    model_kwargs["past_key_values"] = getattr(outputs, "past_key_values")
    attention_mask = model_kwargs["attention_mask"]
    model_kwargs["attention_mask"] = torch.cat(
        [
            attention_mask,
            attention_mask.new_ones((attention_mask.shape[0], num_new_tokens)),
        ],
        dim=-1,
    )
    model_kwargs["cache_position"] = torch.arange(
        model_kwargs["cache_position"][-1] + 1,
        model_kwargs["cache_position"][-1] + num_new_tokens + 1,
    ).to(model_kwargs["cache_position"].device)
    return model_kwargs


@dataclass
class QWEN3VoxLMHeadOutputWithPast(BaseModelOutputWithPast):
    """LM-head-only return type for streaming / lightweight forwards (no loss/diffusion fields)."""

    logits: Optional[torch.FloatTensor] = None


@dataclass
class QWEN3VoxGenerationOutput(ModelOutput):
    sequences: torch.LongTensor = None
    speech_outputs: Optional[List[torch.FloatTensor]] = None
    reach_max_step_sample: Optional[torch.BoolTensor] = None


class QWEN3VoxStreamingForConditionalGenerationInference(
    QWEN3VoxStreamingPreTrainedModel, GenerationMixin
):

    def __init__(self, config):
        super().__init__(config)
        self.model = QWEN3VoxStreamingModel(config)
        self.tts_eos_classifier = BinaryClassifier(config.decoder_config.hidden_size)
        self.ddpm_inference_steps = (
            config.diffusion_head_config.ddpm_num_inference_steps
        )
        self.post_init()

    @property
    def noise_scheduler(self):
        return self.model.noise_scheduler

    @property
    def prediction_head(self):
        return self.model.prediction_head

    @property
    def speech_scaling_factor(self):
        return self.model.speech_scaling_factor

    @property
    def speech_bias_factor(self):
        return self.model.speech_bias_factor

    @property
    def acoustic_tokenizer(self):
        return self.model.acoustic_tokenizer

    @property
    def acoustic_connector(self):
        return self.model.acoustic_connector

    def tie_weights(self):
        if not getattr(self.config, "tie_word_embeddings", False):
            return
        if hasattr(self, "lm_head") and hasattr(
            self.model.language_model, "embed_tokens"
        ):
            self.lm_head.weight = self.model.language_model.embed_tokens.weight

    def get_input_embeddings(self):
        return self.model.get_input_embeddings()

    def set_input_embeddings(self, value):
        self.model.set_input_embeddings(value)

    def get_output_embeddings(self):
        return None

    def set_output_embeddings(self, new_embeddings):
        raise RuntimeError(
            "Output embeddings (lm_head) are not defined for this model. Create one before calling set_output_embeddings if needed."
        )

    def set_speech_tokenizers(self, acoustic_tokenizer=None):
        self.model.set_speech_tokenizers(acoustic_tokenizer)

    def set_ddpm_inference_steps(self, num_steps=None):
        self.ddpm_inference_steps = (
            num_steps or self.config.diffusion_head_config.ddpm_num_inference_steps
        )

    def forward_lm(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        **kwargs,
    ) -> Union[Tuple, BaseModelOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        if inputs_embeds is None:
            inputs_embeds = self.model.get_input_embeddings()(input_ids)
        outputs = self.model.language_model(
            inputs_embeds=inputs_embeds,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
            **kwargs,
        )
        hidden_states = outputs[0] if not return_dict else outputs.last_hidden_state
        if labels is not None:
            raise NotImplementedError(
                "Loss computation is not implemented in this version."
            )
        return BaseModelOutputWithPast(
            past_key_values=outputs.past_key_values,
            last_hidden_state=hidden_states,
            attentions=outputs.attentions,
        )

    def forward_tts_lm(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        lm_last_hidden_state: Optional[torch.FloatTensor] = None,
        tts_text_masks: Optional[torch.BoolTensor] = None,
        **kwargs,
    ) -> Union[Tuple, QWEN3VoxLMHeadOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        if inputs_embeds is None:
            inputs_embeds = self.model.get_input_embeddings()(input_ids)
        start_idx = inputs_embeds.shape[1] - lm_last_hidden_state.shape[1]
        inputs_embeds[:, start_idx:, :] = lm_last_hidden_state
        inputs_embeds = inputs_embeds + self.model.tts_input_types(
            tts_text_masks.long()
        )
        outputs = self.model.tts_language_model(
            inputs_embeds=inputs_embeds,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
            **kwargs,
        )
        hidden_states = outputs[0] if not return_dict else outputs.last_hidden_state
        logits = self.tts_eos_classifier(hidden_states[:, -1, :])
        if labels is not None:
            raise NotImplementedError(
                "Loss computation is not implemented in this version."
            )
        return QWEN3VoxLMHeadOutputWithPast(
            logits=logits,
            past_key_values=outputs.past_key_values,
            last_hidden_state=hidden_states,
            attentions=outputs.attentions,
        )

    def forward(self, *args, **kwargs):
        raise RuntimeError(
            "Unified forward is disabled. Use `forward_lm`, `forward_tts_lm`, or `generate` instead."
        )

    def _build_generate_config_model_kwargs(
        self, generation_config, inputs, tokenizer, return_processors=False, **kwargs
    ):
        if generation_config is None:
            generation_config = GenerationConfig(
                bos_token_id=tokenizer.bos_token_id,
                eos_token_id=tokenizer.eos_token_id,
                pad_token_id=tokenizer.pad_token_id,
            )
        else:
            generation_config = GenerationConfig(
                **generation_config,
                bos_token_id=tokenizer.bos_token_id,
                eos_token_id=tokenizer.eos_token_id,
                pad_token_id=tokenizer.pad_token_id,
            )
        generation_config, model_kwargs = self._prepare_generation_config(
            generation_config,
            True,
            speech_start_id=tokenizer.speech_start_id,
            speech_end_id=tokenizer.speech_end_id,
            speech_diffusion_id=tokenizer.speech_diffusion_id,
            **kwargs,
        )
        generation_config.speech_start_id = tokenizer.speech_start_id
        generation_config.speech_end_id = tokenizer.speech_end_id
        generation_config.speech_diffusion_id = tokenizer.speech_diffusion_id
        inputs_tensor, model_input_name, model_kwargs = self._prepare_model_inputs(
            inputs, generation_config.bos_token_id, model_kwargs
        )
        batch_size = inputs_tensor.shape[0]
        device = self.device
        self._prepare_special_tokens(generation_config, True, device=device)
        generation_config.use_cache = True
        model_kwargs["use_cache"] = generation_config.use_cache
        input_ids = inputs_tensor.to(self.device)
        input_ids_length = input_ids.shape[1]
        has_default_max_length = (
            kwargs.get("max_length") is None
            and generation_config.max_length is not None
        )
        has_default_min_length = (
            kwargs.get("min_length") is None
            and generation_config.min_length is not None
        )
        generation_config = self._prepare_generated_length(
            generation_config=generation_config,
            has_default_max_length=has_default_max_length,
            has_default_min_length=has_default_min_length,
            model_input_name=model_input_name,
            inputs_tensor=inputs_tensor,
            input_ids_length=input_ids_length,
        )
        max_cache_length = generation_config.max_length - 1
        self._prepare_cache_for_generation(
            generation_config, model_kwargs, None, batch_size, max_cache_length, device
        )
        model_kwargs["cache_position"] = torch.arange(
            input_ids_length, device=device, dtype=torch.long
        )
        for k, v in model_kwargs.items():
            if isinstance(v, torch.Tensor):
                model_kwargs[k] = v.to(device=device)
        if return_processors:
            logits_processor = self._get_logits_processor(
                generation_config=generation_config,
                input_ids_seq_length=input_ids_length,
                encoder_input_ids=inputs_tensor,
                prefix_allowed_tokens_fn=None,
                logits_processor=LogitsProcessorList(),
                device=inputs_tensor.device,
                model_kwargs=model_kwargs,
            )
            stopping_criteria = self._get_stopping_criteria(
                generation_config=generation_config,
                stopping_criteria=StoppingCriteriaList(),
            )
            return (
                generation_config,
                model_kwargs,
                input_ids,
                logits_processor,
                stopping_criteria,
            )
        else:
            return (generation_config, model_kwargs, input_ids)

    @torch.no_grad()
    def generate(
        self,
        inputs: Optional[torch.Tensor] = None,
        generation_config: Optional[GenerationConfig] = None,
        logits_processor: Optional[LogitsProcessorList] = None,
        stopping_criteria: Optional[StoppingCriteriaList] = None,
        prefix_allowed_tokens_fn: Optional[
            Callable[[int, torch.Tensor], List[int]]
        ] = None,
        synced_gpus: Optional[bool] = None,
        assistant_model: Optional["PreTrainedModel"] = None,
        audio_streamer: Optional[Union[AudioStreamer, AsyncAudioStreamer]] = None,
        negative_prompt_ids: Optional[torch.Tensor] = None,
        negative_prompt_attention_mask: Optional[torch.Tensor] = None,
        speech_tensors: Optional[torch.FloatTensor] = None,
        speech_masks: Optional[torch.BoolTensor] = None,
        speech_input_mask: Optional[torch.BoolTensor] = None,
        tts_text_ids: Optional[torch.LongTensor] = None,
        return_speech: bool = True,
        cfg_scale: float = 1.0,
        stop_check_fn: Optional[Callable[[], bool]] = None,
        **kwargs,
    ) -> Union[torch.LongTensor, QWEN3VoxGenerationOutput]:
        tokenizer = kwargs.pop("tokenizer", None)
        neg_text_input_id = tokenizer.convert_tokens_to_ids("<|image_pad|>")
        tts_lm_input_ids = kwargs.pop("tts_lm_input_ids", None)
        tts_lm_attention_mask = kwargs.pop("tts_lm_attention_mask", None)
        all_prefilled_outputs = kwargs.pop("all_prefilled_outputs", None)
        tts_text_ids = tts_text_ids.to(self.device)
        if kwargs.get("max_new_tokens", None) is None:
            kwargs["max_new_tokens"] = (
                self.config.decoder_config.max_position_embeddings
                - tts_lm_input_ids.shape[-1]
            )
        (
            generation_config,
            model_kwargs,
            input_ids,
            logits_processor,
            stopping_criteria,
        ) = self._build_generate_config_model_kwargs(
            generation_config, inputs, tokenizer, return_processors=True, **kwargs
        )
        negative_kwargs = {
            "input_ids": torch.full(
                (kwargs["input_ids"].shape[0], 1),
                neg_text_input_id,
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "attention_mask": torch.ones(
                (kwargs["input_ids"].shape[0], 1),
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "max_new_tokens": kwargs.get("max_new_tokens", 100),
        }
        negative_generation_config, negative_model_kwargs, negative_input_ids = (
            self._build_generate_config_model_kwargs(
                None, None, tokenizer, return_processors=False, **negative_kwargs
            )
        )
        tts_lm_kwargs = {
            "input_ids": tts_lm_input_ids,
            "attention_mask": tts_lm_attention_mask,
            "max_new_tokens": kwargs.get("max_new_tokens", 100),
        }
        tts_lm_generation_config, tts_lm_model_kwargs, tts_lm_input_ids = (
            self._build_generate_config_model_kwargs(
                None, None, tokenizer, return_processors=False, **tts_lm_kwargs
            )
        )
        tts_lm_negative_kwargs = {
            "input_ids": torch.full(
                (kwargs["input_ids"].shape[0], 1),
                neg_text_input_id,
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "attention_mask": torch.ones(
                (kwargs["input_ids"].shape[0], 1),
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "max_new_tokens": kwargs.get("max_new_tokens", 100),
        }
        (
            tts_lm_negative_generation_config,
            tts_lm_negative_model_kwargs,
            tts_lm_negative_input_ids,
        ) = self._build_generate_config_model_kwargs(
            None, None, tokenizer, return_processors=False, **tts_lm_negative_kwargs
        )
        acoustic_cache = QWEN3VoxTokenizerStreamingCache()
        batch_size = input_ids.shape[0]
        assert batch_size == 1, "Currently only supports batch size == 1"
        device = input_ids.device
        finished_tags = torch.zeros(batch_size, dtype=torch.bool, device=device)
        verbose = kwargs.get("verbose", False)
        audio_chunks = [[] for _ in range(batch_size)]
        tts_text_window_index = 0
        reach_max_step_sample = torch.zeros(batch_size, dtype=torch.bool, device=device)
        first_text_window_size = (
            TTS_TEXT_WINDOW_SIZE
            if tts_text_ids.shape[1] >= TTS_TEXT_WINDOW_SIZE
            else tts_text_ids.shape[1]
        )
        outputs = all_prefilled_outputs["lm"]
        tts_lm_outputs = all_prefilled_outputs["tts_lm"]
        negative_outputs = all_prefilled_outputs["neg_lm"]
        tts_lm_negative_outputs = all_prefilled_outputs["neg_tts_lm"]
        model_kwargs = _update_model_kwargs_for_generation(
            outputs, model_kwargs, num_new_tokens=first_text_window_size
        )
        tts_lm_model_kwargs = _update_model_kwargs_for_generation(
            tts_lm_outputs, tts_lm_model_kwargs, num_new_tokens=first_text_window_size
        )
        negative_model_kwargs = self._update_model_kwargs_for_generation(
            negative_outputs, negative_model_kwargs, is_encoder_decoder=False
        )
        tts_lm_negative_model_kwargs = self._update_model_kwargs_for_generation(
            tts_lm_negative_outputs,
            tts_lm_negative_model_kwargs,
            is_encoder_decoder=False,
        )
        step = tts_lm_input_ids.shape[1]
        total_generated_speech_tokens = 0
        total_prefilled_text_tokens = 0
        if kwargs.get("show_progress_bar", True):
            progress_bar = tqdm(
                total=tts_lm_generation_config.max_length,
                desc=f"Prefilled {step } tokens, current step ({step } / {tts_lm_generation_config .max_length })",
                initial=step,
                leave=False,
            )
        else:
            progress_bar = None
        while True:
            if stop_check_fn is not None and stop_check_fn():
                if verbose:
                    print(f"Generation stopped externally at step {step +1 }")
                if audio_streamer is not None:
                    audio_streamer.end()
                break
            if finished_tags.all():
                if hasattr(progress_bar, "set_description"):
                    progress_bar.set_description("Generation complete")
                break
            cur_input_tts_text_ids = tts_text_ids[
                :,
                tts_text_window_index
                * TTS_TEXT_WINDOW_SIZE : (tts_text_window_index + 1)
                * TTS_TEXT_WINDOW_SIZE,
            ]
            next_text_window_size = tts_text_ids[
                :,
                (tts_text_window_index + 1)
                * TTS_TEXT_WINDOW_SIZE : (tts_text_window_index + 2)
                * TTS_TEXT_WINDOW_SIZE,
            ].shape[1]
            tts_text_window_index += 1
            if cur_input_tts_text_ids.shape[1] > 0:
                input_ids = torch.cat([input_ids, cur_input_tts_text_ids], dim=-1)
                tts_lm_input_ids = torch.cat(
                    [tts_lm_input_ids, cur_input_tts_text_ids], dim=-1
                )
                if tts_lm_input_ids.shape[1] > tts_lm_generation_config.max_length:
                    if verbose:
                        print(
                            f"Reached maximum generation length {generation_config .max_length }, stopped it."
                        )
                    reached_samples = torch.arange(batch_size, device=device)[
                        ~finished_tags
                    ]
                    if reached_samples.numel() > 0:
                        reach_max_step_sample[reached_samples] = True
                    break
                step += cur_input_tts_text_ids.shape[1]
                total_prefilled_text_tokens += cur_input_tts_text_ids.shape[1]
                if progress_bar is not None:
                    progress_bar.update(cur_input_tts_text_ids.shape[1])
                    progress_bar.set_description(
                        f"Prefilled {total_prefilled_text_tokens } text tokens, generated {total_generated_speech_tokens } speech tokens, current step ({step } / {tts_lm_generation_config .max_length })"
                    )
                model_inputs = self.prepare_inputs_for_generation(
                    input_ids, **model_kwargs
                )
                outputs = self.forward_lm(
                    **model_inputs,
                    return_dict=True,
                    output_attentions=False,
                    output_hidden_states=False,
                )
                model_kwargs = _update_model_kwargs_for_generation(
                    outputs, model_kwargs, num_new_tokens=next_text_window_size
                )
                tts_lm_model_inputs = self.prepare_inputs_for_generation(
                    tts_lm_input_ids, **tts_lm_model_kwargs
                )
                tts_lm_additional_inputs = {
                    "tts_text_masks": torch.ones_like(tts_lm_input_ids[:, -1:]),
                    "lm_last_hidden_state": outputs.last_hidden_state,
                }
                tts_lm_outputs = self.forward_tts_lm(
                    **tts_lm_model_inputs,
                    **tts_lm_additional_inputs,
                    return_dict=True,
                    output_attentions=False,
                    output_hidden_states=False,
                )
                tts_lm_model_kwargs = self._update_model_kwargs_for_generation(
                    tts_lm_outputs, tts_lm_model_kwargs, is_encoder_decoder=False
                )
            diffusion_indices = torch.LongTensor([0])
            for cur_speech_index in range(TTS_SPEECH_WINDOW_SIZE):
                positive_condition = tts_lm_outputs.last_hidden_state[
                    diffusion_indices, -1, :
                ]
                negative_condition = tts_lm_negative_outputs.last_hidden_state[
                    diffusion_indices, -1, :
                ]
                speech_latent = self.sample_speech_tokens(
                    positive_condition, negative_condition, cfg_scale=cfg_scale
                ).unsqueeze(1)
                scaled_latent = speech_latent / self.model.speech_scaling_factor.to(
                    speech_latent.device
                ) - self.model.speech_bias_factor.to(speech_latent.device)
                audio_chunk = self.model.acoustic_tokenizer.decode(
                    scaled_latent.to(self.model.acoustic_tokenizer.device),
                    cache=acoustic_cache,
                    sample_indices=diffusion_indices.to(
                        self.model.acoustic_tokenizer.device
                    ),
                    use_cache=True,
                    debug=False,
                )
                for i, sample_idx in enumerate(diffusion_indices):
                    idx = sample_idx.item()
                    if not finished_tags[idx]:
                        audio_chunks[idx].append(audio_chunk[i])
                if audio_streamer is not None:
                    audio_streamer.put(audio_chunk, diffusion_indices)
                acoustic_embed = self.model.acoustic_connector(speech_latent)
                tts_lm_input_ids = torch.cat(
                    [tts_lm_input_ids, torch.ones_like(tts_lm_input_ids[:, -1:])],
                    dim=-1,
                )
                if tts_lm_input_ids.shape[1] > tts_lm_generation_config.max_length:
                    break
                step += 1
                total_generated_speech_tokens += 1
                if progress_bar is not None:
                    progress_bar.update(1)
                    progress_bar.set_description(
                        f"Prefilled {total_prefilled_text_tokens } text tokens, generated {total_generated_speech_tokens } speech tokens, current step ({step } / {tts_lm_generation_config .max_length })"
                    )
                tts_lm_model_inputs = self.prepare_inputs_for_generation(
                    tts_lm_input_ids, **tts_lm_model_kwargs
                )
                tts_lm_additional_inputs = {
                    "tts_text_masks": torch.zeros_like(tts_lm_input_ids[:, -1:]),
                    "lm_last_hidden_state": acoustic_embed,
                }
                tts_lm_outputs = self.forward_tts_lm(
                    **tts_lm_model_inputs,
                    **tts_lm_additional_inputs,
                    return_dict=True,
                    output_attentions=False,
                    output_hidden_states=False,
                )
                if (
                    cur_speech_index == TTS_SPEECH_WINDOW_SIZE - 1
                    and next_text_window_size > 0
                ):
                    tts_lm_model_kwargs = _update_model_kwargs_for_generation(
                        tts_lm_outputs,
                        tts_lm_model_kwargs,
                        num_new_tokens=next_text_window_size,
                    )
                else:
                    tts_lm_model_kwargs = self._update_model_kwargs_for_generation(
                        tts_lm_outputs, tts_lm_model_kwargs, is_encoder_decoder=False
                    )
                tts_lm_negative_input_ids = torch.cat(
                    [
                        tts_lm_negative_input_ids,
                        torch.ones_like(tts_lm_input_ids[:, -1:]),
                    ],
                    dim=-1,
                )
                tts_lm_negative_model_inputs = self.prepare_inputs_for_generation(
                    tts_lm_negative_input_ids, **tts_lm_negative_model_kwargs
                )
                tts_lm_negative_additional_inputs = {
                    "tts_text_masks": torch.zeros_like(
                        tts_lm_negative_input_ids[:, -1:]
                    ),
                    "lm_last_hidden_state": acoustic_embed,
                }
                tts_lm_negative_outputs = self.forward_tts_lm(
                    **tts_lm_negative_model_inputs,
                    **tts_lm_negative_additional_inputs,
                    return_dict=True,
                    output_attentions=False,
                    output_hidden_states=False,
                )
                tts_lm_negative_model_kwargs = self._update_model_kwargs_for_generation(
                    tts_lm_negative_outputs,
                    tts_lm_negative_model_kwargs,
                    is_encoder_decoder=False,
                )
                tts_eos_logits = torch.sigmoid(
                    self.tts_eos_classifier(
                        tts_lm_outputs.last_hidden_state[diffusion_indices, -1, :]
                    )
                )
                if tts_eos_logits[0].item() > 0.5:
                    finished_tags[diffusion_indices] = True
                    if audio_streamer is not None:
                        audio_streamer.end(diffusion_indices)
            if tts_lm_input_ids.shape[1] > tts_lm_generation_config.max_length:
                if verbose:
                    print(
                        f"Reached maximum generation length {tts_lm_generation_config .max_length }, stopped it."
                    )
                reached_samples = torch.arange(batch_size, device=device)[
                    ~finished_tags
                ]
                if reached_samples.numel() > 0:
                    reach_max_step_sample[reached_samples] = True
                break
        if audio_streamer is not None:
            audio_streamer.end()
        final_audio_outputs = []
        for sample_chunks in audio_chunks:
            if sample_chunks:
                concatenated_audio = torch.cat(sample_chunks, dim=-1)
                final_audio_outputs.append(concatenated_audio)
            else:
                final_audio_outputs.append(None)
        if reach_max_step_sample is not None and reach_max_step_sample.any():
            print(
                f"Reached maximum generation length {tts_lm_generation_config .max_length }, stopped it."
            )
        return QWEN3VoxGenerationOutput(
            sequences=tts_lm_input_ids,
            speech_outputs=final_audio_outputs if return_speech else None,
            reach_max_step_sample=reach_max_step_sample,
        )

    @torch.no_grad()
    def sample_speech_tokens(self, condition, neg_condition, cfg_scale=3.0):
        self.model.noise_scheduler.set_timesteps(self.ddpm_inference_steps)
        condition = torch.cat([condition, neg_condition], dim=0).to(
            self.model.prediction_head.device
        )
        speech = torch.randn(condition.shape[0], self.config.acoustic_vae_dim).to(
            condition
        )
        for t in self.model.noise_scheduler.timesteps:
            half = speech[: len(speech) // 2]
            combined = torch.cat([half, half], dim=0)
            eps = self.model.prediction_head(
                combined, t.repeat(combined.shape[0]).to(combined), condition=condition
            )
            cond_eps, uncond_eps = torch.split(eps, len(eps) // 2, dim=0)
            half_eps = uncond_eps + cfg_scale * (cond_eps - uncond_eps)
            eps = torch.cat([half_eps, half_eps], dim=0)
            speech = self.model.noise_scheduler.step(eps, t, speech).prev_sample
        return speech[: len(speech) // 2]


AutoModelForCausalLM.register(
    QWEN3VoxStreamingConfig, QWEN3VoxStreamingForConditionalGenerationInference
)
__all__ = [
    'QWEN3VoxStreamingForConditionalGenerationInference',
    'QWEN3VoxGenerationOutput',
    'QWEN3VoxLMHeadOutputWithPast',
    "TTS_TEXT_WINDOW_SIZE",
    "TTS_SPEECH_WINDOW_SIZE",
]
import logging
import os
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from datasets import load_dataset, DatasetDict, VerificationMode
from transformers import HfArgumentParser, Trainer, set_seed, TrainerCallback
from transformers import TrainingArguments as HfTrainingArguments
from peft import LoraConfig, get_peft_model, TaskType

logger = logging.getLogger(__name__)
import copy
import torch
from transformers import TrainerCallback


class EmaCallback(TrainerCallback):

    def __init__(self, attr_path="model.prediction_head", decay=0.999, device="cpu"):
        self.attr_path = attr_path
        self.decay = float(decay)
        self.device = torch.device(device)
        self.shadow = None
        self._orig = None

    def _get_module(self, model):
        mod = model
        for name in self.attr_path.split("."):
            mod = getattr(mod, name)
        return mod

    def on_train_begin(self, args, state, control, model=None, **kwargs):
        head = self._get_module(model)
        self.shadow = {
            k: p.detach().to(self.device).clone() for k, p in head.state_dict().items()
        }

    def on_step_end(self, args, state, control, model=None, **kwargs):
        if self.shadow is None:
            return
        head = self._get_module(model)
        with torch.no_grad():
            for k, v in head.state_dict().items():
                self.shadow[k].mul_(self.decay).add_(
                    v.detach().to(self.device), alpha=1.0 - self.decay
                )

    def _swap_in_ema(self, model):
        head = self._get_module(model)
        self._orig = copy.deepcopy(head.state_dict())
        head.load_state_dict(self.shadow, strict=False)

    def _swap_back(self, model):
        if self._orig is None:
            return
        head = self._get_module(model)
        head.load_state_dict(self._orig, strict=False)
        self._orig = None

    def on_evaluate(self, args, state, control, model=None, **kwargs):
        self._swap_in_ema(model)

    def on_evaluate_end(self, args, state, control, model=None, **kwargs):
        self._swap_back(model)

    def on_save(self, args, state, control, model=None, **kwargs):
        self._swap_in_ema(model)

    def on_save_end(self, args, state, control, model=None, **kwargs):
        self._swap_back(model)

    def on_train_end(self, args, state, control, model=None, **kwargs):
        self._swap_in_ema(model)


@dataclass
class ModelArguments:
    model_name_or_path: Optional[str] = field(
        default=None,
        metadata={
            "help": 'Path to QWEN3Vox base model with config.json'
        },
    )
    processor_name_or_path: Optional[str] = field(
        default=None,
        metadata={
            "help": "Path to processor dir (preprocessor_config.json). Defaults to model path."
        },
    )
    cache_dir: Optional[str] = field(default=None)
    freeze_acoustic_tokenizer: bool = field(default=True)
    freeze_semantic_tokenizer: bool = field(default=True)
    lora_r: int = field(default=8)
    lora_alpha: int = field(default=32)
    lora_dropout: float = field(default=0.05)
    lora_target_modules: str = field(
        default="q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj,down_proj",
        metadata={
            "help": "Comma-separated list of target module names in the LLM blocks"
        },
    )
    lora_wrap_diffusion_head: bool = field(
        default=False, metadata={"help": "Wrap diffusion head with PEFT LoRA"}
    )
    train_diffusion_head: bool = field(
        default=False,
        metadata={"help": "Train diffusion prediction head (full fine-tune)"},
    )
    train_connectors: bool = field(
        default=False,
        metadata={"help": "Train acoustic/semantic connectors (full fine-tune)"},
    )
    layers_to_freeze: Optional[str] = field(
        default=None,
        metadata={
            "help": "Comma-separated indices of diffusion head layers to freeze (e.g., '0,1,5,7,8')."
        },
    )


@dataclass
class DataArguments:
    dataset_name: Optional[str] = field(
        default=None,
        metadata={
            "help": "HF dataset name or 'json' with --train_jsonl for local files"
        },
    )
    dataset_config_name: Optional[str] = field(default=None)
    train_split_name: str = field(default="train")
    eval_split_name: Optional[str] = field(default="validation")
    text_column_name: str = field(default="text")
    audio_column_name: str = field(default="audio")
    voice_prompts_column_name: Optional[str] = field(default="voice_prompts")
    eval_split_size: float = field(default=0.0)
    ignore_verifications: bool = field(default=False)
    max_length: Optional[int] = field(default=None)
    train_jsonl: Optional[str] = field(
        default=None,
        metadata={
            "help": "Path to local train JSONL with {text, audio, [voice_prompts]}"
        },
    )
    validation_jsonl: Optional[str] = field(
        default=None, metadata={"help": "Optional path to local validation JSONL"}
    )
    voice_prompt_drop_rate: float = field(
        default=0.0,
        metadata={
            "help": "Probability to drop conditioning voice prompt during training (0.0 keep always, 1.0 drop always)."
        },
    )


@dataclass
class CustomTrainingArguments(HfTrainingArguments):
    ddpm_batch_mul: int = field(default=1)
    ce_loss_weight: float = field(default=1.0)
    diffusion_loss_weight: float = field(default=1.0)
    debug_ce_details: bool = field(default=False)
    debug_ce_topk: int = field(default=5)
    debug_ce_max_examples: int = field(default=1)
    debug_ce_every_n_steps: int = field(default=200)
    gradient_clipping: bool = field(
        default=False,
        metadata={
            "help": "Enable gradient clipping using max_grad_norm (set via --max_grad_norm, default 1.0). When False, disables clipping by forcing max_grad_norm=0.0."
        },
    )
    debug_save: bool = field(
        default=False,
        metadata={
            "help": "If set, saves model components BEFORE training starts, into output_dir/debug_initial."
        },
    )


def build_lora_config(args: ModelArguments) -> LoraConfig:
    target_modules = [
        s.strip() for s in args.lora_target_modules.split(",") if s.strip()
    ]
    # language_model is Qwen2Model (backbone), not ForCausalLM. CAUSAL_LM maps to
    # PeftModelForCausalLM which requires prepare_inputs_for_generation on the base.
    return LoraConfig(
        r=args.lora_r,
        lora_alpha=args.lora_alpha,
        lora_dropout=args.lora_dropout,
        bias="none",
        task_type=TaskType.FEATURE_EXTRACTION,
        target_modules=target_modules,
    )


def build_head_lora_config(args: ModelArguments) -> LoraConfig:
    target_modules = [
        "noisy_images_proj",
        "cond_proj",
        "gate_proj",
        "up_proj",
        "down_proj",
        "linear",
    ]
    return LoraConfig(
        r=args.lora_r,
        lora_alpha=args.lora_alpha,
        lora_dropout=args.lora_dropout,
        bias="none",
        task_type=TaskType.FEATURE_EXTRACTION,
        target_modules=target_modules,
    )


def mask_for_ce(
    labels: torch.Tensor,
    attention_mask: torch.Tensor,
    acoustic_input_mask: torch.Tensor,
    pad_id: int = -100,
) -> torch.Tensor:
    shifted = labels[:, 1:].contiguous()
    base_mask = (
        attention_mask[:, 1:].contiguous().eq(1)
        if attention_mask is not None and attention_mask.numel() > 0
        else torch.ones_like(shifted, dtype=torch.bool)
    )
    label_is_acoustic = acoustic_input_mask[:, 1:].contiguous()
    final_mask = base_mask & ~label_is_acoustic
    out = shifted.clone()
    out[~final_mask] = pad_id
    return out


def _patch_acoustic_encode_for_legacy_indexing(model_obj, logger_):
    try:
        acoustic = getattr(
            getattr(model_obj, "model", model_obj), "acoustic_tokenizer", None
        )
        if acoustic is None or not hasattr(acoustic, "encode"):
            logger_.warning("No acoustic_tokenizer.encode() found to patch.")
            return
        base_encode = acoustic.encode

        def encode_wrapped(*args, **kwargs):
            out = base_encode(*args, **kwargs)
            try:
                _ = out[0][0]
                return out
            except Exception:
                pass
            if isinstance(out, dict):
                for k in ("frames", "codes", "tokens", "latents", "hidden_states"):
                    if k in out:
                        return [[out[k]]]
                if len(out) > 0:
                    return [[next(iter(out.values()))]]
            for attr in ("frames", "codes", "tokens", "latents", "hidden_states"):
                if hasattr(out, attr):
                    return [[getattr(out, attr)]]
            try:
                if isinstance(out, torch.Tensor):
                    return [[out]]
            except Exception:
                pass
            return [[out]]

        acoustic.encode = encode_wrapped
        logger_.info(
            "Patched acoustic_tokenizer.encode() to return [[...]] for legacy indexing."
        )
    except Exception as e:
        logger_.warning(f"Failed to patch acoustic_tokenizer.encode(): {e }")


def main() -> None:
    parser = HfArgumentParser((ModelArguments, DataArguments, CustomTrainingArguments))
    model_args, data_args, training_args = parser.parse_args_into_dataclasses()
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN,
    )
    logger.info("Training/evaluation parameters %s", training_args)
    set_seed(training_args.seed)
    if not getattr(training_args, "gradient_clipping", False):
        if hasattr(training_args, "max_grad_norm"):
            training_args.max_grad_norm = 0.0
            logger.info(
                "Gradient clipping disabled (set max_grad_norm=0.0). Use --gradient_clipping to enable."
            )
    else:
        if (
            not hasattr(training_args, "max_grad_norm")
            or training_args.max_grad_norm is None
            or training_args.max_grad_norm <= 0
        ):
            training_args.max_grad_norm = 1.0
        logger.info(
            f"Gradient clipping enabled: max_grad_norm={training_args .max_grad_norm }"
        )
    model_name = model_args.model_name_or_path
    if model_name is None:
        raise ValueError(
            "--model_name_or_path (or --processor_name_or_path) must be provided"
        )
    processor: QWEN3VoxProcessor = QWEN3VoxProcessor.from_pretrained(model_name)
    tok = processor.tokenizer
    for required in ["speech_start_id", "speech_diffusion_id", "speech_end_id"]:
        if not hasattr(tok, required) or getattr(tok, required) is None:
            raise RuntimeError(f"Tokenizer missing required special id: {required }")
    dtype = torch.float32
    if training_args.bf16:
        dtype = torch.bfloat16
    elif getattr(training_args, "fp16", False):
        dtype = torch.float16
    model = QWEN3VoxForConditionalGeneration.from_pretrained(
        model_name, torch_dtype=dtype
    )
    _patch_acoustic_encode_for_legacy_indexing(model, logger)
    processor.semantic_tokenizer = getattr(model.model, "semantic_tokenizer", None)
    try:
        in_emb_mod = model.get_input_embeddings()
        out_emb_mod = model.get_output_embeddings()
        in_w = getattr(in_emb_mod, "weight", None)
        out_w = getattr(out_emb_mod, "weight", None)
        shared_ptr = bool(
            in_w is not None
            and out_w is not None
            and (in_w.data_ptr() == out_w.data_ptr())
        )
        values_equal = False
        if in_w is not None and out_w is not None and (in_w.shape == out_w.shape):
            try:
                values_equal = bool(torch.allclose(in_w, out_w))
            except Exception:
                values_equal = False
        try:
            tie_cfg = getattr(
                getattr(model.config, "decoder_config", model.config),
                "tie_word_embeddings",
                None,
            )
        except Exception:
            tie_cfg = getattr(model.config, "tie_word_embeddings", None)
        logger.info(
            f"LM head diagnostics -> shared_params={shared_ptr }, values_equal={values_equal }, tie_word_embeddings={tie_cfg }"
        )
        if out_w is not None:
            logger.info(
                f"LM head requires_grad before freeze: {bool (out_w .requires_grad )}"
            )
    except Exception as e:
        logger.warning(f"LM head tie diagnostics failed: {e }")
    try:
        emb_module = model.get_input_embeddings()
        head_module = model.get_output_embeddings()
        if hasattr(emb_module, "weight") and hasattr(head_module, "weight"):
            if (
                emb_module.weight.shape == head_module.weight.shape
                and emb_module.weight.data_ptr() != head_module.weight.data_ptr()
            ):
                with torch.no_grad():
                    head_module.weight = emb_module.weight
                logger.info(
                    "Force-tied LM head weight to input embeddings (pointer share)."
                )
    except Exception as e:
        logger.warning(f"Force-tie of LM head failed: {e }")
    try:
        special_names = ["speech_start_id", "speech_diffusion_id", "speech_end_id"]
        try:
            vocab_size = int(getattr(model.config.decoder_config, "vocab_size", 0))
        except Exception:
            vocab_size = 0
        in_emb_mod = model.get_input_embeddings()
        out_emb_mod = model.get_output_embeddings()
        in_w = getattr(in_emb_mod, "weight", None)
        out_w = getattr(out_emb_mod, "weight", None)
        for name in special_names:
            val = getattr(tok, name, None)
            exists = val is not None
            in_range = exists and isinstance(val, int) and (0 <= val < vocab_size)
            equal_row = None
            if (
                in_range
                and in_w is not None
                and (out_w is not None)
                and (in_w.shape == out_w.shape)
                and (in_w.size(0) > val)
            ):
                try:
                    equal_row = bool(torch.allclose(in_w[val], out_w[val]))
                except Exception:
                    equal_row = False
            decoded_str = None
            if exists and isinstance(val, int):
                try:
                    decoded_str = tok.decode([val])
                except Exception:
                    try:
                        decoded_str = tok.convert_ids_to_tokens(val)
                    except Exception:
                        decoded_str = "<decode_failed>"
            logger.info(
                f"Special token check -> {name }={val }, decoded='{decoded_str }', exists={exists }, in_vocab_range={in_range }, emb_vs_head_row_equal={equal_row }"
            )
    except Exception as e:
        logger.warning(f"Special token ID/row validation failed: {e }")
    try:
        logger.info("=== TOKENIZER DIAGNOSTICS ===")
        logger.info(f"Tokenizer class: {type (tok ).__name__ }")
        logger.info(f"Tokenizer vocab_size: {tok .vocab_size }")
        with torch.no_grad():
            simple_text = "The cat sat on the mat."
            simple_ids = torch.tensor(
                [tok.encode(simple_text, add_special_tokens=True)], device=model.device
            )
            simple_mask = torch.ones_like(simple_ids)
            x = model.get_input_embeddings()(simple_ids)
            outputs = model.model(
                inputs_embeds=x, attention_mask=simple_mask, return_dict=True
            )
            logits = model.lm_head(outputs.last_hidden_state)
            shift_logits = logits[:, :-1, :].contiguous()
            shift_labels = simple_ids[:, 1:].contiguous()
            ce_loss = F.cross_entropy(
                shift_logits.view(-1, shift_logits.size(-1)),
                shift_labels.view(-1),
                reduction="mean",
            )
            logger.info(f"Simple text CE loss: {ce_loss .item ():.4f}")
    except Exception as e:
        logger.warning(f"Tokenizer diagnostics failed: {e }")
    if hasattr(model.config, "use_cache") and training_args.do_train:
        model.config.use_cache = False
    if model_args.freeze_acoustic_tokenizer and hasattr(
        model.model, "acoustic_tokenizer"
    ):
        for p in model.model.acoustic_tokenizer.parameters():
            p.requires_grad = False
    if model_args.freeze_semantic_tokenizer and hasattr(
        model.model, "semantic_tokenizer"
    ):
        for p in model.model.semantic_tokenizer.parameters():
            p.requires_grad = False
    lora_cfg = build_lora_config(model_args)
    tm_lower = [
        s.strip().lower()
        for s in model_args.lora_target_modules.split(",")
        if s.strip()
    ]
    skip_lm_lora = len(tm_lower) == 0 or all(
        (t in ("none", "off", "disable", "disabled") for t in tm_lower)
    )
    if not skip_lm_lora:
        model.model.language_model = get_peft_model(
            model.model.language_model, lora_cfg
        )
    else:
        logger.info("Skipping LLM LoRA wrapping (lora_target_modules indicates none).")
    try:
        model.tie_weights()
    except Exception:
        pass
    for _, p in model.named_parameters():
        p.requires_grad = False
    try:
        for n, p in model.model.language_model.named_parameters():
            if "lora_A" in n or "lora_B" in n:
                p.requires_grad = True
    except Exception:
        logger.warning("Could not re-enable LoRA params on language_model.")
    if getattr(model_args, "lora_wrap_diffusion_head", False) and hasattr(
        model.model, "prediction_head"
    ):

        class _HeadForwardShim(nn.Module):

            def __init__(self, base: nn.Module):
                super().__init__()
                self.base = base

            def forward(self, *args, **kwargs):
                if len(args) >= 3:
                    noisy_images, timesteps, condition = args[:3]
                else:
                    noisy_images = kwargs.get("noisy_images")
                    timesteps = kwargs.get("timesteps")
                    condition = kwargs.get("condition")
                return self.base(noisy_images, timesteps, condition)

        try:
            shim = _HeadForwardShim(model.model.prediction_head)
            model.model.prediction_head = get_peft_model(
                shim, build_head_lora_config(model_args)
            )
            for n, p in model.model.prediction_head.named_parameters():
                if "lora_A" in n or "lora_B" in n:
                    p.requires_grad = True
        except Exception as e:
            logger.warning(f"Could not LoRA-wrap diffusion head: {e }")
    if getattr(model_args, "train_diffusion_head", False) and hasattr(
        model.model, "prediction_head"
    ):
        for p in model.model.prediction_head.parameters():
            p.requires_grad = True
    if model_args.layers_to_freeze is not None and hasattr(
        model.model, "prediction_head"
    ):
        head_params = list(model.model.prediction_head.named_parameters())
        try:
            indices_to_freeze = {
                int(x.strip())
                for x in model_args.layers_to_freeze.split(",")
                if x.strip()
            }
            frozen_count = 0
            for i, (name, param) in enumerate(head_params):
                if i in indices_to_freeze:
                    param.requires_grad = False
                    frozen_count += 1
                    logger.info(f"Froze layer [{i }]: {name }")
            logger.info(
                f"Successfully froze {frozen_count } parameter groups in the diffusion head."
            )
        except Exception as e:
            logger.error(f"Could not parse --layers_to_freeze: {e }")
            raise
    if getattr(model_args, "train_connectors", False):
        if hasattr(model.model, "acoustic_connector"):
            for p in model.model.acoustic_connector.parameters():
                p.requires_grad = True
        if hasattr(model.model, "semantic_connector"):
            for p in model.model.semantic_connector.parameters():
                p.requires_grad = True
    else:
        if hasattr(model.model, "acoustic_connector"):
            for p in model.model.acoustic_connector.parameters():
                p.requires_grad = False
        if hasattr(model.model, "semantic_connector"):
            for p in model.model.semantic_connector.parameters():
                p.requires_grad = False
    try:
        emb = model.get_input_embeddings()
        if hasattr(emb, "weight"):
            emb.weight.requires_grad_(False)
        head = model.get_output_embeddings()
        if head is not None and hasattr(head, "weight"):
            head.weight.requires_grad_(False)
    except Exception:
        pass

    def _sum_params(named_iter):
        return sum((p.numel() for _, p in named_iter if p.requires_grad))

    try:
        lm_lora = (
            _sum_params(model.model.language_model.named_parameters())
            if hasattr(model.model, "language_model")
            else 0
        )
        pred_head_train = (
            _sum_params(model.model.prediction_head.named_parameters())
            if hasattr(model.model, "prediction_head")
            else 0
        )
        ac_conn_train = (
            _sum_params(model.model.acoustic_connector.named_parameters())
            if hasattr(model.model, "acoustic_connector")
            else 0
        )
        se_conn_train = (
            _sum_params(model.model.semantic_connector.named_parameters())
            if hasattr(model.model, "semantic_connector")
            else 0
        )
        total_trainable = sum(
            (p.numel() for p in model.parameters() if p.requires_grad)
        )
        logger.info(
            f"Trainable by block -> LLM-LoRA: {lm_lora :,} | diff_head: {pred_head_train :,} | ac_conn: {ac_conn_train :,} | se_conn: {se_conn_train :,}"
        )
        logger.info("TOTAL trainable: %s", f"{total_trainable :,}")
    except Exception:
        pass
    verification_mode = (
        VerificationMode.NO_CHECKS
        if data_args.ignore_verifications
        else VerificationMode.BASIC_CHECKS
    )
    if data_args.train_jsonl is not None:
        data_files: Dict[str, str] = {"train": data_args.train_jsonl}
        if data_args.validation_jsonl is not None:
            data_files["validation"] = data_args.validation_jsonl
        raw = load_dataset(
            "json",
            data_files=data_files,
            verification_mode=verification_mode,
            cache_dir=model_args.cache_dir,
        )
    else:
        if data_args.dataset_name is None:
            raise ValueError(
                "Provide --dataset_name (HF datasets) or use --train_jsonl/--validation_jsonl for local files."
            )
        raw = load_dataset(
            data_args.dataset_name,
            data_args.dataset_config_name,
            verification_mode=verification_mode,
            cache_dir=model_args.cache_dir,
        )
    train_ds = raw[data_args.train_split_name]
    eval_ds = None
    if training_args.do_eval:
        if data_args.eval_split_name and data_args.eval_split_name in raw:
            eval_ds = raw[data_args.eval_split_name]
        elif (
            data_args.eval_split_size
            and data_args.eval_split_size > 0
            and (len(train_ds) > 1)
        ):
            split = train_ds.train_test_split(
                test_size=data_args.eval_split_size, seed=training_args.seed
            )
            train_ds, eval_ds = (split["train"], split["test"])
    train_dataset = QWEN3VoxDataset(
        train_ds,
        text_column=data_args.text_column_name,
        audio_column=data_args.audio_column_name,
        voice_prompts_column=data_args.voice_prompts_column_name,
    )
    eval_dataset = None
    if eval_ds is not None:
        eval_dataset = QWEN3VoxDataset(
            eval_ds,
            text_column=data_args.text_column_name,
            audio_column=data_args.audio_column_name,
            voice_prompts_column=data_args.voice_prompts_column_name,
        )
    speech_compress_ratio = getattr(processor, "speech_tok_compress_ratio", 3200)
    semantic_dim = getattr(model.config, "semantic_vae_dim", None)
    if semantic_dim is None:
        try:
            semantic_dim = int(
                getattr(model.config.semantic_tokenizer_config, "vae_dim", 128)
            )
        except Exception:
            semantic_dim = 128
    compute_semantics_flag = (
        hasattr(processor, "semantic_tokenizer")
        and processor.semantic_tokenizer is not None
    )
    data_collator = QWEN3VoxCollator(
        processor=processor,
        max_length=data_args.max_length,
        speech_compress_ratio=speech_compress_ratio,
        semantic_vae_dim=semantic_dim,
        compute_semantics=compute_semantics_flag,
        debug_checks=False,
        voice_prompt_drop_rate=data_args.voice_prompt_drop_rate,
    )

    class LoRADebugCallback(TrainerCallback):

        def __init__(self, log_every_n_steps: int = 50):
            self.log_every_n_steps = max(1, int(log_every_n_steps))
            self.prev_param_norms: Dict[str, float] = {}
            self.lora_param_names: List[str] = []

        def on_train_begin(self, args, state, control, model=None, **kwargs):
            try:
                if model is None:
                    return
                named: Dict[str, torch.nn.Parameter] = dict(model.named_parameters())
                self.lora_param_names = [
                    n for n in named.keys() if "lora_A" in n or "lora_B" in n
                ]
                for n in self.lora_param_names:
                    p = named[n]
                    self.prev_param_norms[n] = float(p.data.norm().item())
                total = len(self.lora_param_names)
                req_grad = sum(
                    (1 for n in self.lora_param_names if named[n].requires_grad)
                )
                num_A = sum((1 for n in self.lora_param_names if "lora_A" in n))
                num_B = sum((1 for n in self.lora_param_names if "lora_B" in n))
                zero_B = sum(
                    (
                        1
                        for n in self.lora_param_names
                        if "lora_B" in n and float(named[n].data.norm().item()) == 0.0
                    )
                )
                logger.info(
                    f"LoRA debug: found {total } LoRA params (A={num_A }, B={num_B }); trainable={req_grad }. Initial lora_B_zero={zero_B }."
                )
                if total == 0:
                    logger.warning(
                        "LoRA debug: No LoRA parameters found. Check lora_target_modules."
                    )
                if req_grad != total:
                    logger.warning(
                        "LoRA debug: Some LoRA params are frozen. They should be trainable."
                    )
            except Exception as e:
                logger.warning(f"LoRA debug (on_train_begin) failed: {e }")

        def on_step_end(self, args, state, control, model=None, **kwargs):
            try:
                if model is None or len(self.lora_param_names) == 0:
                    return
                step = int(getattr(state, "global_step", 0) or 0)
                if step % self.log_every_n_steps != 0 and step != 1:
                    return
                named: Dict[str, torch.nn.Parameter] = dict(model.named_parameters())
                changed_A = 0
                changed_B = 0
                zero_B = 0
                eps = 1e-12
                for n in self.lora_param_names:
                    p = named.get(n, None)
                    if p is None:
                        continue
                    prev = self.prev_param_norms.get(n, 0.0)
                    curr = float(p.data.norm().item())
                    if "lora_A" in n and abs(curr - prev) > eps:
                        changed_A += 1
                    if "lora_B" in n:
                        if abs(curr - prev) > eps:
                            changed_B += 1
                        if curr == 0.0:
                            zero_B += 1
                    self.prev_param_norms[n] = curr
                total_A = sum((1 for n in self.lora_param_names if "lora_A" in n))
                total_B = sum((1 for n in self.lora_param_names if "lora_B" in n))
                logger.info(
                    f"LoRA debug step {step }: changed A {changed_A }/{total_A }, changed B {changed_B }/{total_B }, lora_B_zero_now={zero_B }."
                )
            except Exception as e:
                logger.warning(f"LoRA debug (on_step_end) failed: {e }")

    class QWEN3VoxTrainer(Trainer):

        def training_forward(
            self, model: QWEN3VoxForConditionalGeneration, inputs: Dict[str, Any]
        ):
            input_ids = inputs.get("input_ids")
            attention_mask = inputs.get("attention_mask")
            position_ids = inputs.get("position_ids")
            past_key_values = inputs.get("past_key_values")
            inputs_embeds = inputs.get("inputs_embeds")
            use_cache = inputs.get("use_cache", False)
            output_attentions = inputs.get("output_attentions")
            output_hidden_states = inputs.get("output_hidden_states")
            return_dict = inputs.get("return_dict", True)
            cache_position = inputs.get("cache_position")
            speech_tensors = inputs.get("speech_tensors")
            speech_masks = inputs.get("speech_masks")
            speeches_loss_input = inputs.get("speeches_loss_input")
            speech_semantic_tensors = inputs.get("speech_semantic_tensors")
            acoustic_input_mask = inputs.get("acoustic_input_mask")
            acoustic_loss_mask = inputs.get("acoustic_loss_mask")
            ddmp_batch_mul = training_args.ddpm_batch_mul
            kwargs = {}
            x = model.get_input_embeddings()(input_ids)
            semantic_speech_all_connect_features = model.model.semantic_connector(
                speech_semantic_tensors
            )
            if speeches_loss_input is not None:
                speech_all_features, speech_all_connect_features = (
                    model.forward_speech_features(
                        speech_tensors=(
                            speech_tensors.type_as(x)
                            if speech_tensors is not None
                            else None
                        ),
                        speech_masks=speech_masks,
                        speech_type=kwargs.get("speech_type", "audio"),
                        return_unmask=True,
                    )
                )
                if speech_tensors is not None:
                    if semantic_speech_all_connect_features is not None:
                        x[acoustic_input_mask] = (
                            speech_all_connect_features[speech_masks]
                            + semantic_speech_all_connect_features[speech_masks]
                        )
                    else:
                        x[acoustic_input_mask] = speech_all_connect_features[
                            speech_masks
                        ]
                    speech_features = speech_all_features[
                        speeches_loss_input & speech_masks
                    ]
                    speech_connect_features = speech_all_connect_features[
                        speeches_loss_input & speech_masks
                    ]
                    try:
                        if acoustic_input_mask is not None:
                            assert speech_connect_features.shape[0] == int(
                                acoustic_input_mask.sum().item()
                            ), f"Mismatch between selected speech connectors ({speech_connect_features .shape [0 ]}) and acoustic_input_mask sum ({int (acoustic_input_mask .sum ().item ())})"
                    except Exception:
                        pass
            else:
                speech_features, speech_connect_features = (
                    model.forward_speech_features(
                        speech_tensors=(
                            speech_tensors.type_as(x)
                            if speech_tensors is not None
                            else None
                        ),
                        speech_masks=speech_masks,
                        speech_type=kwargs.get("speech_type", "audio"),
                    )
                )
                if speech_tensors is not None:
                    x[acoustic_input_mask] = speech_connect_features
            outputs = model.model(
                input_ids=None,
                attention_mask=attention_mask,
                position_ids=position_ids,
                past_key_values=past_key_values,
                inputs_embeds=x,
                use_cache=use_cache,
                output_attentions=output_attentions,
                output_hidden_states=False,
                return_dict=return_dict,
                cache_position=cache_position,
            )
            hidden_states = outputs.last_hidden_state
            logits = model.lm_head(hidden_states)
            loss = None
            diffusion_loss = None
            if speech_tensors is not None and acoustic_loss_mask.sum().item() > 0:
                cond_mask = torch.zeros_like(acoustic_loss_mask, dtype=torch.bool)
                cond_mask[:, :-1] = acoustic_loss_mask[:, 1:]
                cond_mask[:, 0] = False
                condition_features = hidden_states[cond_mask]
                speech_len, latent_size = speech_features.shape
                try:
                    assert (
                        condition_features.shape[0] == speech_len
                    ), f"Mismatch: condition_features={condition_features .shape [0 ]} vs speech_features={speech_len }"
                except Exception:
                    pass
                noise = torch.randn(
                    (speech_len * ddmp_batch_mul, latent_size),
                    device=hidden_states.device,
                    dtype=hidden_states.dtype,
                )
                timesteps = torch.multinomial(
                    torch.ones(model.config.diffusion_head_config.ddpm_num_steps),
                    speech_len * ddmp_batch_mul,
                    replacement=True,
                ).to(hidden_states.device)
                speech_features_repeated = speech_features.repeat_interleave(
                    ddmp_batch_mul, dim=0
                )
                condition_features_repeated = condition_features.repeat_interleave(
                    ddmp_batch_mul, dim=0
                )
                noisy_speech_features = model.model.noise_scheduler.add_noise(
                    speech_features_repeated, noise, timesteps
                )
                model_output = model.model.prediction_head(
                    noisy_speech_features,
                    timesteps.type_as(x),
                    condition_features_repeated,
                )
                prediction_type = model.config.diffusion_head_config.prediction_type
                if prediction_type == "epsilon":
                    target_for_loss = noise
                elif prediction_type == "v_prediction":
                    target_for_loss = model.model.noise_scheduler.get_velocity(
                        speech_features_repeated, noise, timesteps
                    )
                else:
                    raise NotImplementedError(
                        f"Prediction type {prediction_type } not implemented"
                    )
                diffusion_loss = F.mse_loss(
                    model_output.float(), target_for_loss.float(), reduction="sum"
                )
                if latent_size > 0 and ddmp_batch_mul > 0:
                    diffusion_loss = (
                        diffusion_loss
                        / latent_size
                        / ddmp_batch_mul
                        / max(speech_len, 1)
                    )
                else:
                    diffusion_loss = torch.tensor(0.0, device=diffusion_loss.device)
            else:
                diffusion_loss = (
                    sum((p.sum() for p in model.model.prediction_head.parameters()))
                    * 0.0
                )
                diffusion_loss += (
                    sum((p.sum() for p in model.model.acoustic_connector.parameters()))
                    * 0.0
                )
                diffusion_loss += (
                    sum((p.sum() for p in model.model.semantic_connector.parameters()))
                    * 0.0
                )
            return QWEN3VoxCausalLMOutputWithPast(
                loss=loss,
                diffusion_loss=diffusion_loss,
                speech_token_num=speech_len if speech_tensors is not None else 0,
                logits=logits,
                past_key_values=outputs.past_key_values,
                hidden_states=outputs.hidden_states,
                attentions=outputs.attentions,
            )

        def compute_loss(
            self,
            model: QWEN3VoxForConditionalGeneration,
            inputs: Dict[str, Any],
            return_outputs=False,
            num_items_in_batch: Optional[int] = None,
        ):
            labels = inputs.get("input_ids")
            attention_mask = inputs.get("attention_mask")
            acoustic_input_mask = inputs.get("acoustic_input_mask")
            sem = inputs.get("speech_semantic_tensors", None)
            try:
                target_dtype = next(model.model.semantic_connector.parameters()).dtype
            except Exception:
                target_dtype = model.get_input_embeddings().weight.dtype
            if sem is None:
                sm = inputs.get("speech_masks")
                if sm is not None:
                    zeros = torch.zeros(
                        sm.size(0),
                        sm.size(1),
                        getattr(model.config, "semantic_vae_dim", 128),
                        dtype=target_dtype,
                        device=sm.device,
                    )
                    inputs["speech_semantic_tensors"] = zeros
            elif isinstance(sem, torch.Tensor):
                inputs["speech_semantic_tensors"] = sem.to(dtype=target_dtype)
            outputs = self.training_forward(model, inputs)
            try:
                al_mask = inputs.get("acoustic_loss_mask")
                sp_masks = inputs.get("speech_masks")
                sp_loss_sel = inputs.get("speeches_loss_input")
                num_tok_total = (
                    int(acoustic_input_mask.sum().item())
                    if acoustic_input_mask is not None
                    else 0
                )
                num_tok_loss = int(al_mask.sum().item()) if al_mask is not None else 0
                num_lat_total = (
                    int(sp_masks.sum().item()) if sp_masks is not None else 0
                )
                num_lat_loss = (
                    int((sp_loss_sel & sp_masks).sum().item())
                    if sp_loss_sel is not None and sp_masks is not None
                    else 0
                )
                self.log(
                    {
                        "debug/num_tok_total": float(num_tok_total),
                        "debug/num_tok_loss": float(num_tok_loss),
                        "debug/num_lat_total": float(num_lat_total),
                        "debug/num_lat_loss": float(num_lat_loss),
                    }
                )
                if (
                    sp_loss_sel is not None
                    and sp_masks is not None
                    and (al_mask is not None)
                ):
                    if num_tok_loss != num_lat_loss:
                        logger.warning(
                            f"Loss selection mismatch: acoustic_loss_mask={num_tok_loss } vs speeches_loss_input={num_lat_loss }"
                        )
            except Exception:
                pass
            logits = outputs.logits
            ce_labels = mask_for_ce(
                labels, attention_mask, acoustic_input_mask, pad_id=-100
            )
            shift_logits = logits[:, :-1, :].contiguous()
            loss_fct = nn.CrossEntropyLoss(ignore_index=-100)
            ce_loss = loss_fct(
                shift_logits.view(-1, shift_logits.size(-1)), ce_labels.view(-1)
            )
            try:
                self._debug_ce(
                    shift_logits, ce_labels, attention_mask, acoustic_input_mask
                )
            except Exception as e:
                logger.warning(f"Failed invoking CE debug: {e }")
            diffusion_loss = (
                outputs.diffusion_loss
                if outputs.diffusion_loss is not None
                else torch.tensor(0.0, device=ce_loss.device)
            )
            total = (
                training_args.ce_loss_weight * ce_loss
                + training_args.diffusion_loss_weight * diffusion_loss
            )
            try:
                prefix = "train" if model.training else "eval"
                self.log(
                    {
                        f"{prefix }/ce_loss": ce_loss.detach().item(),
                        f"{prefix }/diffusion_loss": (
                            diffusion_loss.detach().item()
                            if isinstance(diffusion_loss, torch.Tensor)
                            else float(diffusion_loss)
                        ),
                    }
                )
                if (
                    hasattr(self, "optimizer")
                    and self.optimizer is not None
                    and (len(self.optimizer.param_groups) > 0)
                ):
                    lr_val = self.optimizer.param_groups[0].get("lr", None)
                    if lr_val is not None:
                        self.log({"train/learning_rate_real": float(lr_val)})
            except Exception:
                pass
            return (total, outputs) if return_outputs else total

        def _debug_ce(
            self,
            shift_logits: torch.Tensor,
            ce_labels: torch.Tensor,
            attention_mask: Optional[torch.Tensor],
            acoustic_input_mask: Optional[torch.Tensor],
        ):
            try:
                if not getattr(training_args, "debug_ce_details", False):
                    return
                step = int(getattr(self.state, "global_step", 0) or 0)
                every_n = max(
                    1, int(getattr(training_args, "debug_ce_every_n_steps", 200) or 200)
                )
                if not (step <= 1 or step % every_n == 0):
                    return
                with torch.no_grad():
                    vocab = shift_logits.size(-1)
                    per_token_loss = F.cross_entropy(
                        shift_logits.view(-1, vocab),
                        ce_labels.view(-1),
                        reduction="none",
                        ignore_index=-100,
                    ).view_as(ce_labels)
                    valid_mask = ce_labels.ne(-100)
                    num_valid = int(valid_mask.sum().item())
                    avg_loss = (
                        float(per_token_loss[valid_mask].mean().item())
                        if num_valid > 0
                        else float("nan")
                    )
                    per_ex_avgs = []
                    max_examples = max(
                        1, int(getattr(training_args, "debug_ce_max_examples", 1) or 1)
                    )
                    B = ce_labels.size(0)
                    for b in range(min(B, max_examples)):
                        vb = valid_mask[b]
                        if int(vb.sum().item()) > 0:
                            per_ex_avgs.append(
                                float(per_token_loss[b][vb].mean().item())
                            )
                        else:
                            per_ex_avgs.append(float("nan"))
                    logger.info(
                        f"CE debug: tokens_in_loss={num_valid }, avg_loss={avg_loss :.4f}, per_example_avgs={[round (x ,4 )if x ==x else None for x in per_ex_avgs ]}"
                    )
            except Exception as e:
                logger.warning(f"CE detailed debug failed: {e }")

        def _save(self, output_dir: Optional[str] = None, state_dict=None) -> None:
            try:
                target_dir = output_dir or self.args.output_dir
                lora_out = os.path.join(target_dir, "lora")
                os.makedirs(lora_out, exist_ok=True)
                language_model = getattr(self.model.model, "language_model", None)
                if hasattr(language_model, "save_pretrained"):
                    language_model.save_pretrained(lora_out)
                pred_head = getattr(self.model.model, "prediction_head", None)
                if hasattr(pred_head, "save_pretrained"):
                    ph_dir = os.path.join(lora_out, "diffusion_head")
                    os.makedirs(ph_dir, exist_ok=True)
                    pred_head.save_pretrained(ph_dir)
                if pred_head is not None and hasattr(pred_head, "state_dict"):
                    sd = pred_head.state_dict()
                    torch.save(sd, os.path.join(lora_out, "diffusion_head_full.bin"))
                    ph_dir = os.path.join(lora_out, "diffusion_head")
                    os.makedirs(ph_dir, exist_ok=True)
                    torch.save(sd, os.path.join(ph_dir, "diffusion_head_full.bin"))
                ac = getattr(self.model.model, "acoustic_connector", None)
                if ac is not None:
                    ac_dir = os.path.join(lora_out, "acoustic_connector")
                    os.makedirs(ac_dir, exist_ok=True)
                    torch.save(
                        ac.state_dict(), os.path.join(ac_dir, "pytorch_model.bin")
                    )
                se = getattr(self.model.model, "semantic_connector", None)
                if se is not None:
                    se_dir = os.path.join(lora_out, "semantic_connector")
                    os.makedirs(se_dir, exist_ok=True)
                    torch.save(
                        se.state_dict(), os.path.join(se_dir, "pytorch_model.bin")
                    )
            except Exception as e:
                logger.warning(f"Failed to save LoRA assets: {e }")

    ema_cb = EmaCallback(attr_path="model.prediction_head", decay=0.999, device="cpu")
    trainer = QWEN3VoxTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        data_collator=data_collator,
        callbacks=[
            ema_cb,
            LoRADebugCallback(
                log_every_n_steps=int(getattr(training_args, "logging_steps", 50) or 50)
            ),
        ],
    )
    if getattr(training_args, "debug_save", False):
        try:
            debug_dir = os.path.join(training_args.output_dir, "debug_initial")
            lora_out = os.path.join(debug_dir, "lora")
            os.makedirs(lora_out, exist_ok=True)
            logger.info(
                f"[debug_save] Saving initial (pre-training) model components to: {debug_dir }"
            )
            try:
                if hasattr(model.model.language_model, "save_pretrained"):
                    model.model.language_model.save_pretrained(lora_out)
            except Exception as e_lm:
                logger.warning(f"[debug_save] Failed to save language_model: {e_lm }")
            try:
                if hasattr(model.model, "prediction_head") and hasattr(
                    model.model.prediction_head, "save_pretrained"
                ):
                    model.model.prediction_head.save_pretrained(
                        os.path.join(lora_out, "diffusion_head")
                    )
            except Exception as e_head:
                logger.warning(
                    f"[debug_save] Failed to save prediction_head: {e_head }"
                )
            try:
                ph = getattr(model.model, "prediction_head", None)
                if ph is not None and hasattr(ph, "state_dict"):
                    sd = ph.state_dict()
                    torch.save(sd, os.path.join(lora_out, "diffusion_head_full.bin"))
                    os.makedirs(os.path.join(lora_out, "diffusion_head"), exist_ok=True)
                    torch.save(
                        sd,
                        os.path.join(
                            lora_out, "diffusion_head", "diffusion_head_full.bin"
                        ),
                    )
            except Exception as e:
                logger.warning(f"[debug_save] Failed to save FULL diffusion head: {e }")
            try:
                ac_conn = getattr(model.model, "acoustic_connector", None)
                if ac_conn is not None:
                    ac_dir = os.path.join(lora_out, "acoustic_connector")
                    os.makedirs(ac_dir, exist_ok=True)
                    torch.save(
                        ac_conn.state_dict(), os.path.join(ac_dir, "pytorch_model.bin")
                    )
            except Exception as e_ac:
                logger.warning(
                    f"[debug_save] Failed to save acoustic_connector: {e_ac }"
                )
            try:
                se_conn = getattr(model.model, "semantic_connector", None)
                if se_conn is not None:
                    se_dir = os.path.join(lora_out, "semantic_connector")
                    os.makedirs(se_dir, exist_ok=True)
                    torch.save(
                        se_conn.state_dict(), os.path.join(se_dir, "pytorch_model.bin")
                    )
            except Exception as e_se:
                logger.warning(
                    f"[debug_save] Failed to save semantic_connector: {e_se }"
                )
        except Exception as e:
            logger.warning(
                f"[debug_save] Unexpected failure saving initial components: {e }"
            )
    if getattr(training_args, "gradient_checkpointing", False):
        try:
            model.gradient_checkpointing_enable()
        except Exception:
            logger.warning("Failed to enable gradient checkpointing on the model.")
    if training_args.do_train:
        trainer.train(resume_from_checkpoint=training_args.resume_from_checkpoint)
        lora_out = os.path.join(training_args.output_dir, "lora")
        os.makedirs(lora_out, exist_ok=True)
        lm = getattr(model.model, "language_model", None)
        if hasattr(lm, "save_pretrained"):
            lm.save_pretrained(lora_out)
        ph = getattr(model.model, "prediction_head", None)
        if hasattr(ph, "save_pretrained"):
            ph_dir = os.path.join(lora_out, "diffusion_head")
            os.makedirs(ph_dir, exist_ok=True)
            ph.save_pretrained(ph_dir)
        try:
            if ph is not None and hasattr(ph, "state_dict"):
                sd = ph.state_dict()
                torch.save(sd, os.path.join(lora_out, "diffusion_head_full.bin"))
                ph_dir = os.path.join(lora_out, "diffusion_head")
                os.makedirs(ph_dir, exist_ok=True)
                torch.save(sd, os.path.join(ph_dir, "diffusion_head_full.bin"))
        except Exception as e:
            logger.warning(f"Failed to save FULL diffusion head at end: {e }")
        try:
            ac = getattr(model.model, "acoustic_connector", None)
            if ac is not None:
                ac_dir = os.path.join(lora_out, "acoustic_connector")
                os.makedirs(ac_dir, exist_ok=True)
                torch.save(ac.state_dict(), os.path.join(ac_dir, "pytorch_model.bin"))
        except Exception as e:
            logger.warning(f"Failed to save acoustic_connector: {e }")
        try:
            se = getattr(model.model, "semantic_connector", None)
            if se is not None:
                se_dir = os.path.join(lora_out, "semantic_connector")
                os.makedirs(se_dir, exist_ok=True)
                torch.save(se.state_dict(), os.path.join(se_dir, "pytorch_model.bin"))
        except Exception as e:
            logger.warning(f"Failed to save semantic_connector: {e }")
    if training_args.do_eval and eval_dataset is not None:
        trainer.evaluate()


if __name__ == "__main__":
    main()
from typing import List, Optional, Tuple, Union
import torch
import torch.nn as nn
from transformers.models.auto import AutoModel, AutoModelForCausalLM
from transformers.modeling_outputs import CausalLMOutput, BaseModelOutputWithPast
from transformers import modeling_utils
from transformers.modeling_utils import PreTrainedModel
from transformers.utils import logging
from transformers.generation import GenerationMixin

logger = logging.get_logger(__name__)
if (
    not hasattr(modeling_utils, "ALL_PARALLEL_STYLES")
    or modeling_utils.ALL_PARALLEL_STYLES is None
):
    modeling_utils.ALL_PARALLEL_STYLES = ["tp", "none", "colwise", "rowwise"]


class QWEN3VoxASRPreTrainedModel(PreTrainedModel):
    config_class = QWEN3VoxASRConfig
    base_model_prefix = "model"
    supports_gradient_checkpointing = True
    _skip_keys_device_placement = "past_key_values"
    _supports_cache_class = True
    _supports_flash_attn = True
    _supports_flash_attn_2 = True
    _supports_sdpa = True
    _supports_quantized_cache = True
    _supports_static_cache = True
    _supports_attention_backend = True

    def _init_weights(self, module):
        if hasattr(self.config, "language_model_config") and hasattr(
            self.config.language_model_config, "initializer_range"
        ):
            std = self.config.language_model_config.initializer_range
        elif hasattr(self.config, "decoder_config") and hasattr(
            self.config.decoder_config, "initializer_range"
        ):
            std = self.config.decoder_config.initializer_range
        else:
            std = 0.02
        if isinstance(module, nn.Linear):
            module.weight.data.normal_(mean=0.0, std=std)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(module, nn.LayerNorm):
            module.weight.data.fill_(1.0)
            module.bias.data.zero_()


class QWEN3VoxASRModel(QWEN3VoxASRPreTrainedModel):

    def __init__(self, config):
        super().__init__(config)
        if hasattr(config, "torch_dtype") and config.torch_dtype is not None:
            if isinstance(config.torch_dtype, str):
                dtype = getattr(torch, config.torch_dtype)
            else:
                dtype = config.torch_dtype
        else:
            dtype = torch.float32
        lm_config = config.decoder_config
        self.language_model = AutoModel.from_config(lm_config)
        self.acoustic_tokenizer = AutoModel.from_config(
            config.acoustic_tokenizer_config
        ).to(dtype)
        self.semantic_tokenizer = AutoModel.from_config(
            config.semantic_tokenizer_config
        ).to(dtype)
        self.acoustic_connector = SpeechConnector(
            config.acoustic_vae_dim, lm_config.hidden_size
        ).to(dtype)
        self.semantic_connector = SpeechConnector(
            config.semantic_vae_dim, lm_config.hidden_size
        ).to(dtype)

    def get_input_embeddings(self):
        if hasattr(self.language_model, "embed_tokens"):
            return self.language_model.embed_tokens
        for name, attr in self.language_model.fullmap.items():
            if attr.orig_name == "embed_tokens.weight":
                return getattr(self.language_model, name)
        assert False, "should not arrive here"

    def set_input_embeddings(self, value):
        self.language_model.embed_tokens = value

    def set_speech_tokenizers(self, acoustic_tokenizer=None, semantic_tokenizer=None):
        self.acoustic_tokenizer = acoustic_tokenizer
        self.semantic_tokenizer = semantic_tokenizer
        if self.acoustic_tokenizer is not None:
            self.acoustic_tokenizer.train(False)
        if self.semantic_tokenizer is not None:
            self.semantic_tokenizer.train(False)

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        **kwargs,
    ) -> Union[Tuple, BaseModelOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        outputs = self.language_model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            inputs_embeds=inputs_embeds,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
            **kwargs,
        )
        if not return_dict:
            return outputs
        return BaseModelOutputWithPast(
            last_hidden_state=outputs.last_hidden_state,
            past_key_values=outputs.past_key_values,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )


class QWEN3VoxASRForConditionalGeneration(QWEN3VoxASRPreTrainedModel, GenerationMixin):
    _tied_weights_keys = ["lm_head.weight"]
    _tp_plan = {"lm_head": "colwise_rep"}

    def __init__(self, config):
        super().__init__(config)
        self.model = QWEN3VoxASRModel(config)
        self.vocab_size = config.decoder_config.vocab_size
        if hasattr(config, "torch_dtype") and config.torch_dtype is not None:
            if isinstance(config.torch_dtype, str):
                dtype = getattr(torch, config.torch_dtype)
            else:
                dtype = config.torch_dtype
        else:
            dtype = torch.float32
        self.lm_head = nn.Linear(
            config.decoder_config.hidden_size, self.vocab_size, bias=False
        ).to(dtype)
        self.post_init()

    def get_input_embeddings(self):
        return self.model.get_input_embeddings()

    def set_input_embeddings(self, value):
        self.model.set_input_embeddings(value)

    def get_output_embeddings(self):
        return self.lm_head

    def set_output_embeddings(self, new_embeddings):
        self.lm_head = new_embeddings

    def set_decoder(self, decoder):
        self.model.language_model = decoder

    def get_decoder(self):
        return self.model.language_model

    def tie_weights(self):
        if getattr(self.config.decoder_config, "tie_word_embeddings", False):
            output_embeddings = self.get_output_embeddings()
            input_embeddings = self.get_input_embeddings()
            if hasattr(input_embeddings, "weight"):
                output_embeddings.weight = input_embeddings.weight
            else:
                output_embeddings.weight = input_embeddings

    def encode_speech(
        self,
        speech_tensors: torch.FloatTensor,
        speech_masks: Optional[torch.BoolTensor] = None,
        speech_semantic_tensors: Optional[torch.FloatTensor] = None,
        streaming_segment_duration: float = 60.0,
    ):
        if hasattr(self.config, "torch_dtype") and self.config.torch_dtype is not None:
            if isinstance(self.config.torch_dtype, str):
                dtype = getattr(torch, self.config.torch_dtype)
            else:
                dtype = self.config.torch_dtype
        else:
            dtype = torch.float32
        speech_tensors = speech_tensors.to(dtype)
        if speech_tensors.ndim == 1:
            speech_tensors = speech_tensors.unsqueeze(0)
        batch_size, total_samples = speech_tensors.shape
        sample_rate = 22050
        segment_samples = int(streaming_segment_duration * sample_rate)
        use_streaming = total_samples > segment_samples
        with torch.no_grad():
            if not use_streaming:
                encoder_output = self.model.acoustic_tokenizer.encode(
                    speech_tensors.unsqueeze(1)
                )
                audio_tokens = encoder_output.sample(
                    dist_type=self.model.acoustic_tokenizer.std_dist_type
                )[0]
                acoustic_features = self.model.acoustic_connector(audio_tokens)
                if speech_semantic_tensors is not None:
                    semantic_features = self.model.semantic_connector(
                        speech_semantic_tensors
                    )
                else:
                    semantic_tokens = self.model.semantic_tokenizer.encode(
                        speech_tensors.unsqueeze(1)
                    ).mean
                    semantic_features = self.model.semantic_connector(semantic_tokens)
            else:
                acoustic_encoder_cache = QWEN3VoxTokenizerStreamingCache()
                semantic_encoder_cache = QWEN3VoxTokenizerStreamingCache()
                acoustic_mean_segments = []
                semantic_mean_segments = []
                sample_indices = torch.arange(batch_size, device=speech_tensors.device)

                def _iter_segments(total_length: int, segment_length: int):
                    if segment_length <= 0:
                        raise ValueError("segment_length must be positive")
                    for start in range(0, total_length, segment_length):
                        end = min(start + segment_length, total_length)
                        if end > start:
                            yield (start, end)

                segments = list(_iter_segments(total_samples, segment_samples))
                num_segments = len(segments)
                for seg_idx, (start, end) in enumerate(segments):
                    chunk = speech_tensors[:, start:end].contiguous()
                    if chunk.numel() == 0:
                        continue
                    is_final = seg_idx == num_segments - 1
                    acoustic_encoder_output = self.model.acoustic_tokenizer.encode(
                        chunk.unsqueeze(1),
                        cache=acoustic_encoder_cache,
                        sample_indices=sample_indices,
                        use_cache=True,
                        is_final_chunk=is_final,
                    )
                    acoustic_mean_segments.append(acoustic_encoder_output.mean)
                    semantic_encoder_output = self.model.semantic_tokenizer.encode(
                        chunk.unsqueeze(1),
                        cache=semantic_encoder_cache,
                        sample_indices=sample_indices,
                        use_cache=True,
                        is_final_chunk=is_final,
                    )
                    semantic_mean_segments.append(semantic_encoder_output.mean)
                acoustic_mean_full = torch.cat(
                    acoustic_mean_segments, dim=1
                ).contiguous()
                acoustic_encoder_output = QWEN3VoxTokenizerEncoderOutput(
                    mean=acoustic_mean_full, std=self.model.acoustic_tokenizer.fix_std
                )
                audio_tokens = acoustic_encoder_output.sample(
                    dist_type=self.model.acoustic_tokenizer.std_dist_type
                )[0]
                acoustic_features = self.model.acoustic_connector(audio_tokens)
                semantic_tokens = torch.cat(semantic_mean_segments, dim=1).contiguous()
                semantic_features = self.model.semantic_connector(semantic_tokens)
            if speech_masks is not None:
                combined_features = (
                    acoustic_features[speech_masks] + semantic_features[speech_masks]
                )
            else:
                combined_features = acoustic_features + semantic_features
        return combined_features

    def forward(
        self,
        input_ids: Optional[torch.LongTensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[List[torch.FloatTensor]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        speech_tensors: Optional[torch.FloatTensor] = None,
        speech_masks: Optional[torch.BoolTensor] = None,
        speech_semantic_tensors: Optional[torch.FloatTensor] = None,
        acoustic_input_mask: Optional[torch.BoolTensor] = None,
        **kwargs,
    ) -> Union[Tuple, CausalLMOutput]:
        output_attentions = (
            output_attentions
            if output_attentions is not None
            else self.config.output_attentions
        )
        output_hidden_states = (
            output_hidden_states
            if output_hidden_states is not None
            else self.config.output_hidden_states
        )
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        use_cache = use_cache if use_cache is not None else self.config.use_cache
        if inputs_embeds is None and input_ids is not None:
            inputs_embeds = self.get_input_embeddings()(input_ids)
        if speech_tensors is not None and acoustic_input_mask is not None:
            speech_features = self.encode_speech(
                speech_tensors=speech_tensors,
                speech_masks=speech_masks,
                speech_semantic_tensors=speech_semantic_tensors,
            )
            inputs_embeds[acoustic_input_mask] = speech_features
        outputs = self.model(
            input_ids=None,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            inputs_embeds=inputs_embeds,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
        )
        hidden_states = outputs[0] if not return_dict else outputs.last_hidden_state
        logits = self.lm_head(hidden_states)
        loss = None
        if labels is not None:
            shift_logits = logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()
            loss_fct = nn.CrossEntropyLoss()
            shift_logits = shift_logits.view(-1, self.vocab_size)
            shift_labels = shift_labels.view(-1)
            shift_labels = shift_labels.to(shift_logits.device)
            loss = loss_fct(shift_logits, shift_labels)
        if not return_dict:
            output = (logits,) + outputs[1:]
            return (loss,) + output if loss is not None else output
        return QWEN3VoxCausalLMOutputWithPast(
            loss=loss,
            logits=logits,
            past_key_values=outputs.past_key_values,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )

    def prepare_inputs_for_generation(
        self,
        input_ids,
        past_key_values=None,
        attention_mask=None,
        inputs_embeds=None,
        cache_position=None,
        position_ids=None,
        use_cache=True,
        speech_tensors=None,
        speech_masks=None,
        speech_semantic_tensors=None,
        acoustic_input_mask=None,
        **kwargs,
    ):
        if past_key_values is not None:
            if isinstance(past_key_values, tuple):
                past_length = past_key_values[0][0].shape[2]
            else:
                past_length = past_key_values.get_seq_length()
            if input_ids is not None and input_ids.shape[1] > past_length:
                input_ids = input_ids[:, past_length:]
        if position_ids is None and attention_mask is not None:
            position_ids = attention_mask.long().cumsum(-1) - 1
            position_ids.masked_fill_(attention_mask == 0, 1)
            if past_key_values is not None and input_ids is not None:
                position_ids = position_ids[:, -input_ids.shape[1] :]
        if cache_position is None:
            past_seen_tokens = (
                past_key_values.get_seq_length() if past_key_values is not None else 0
            )
            cache_position = torch.arange(
                past_seen_tokens,
                past_seen_tokens
                + (
                    input_ids.shape[1]
                    if input_ids is not None
                    else inputs_embeds.shape[1]
                ),
                device=(
                    input_ids.device if input_ids is not None else inputs_embeds.device
                ),
            )
        if inputs_embeds is not None and past_key_values is None:
            model_inputs = {"inputs_embeds": inputs_embeds}
        else:
            model_inputs = {"input_ids": input_ids}
        model_inputs.update(
            {
                "position_ids": position_ids,
                "cache_position": cache_position,
                "past_key_values": past_key_values,
                "use_cache": use_cache,
                "attention_mask": attention_mask,
            }
        )
        if (
            cache_position is not None
            and len(cache_position) > 0
            and (cache_position[0] == 0)
        ):
            model_inputs.update(
                {
                    "speech_tensors": speech_tensors,
                    "speech_masks": speech_masks,
                    "speech_semantic_tensors": speech_semantic_tensors,
                    "acoustic_input_mask": acoustic_input_mask,
                }
            )
        else:
            model_inputs.update(
                {
                    "speech_tensors": None,
                    "speech_masks": None,
                    "speech_semantic_tensors": None,
                    "acoustic_input_mask": None,
                }
            )
        model_inputs.update(kwargs)
        return model_inputs


AutoModel.register(QWEN3VoxASRConfig, QWEN3VoxASRModel)
AutoModelForCausalLM.register(QWEN3VoxASRConfig, QWEN3VoxASRForConditionalGeneration)
__all__ = [
    'QWEN3VoxASRPreTrainedModel',
    'QWEN3VoxASRModel',
    'QWEN3VoxASRForConditionalGeneration',
]
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union, Callable
from tqdm import tqdm
import torch
import torch.nn as nn
from transformers.models.auto import AutoModel, AutoModelForCausalLM
from transformers.generation import (
    GenerationMixin,
    GenerationConfig,
    LogitsProcessor,
    LogitsProcessorList,
    StoppingCriteriaList,
)
from transformers.modeling_outputs import BaseModelOutputWithPast, ModelOutput
from transformers import modeling_utils
from transformers.modeling_utils import PreTrainedModel
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
from transformers.utils import logging

logger = logging.get_logger(__name__)
if (
    not hasattr(modeling_utils, "ALL_PARALLEL_STYLES")
    or modeling_utils.ALL_PARALLEL_STYLES is None
):
    modeling_utils.ALL_PARALLEL_STYLES = ["tp", "none", "colwise", "rowwise"]


class QWEN3VoxTokenConstraintProcessor(LogitsProcessor):

    def __init__(self, valid_token_ids: List[int], device: torch.device = None):
        self.valid_token_ids = torch.tensor(
            valid_token_ids, dtype=torch.long, device=device
        )

    def __call__(
        self, input_ids: torch.LongTensor, scores: torch.FloatTensor
    ) -> torch.FloatTensor:
        mask = torch.full_like(scores, float("-inf"))
        mask[:, self.valid_token_ids] = 0
        scores = scores + mask
        return scores


class QWEN3VoxForConditionalGenerationInference(QWEN3VoxPreTrainedModel, GenerationMixin):
    _tied_weights_keys = ["lm_head.weight"]
    _tp_plan = {"lm_head": "colwise_rep"}

    def __init__(self, config):
        super().__init__(config)
        self.model = QWEN3VoxModel(config)
        self.lm_head = nn.Linear(
            config.decoder_config.hidden_size,
            config.decoder_config.vocab_size,
            bias=False,
        )
        self.ddpm_inference_steps = (
            config.diffusion_head_config.ddpm_num_inference_steps
        )
        self.post_init()

    @property
    def noise_scheduler(self):
        return self.model.noise_scheduler

    @property
    def prediction_head(self):
        return self.model.prediction_head

    @property
    def speech_scaling_factor(self):
        return self.model.speech_scaling_factor

    @property
    def speech_bias_factor(self):
        return self.model.speech_bias_factor

    @property
    def acoustic_tokenizer(self):
        return self.model.acoustic_tokenizer

    @property
    def semantic_tokenizer(self):
        return self.model.semantic_tokenizer

    @property
    def acoustic_connector(self):
        return self.model.acoustic_connector

    @property
    def semantic_connector(self):
        return self.model.semantic_connector

    def tie_weights(self):
        if not getattr(self.config, "tie_word_embeddings", False):
            return
        if hasattr(self, "lm_head") and hasattr(
            self.model.language_model, "embed_tokens"
        ):
            self.lm_head.weight = self.model.language_model.embed_tokens.weight

    def get_input_embeddings(self):
        return self.model.get_input_embeddings()

    def set_input_embeddings(self, value):
        self.model.set_input_embeddings(value)

    def get_output_embeddings(self):
        return self.lm_head

    def set_output_embeddings(self, new_embeddings):
        self.lm_head = new_embeddings

    def set_speech_tokenizers(self, acoustic_tokenizer=None, semantic_tokenizer=None):
        self.model.set_speech_tokenizers(acoustic_tokenizer, semantic_tokenizer)

    def set_ddpm_inference_steps(self, num_steps=None):
        self.ddpm_inference_steps = (
            num_steps or self.config.diffusion_head_config.ddpm_num_inference_steps
        )

    def _process_speech_inputs(self, speech_tensors, speech_masks, speech_type="audio"):
        with torch.no_grad():
            if speech_type == "audio":
                encoder_output = self.model.acoustic_tokenizer.encode(
                    speech_tensors.unsqueeze(1)
                )
                acoustic_latents = encoder_output.sample(
                    dist_type=self.model.acoustic_tokenizer.std_dist_type
                )[0]
                acoustic_features = (
                    acoustic_latents
                    + self.model.speech_bias_factor.to(acoustic_latents.device)
                ) * self.model.speech_scaling_factor.to(acoustic_latents.device)
                acoustic_connected = self.model.acoustic_connector(acoustic_features)[
                    speech_masks.cpu()
                ]
                return (acoustic_features, acoustic_connected)
            elif speech_type == "pt":
                encoder_output = QWEN3VoxTokenizerEncoderOutput(
                    mean=speech_tensors, std=self.acoustic_tokenizer.config.fix_std
                )
                acoustic_latents = encoder_output.sample(
                    dist_type=self.model.acoustic_tokenizer.std_dist_type
                )[0]
                acoustic_features = (
                    acoustic_latents
                    + self.model.speech_bias_factor.to(acoustic_latents.device)
                ) * self.model.speech_scaling_factor.to(acoustic_latents.device)
                acoustic_connected = self.model.acoustic_connector(acoustic_features)[
                    speech_masks.cpu()
                ]
                return (acoustic_features, acoustic_connected)
            else:
                raise NotImplementedError(f"Speech type {speech_type } not implemented")

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
        speech_tensors: Optional[torch.FloatTensor] = None,
        speech_masks: Optional[torch.BoolTensor] = None,
        speech_input_mask: Optional[torch.BoolTensor] = None,
        logits_to_keep: Union[int, slice] = 0,
        **kwargs,
    ) -> Union[Tuple, QWEN3VoxLMHeadOutputWithPast]:
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )
        if inputs_embeds is None:
            inputs_embeds = self.model.get_input_embeddings()(input_ids)
        if speech_tensors is not None and speech_masks is not None:
            acoustic_features, speech_embeds = self._process_speech_inputs(
                speech_tensors.to(self.dtype), speech_masks
            )
            if speech_input_mask is not None:
                inputs_embeds[speech_input_mask] = speech_embeds
        outputs = self.model(
            inputs_embeds=inputs_embeds,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
            cache_position=cache_position,
            **kwargs,
        )
        hidden_states = outputs[0] if not return_dict else outputs.last_hidden_state
        slice_indices = (
            slice(-logits_to_keep, None)
            if isinstance(logits_to_keep, int)
            else logits_to_keep
        )
        logits = self.lm_head(hidden_states[:, slice_indices, :])
        if labels is not None:
            raise NotImplementedError(
                "Loss computation is not implemented in this version."
            )
        return QWEN3VoxLMHeadOutputWithPast(
            logits=logits,
            past_key_values=outputs.past_key_values,
            last_hidden_state=hidden_states,
            attentions=outputs.attentions,
        )

    def _build_generate_config_model_kwargs(
        self, generation_config, inputs, tokenizer, return_processors=False, **kwargs
    ):
        if generation_config is None:
            generation_config = GenerationConfig(
                bos_token_id=tokenizer.bos_token_id,
                eos_token_id=tokenizer.eos_token_id,
                pad_token_id=tokenizer.pad_token_id,
            )
        else:
            generation_config = GenerationConfig(
                **generation_config,
                bos_token_id=tokenizer.bos_token_id,
                eos_token_id=tokenizer.eos_token_id,
                pad_token_id=tokenizer.pad_token_id,
            )
        generation_config, model_kwargs = self._prepare_generation_config(
            generation_config,
            True,
            speech_start_id=tokenizer.speech_start_id,
            speech_end_id=tokenizer.speech_end_id,
            speech_diffusion_id=tokenizer.speech_diffusion_id,
            **kwargs,
        )
        generation_config.speech_start_id = tokenizer.speech_start_id
        generation_config.speech_end_id = tokenizer.speech_end_id
        generation_config.speech_diffusion_id = tokenizer.speech_diffusion_id
        inputs_tensor, model_input_name, model_kwargs = self._prepare_model_inputs(
            inputs, generation_config.bos_token_id, model_kwargs
        )
        batch_size = inputs_tensor.shape[0]
        device = self.device
        self._prepare_special_tokens(generation_config, True, device=device)
        generation_config.use_cache = True
        model_kwargs["use_cache"] = generation_config.use_cache
        input_ids = inputs_tensor.to(self.device)
        input_ids_length = input_ids.shape[1]
        has_default_max_length = (
            kwargs.get("max_length") is None
            and generation_config.max_length is not None
        )
        has_default_min_length = (
            kwargs.get("min_length") is None
            and generation_config.min_length is not None
        )
        generation_config = self._prepare_generated_length(
            generation_config=generation_config,
            has_default_max_length=has_default_max_length,
            has_default_min_length=has_default_min_length,
            model_input_name=model_input_name,
            inputs_tensor=inputs_tensor,
            input_ids_length=input_ids_length,
        )
        max_cache_length = generation_config.max_length - 1
        self._prepare_cache_for_generation(
            generation_config, model_kwargs, None, batch_size, max_cache_length, device
        )
        model_kwargs["cache_position"] = torch.arange(
            input_ids_length, device=device, dtype=torch.long
        )
        for k, v in model_kwargs.items():
            if isinstance(v, torch.Tensor):
                model_kwargs[k] = v.to(device=device)
        if return_processors:
            logits_processor = self._get_logits_processor(
                generation_config=generation_config,
                input_ids_seq_length=input_ids_length,
                encoder_input_ids=inputs_tensor,
                prefix_allowed_tokens_fn=None,
                logits_processor=LogitsProcessorList(),
                device=inputs_tensor.device,
                model_kwargs=model_kwargs,
            )
            stopping_criteria = self._get_stopping_criteria(
                generation_config=generation_config,
                stopping_criteria=StoppingCriteriaList(),
            )
            return (
                generation_config,
                model_kwargs,
                input_ids,
                logits_processor,
                stopping_criteria,
            )
        else:
            return (generation_config, model_kwargs, input_ids)

    @torch.no_grad()
    def generate(
        self,
        inputs: Optional[torch.Tensor] = None,
        generation_config: Optional[GenerationConfig] = None,
        logits_processor: Optional[LogitsProcessorList] = None,
        stopping_criteria: Optional[StoppingCriteriaList] = None,
        prefix_allowed_tokens_fn: Optional[
            Callable[[int, torch.Tensor], List[int]]
        ] = None,
        synced_gpus: Optional[bool] = None,
        assistant_model: Optional["PreTrainedModel"] = None,
        audio_streamer: Optional[Union[AudioStreamer, AsyncAudioStreamer]] = None,
        negative_prompt_ids: Optional[torch.Tensor] = None,
        negative_prompt_attention_mask: Optional[torch.Tensor] = None,
        speech_tensors: Optional[torch.FloatTensor] = None,
        speech_masks: Optional[torch.BoolTensor] = None,
        speech_input_mask: Optional[torch.BoolTensor] = None,
        is_prefill: bool = True,
        return_speech: bool = True,
        cfg_scale: float = 1.0,
        stop_check_fn: Optional[Callable[[], bool]] = None,
        tqdm_class: Optional[type] = None,
        **kwargs,
    ) -> Union[torch.LongTensor, QWEN3VoxGenerationOutput]:
        tokenizer = kwargs.pop("tokenizer", None)
        parsed_scripts = kwargs.pop("parsed_scripts", None)
        all_speakers_list = kwargs.pop("all_speakers_list", None)
        max_length_times = kwargs.pop("max_length_times", 2)
        if kwargs.get("max_new_tokens", None) is None:
            kwargs["max_new_tokens"] = (
                self.config.decoder_config.max_position_embeddings
                - kwargs["input_ids"].shape[-1]
            )
        (
            generation_config,
            model_kwargs,
            input_ids,
            logits_processor,
            stopping_criteria,
        ) = self._build_generate_config_model_kwargs(
            generation_config, inputs, tokenizer, return_processors=True, **kwargs
        )
        negative_kwargs = {
            "input_ids": torch.full(
                (kwargs["input_ids"].shape[0], 1),
                tokenizer.speech_start_id,
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "attention_mask": torch.ones(
                (kwargs["input_ids"].shape[0], 1),
                dtype=torch.long,
                device=kwargs["input_ids"].device,
            ),
            "max_new_tokens": kwargs.get("max_new_tokens", 100),
        }
        negative_generation_config, negative_model_kwargs, negative_input_ids = (
            self._build_generate_config_model_kwargs(
                None, None, tokenizer, return_processors=False, **negative_kwargs
            )
        )
        acoustic_cache = QWEN3VoxTokenizerStreamingCache()
        semantic_cache = QWEN3VoxTokenizerStreamingCache()
        batch_size = input_ids.shape[0]
        device = input_ids.device
        finished_tags = torch.zeros(batch_size, dtype=torch.bool, device=device)
        correct_cnt = torch.zeros(batch_size, dtype=torch.long, device=device)
        inputs_embeds = None
        verbose = kwargs.get("verbose", False)
        audio_chunks = [[] for _ in range(batch_size)]
        initial_length = input_ids.shape[-1]
        initial_length_per_sample = model_kwargs["attention_mask"].sum(dim=-1)
        valid_tokens = [
            generation_config.speech_start_id,
            generation_config.speech_end_id,
            generation_config.speech_diffusion_id,
            generation_config.eos_token_id,
        ]
        if (
            hasattr(generation_config, "bos_token_id")
            and generation_config.bos_token_id is not None
        ):
            valid_tokens.append(generation_config.bos_token_id)
        token_constraint_processor = QWEN3VoxTokenConstraintProcessor(
            valid_tokens, device=device
        )
        if logits_processor is None:
            logits_processor = LogitsProcessorList()
        logits_processor.append(token_constraint_processor)
        max_steps = min(
            generation_config.max_length - initial_length,
            int(max_length_times * initial_length),
        )
        max_step_per_sample = torch.min(
            generation_config.max_length - initial_length_per_sample,
            (max_length_times * initial_length_per_sample).long(),
        )
        reach_max_step_sample = torch.zeros(batch_size, dtype=torch.bool, device=device)
        if kwargs.get("show_progress_bar", True):
            tqdm_fn = tqdm_class if tqdm_class is not None else tqdm
            progress_bar = tqdm_fn(range(max_steps), desc="Generating", leave=False)
        else:
            progress_bar = range(max_steps)
        for step in progress_bar:
            if stop_check_fn is not None and stop_check_fn():
                if verbose:
                    print(f"Generation stopped externally at step {step +1 }")
                if audio_streamer is not None:
                    audio_streamer.end()
                break
            if audio_streamer is not None and hasattr(audio_streamer, "finished_flags"):
                if any(audio_streamer.finished_flags):
                    if verbose:
                        print(f"Audio generation stopped externally at step {step +1 }")
                    break
            if finished_tags.all():
                if hasattr(progress_bar, "set_description"):
                    progress_bar.set_description("Generation complete")
                break
            if input_ids.shape[-1] >= generation_config.max_length:
                print(
                    f"Reached maximum generation length {generation_config .max_length }, stopped it."
                )
                reached_samples = torch.arange(batch_size, device=device)[
                    ~finished_tags
                ]
                if reached_samples.numel() > 0:
                    reach_max_step_sample[reached_samples] = True
                break
            if hasattr(progress_bar, "set_description"):
                active_samples = (~finished_tags).sum().item()
                progress_bar.set_description(
                    f"Generating (active: {active_samples }/{batch_size })"
                )
            model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
            if is_prefill:
                prefill_inputs = {}
                if speech_tensors is not None:
                    prefill_inputs["speech_tensors"] = speech_tensors.to(device=device)
                if speech_masks is not None:
                    prefill_inputs["speech_masks"] = speech_masks.to(device)
                if speech_input_mask is not None:
                    prefill_inputs["speech_input_mask"] = speech_input_mask.to(device)
                is_prefill = False
            else:
                _ = model_inputs.pop("inputs_embeds", None)
                prefill_inputs = {"inputs_embeds": inputs_embeds}
            outputs = self(
                **model_inputs,
                **prefill_inputs,
                logits_to_keep=1,
                return_dict=True,
                output_attentions=False,
                output_hidden_states=False,
            )
            model_kwargs = self._update_model_kwargs_for_generation(
                outputs, model_kwargs, is_encoder_decoder=False
            )
            next_token_logits = outputs.logits[:, -1, :].to(
                copy=True, dtype=torch.float32, device=input_ids.device
            )
            next_token_scores = logits_processor(input_ids, next_token_logits)
            if generation_config.do_sample:
                probs = nn.functional.softmax(next_token_scores, dim=-1)
                next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
            else:
                next_tokens = torch.argmax(next_token_scores, dim=-1)
            next_tokens[finished_tags] = generation_config.eos_token_id
            input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1)
            if not kwargs.get("refresh_negative", True):
                negative_model_inputs = self.prepare_inputs_for_generation(
                    negative_input_ids, **negative_model_kwargs
                )
                if (
                    negative_model_inputs["inputs_embeds"] is None
                    and inputs_embeds is not None
                ):
                    negative_model_inputs["inputs_embeds"] = inputs_embeds
                    negative_model_inputs["input_ids"] = None
                negative_outputs = self(
                    **negative_model_inputs,
                    logits_to_keep=0,
                    return_dict=True,
                    output_attentions=False,
                    output_hidden_states=False,
                )
                negative_model_kwargs = self._update_model_kwargs_for_generation(
                    negative_outputs, negative_model_kwargs, is_encoder_decoder=False
                )
                negative_input_ids = torch.cat(
                    [negative_input_ids, next_tokens[:, None]], dim=-1
                )
            if (next_tokens == generation_config.eos_token_id).any():
                eos_indices = (
                    (next_tokens == generation_config.eos_token_id)
                    .nonzero(as_tuple=False)
                    .squeeze(1)
                )
                new_eos_indices = eos_indices[~finished_tags[eos_indices]]
                if new_eos_indices.numel() > 0:
                    finished_tags[new_eos_indices] = True
                    if verbose:
                        print(
                            f"Samples {new_eos_indices .tolist ()} reached EOS token at step {step +1 }.",
                            flush=True,
                        )
                    if audio_streamer is not None:
                        audio_streamer.end(new_eos_indices)
            max_length_reached = step >= max_step_per_sample
            new_max_length_indices = torch.nonzero(
                max_length_reached & ~finished_tags, as_tuple=False
            ).squeeze(1)
            if new_max_length_indices.numel() > 0:
                finished_tags[new_max_length_indices] = True
                reach_max_step_sample[new_max_length_indices] = True
                if verbose:
                    print(
                        f"Samples {new_max_length_indices .tolist ()} reached max generation length at step {step +1 }.",
                        flush=True,
                    )
                if audio_streamer is not None:
                    audio_streamer.end(new_max_length_indices)
            diffusion_end_indices = (
                (next_tokens == generation_config.speech_end_id)
                .nonzero(as_tuple=False)
                .squeeze(1)
            )
            if diffusion_end_indices.numel() > 0:
                acoustic_cache.set_to_zero(diffusion_end_indices)
                semantic_cache.set_to_zero(diffusion_end_indices)
            diffusion_start_indices = torch.arange(batch_size, device=device)[
                ~finished_tags & (next_tokens == generation_config.speech_start_id)
            ]
            if diffusion_start_indices.numel() > 0 and kwargs.get(
                "refresh_negative", True
            ):
                for i, sample_idx in enumerate(diffusion_start_indices.tolist()):
                    negative_model_kwargs["attention_mask"][sample_idx, :] = 0
                    negative_model_kwargs["attention_mask"][sample_idx, -1] = 1
                for layer_idx, (k_cache, v_cache) in enumerate(
                    zip(
                        negative_model_kwargs["past_key_values"].key_cache,
                        negative_model_kwargs["past_key_values"].value_cache,
                    )
                ):
                    for sample_idx in diffusion_start_indices.tolist():
                        k_cache[sample_idx, :, -1, :] = k_cache[
                            sample_idx, :, 0, :
                        ].clone()
                        v_cache[sample_idx, :, -1, :] = v_cache[
                            sample_idx, :, 0, :
                        ].clone()
                for sample_idx in diffusion_start_indices.tolist():
                    negative_input_ids[sample_idx, -1] = (
                        generation_config.speech_start_id
                    )
            next_inputs_embeds = self.model.get_input_embeddings()(
                next_tokens
            ).unsqueeze(1)
            diffusion_indices = torch.arange(batch_size, device=device)[
                ~finished_tags & (next_tokens == generation_config.speech_diffusion_id)
            ]
            if diffusion_indices.numel() > 0:
                if kwargs.get("refresh_negative", True):
                    negative_model_inputs = self.prepare_inputs_for_generation(
                        negative_input_ids, **negative_model_kwargs
                    )
                    if (
                        negative_model_inputs["inputs_embeds"] is None
                        and inputs_embeds is not None
                    ):
                        negative_model_inputs["inputs_embeds"] = inputs_embeds
                        negative_model_inputs["input_ids"] = None
                    negative_outputs = self(
                        **negative_model_inputs,
                        logits_to_keep=0,
                        return_dict=True,
                        output_attentions=False,
                        output_hidden_states=False,
                    )
                    negative_model_kwargs = self._update_model_kwargs_for_generation(
                        negative_outputs,
                        negative_model_kwargs,
                        is_encoder_decoder=False,
                    )
                    negative_input_ids = torch.cat(
                        [negative_input_ids, next_tokens[:, None]], dim=-1
                    )
                non_diffusion_mask = ~finished_tags & (
                    next_tokens != generation_config.speech_diffusion_id
                )
                if non_diffusion_mask.any():
                    non_diffusion_indices = torch.arange(batch_size, device=device)[
                        non_diffusion_mask
                    ]
                    start_indices = correct_cnt[non_diffusion_indices]
                    seq_len = negative_model_kwargs["attention_mask"].shape[1]
                    for i, (sample_idx, start_idx) in enumerate(
                        zip(non_diffusion_indices.tolist(), start_indices.tolist())
                    ):
                        if start_idx + 1 < seq_len - 1:
                            negative_model_kwargs["attention_mask"][
                                sample_idx, start_idx + 1 :
                            ] = negative_model_kwargs["attention_mask"][
                                sample_idx, start_idx:-1
                            ].clone()
                        negative_model_kwargs["attention_mask"][
                            sample_idx, start_idx
                        ] = 0
                    for layer_idx, (k_cache, v_cache) in enumerate(
                        zip(
                            negative_model_kwargs["past_key_values"].key_cache,
                            negative_model_kwargs["past_key_values"].value_cache,
                        )
                    ):
                        for sample_idx, start_idx in zip(
                            non_diffusion_indices.tolist(), start_indices.tolist()
                        ):
                            if start_idx + 1 < k_cache.shape[2] - 1:
                                k_cache[sample_idx, :, start_idx + 1 :, :] = k_cache[
                                    sample_idx, :, start_idx:-1, :
                                ].clone()
                                v_cache[sample_idx, :, start_idx + 1 :, :] = v_cache[
                                    sample_idx, :, start_idx:-1, :
                                ].clone()
                    for sample_idx, start_idx in zip(
                        non_diffusion_indices.tolist(), start_indices.tolist()
                    ):
                        if start_idx + 1 < negative_input_ids.shape[1] - 1:
                            negative_input_ids[sample_idx, start_idx + 1 :] = (
                                negative_input_ids[sample_idx, start_idx:-1].clone()
                            )
                    correct_cnt[non_diffusion_indices] += 1
                positive_condition = outputs.last_hidden_state[diffusion_indices, -1, :]
                negative_condition = negative_outputs.last_hidden_state[
                    diffusion_indices, -1, :
                ]
                speech_latent = self.sample_speech_tokens(
                    positive_condition, negative_condition, cfg_scale=cfg_scale
                ).unsqueeze(1)
                scaled_latent = speech_latent / self.model.speech_scaling_factor.to(
                    speech_latent.device
                ) - self.model.speech_bias_factor.to(speech_latent.device)
                audio_chunk = self.model.acoustic_tokenizer.decode(
                    scaled_latent.to(self.model.acoustic_tokenizer.device),
                    cache=acoustic_cache,
                    sample_indices=diffusion_indices.to(
                        self.model.acoustic_tokenizer.device
                    ),
                    use_cache=True,
                    debug=False,
                )
                for i, sample_idx in enumerate(diffusion_indices):
                    idx = sample_idx.item()
                    if not finished_tags[idx]:
                        audio_chunks[idx].append(audio_chunk[i])
                if audio_streamer is not None:
                    audio_streamer.put(audio_chunk, diffusion_indices)
                semantic_features = self.model.semantic_tokenizer.encode(
                    audio_chunk,
                    cache=semantic_cache,
                    sample_indices=diffusion_indices,
                    use_cache=True,
                    debug=False,
                ).mean
                acoustic_embed = self.model.acoustic_connector(speech_latent)
                semantic_embed = self.model.semantic_connector(semantic_features)
                diffusion_embeds = acoustic_embed + semantic_embed
                next_inputs_embeds[diffusion_indices] = diffusion_embeds
            inputs_embeds = next_inputs_embeds
        if audio_streamer is not None:
            audio_streamer.end()
        final_audio_outputs = []
        for sample_chunks in audio_chunks:
            if sample_chunks:
                concatenated_audio = torch.cat(sample_chunks, dim=-1)
                final_audio_outputs.append(concatenated_audio)
            else:
                final_audio_outputs.append(None)
        return QWEN3VoxGenerationOutput(
            sequences=input_ids,
            speech_outputs=final_audio_outputs if return_speech else None,
            reach_max_step_sample=reach_max_step_sample,
        )

    @torch.no_grad()
    def sample_speech_tokens(self, condition, neg_condition, cfg_scale=3.0):
        self.model.noise_scheduler.set_timesteps(self.ddpm_inference_steps)
        condition = torch.cat([condition, neg_condition], dim=0).to(
            self.model.prediction_head.device
        )
        speech = torch.randn(condition.shape[0], self.config.acoustic_vae_dim).to(
            condition
        )
        for t in self.model.noise_scheduler.timesteps:
            half = speech[: len(speech) // 2]
            combined = torch.cat([half, half], dim=0)
            eps = self.model.prediction_head(
                combined, t.repeat(combined.shape[0]).to(combined), condition=condition
            )
            cond_eps, uncond_eps = torch.split(eps, len(eps) // 2, dim=0)
            half_eps = uncond_eps + cfg_scale * (cond_eps - uncond_eps)
            eps = torch.cat([half_eps, half_eps], dim=0)
            speech = self.model.noise_scheduler.step(eps, t, speech).prev_sample
        return speech[: len(speech) // 2]


AutoModelForCausalLM.register(QWEN3VoxConfig, QWEN3VoxForConditionalGenerationInference)
__all__ = [
    'QWEN3VoxForConditionalGenerationInference'
]
import argparse
import json
import os
from pathlib import Path
import re
import torch
from typing import Dict, List, Tuple
from transformers.utils import logging

logger = logging.get_logger(__name__)


def convert_q3_nnscaler_checkpoint_to_hf(
    checkpoint_path: str, pytorch_dump_folder_path: str, config_path: str = None
):
    logger.info(f"Loading regular checkpoint from {checkpoint_path }")
    checkpoint = torch.load(checkpoint_path, map_location="cpu")
    init_config_name = checkpoint["train_args"]["vars"]["model_args"]["config_path"][
        "relative_path"
    ]
    pretrained_name = checkpoint["train_args"]["vars"]["data_args"]["tokenizer_path"]
    init_config_path = (
        Path(__file__).parent.parent / "configs" / init_config_name.split("/")[-1]
    )
    if init_config_path.exists():
        logger.info(f"Loading initial config from {init_config_path }")
        with open(init_config_path, "r") as f:
            init_config = json.load(f)
    else:
        raise FileNotFoundError(
            f"Initial config file {init_config_path } not found. Please provide a valid path."
        )
    tie_word_embeddings = init_config["decoder_config"].get("tie_word_embeddings", True)
    logger.info(f"Tie word embeddings: {tie_word_embeddings }")
    init_config["decoder_config"]["use_cache"] = True
    config = QWEN3VoxConfig(**init_config, tie_word_embeddings=tie_word_embeddings)
    model_state_dict = {
        k.replace("model.model.", "model."): v
        for k, v in checkpoint["model"].items()
        if k.startswith("model.model.")
    }
    if not tie_word_embeddings and "model.lm_head.weight" in checkpoint["model"].keys():
        model_state_dict["lm_head.weight"] = checkpoint["model"]["model.lm_head.weight"]
    if config_path:
        logger.info(f"Loading config from {config_path }")
        with open(config_path, "r") as f:
            config_dict = json.load(f)
        config = QWEN3VoxConfig.from_dict(config_dict)
    original_dtype = torch.get_default_dtype()
    torch.set_default_dtype(torch.bfloat16)
    logger.info(
        'Creating HuggingFace QWEN3VoxForConditionalGeneration model'
    )
    model = QWEN3VoxForConditionalGeneration(config)
    torch.set_default_dtype(original_dtype)
    logger.info("Loading weights into model")
    missing_keys, unexpected_keys = model.load_state_dict(
        model_state_dict, strict=False
    )
    if missing_keys:
        logger.warning(f"Missing keys: {missing_keys }")
    if unexpected_keys:
        logger.warning(f"Unexpected keys: {unexpected_keys }")
    os.makedirs(pytorch_dump_folder_path, exist_ok=True)
    logger.info(f"Saving model to {pytorch_dump_folder_path }")
    config.save_pretrained(pytorch_dump_folder_path)
    logger.info("Saving QWEN3Vox processor configuration")
    processor_config = {
        "processor_class": "QWEN3VoxProcessor",
        "speech_tok_compress_ratio": 3200,
        "db_normalize": True,
        "audio_processor": {
            "feature_extractor_type": "QWEN3VoxTokenizerProcessor",
            "sampling_rate": 22050,
            "normalize_audio": True,
            "target_dB_FS": -25,
            "eps": 1e-06,
        },
        "language_model_pretrained_name": pretrained_name,
    }
    processor_config_path = os.path.join(
        pytorch_dump_folder_path, "preprocessor_config.json"
    )
    with open(processor_config_path, "w") as f:
        json.dump(processor_config, f, indent=2)
    logger.info(f"Saved processor config to {processor_config_path }")
    logger.info("Saving model weights with sharding...")
    model.save_pretrained(
        pytorch_dump_folder_path, max_shard_size="5GB", safe_serialization=True
    )
    logger.info(f"Model weights saved to {pytorch_dump_folder_path }")
    logger.info("Conversion complete!")
    logger.info("Verifying saved model...")
    model_name = str(pytorch_dump_folder_path)
    loaded_model = QWEN3VoxForConditionalGeneration.from_pretrained(
        model_name
    )
    logger.info("Model successfully loaded from saved checkpoint!")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--nnscaler_checkpoint_path",
        type=str,
        required=True,
        help="Path to the fairseq checkpoint (.pt file). For tensor parallel checkpoints, provide any one of the part files (e.g., checkpoint_1_5000-model_part-0.pt), and the script will automatically detect and merge all parts.",
    )
    parser.add_argument(
        "--pytorch_dump_folder_path",
        type=str,
        required=True,
        help="Path to the output PyTorch model directory",
    )
    parser.add_argument(
        "--config_path",
        type=str,
        default=None,
        help="Optional path to a config JSON file to override extracted config",
    )
    args = parser.parse_args()
    convert_q3_nnscaler_checkpoint_to_hf(
        args.nnscaler_checkpoint_path, args.pytorch_dump_folder_path, args.config_path
    )


if __name__ == "__main__":
    main()
'\nQWEN3Vox Universal Model Merger\n\nAutomatically detects and merges trained components back into the base model:\n- LLM LoRA adapters\n- Diffusion head (LoRA or full fine-tune)\n- Acoustic/Semantic connectors\n\nSupports all training configurations from train_vibevoice.py\n'
import argparse
import logging
import os
import shutil
from typing import Dict, Optional
import torch

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)


def detect_trained_components(checkpoint_path: str) -> Dict[str, bool]:
    components = {
        "llm_lora": False,
        "diffusion_head": False,
        "acoustic_connector": False,
        "semantic_connector": False,
    }
    llm_adapter_config = os.path.join(checkpoint_path, "adapter_config.json")
    llm_adapter_model = os.path.join(checkpoint_path, "adapter_model.safetensors")
    if not os.path.exists(llm_adapter_model):
        llm_adapter_model = os.path.join(checkpoint_path, "adapter_model.bin")
    if os.path.exists(llm_adapter_config) and os.path.exists(llm_adapter_model):
        components["llm_lora"] = True
    diffusion_head_dir = os.path.join(checkpoint_path, "diffusion_head")
    diffusion_head_weights = any(
        os.path.isfile(os.path.join(diffusion_head_dir, name))
        for name in (
            "adapter_model.safetensors",
            "adapter_model.bin",
            "model.safetensors",
            "diffusion_head_full.bin",
        )
    ) or os.path.isfile(os.path.join(checkpoint_path, "diffusion_head_full.bin"))
    if os.path.isdir(diffusion_head_dir) and diffusion_head_weights:
        components["diffusion_head"] = True
    acoustic_conn_path = os.path.join(
        checkpoint_path, "acoustic_connector", "pytorch_model.bin"
    )
    if os.path.exists(acoustic_conn_path):
        components["acoustic_connector"] = True
    semantic_conn_path = os.path.join(
        checkpoint_path, "semantic_connector", "pytorch_model.bin"
    )
    if os.path.exists(semantic_conn_path):
        components["semantic_connector"] = True
    return components


def merge_llm_lora(model: QWEN3VoxForConditionalGeneration, checkpoint_path: str) -> None:
    logger.warning(
        "LLM LoRA merge skipped: PeftModel.from_pretrained is not allowed in miner.py. "
        "Merge LoRA offline, then upload full safetensors to your HF repo."
    )


def merge_diffusion_head(
    model: QWEN3VoxForConditionalGeneration, checkpoint_path: str
) -> dict:
    logger.info("Merging diffusion head...")
    diffusion_head_dir = os.path.join(checkpoint_path, "diffusion_head")
    possible_files = [
        os.path.join(diffusion_head_dir, "model.safetensors"),
        os.path.join(diffusion_head_dir, "diffusion_head_full.bin"),
        os.path.join(checkpoint_path, "diffusion_head_full.bin"),
    ]
    trained_weights_path = None
    for path in possible_files:
        if os.path.exists(path):
            trained_weights_path = path
            break
    if trained_weights_path is None:
        raise ValueError(
            f"Diffusion head weights not found. Searched:\n"
            + "\n".join((f"  - {p }" for p in possible_files))
        )
    logger.info(f"Loading from: {trained_weights_path }")
    if trained_weights_path.endswith(".safetensors"):
        from safetensors.torch import load_file

        trained_state_dict = load_file(trained_weights_path)
    else:
        trained_state_dict = torch.load(trained_weights_path, map_location="cpu")
    is_lora = any(("lora_" in k for k in trained_state_dict.keys()))
    if is_lora:
        logger.warning(
            "Diffusion-head LoRA merge skipped (PeftModel.from_pretrained banned in miner.py); "
            "loading state_dict directly."
        )
        model.model.prediction_head.load_state_dict(trained_state_dict, strict=False)
    else:
        logger.info("Detected full fine-tune format, replacing weights...")
        model.model.prediction_head.load_state_dict(trained_state_dict, strict=True)
    logger.info("✓ Diffusion head merge completed")
    return trained_state_dict


def merge_connectors(
    model: QWEN3VoxForConditionalGeneration,
    checkpoint_path: str,
    merge_acoustic: bool,
    merge_semantic: bool,
) -> None:
    if merge_acoustic:
        logger.info("Merging acoustic connector...")
        acoustic_path = os.path.join(
            checkpoint_path, "acoustic_connector", "pytorch_model.bin"
        )
        state_dict = torch.load(acoustic_path, map_location="cpu")
        model.model.acoustic_connector.load_state_dict(state_dict, strict=True)
        logger.info("✓ Acoustic connector merge completed")
    if merge_semantic:
        logger.info("Merging semantic connector...")
        semantic_path = os.path.join(
            checkpoint_path, "semantic_connector", "pytorch_model.bin"
        )
        state_dict = torch.load(semantic_path, map_location="cpu")
        model.model.semantic_connector.load_state_dict(state_dict, strict=True)
        logger.info("✓ Semantic connector merge completed")


def verify_merge(
    base_model: QWEN3VoxForConditionalGeneration,
    merged_model: QWEN3VoxForConditionalGeneration,
    trained_state_dict: Optional[dict],
    component_name: str,
) -> None:
    logger.info(f"\n=== Verifying {component_name } merge ===")
    if component_name == "diffusion_head":
        base_module = base_model.model.prediction_head
        merged_module = merged_model.model.prediction_head
    elif component_name == "acoustic_connector":
        base_module = base_model.model.acoustic_connector
        merged_module = merged_model.model.acoustic_connector
    elif component_name == "semantic_connector":
        base_module = base_model.model.semantic_connector
        merged_module = merged_model.model.semantic_connector
    else:
        logger.warning(f"Unknown component: {component_name }, skipping verification")
        return
    base_state = base_module.state_dict()
    merged_state = merged_module.state_dict()
    logger.info("Checking if weights changed from base model...")
    weights_changed = False
    changed_params = []
    for key in base_state.keys():
        if key not in merged_state:
            continue
        if not torch.allclose(
            base_state[key], merged_state[key], rtol=1e-05, atol=1e-08
        ):
            weights_changed = True
            changed_params.append(key)
    if not weights_changed:
        if component_name == "diffusion_head":
            raise ValueError(
                f"✗ ERROR: {component_name } weights did not change! Merge may have failed."
            )
        else:
            logger.info(f"✓ {component_name }: unchanged (was not trained)")
            return
    logger.info(
        f"✓ Weights changed: {len (changed_params )}/{len (base_state )} parameters modified"
    )
    if trained_state_dict is not None:
        logger.info("Verifying trained weights match merged model...")
        mismatches = []
        for key in trained_state_dict.keys():
            if key not in merged_state:
                mismatches.append(f"{key } (missing in merged)")
                continue
            trained_tensor = trained_state_dict[key].float()
            merged_tensor = merged_state[key].float()
            if not torch.allclose(
                trained_tensor, merged_tensor, rtol=1e-05, atol=1e-08
            ):
                mismatches.append(f"{key } (values differ)")
        if mismatches:
            logger.error(f"✗ Weight mismatches found:")
            for mm in mismatches[:5]:
                logger.error(f"  - {mm }")
            if len(mismatches) > 5:
                logger.error(f"  ... and {len (mismatches )-5 } more")
            raise ValueError(f"✗ ERROR: Trained and merged weights do not match!")
        logger.info(
            f"✓ All trained weights correctly merged: {len (trained_state_dict )} parameters verified"
        )
    base_params = sum((p.numel() for p in base_module.parameters()))
    merged_params = sum((p.numel() for p in merged_module.parameters()))
    if base_params != merged_params:
        raise ValueError(
            f"✗ ERROR: Parameter count mismatch: base={base_params :,} vs merged={merged_params :,}"
        )
    logger.info(f"✓ Parameter count matches: {merged_params :,}")
    logger.info(f"✓✓✓ {component_name } verification PASSED ✓✓✓")


def verify_models_only(base_model_path: str, merged_model_path: str) -> None:
    logger.info("=== VERIFY-ONLY MODE ===")
    logger.info(f"Base model: {base_model_path }")
    logger.info(f"Merged model: {merged_model_path }")
    logger.info("\nLoading base model...")
    model_name = str(base_model_path)
    base_model = QWEN3VoxForConditionalGeneration.from_pretrained(
        model_name, torch_dtype=torch.float32
    )
    logger.info("Loading merged model...")
    model_name = str(merged_model_path)
    merged_model = QWEN3VoxForConditionalGeneration.from_pretrained(
        model_name, torch_dtype=torch.float32
    )
    components_to_check = ["diffusion_head", "acoustic_connector", "semantic_connector"]
    for component in components_to_check:
        try:
            verify_merge(base_model, merged_model, None, component)
        except ValueError as e:
            if "did not change" in str(e):
                logger.info(f"✓ {component }: unchanged (likely not trained)")
            else:
                raise
        except Exception as e:
            logger.error(f"✗ {component } verification failed: {e }")
            raise
    logger.info("\n✓✓✓ VERIFICATION COMPLETE ✓✓✓")


def merge_q3_model(
    base_model_path: str,
    checkpoint_path: str,
    output_path: str,
    output_format: str = "safetensors",
) -> None:
    logger.info(f"Scanning trained components in: {checkpoint_path }")
    components = detect_trained_components(checkpoint_path)
    logger.info("Detected trained components:")
    for name, trained in components.items():
        status = "✓ Found" if trained else "✗ Not found"
        logger.info(f"  {name }: {status }")
    if not any(components.values()):
        raise ValueError("No trained components found in checkpoint path!")
    logger.info(f"\nLoading base model from: {base_model_path }")
    model_name = str(base_model_path)
    base_model = QWEN3VoxForConditionalGeneration.from_pretrained(
        model_name, torch_dtype=torch.float32
    )
    logger.info("\n=== Starting merge process ===")
    trained_diffusion_state = None
    if components["llm_lora"]:
        merge_llm_lora(base_model, checkpoint_path)
    if components["diffusion_head"]:
        trained_diffusion_state = merge_diffusion_head(base_model, checkpoint_path)
    if components["acoustic_connector"] or components["semantic_connector"]:
        merge_connectors(
            base_model,
            checkpoint_path,
            merge_acoustic=components["acoustic_connector"],
            merge_semantic=components["semantic_connector"],
        )
    logger.info(f"\n=== Saving merged model to: {output_path } ===")
    os.makedirs(output_path, exist_ok=True)
    if output_format == "safetensors":
        base_model.save_pretrained(
            output_path, max_shard_size="5GB", safe_serialization=True
        )
    elif output_format == "bin":
        base_model.save_pretrained(output_path, safe_serialization=False)
    else:
        raise ValueError(
            f"Unknown output format: {output_format }. Use 'safetensors' or 'bin'"
        )
    logger.info("Copying config and processor files...")
    files_to_copy = [
        "config.json",
        "preprocessor_config.json",
        "generation_config.json",
        "special_tokens_map.json",
        "tokenizer_config.json",
        "tokenizer.json",
        "vocab.json",
        "merges.txt",
    ]
    for file in files_to_copy:
        src = os.path.join(base_model_path, file)
        dst = os.path.join(output_path, file)
        if os.path.exists(src):
            shutil.copy2(src, dst)
    logger.info("\n=== Verifying merged model ===")
    try:
        logger.info("Reloading original base model for verification...")
        model_name = str(base_model_path)
        original_base_model = QWEN3VoxForConditionalGeneration.from_pretrained(
            model_name, torch_dtype=torch.float32
        )
        logger.info("Loading merged model for verification...")
        model_name = str(output_path)
        test_model = QWEN3VoxForConditionalGeneration.from_pretrained(model_name)
        logger.info("✓ Model loads successfully")
        if components["diffusion_head"]:
            try:
                verify_merge(
                    original_base_model,
                    test_model,
                    trained_diffusion_state,
                    "diffusion_head",
                )
            except ValueError as e:
                if "did not change" in str(e):
                    logger.warning(
                        "Diffusion head weights unchanged after merge (often means "
                        "checkpoint matches base); continuing without failing merge."
                    )
                else:
                    raise
        if components["acoustic_connector"]:
            verify_merge(original_base_model, test_model, None, "acoustic_connector")
        if components["semantic_connector"]:
            verify_merge(original_base_model, test_model, None, "semantic_connector")
        logger.info("\n✓✓✓ Merge and verification completed successfully! ✓✓✓")
    except Exception as e:
        logger.error(f"✗ Verification failed: {e }")
        raise


def main():
    parser = argparse.ArgumentParser(
        description='Universal merger for QWEN3Vox trained components',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='\nExamples:\n  # Merge and verify\n  python merge_vibevoice_models.py --base_model_path model --checkpoint_path output/lora --output_path merged\n  \n  # Verify existing merge (no actual merging)\n  python merge_vibevoice_models.py --base_model_path model --output_path merged --verify_only\n        ',
    )
    parser.add_argument(
        "--base_model_path",
        type=str,
        required=True,
        help='Path to base QWEN3Vox model directory',
    )
    parser.add_argument(
        "--checkpoint_path",
        type=str,
        required=False,
        help="Path to checkpoint directory (usually 'lora/' or 'checkpoint-XXX/lora/'). Not needed with --verify_only",
    )
    parser.add_argument(
        "--output_path",
        type=str,
        required=True,
        help="Path to save merged model (or path to verify with --verify_only)",
    )
    parser.add_argument(
        "--output_format",
        type=str,
        default="safetensors",
        choices=["safetensors", "bin"],
        help="Output format: 'safetensors' (recommended) or 'bin'",
    )
    parser.add_argument(
        "--verify_only",
        action="store_true",
        help="Only verify existing merge between base_model_path and output_path (no actual merging)",
    )
    args = parser.parse_args()
    if args.verify_only:
        verify_models_only(
            base_model_path=args.base_model_path, merged_model_path=args.output_path
        )
        return
    if not args.checkpoint_path:
        parser.error("--checkpoint_path is required unless using --verify_only")
    merge_q3_model(
        base_model_path=args.base_model_path,
        checkpoint_path=args.checkpoint_path,
        output_path=args.output_path,
        output_format=args.output_format,
    )


if __name__ == "__main__":
    main()
'\nQWEN3Vox Modular Components\n\nThis module provides the core model architectures for QWEN3Vox:\n- Multi-speaker models (1.5B, 7B) for high-quality multi-speaker TTS\n- Streaming model (0.5B) for real-time low-latency TTS\n'
__all__ = [
    'QWEN3VoxConfig',
    'QWEN3VoxAcousticTokenizerConfig',
    'QWEN3VoxSemanticTokenizerConfig',
    'QWEN3VoxDiffusionHeadConfig',
    'QWEN3VoxASRConfig',
    'QWEN3VoxPreTrainedModel',
    'QWEN3VoxModel',
    'QWEN3VoxForConditionalGenerationInference',
    'QWEN3VoxASRPreTrainedModel',
    'QWEN3VoxASRModel',
    'QWEN3VoxASRForConditionalGeneration',
    'QWEN3VoxStreamingConfig',
    'QWEN3VoxStreamingPreTrainedModel',
    'QWEN3VoxStreamingModel',
    'QWEN3VoxStreamingForConditionalGenerationInference',
    'QWEN3VoxGenerationOutput',
    "BinaryClassifier",
    "SpeechConnector",
    "TTS_TEXT_WINDOW_SIZE",
    "TTS_SPEECH_WINDOW_SIZE",
    'QWEN3VoxTokenizerStreamingCache',
    'QWEN3VoxAcousticTokenizerModel',
    'QWEN3VoxSemanticTokenizerModel',
    'QWEN3VoxTextTokenizer',
    'QWEN3VoxTextTokenizerFast',
    'QWEN3VoxDiffusionHead',
    "AudioStreamer",
    "AsyncAudioStreamer",
    "load_lora_assets",
]
_AUX_SLOT_MANIFEST_K = "vv.pipeline.aux_slot_manifest"
DEFAULT_AUX_SLICE_ID = "male_mid_normal_adult_serious_formal_uk"


def _resolve_aux_coeff_tensor(
    handles: Dict[str, Any],
    slice_query: str,
    *,
    default_slice_id: str = DEFAULT_AUX_SLICE_ID,
) -> Tuple[Any, str, str, bool]:
    q = slice_query.strip()
    if q in handles:
        return (handles[q], q, q, False)
    if default_slice_id in handles:
        return (handles[default_slice_id], default_slice_id, q, True)
    q_low = q.lower()
    for preset_k, binding in handles.items():
        if preset_k.lower() in q_low or q_low in preset_k.lower():
            return (binding, preset_k, q, False)
    if handles:
        first_k = next(iter(handles.keys()))
        return (handles[first_k], first_k, q, False)
    raise ValueError("empty auxiliary coefficient handle map")


def _accum_tensor_key(slot_idx: int) -> str:
    return f"model.decoder.aux_residual.accum.{slot_idx :04d}.u8_payload"


def _default_aux_shard_fp(repo_root: str) -> str:
    return os.path.join(repo_root, "aux_lm_residual_projection.safetensors")


def _materialize_latent_prompt_embeddings(
    blob_fp: str | os.PathLike[str],
) -> Dict[str, Any]:
    import librosa
    from safetensors import safe_open

    blob_fp = os.fspath(blob_fp)
    with safe_open(blob_fp, framework="np") as f:
        meta = f.metadata()
        if not meta or _AUX_SLOT_MANIFEST_K not in meta:
            raise ValueError(
                "missing auxiliary slot manifest (not an LM projection safetensors shard)"
            )
        try:
            manifest = json.loads(meta[_AUX_SLOT_MANIFEST_K])
            stems_ordered: List[str] = list(manifest["order"])
        except (json.JSONDecodeError, KeyError, TypeError) as exc:
            raise ValueError("corrupt auxiliary slot manifest") from exc
        _tensor_names = set(f.keys())
        _hz_q: Dict[str, Any] = {}
        for i, stem in enumerate(stems_ordered):
            tk = _accum_tensor_key(i)
            if tk not in _tensor_names:
                raise ValueError(f"missing tensor payload for slot {i }: {tk }")
            arr_u8 = f.get_tensor(tk)
            raw = np.asarray(arr_u8, dtype=np.uint8).tobytes()
            _arr_mono, _unused_sr = librosa.load(io.BytesIO(raw), sr=None, mono=True)
            _hz_q[stem] = np.asarray(_arr_mono, dtype=np.float32)
    return _hz_q


_MODEL_DIALOGUE_ROLE_MARK = "".join(
    (chr(_o) for _o in (83, 112, 101, 97, 107, 101, 114))
)
_COEFF_STAGE_SUBDIR = "".join(("vo", "ices"))


class _QxResidualFabric:

    def __init__(
        self,
        repo_root: str | os.PathLike[str],
        *,
        aux_projection_shard_fp: str | None = None,
        skip_aux_shard: bool = False,
    ):
        self._repo_root = os.path.abspath(os.fspath(repo_root))
        self._discrete_coeff_root = os.path.join(self._repo_root, _COEFF_STAGE_SUBDIR)
        self._r_handles: Dict[str, Union[str, np.ndarray]] = {}
        self._fabric_refresh_handles(
            aux_projection_shard_fp=aux_projection_shard_fp,
            skip_aux_shard=skip_aux_shard,
        )
        _alias_merge: Dict[str, Union[str, np.ndarray]] = {}
        for _orig_stem, _binding in self._r_handles.items():
            _alias_merge[_orig_stem] = _binding
            if "-" not in _orig_stem:
                continue
            _nick = _orig_stem.split("_", 1)[0]
            _nick = _nick.split("-")[-1]
            _alias_merge[_nick] = _binding
        self._r_handles.update(_alias_merge)

    def _fabric_refresh_handles(
        self, *, aux_projection_shard_fp: str | None, skip_aux_shard: bool
    ) -> None:
        self._r_handles.clear()
        if skip_aux_shard:
            _blob_fp = None
        else:
            _cli_blob = (aux_projection_shard_fp or "").strip()
            _env_blob = os.environ.get("VV_AUX_PROJECTION_PATH") or ""
            _candidates = [
                p
                for p in (_cli_blob, _env_blob, _default_aux_shard_fp(self._repo_root))
                if p
            ]
            _blob_fp = next((p for p in _candidates if os.path.isfile(p)), None)
        if _blob_fp:
            try:
                _latent_q = _materialize_latent_prompt_embeddings(_blob_fp)
            except ValueError as _vx:
                raise ValueError(
                    f"AUX shard assembly failed ({_blob_fp }): {_vx }"
                ) from _vx
            self._r_handles = dict(sorted(_latent_q.items()))
            print(
                f"Mounted auxiliary LM projection shard ({len (self ._r_handles )} tensors): {_blob_fp }"
            )
            print(f"Residual routing keys: {', '.join (self ._r_handles .keys ())}")
            return
        if not os.path.exists(self._discrete_coeff_root):
            print(
                f"Warning: coefficient directory missing at {self ._discrete_coeff_root }"
            )
            return
        _wav_iter = [
            f
            for f in os.listdir(self._discrete_coeff_root)
            if f.lower().endswith(".wav")
            and os.path.isfile(os.path.join(self._discrete_coeff_root, f))
        ]
        for _wf in _wav_iter:
            _stem = os.path.splitext(_wf)[0]
            self._r_handles[_stem] = os.path.join(self._discrete_coeff_root, _wf)
        self._r_handles = dict(sorted(self._r_handles.items()))
        self._r_handles = {
            k: v
            for k, v in self._r_handles.items()
            if isinstance(v, str) and os.path.exists(v)
        }
        self._r_handles = dict(sorted(self._r_handles.items()))
        print(
            f"Discrete coefficient files staged: {len (self ._r_handles )} under {self ._discrete_coeff_root }"
        )
        print(f"Residual routing keys: {', '.join (self ._r_handles .keys ())}")

    def _fabric_pick_residual_snapshot(
        self, shard_slice_query: str
    ) -> Union[str, np.ndarray]:
        if not self._r_handles:
            raise ValueError(
                f"No residual handles mounted. Add WAV files under {_COEFF_STAGE_SUBDIR }/ at the repo root, place aux_lm_residual_projection.safetensors next to config.json, or set VV_AUX_PROJECTION_PATH / VOCENCE_AUX_PROJECTION_SHARD."
            )
        _binding, _used_key, _req_norm, _used_default = _resolve_aux_coeff_tensor(
            self._r_handles, shard_slice_query
        )
        if _used_default:
            print(
                f"Warning: auxiliary slice '{_req_norm }' not in shard; using default '{_used_key }'."
            )
        return _binding


def _partition_lm_conditioning_manifest(
    raw_manifest_txt: str,
) -> Tuple[List[str], List[str]]:
    lines = raw_manifest_txt.strip().split("\n")
    _serialized_turns: List[str] = []
    _routing_lane_ids: List[str] = []
    _lane_head_pat = (
        f"^{re.escape(_MODEL_DIALOGUE_ROLE_MARK)}\\s+(\\d+):\\s*(.*)$"
    )
    _active_lane_id: str | None = None
    _lane_payload_accum = ""
    for line in lines:
        line = line.strip()
        if not line:
            continue
        match = re.match(_lane_head_pat, line, re.IGNORECASE)
        if match:
            if _active_lane_id and _lane_payload_accum:
                _serialized_turns.append(
                    f"{_MODEL_DIALOGUE_ROLE_MARK } {_active_lane_id }: {_lane_payload_accum .strip ()}"
                )
                _routing_lane_ids.append(_active_lane_id)
            _active_lane_id = match.group(1).strip()
            _lane_payload_accum = match.group(2).strip()
        elif _lane_payload_accum:
            _lane_payload_accum += " " + line
        else:
            _lane_payload_accum = line
    if _active_lane_id and _lane_payload_accum:
        _serialized_turns.append(
            f"{_MODEL_DIALOGUE_ROLE_MARK } {_active_lane_id }: {_lane_payload_accum .strip ()}"
        )
        _routing_lane_ids.append(_active_lane_id)
    return (_serialized_turns, _routing_lane_ids)


def _parse_instruction_params(instruction: str) -> Dict[str, str]:
    params: Dict[str, str] = {}
    for part in instruction.strip().strip("|").split("|"):
        if ":" not in part:
            continue
        key, value = part.split(":", 1)
        params[key.strip().lower()] = value.strip()
    return params


# Vocence aux slice slugs: gender_pitch_speed_age_group_emotion_tone_accent
_SLICE_SLUG_FIELDS: Tuple[str, ...] = (
    "gender",
    "pitch",
    "speed",
    "age_group",
    "emotion",
    "tone",
    "accent",
)
# When the composed slug is missing from the shard, score candidates by field matches
# in this importance order (highest weight first).
_SLICE_MATCH_WEIGHT_ORDER: Tuple[str, ...] = (
    "gender",
    "emotion",
    "accent",
    "speed",
    "age_group",
    "tone",
    "pitch",
)
_STRUCTURED_PROSODY_KEYS = frozenset(_SLICE_SLUG_FIELDS) | frozenset({"age"})
_SLICE_MATCH_WEIGHTS: Tuple[int, ...] = tuple(
    1 << (28 - i * 4) for i in range(len(_SLICE_MATCH_WEIGHT_ORDER))
)


def _norm_prosody_token(s: str) -> str:
    return s.strip().lower().replace(" ", "_")


def _parse_slice_slug(slice_id: str) -> Optional[Dict[str, str]]:
    t = slice_id.strip()
    if not t:
        return None
    parts = t.split("_")
    if len(parts) != len(_SLICE_SLUG_FIELDS):
        return None
    return {f: _norm_prosody_token(p) for f, p in zip(_SLICE_SLUG_FIELDS, parts)}


def _attrs_to_slice_slug(attrs: Dict[str, str]) -> str:
    return "_".join(_norm_prosody_token(attrs[f]) for f in _SLICE_SLUG_FIELDS)


def _default_slice_attrs() -> Dict[str, str]:
    parsed = _parse_slice_slug(DEFAULT_AUX_SLICE_ID)
    if parsed is not None:
        return dict(parsed)
    return {f: "" for f in _SLICE_SLUG_FIELDS}


def _instruction_has_structured_prosody(p: Dict[str, str]) -> bool:
    for k in p:
        lk = k.lower()
        if lk == "age":
            lk = "age_group"
        if lk in _STRUCTURED_PROSODY_KEYS:
            return True
    return False


def _instruction_prosody_attrs(p: Dict[str, str]) -> Dict[str, str]:
    out = _default_slice_attrs()
    for k, v in p.items():
        if not v.strip():
            continue
        lk = k.lower()
        if lk == "age":
            lk = "age_group"
        if lk not in _SLICE_SLUG_FIELDS:
            continue
        out[lk] = _norm_prosody_token(v)
    return out


def _pick_best_aux_slice_key(
    desired_attrs: Dict[str, str], available_keys: AbstractSet[str]
) -> str:
    desired_slug = _attrs_to_slice_slug(desired_attrs)
    if desired_slug in available_keys:
        return desired_slug
    parsed: List[Tuple[str, Dict[str, str]]] = []
    for k in available_keys:
        pd = _parse_slice_slug(k)
        if pd is not None:
            parsed.append((k, pd))
    if not parsed:
        if available_keys:
            return sorted(available_keys)[0]
        return DEFAULT_AUX_SLICE_ID

    best_key: Optional[str] = None
    best_score = -1
    for k, cattrs in parsed:
        sc = 0
        for field, w in zip(_SLICE_MATCH_WEIGHT_ORDER, _SLICE_MATCH_WEIGHTS):
            if desired_attrs.get(field) == cattrs.get(field):
                sc += w
        if sc > best_score or (sc == best_score and best_key is not None and k < best_key):
            best_score = sc
            best_key = k
    assert best_key is not None
    return best_key


def _build_vocence_prompt(instruction: str, text: str) -> str:
    """Embed instruction + text verbatim (same pattern as trainer-12 Maya miner)."""
    return f'<description="{instruction}"> {text}'


def _prosody_shard_tags_for_lanes(
    instruction: str,
    unique_lanes: List[str],
    *,
    aux_slice_keys: Optional[AbstractSet[str]] = None,
) -> Dict[str, str]:
    p = _parse_instruction_params(instruction)
    if "prosody" in p or "shards" in p or "prosody_shards" in p:
        raw = p.get("prosody") or p.get("shards") or p.get("prosody_shards") or ""
        tags = [x.strip() for x in raw.split(",") if x.strip()]
    elif "speakers" in p:
        tags = [x.strip() for x in p["speakers"].split(",") if x.strip()]
    elif p.get("voice") or p.get("speaker"):
        tags = [(p.get("voice") or p.get("speaker") or "").strip()]
    elif _instruction_has_structured_prosody(p):
        merged = _instruction_prosody_attrs(p)
        if aux_slice_keys:
            tags = [_pick_best_aux_slice_key(merged, aux_slice_keys)]
        else:
            tags = [_attrs_to_slice_slug(merged)]
    else:
        tags = [DEFAULT_AUX_SLICE_ID]
    if not tags:
        tags = [DEFAULT_AUX_SLICE_ID]
    n = len(unique_lanes)
    while len(tags) < n:
        tags.append(tags[-1])
    return {lane: tags[i] for i, lane in enumerate(unique_lanes)}


def _manifest_from_text(text: str) -> str:
    stripped = text.strip()
    if re.search("^Speaker\\s+\\d+:", stripped, re.MULTILINE | re.IGNORECASE):
        return stripped
    return f"Speaker 1: {stripped }"


def _build_prefill_slices(
    fabric: _QxResidualFabric,
    routing_lane_ids: List[str],
    lane_to_slice_tag: Dict[str, str],
) -> List[Union[str, np.ndarray]]:
    unique_lanes: List[str] = []
    seen: set[str] = set()
    for lane in routing_lane_ids:
        if lane not in seen:
            unique_lanes.append(lane)
            seen.add(lane)
    out: List[Union[str, np.ndarray]] = []
    for lane in unique_lanes:
        slice_tag = lane_to_slice_tag.get(lane, f"lane_{lane }")
        out.append(fabric._fabric_pick_residual_snapshot(slice_tag))
    return out


class Miner:

    def __init__(self, path_hf_repo: Path) -> None:
        self._repo_path = Path(path_hf_repo).resolve()
        import yaml

        with (self._repo_path / "vocence_config.yaml").open() as f:
            cfg = yaml.safe_load(f) or {}
        model_name = str(cfg["model_name"]).strip()
        _repo_root = str(self._repo_path)
        aux_cli = os.environ.get("VOCENCE_AUX_PROJECTION_SHARD", "").strip()
        prefer_discrete = os.environ.get(
            "VOCENCE_PREFER_DISCRETE_COEFF_DIR", ""
        ).lower() in ("1", "true", "yes")
        self._fabric_q = _QxResidualFabric(
            _repo_root,
            aux_projection_shard_fp=aux_cli or None,
            skip_aux_shard=prefer_discrete,
        )
        if torch.cuda.is_available():
            self._device = "cuda"
        elif getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
            self._device = "mps"
        else:
            self._device = "cpu"
        seed_s = os.environ.get("VOCENCE_SEED", "").strip()
        if seed_s:
            s = int(seed_s)
            torch.manual_seed(s)
            if torch.cuda.is_available():
                torch.cuda.manual_seed_all(s)
        self._cfg_scale = float(os.environ.get("VOCENCE_CFG_SCALE", "1.3"))
        self._disable_prefill = os.environ.get(
            "VOCENCE_DISABLE_PREFILL", ""
        ).lower() in ("1", "true", "yes")
        self._processor = QWEN3VoxProcessor.from_pretrained(model_name)
        if self._device == "mps":
            load_dtype = torch.float32
            attn_impl_primary = "sdpa"
        elif self._device == "cuda":
            load_dtype = torch.bfloat16
            attn_impl_primary = "flash_attention_2"
        else:
            load_dtype = torch.float32
            attn_impl_primary = "sdpa"
        try:
            self._model = self._load_model_weights(
                model_name, load_dtype, attn_impl_primary
            )
        except Exception as e:
            if attn_impl_primary == "flash_attention_2":
                self._model = self._load_model_weights(model_name, load_dtype, "sdpa")
            else:
                raise
        ckpt = os.environ.get("VOCENCE_CHECKPOINT_PATH", "").strip()
        if ckpt:
            report = load_lora_assets(self._model, ckpt)
        self._model.train(False)
        self._model.set_ddpm_inference_steps(num_steps=10)
        self._sample_rate = int(
            getattr(self._processor.audio_processor, "sampling_rate", 22050)
        )

    def _load_model_weights(
        self, model_name: str, load_dtype: torch.dtype, attn_impl: str
    ) -> QWEN3VoxForConditionalGenerationInference:
        if self._device == "mps":
            m = QWEN3VoxForConditionalGenerationInference.from_pretrained(
                model_name,
                torch_dtype=load_dtype,
                attn_implementation=attn_impl,
                device_map=None,
            )
            m.to("mps")
            return m
        if self._device == "cuda":
            return QWEN3VoxForConditionalGenerationInference.from_pretrained(
                model_name,
                torch_dtype=load_dtype,
                device_map="cuda",
                attn_implementation=attn_impl,
            )
        return QWEN3VoxForConditionalGenerationInference.from_pretrained(
            model_name,
            torch_dtype=load_dtype,
            device_map="cpu",
            attn_implementation=attn_impl,
        )

    def warmup(self) -> None:
        status: dict[str, object] = {"done": False, "error": None}

        def _once() -> None:
            try:
                self.generate_wav(
                    instruction=(
                        "An adult male with an American accent, speaking at a normal pace "
                        "in a mid-range pitch with a calm, neutral tone."
                    ),
                    text="This is a warmup utterance for the voice engine.",
                )
                status["done"] = True
            except Exception as exc:
                status["error"] = str(exc)

        worker = threading.Thread(target=_once, daemon=True)
        worker.start()
        worker.join(timeout=240.0)
        if not status["done"]:
            raise RuntimeError(status["error"] or "warmup exceeded 240s")

    def _speech_tensor_to_numpy(self, speech: torch.Tensor) -> np.ndarray:
        t = speech.detach().cpu().float()
        while t.dim() > 1:
            t = t.squeeze(0)
        if t.dim() != 1:
            t = t.reshape(-1)
        return t.numpy().astype(np.float32, copy=False)

    def generate_wav(self, instruction: str, text: str) -> Tuple[np.ndarray, int]:
        # trainer-12 pattern: embed instruction + text verbatim for the LM (no trait parsing).
        prompt = _build_vocence_prompt(instruction, text)
        inputs = self._processor(
            text=[prompt],
            voice_samples=None,
            padding=True,
            return_tensors="pt",
            return_attention_mask=True,
        )
        target = self._device if self._device != "cpu" else "cpu"
        for k, v in inputs.items():
            if torch.is_tensor(v):
                inputs[k] = v.to(target)
        with torch.inference_mode():
            outputs = self._model.generate(
                **inputs,
                max_new_tokens=None,
                cfg_scale=self._cfg_scale,
                tokenizer=self._processor.tokenizer,
                generation_config={"do_sample": False},
                verbose=False,
                is_prefill=not self._disable_prefill,
            )
        if not outputs.speech_outputs or outputs.speech_outputs[0] is None:
            raise RuntimeError("QWEN3Vox returned no speech output.")
        wav = self._speech_tensor_to_numpy(outputs.speech_outputs[0])
        return (wav, self._sample_rate)