§
    ‚ŠtjÈâ  ã                   ó8  — U d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 ddl
mZmZ  ej        e¦  «        Z e¦   «         rd dlZerddlmZ d„ Z	 	 	 	 d"d	ed
         ded         dedz  dedz  dedef         f
d„Z	 	 	 	 	 d#d	ed
         ded         dedz  dedz  dededef         fd„Z	 	 	 	 d"d	ed
         ded         dedz  dedz  dedef         f
d„Z	 	 	 d$d	d
ded         dedz  dedz  dedef         f
d„Z	 	 	 d$d	d
ded         dedz  dedz  dedef         f
d„Z	 	 	 d$d	d
ded         dedz  dedz  dedef         f
d„ZeeeeeedœZeeededef         f         f         e d<    G d„ de	¦  «        Z! G d„ d¦  «        Z"d%d	e"d e#dz  fd!„Z$dS )&é    N)ÚCallable©Úwraps)ÚTYPE_CHECKINGÚOptionalÚ	TypedDicté   )Úis_torch_availableÚlogging)ÚPreTrainedConfigc                 óV   ‡ ‡‡— dd„Šdd„Št          ‰ ¦  «        dˆˆˆ fd„	¦   «         }|S )ad  
    Decorator function to update the RoPE parameters in the forward pass, if the model is using a dynamic RoPE
    (i.e. a RoPE implementation that may recompute its frequencies in the forward pass).

    Args:
        rope_forward (Callable):
            The forward pass of the RoPE implementation.

    Returns:
        The decorated forward pass.
    Nc                 ób  — t          j        |¦  «        dz   }|€#| j        }| j        }d}| j        j        d         }n=| j        |         }t          | |› d�¦  «        }|› d�}| j        j        |         d         }||k    rkt          | |› d�¦  «        s't          |         }	 |	| j        ||dz   |¬¦  «        \  }
}|  	                    |› d	�|
d
¬¦  «         t          | |› d�|
¦  «         dS |                     |¦  «        }|  	                    |› d	�|d
¬¦  «         t          | |› d�|¦  «         dS )zbLongrope uses long factor if sequence is larger than original pretraining length, short otherwise.r	   NÚ Ú original_max_position_embeddingsÚ_original_inv_freqÚ_Ú_long_inv_freq©Úseq_lenÚ
layer_typeÚinv_freqF©Ú
persistentÚlong_inv_freqÚoriginal_inv_freq)ÚtorchÚmaxÚ	rope_typer   ÚconfigÚrope_parametersÚgetattrÚhasattrÚROPE_INIT_FUNCTIONSÚregister_bufferÚsetattrÚto)ÚselfÚposition_idsÚdevicer   r   r   r   Úprefixr   Úrope_init_fnr   r   s               ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/modeling_rope_utils.pyÚlongrope_frequency_updatez6dynamic_rope_update.<locals>.longrope_frequency_update/   s—  € å”)˜LÑ)Ô)¨AÑ-ˆàÐØœˆIØ $Ô 6ÐØˆFØ/3¬{Ô/JÐKmÔ/nÐ,Ð,àœ zÔ2ˆIÝ '¨°Ð.OÐ.OÐ.OÑ PÔ PÐØ"Ð%Ð%Ð%ˆFØ/3¬{Ô/JÈ:Ô/VØ2ô0Ð,ð Ð5Ò5Ð5Ý˜4 JÐ!>Ð!>Ð!>Ñ?Ô?ð Ý2°9Ô=�Ø#/ <Ø”KØØ<¸qÑ@Ø)ð	$ñ $ô $Ñ �˜qð × Ò  FÐ!4Ð!4Ð!4°mÐPUÐ ÑVÔVÐVÝ�D˜VÐ2Ð2Ð2°MÑBÔBÐBÐBÐBð !2× 4Ò 4°VÑ <Ô <ÐØ× Ò  FÐ!4Ð!4Ð!4Ð6GÐTYÐ ÑZÔZÐZÝ�D˜VÐ6Ð6Ð6Ð8IÑJÔJÐJÐJÐJó    c                 óŽ  — t          j        |¦  «        dz   }|€| j        }| j        }| j        }d}n>| j        |         }t          | |› d�| j        ¦  «        }t          | |› d�¦  «        }|› d�}||k    rXt          |         }	 |	| j        |||¬¦  «        \  }
| _        |  	                    |› d�|
d	¬
¦  «         t          | |› d�|¦  «         || j        k     rj|| j        k    ra|                     |¦  «        }|  	                    |› d�|d	¬
¦  «         t          | |› d�|¦  «         t          | |› d�| j        ¦  «         dS dS dS )a  
        dynamic RoPE layers should recompute `inv_freq` in the following situations:
        1 - growing beyond the cached sequence length (allow scaling)
        2 - the current sequence length is in the original scale (avoid losing precision with small sequences)
        r	   Nr   Ú_max_seq_len_cachedr   r   r   r   Fr   Úmax_seq_len_cachedr   )r   r   r   r1   r   r!   r#   r   Úattention_scalingr$   r%   Úoriginal_max_seq_lenr&   )r'   r(   r)   r   r   r   r1   r   r*   r+   r   s              r,   Údynamic_frequency_updatez5dynamic_rope_update.<locals>.dynamic_frequency_updateR   s¹  € õ ”)˜LÑ)Ô)¨AÑ-ˆØÐØœˆIØ!%Ô!8ÐØ $Ô 6ÐØˆFˆFàœ zÔ2ˆIÝ!(¨°*Ð/QÐ/QÐ/QÐSWÔSjÑ!kÔ!kÐÝ '¨°Ð.OÐ.OÐ.OÑ PÔ PÐØ"Ð%Ð%Ð%ˆFàÐ'Ò'Ð'Ý.¨yÔ9ˆLØ/;¨|Ø”ØØØ%ð	0ñ 0ô 0Ñ,ˆH�dÔ,ð × Ò  FÐ!4Ð!4Ð!4°hÈ5Ð ÑQÔQÐQÝ�D˜VÐ7Ð7Ð7¸ÑAÔAÐAà�TÔ.Ò.Ð.Ð3EÈÔHaÒ3aÐ3að !2× 4Ò 4°VÑ <Ô <ÐØ× Ò  FÐ!4Ð!4Ð!4Ð6GÐTYÐ ÑZÔZÐZÝ�D˜VÐ6Ð6Ð6Ð8IÑJÔJÐJÝ�D˜VÐ7Ð7Ð7¸Ô9RÑSÔSÐSÐSÐSð /Ð.Ð3aÐ3ar.   c                 ó°   •— |€| j         n| j         |         }|�d|ini }d|v r ‰| |fd|j        i|¤Ž n|dk    r ‰| |fd|j        i|¤Ž  ‰| ||fi |¤ŽS )Nr   Údynamicr)   Úlongrope)r   r)   )	r'   Úxr(   r   r   Úkwargsr4   r-   Úrope_forwards	         €€€r,   Úwrapperz$dynamic_rope_update.<locals>.wrapperx   s§   ø€ à&0Ð&8�D”N�N¸d¼nÈZÔ>Xˆ	Ø/9Ð/E�, 
Ð+Ð+È2ˆØ˜	Ð!Ð!Ø$Ð$ T¨<ÐSÐSÀÄÐSÈFÐSÐSÐSÐSØ˜*Ò$Ð$Ø%Ð% d¨LÐTÐTÀÄÐTÈVÐTÐTÐTØˆ|˜D ! \Ð<Ð<°VÐ<Ð<Ð<r.   ©Nr   )r:   r;   r4   r-   s   ` @@r,   Údynamic_rope_updater=   "   s{   øøø€ ð!Kð !Kð !Kð !KðF$Tð $Tð $Tð $TõL ˆ<ÑÔð=ð =ð =ð =ð =ð =ð =ñ Ôð=ð €Nr.   r   r   r)   ztorch.devicer   r   Úreturnztorch.Tensorc                 ó°  — |                       ¦   «          |�| j        |         n| j        }|d         }|d         }|                     dd¦  «        }t          | dd¦  «        p| j        | j        z  }t          ||z  ¦  «        }	d}
d|t          j        d|	dt          j	        ¬	¦  «         
                    |t          j        ¬
¦  «        |	z  z  z  }||z  }||
fS )aX  
    Computes the inverse frequencies with linear scaling. Credits to the Reddit user /u/kaiokendev
    Args:
        config ([`~transformers."PreTrainedConfig"`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*): If less than 1.0, inverse frequencies will be returned for
                the first fraction of the head_dim. Defaults to 1.0.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length. Unused for this type of RoPE.

    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
    NÚfactorÚ
rope_thetaÚpartial_rotary_factorç      ð?Úhead_dimr   é   ©Údtype©r)   rG   )Ústandardize_rope_paramsr    Úgetr!   Úhidden_sizeÚnum_attention_headsÚintr   ÚarangeÚint64r&   Úfloat)r   r)   r   r   Úrope_parameters_dictr@   ÚbaserB   rD   ÚdimÚattention_factorr   s               r,   Ú'_compute_linear_scaling_rope_parametersrU   …   sö   € ðB ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐØ! (Ô+€Fð   Ô-€DØ0×4Ò4Ð5LÈcÑRÔRÐÝ�v˜z¨4Ñ0Ô0Ðd°FÔ4FÈ&ÔJdÑ4d€HÝ
ˆhÐ.Ñ.Ñ
/Ô
/€CØÐð �d�uœ|¨A¨s°A½U¼[ÐIÑIÔI×LÒLÐTZÕbgÔbmÐLÑnÔnÐqtÑtÑuÑv€Hð
 �Ñ€HØÐ%Ð%Ð%r.   rD   Úhead_dim_keyc                 óh  — |                       ¦   «          |�| j        |         n| j        }t          | |d¦  «        p| j        | j        z  }|d         }|                     dd¦  «        }|                     dd¦  «        }	d}
t          |	|z  dz  ¦  «        }d|t          j        dd|z  dt          j	        ¬¦  «         
                    |t          j        ¬	¦  «        |z  z  z  }|dz  |z
  }|dk    r8t          j        |t          j        |t          j        |¬
¦  «        fd¬¦  «        }n|}||z  }||
fS )aê  
    Computes the inverse frequencies with proportional RoPE.

    Args:
        config ([`~transformers.PretrainedConfig`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*, defaults to 1.0): The proportion of the embedding dimension
                to apply rotary positional encoding, e.g., [0.0, 0.25, 0.5, 0.75, 1.0]. Unlike other RoPE functions
                that use this parameter, proportional RoPE will always return an encoding that is the size of
                `head_dim`.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length. Unused for this type of RoPE.

    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
    NrA   r@   rC   rB   rE   r   rF   rH   ©rG   r)   )rS   )rI   r    r!   rK   rL   rJ   rM   r   rN   rO   r&   rP   ÚcatÚzerosÚfloat32)r   r)   r   r   rV   rQ   rD   rR   r@   Úrope_proportionrT   Úrope_anglesÚinv_freq_rotatedÚnope_anglesr   s                  r,   Ú%_compute_proportional_rope_parametersr`   »   sd  € ðJ ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐå�v˜|¨TÑ2Ô2Ðf°fÔ6HÈFÔLfÑ6f€HØ Ô-€DØ!×%Ò% h°Ñ4Ô4€FØ*×.Ò.Ð/FÈÑLÔL€OàÐå�o¨Ñ0°AÑ5Ñ6Ô6€KàØÝŒL˜˜A ™O¨Qµe´kÐBÑBÔB×EÒEÈVÕ[`Ô[fÐEÑgÔgÐjrÑrñ	tñÐð
 ˜a‘- +Ñ-€KØ�Q‚€Ý”9à Ý”˜K­u¬}ÀVÐLÑLÔLðð ð
ñ 
ô 
ˆˆð $ˆà�Ñ€HØÐ%Ð%Ð%r.   c                 óÆ  — |                       ¦   «          |�| j        |         n| j        }|d         }|                     dd¦  «        }t          | d| j        | j        z  ¦  «        }t          ||z  ¦  «        }|d         }	d}
|€| j        }nit          |t          j
        ¦  «        r:t          j        |t          j        | j        |j        |j        ¬¦  «        ¦  «        }nt          || j        ¦  «        }||	|z  | j        z  |	dz
  z
  ||d	z
  z  z  z  }d|t          j        d
|d	t          j        ¬¦  «                             |t          j        ¬¦  «        |z  z  z  }||
fS )a
  
    Computes the inverse frequencies with NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla

    Args:
        config ([`~transformers."PreTrainedConfig"`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.
            *   max_position_embeddings (`int`): The default sequence length used to update the dynamic RoPE at
                inference time
            *   rope_parameters (`dict[str, float]`): The standard RoPE scaling parameters, from which `factor`
                will be accessed. The value of `factor` is used to determine the new base frequency, along with the
                current sequence length (seq_len), the maximum positional embeddings (max_position_embeddings), and the
                computed dimensionality (dim) of the rotary embeddings. If seq_len <= max_position_embeddings, this
                factor has no effect. If seq_len <= max_position_embeddings, this factor effectively stretches the
                context window using an exponent derived from `dim`.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*): If less than 1.0, inverse frequencies will be returned for
                the first fraction of the head_dim. Defaults to 1.0.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length, used to update the dynamic RoPE at inference time. If `None` or shorter than
            max_position_embeddings, this value will be overridden by max_position_embeddings.

    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
    NrA   rB   rC   rD   r@   rX   r	   rE   r   rF   rH   )rI   r    rJ   r!   rK   rL   rM   Úmax_position_embeddingsÚ
isinstancer   ÚTensorÚmaximumÚtensorrG   r)   r   rN   rO   r&   rP   )r   r)   r   r   rQ   rR   rB   rD   rS   r@   rT   r   s               r,   Ú_compute_dynamic_ntk_parametersrg     s‚  € ðV ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐà Ô-€DØ0×4Ò4Ð5LÈcÑRÔRÐÝ�v˜z¨6Ô+=ÀÔA[Ñ+[Ñ\Ô\€HÝ
ˆhÐ.Ñ.Ñ
/Ô
/€CØ! (Ô+€FØÐð €ØÔ0ˆˆÝ	�G�Uœ\Ñ	*Ô	*ð ?Ý”-ØÝŒL˜Ô7¸w¼}ÐU\ÔUcÐdÑdÔdñ
ô 
ˆˆõ
 �g˜vÔ=Ñ>Ô>ˆð �F˜WÑ$ vÔ'EÑEÈ&ÐSTÉ*ÑUÐ[^ÐbeÐhiÑbiÑ[jÑkÑk€DØ�d�uœ|¨A¨s°A½U¼[ÐIÑIÔI×LÒLÐTZÕbgÔbmÐLÑnÔnÐqtÑtÑuÑv€HØÐ%Ð%Ð%r.   c                 ó  ‡— |                       ¦   «          |�| j        |         n| j        }|d         }|                     dd¦  «        }t          | d| j        | j        z  ¦  «        }t          ||z  ¦  «        }|d         }	|                     d¦  «        }
|                     d¦  «        }|                     d	¦  «        }|d
         }|	€
| j        |z  }	dd„}|
€6|r)|r't           ||	|¦  «         ||	|¦  «        z  ¦  «        }
n ||	¦  «        }
|                     d¦  «        pd}|                     d¦  «        pd}d„ Šˆfd„}d„ }|t          j
        d|d¦  «                             |t          j        ¬¦  «        |z  z  }d|z  }d|	|z  z  }| j                             dd¦  «        } |||||||¦  «        \  }}d ||||dz  ¦  «                             |t          j        ¬¦  «        z
  }|d|z
  z  ||z  z   }||
fS )aD  
    Computes the inverse frequencies with NTK scaling. Please refer to the
    [original paper](https://huggingface.co/papers/2309.00071)

    Args:
        config ([`~transformers."PreTrainedConfig"`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.
            *   max_position_embeddings (`int`): The maximum length of the positional embeddings.
            *   rope_parameters (`dict[str, float | int]`): The standard RoPE scaling parameters, from which the following
                keys will be accessed:
                *   `attention_factor` (`float`, *optional*): The scaling factor to be applied to the computed cos/sin.
                    If None, the value is inferred from `factor`, `mscale`, and `mscale_all_dim` as available.
                *   `beta_fast` (`float`, *optional*, defaults to 32): Parameter to set the boundary for extrapolation
                    (only) in the linear ramp function.
                *   `beta_slow` (`float`, *optional*, defaults to 1): Parameter to set the boundary for interpolation
                    (only) in the linear ramp function.
                *   `factor` (`float`, *optional*): The scaling factor applied when interpolating the position IDs to
                    extend the possible context length. Additionally, if `attention_factor` is None, the log of this
                    value is used to compute a value for `attention_factor`, possibly in conjunction with `mscale` and
                    `mscale_all_dim`, if provided.
                *   `mscale` (`float`, *optional*): If `attention_factor` is None and both `mscale` and
                    `mscale_all_dim` are provided, `mscale` acts scalar augmenting `log(factor)` when computing the
                    numerator for the inferred value of `attention_factor`. If not provided, `attention_factor` will be
                    calculated based on `factor` only.
                *   `mscale_all_dim` (`float`, *optional*): If `attention_factor` is None and both `mscale` and
                    `mscale_all_dim` are provided, `mscale_all_dim` acts scalar augmenting `log(factor)` when computing
                    the denominator for the inferred value of `attention_factor`. If not provided, `attention_factor`
                    will be calculated based on `factor` only.
                *   `original_max_position_embeddings` (`int`): The original max position embeddings used during pretraining.
                *   `truncate` (`bool`, *optional*): Whether to truncate the correction range.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*, defaults to 1.0): If less than 1.0, inverse frequencies
                will be returned for the first fraction of the head_dim.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length. Unused for this type of RoPE.

    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin.
    NrA   rB   rC   rD   r@   rT   ÚmscaleÚmscale_all_dimr   r	   c                 óL   — | dk    rdS d|z  t          j        | ¦  «        z  dz   S )Nr	   rC   gš™™™™™¹?)ÚmathÚlog)Úscaleri   s     r,   Ú
get_mscalez,_compute_yarn_parameters.<locals>.get_mscale•  s,   € Ø�AŠ:ˆ:Ø�3Ø�V‰|�dœh u™oœoÑ-°Ñ3Ð3r.   Ú	beta_fasté    Ú	beta_slowc                 ó†   — |t          j        || dz  t           j        z  z  ¦  «        z  dt          j        |¦  «        z  z  S )zPInverse dimension formula to find the dimension based on the number of rotationsrE   )rl   rm   Úpi)Únum_rotationsrS   rR   rb   s       r,   Úfind_correction_dimz5_compute_yarn_parameters.<locals>.find_correction_dim§  sA   € à•d”hÐ6¸-È!Ñ:KÍdÌgÑ:UÑVÑWÔWÑWÐ\]Õ`dÔ`hÐimÑ`nÔ`nÑ\nÑoÐor.   c                 óÖ   •—  ‰| |||¦  «        } ‰||||¦  «        }|r(t          j        |¦  «        }t          j        |¦  «        }t          |d¦  «        t	          ||dz
  ¦  «        fS )z.Find dimension range bounds based on rotationsr   r	   )rl   ÚfloorÚceilr   Úmin)	Úlow_rotÚhigh_rotrS   rR   rb   ÚtruncateÚlowÚhighrv   s	           €r,   Úfind_correction_rangez7_compute_yarn_parameters.<locals>.find_correction_range«  st   ø€ à!Ð! '¨3°Ð6MÑNÔNˆØ"Ð" 8¨S°$Ð8OÑPÔPˆØð 	#Ý”*˜S‘/”/ˆCÝ”9˜T‘?”?ˆDÝ�3˜‰{Œ{�C  c¨A¡gÑ.Ô.Ð.Ð.r.   c                 óš   — | |k    r|dz  }t          j        |t           j        ¬¦  «        | z
  || z
  z  }t          j        |dd¦  «        }|S )Ngü©ñÒMbP?rF   r   r	   )r   rN   r[   Úclamp)rz   r   rS   Úlinear_funcÚ	ramp_funcs        r,   Úlinear_ramp_factorz4_compute_yarn_parameters.<locals>.linear_ramp_factor´  sQ   € Ø�#Š:ˆ:Ø�5‰LˆCå”| C­u¬}Ð=Ñ=Ô=ÀÑCÈÈcÉ	ÑRˆÝ”K ¨Q°Ñ2Ô2ˆ	ØÐr.   r   rE   rH   r}   T)r	   )rI   r    rJ   r!   rK   rL   rM   rb   rP   r   rN   r&   )r   r)   r   r   rQ   rR   rB   rD   rS   r@   rT   ri   rj   r   ro   rp   rr   r€   r…   Ú	pos_freqsÚinv_freq_extrapolationÚinv_freq_interpolationr}   r~   r   Úinv_freq_extrapolation_factorr   rv   s                              @r,   Ú_compute_yarn_parametersrŠ   G  s±  ø€ ðt ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐà Ô-€DØ0×4Ò4Ð5LÈcÑRÔRÐÝ�v˜z¨6Ô+=ÀÔA[Ñ+[Ñ\Ô\€HÝ
ˆhÐ.Ñ.Ñ
/Ô
/€Cà! (Ô+€FØ+×/Ò/Ð0BÑCÔCÐØ!×%Ò% hÑ/Ô/€FØ)×-Ò-Ð.>Ñ?Ô?€NØ';Ð<^Ô'_Ð$ð
 €~ØÔ/Ð2RÑRˆð4ð 4ð 4ð 4ð ÐØð 	2�nð 	2Ý$ Z Z°¸Ñ%?Ô%?À*À*ÈVÐUcÑBdÔBdÑ%dÑeÔeÐÐà)˜z¨&Ñ1Ô1Ðð %×(Ò(¨Ñ5Ô5Ð;¸€IØ$×(Ò(¨Ñ5Ô5Ð:¸€Iðpð pð pð/ð /ð /ð /ð /ðð ð ð �œ a¨¨aÑ0Ô0×3Ò3¸6ÍÌÐ3ÑUÔUÐX[Ñ[Ñ\€IØ  9™_ÐØ  F¨YÑ$6Ñ7ÐàÔ%×)Ò)¨*°dÑ;Ô;€HØ%Ð% i°¸CÀÐGgÐiqÑrÔr�I€Cˆð %&Ð(:Ð(:¸3ÀÀcÈQÁhÑ(OÔ(O×(RÒ(RÐZ`ÕhmÔhsÐ(RÑ(tÔ(tÑ$tÐ!à !Ð&CÑ"CÑDØ
 Ð#@Ñ
@ñ	Að ð Ð%Ð%Ð%r.   c                 óD  — |                       ¦   «          |�| j        |         n| j        }|d         }|                     dd¦  «        }t          | d| j        | j        z  ¦  «        }t          ||z  ¦  «        }|d         }	|d         }
|                     d¦  «        }|                     d	¦  «        }|d
         }|€
| j        |z  }|€G|dk    rd}n>t          j	        dt          j
        |¦  «        t          j
        |¦  «        z  z   ¦  «        }|r(||k    r"t          j        |	t          j        |¬¦  «        }n!t          j        |
t          j        |¬¦  «        }t          j        d|dt          j        |¬¦  «                             ¦   «         |z  }d|||z  z  z  }||fS )a  
    Computes the inverse frequencies with LongRoPE scaling. Please refer to the
    [original implementation](https://github.com/microsoft/LongRoPE)

    Args:
        config ([`~transformers."PreTrainedConfig"`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.
            *   max_position_embeddings (`int`): The maximum length of the positional embeddings.
            *   original_max_position_embeddings (`int`, *optional*): The original max position embeddings used during
                pretraining. If not provided, defaults to `max_position_embeddings`.
            *   rope_parameters (`dict[str, float]`): The standard RoPE scaling parameters, from which the following keys
                will be accessed:
                *   `attention_factor` (`float`, *optional*): The scaling factor to be applied on the attention
                    computation. If unspecified, it defaults to value recommended by the implementation, inferred from
                    the value of `factor`.
                *   `factor` (`float`, *optional*): The scaling factor to apply to the RoPE embeddings. If both
                    `max_position_embeddings` and `original_max_position_embeddings` are provided, this value will be
                    overridden s the ratio between those values.
                *   `long_factor` (`float`, *optional*): The scale factor applied when computing the inverse
                    frequencies if `seq_len` is provided and greater than `original_max_position_embeddings`.
                *   `short_factor` (`float`, *optional*): The scale factor applied when computing the inverse
                    frequencies if `seq_len` is None or less-than-or-equal-to `original_max_position_embeddings`.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*, defaults to 1.0): If less than 1.0, inverse frequencies
                will be returned for the first fraction of the head_dim.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length.

    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin.
    NrA   rB   rC   rD   Úlong_factorÚshort_factorr@   rT   r   r	   rX   r   rE   )rI   r    rJ   r!   rK   rL   rM   rb   rl   Úsqrtrm   r   rf   r[   rN   rO   rP   )r   r)   r   r   rQ   rR   rB   rD   rS   rŒ   r�   r@   rT   r   Úext_factorsÚinv_freq_shaper   s                    r,   Ú_compute_longrope_parametersr‘   Î  s¿  € ðd ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐà Ô-€DØ0×4Ò4Ð5LÈcÑRÔRÐÝ�v˜z¨6Ô+=ÀÔA[Ñ+[Ñ\Ô\€HÝ
ˆhÐ.Ñ.Ñ
/Ô
/€Cà& }Ô5€KØ'¨Ô7€LØ!×%Ò% hÑ/Ô/€FØ+×/Ò/Ð0BÑCÔCÐØ';Ð<^Ô'_Ð$ð
 €~ØÔ/Ð2RÑRˆð ÐØ�SŠ=ˆ=Ø"ÐÐå#œy¨­T¬X°fÑ-=Ô-=ÅÄÐIiÑ@jÔ@jÑ-jÑ)jÑkÔkÐð ð U�7Ð=Ò=Ð=Ý”l ;µe´mÈFÐSÑSÔSˆˆå”l <µu´}ÈVÐTÑTÔTˆÝ”\ ! S¨!µ5´;ÀvÐNÑNÔN×TÒTÑVÔVÐY\Ñ\€NØ�k D¨.Ñ$8Ñ8Ñ9€HàÐ%Ð%Ð%r.   c                 óÐ  — |                       ¦   «          |�| j        |         n| j        }|d         }|                     dd¦  «        }t          | dd¦  «        p| j        | j        z  }t          ||z  ¦  «        }d}	d|t          j        d|dt          j	        ¬¦  «         
                    |t          j        ¬	¦  «        |z  z  z  }
|d
         }|d         }|d         }|d         }||z  }||z  }dt          j        z  |
z  }t          j        ||k    |
|z  |
¦  «        }||z  |z
  ||z
  z  }d|z
  |z  |z  ||z  z   }||k      ||k     z  }t          j        |||¦  «        }||	fS )a°
  
    Computes the inverse frequencies for llama 3.1.

    Args:
        config ([`~transformers."PreTrainedConfig"`]):
            The model configuration. This function assumes that the config will provide at least the following
            properties:

            *   rope_theta (`float`, *optional*): The base wavelength from which the inverse frequencies will be derived. Defaults to `config.default_theta` if omitted.
            *   hidden_size (`int`): The numerator when deriving a head_dim, if not provided directly.
            *   num_attention_heads (`int`): The denominator when deriving a head_dim, if not provided directly.
            *   rope_parameters (`dict[str, float | int]`): The standard RoPE scaling parameters, from which the following
                keys will be accessed:
                *   `factor` (`float`, *optional*): The scaling factor applied to the inverse frequencies when 1) the
                    wavelength is greater than `low_freq_wavelen` prior to smoothing, and 2) to all inverse frequencies
                    during smoothing.
                *   `high_freq_factor` (`float`): The scale factor used to compute `high_freq_wavelen` and
                    the value for the denominator of the smoothing factor prior to the `low_freq_factor` shift.
                *   `low_freq_factor` (`float`): The scale factor used to compute `low_freq_wavelen` and
                    the shift applied to the numerator and denominator of the smoothing factor.
                    frequencies if `seq_len` is None or less-than-or-equal-to `original_max_position_embeddings`.
                *   `original_max_position_embeddings` (`int`): The original max position embeddings used
                    during pretraining. If not provided, the function falls back to `max_position_embeddings`.

            Additionally, this function will make use of the following properties if they are found in the config:

            *   head_dim (`int`, *optional*): The size of the key-value heads in the model. If None, this value will be
                derived as hidden_size // num_attention_heads.
            *   partial_rotary_factor (`float`, *optional*): If less than 1.0, inverse frequencies will be returned for
                the first fraction of the head_dim. Defaults to 1.0.
        device (`torch.device`):
            The device to use for initialization of the inverse frequencies.
        seq_len (`int`, *optional*):
            The current sequence length. Unused for this type of RoPE.
    Returns:
        Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
        post-processing scaling factor applied to the computed cos/sin.
    NrA   rB   rC   rD   r   rE   rF   rH   r@   Úlow_freq_factorÚhigh_freq_factorr   r	   )rI   r    rJ   r!   rK   rL   rM   r   rN   rO   r&   rP   rl   rt   Úwhere)r   r)   r   r   rQ   rR   rB   rD   rS   rT   r   r@   r“   r”   Úold_context_lenÚlow_freq_wavelenÚhigh_freq_wavelenÚwavelenÚinv_freq_llamaÚsmooth_factorÚsmoothed_inv_freqÚis_medium_freqs                         r,   Ú_compute_llama3_parametersrž   &  s¿  € ðZ ×"Ò"Ñ$Ô$Ð$ØAKÐAW˜6Ô1°*Ô=Ð=Ð]cÔ]sÐð   Ô-€DØ0×4Ò4Ð5LÈcÑRÔRÐÝ�v˜z¨4Ñ0Ô0Ðd°FÔ4FÈ&ÔJdÑ4d€HÝ
ˆhÐ.Ñ.Ñ
/Ô
/€CØÐð �d�uœ|¨A¨s°A½U¼[ÐIÑIÔI×LÒLÐTZÕbgÔbmÐLÑnÔnÐqtÑtÑuÑv€Hà! (Ô+€FØ*Ð+<Ô=€OØ+Ð,>Ô?ÐØ*Ð+MÔN€Oà&¨Ñ8ÐØ'Ð*:Ñ:Ðà•$”'‰k˜HÑ$€Gõ ”[ Ð+;Ò!;¸XÈÑ=NÐPXÑYÔY€Nà$ wÑ.°Ñ@ÐEUÐXgÑEgÑh€MØ˜]Ñ*¨nÑ<¸vÑEÈÐXfÑHfÑfÐØÐ!2Ò2Ð3¸ÐBRÒ8RÐ6SÑS€NÝ”[ Ð1BÀNÑSÔS€NàÐ+Ð+Ð+r.   )Úlinearr6   Úyarnr7   Úllama3Úproportional.r#   c                   óì   — e Zd ZU dZedz  ed<   edz  ed<   edz  ed<   edz  ed<   edz  ed<   edz  ed<   edz  ed	<   edz  ed
<   ee         dz  ed<   ee         dz  ed<   edz  ed<   edz  ed<   dS )ÚRopeParametersu  
    Args:
        rope_theta (`float`, *optional*, defaults to `RotaryEmbeddingConfigMixin.default_theta`):
            The base period of the RoPE embeddings. Optional in serialized configs â€” if omitted,
            the model's `default_theta` (typically 10000.0) is used.
        rope_type (`str`, *optional*, defaults to "default"):
            The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
            'llama3'], with 'default' being the original RoPE implementation.
        partial_rotary_factor (`float`, *optional*):
            The percentage of the query and key head embedding on which RoPE will be applied.
        factor (`float`, *optional*):
            Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
            most scaling types, a `factor` of x will enable the model to handle sequences of length x *
            original maximum pre-trained length.
        original_max_position_embeddings (`int`, *optional*):
            Used with 'yarn', 'longrope' and 'llama3'. The original max position embeddings used during
            pretraining.
        attention_factor (`float`, *optional*):
            Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
            computation. If unspecified, it defaults to value recommended by the implementation, using the
            `factor` field to infer the suggested value.
        beta_fast (`float`, *optional*):
            Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
            ramp function. If unspecified, it defaults to 32.
        beta_slow (`float`, *optional*):
            Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
            ramp function. If unspecified, it defaults to 1.
        short_factor (`list[float]`, *optional*):
            Only used with 'longrope'. The scaling factor to be applied to short contexts (<
            `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
            size divided by the number of attention heads divided by 2
        long_factor (`list[float]`, *optional*):
            Only used with 'longrope'. The scaling factor to be applied to long contexts (<
            `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
            size divided by the number of attention heads divided by 2
        low_freq_factor (`float`, *optional*):
            Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
        high_freq_factor (`float`, *optional*):
            Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
    NrA   r   rB   r@   r   rT   rp   rr   r�   rŒ   r“   r”   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__rP   Ú__annotations__ÚstrrM   Úlist© r.   r,   r¤   r¤   ‚  sé   € € € € € € ð'ð 'ðR ˜‘ÐÐÑØ�T‰zÐÐÑØ  4™<Ð'Ð'Ñ'Ø�D‰LÐÐÑØ&)¨D¡jÐ0Ð0Ñ0Ø˜d‘lÐ"Ð"Ñ"Ø�t‰|ÐÐÑØ�t‰|ÐÐÑØ�u”+ Ñ$Ð$Ð$Ñ$Ø�e”˜tÑ#Ð#Ð#Ñ#Ø˜T‘\Ð!Ð!Ñ!Ø˜d‘lÐ"Ð"Ñ"Ð"Ð"r.   r¤   c                   ó$  — e Zd ZdZdZ e¦   «         Zd„ Zd„ Zdd„Z	dd	e
d
edz  fd„Zdd	e
d
edz  fd„Zdd	e
d
edz  fd„Zdd	e
d
edz  fd„Zdd	e
d
edz  fd„Zdd	e
d
edz  fd„Zdd	e
d
edz  fd„Ze	 	 ddededededz  d
edz  f
d„¦   «         ZdS )ÚRotaryEmbeddingConfigMixinz[
    A Mixin containing the functionality to standardize and validate RoPE parameters.
    g     ˆÃ@c                 óì  — |                      dd ¦  «        }|p| j        | _        | j        �| j        ni | _        |                      dt          | d| j        ¦  «        ¦  «        }| j                             d|¦  «         |                     dt          | dd ¦  «        ¦  «        }|�:| j                             d|¦  «         t          | j        pg ¦  «        dhz  | _        |                      ¦   «          |S )NÚrope_scalingrA   rB   )	Úpopr    r!   Údefault_thetaÚ
setdefaultrJ   ÚsetÚignore_keys_at_rope_validationrI   )r'   r9   r°   rA   rB   s        r,   Úconvert_rope_params_to_dictz6RotaryEmbeddingConfigMixin.convert_rope_params_to_dictÂ  sÿ   € Ø—z’z .°$Ñ7Ô7ˆØ+ÐC¨tÔ/CˆÔØ7;Ô7KÐ7W˜tÔ3Ð3Ð]_ˆÔð —Z’Z ­g°d¸LÈ$ÔJ\Ñ.]Ô.]Ñ^Ô^ˆ
ØÔ×'Ò'¨°jÑAÔAÐAà &§
¢
Ð+BÅGÈDÐRiÐkoÑDpÔDpÑ qÔ qÐØ Ð,ØÔ ×+Ò+Ð,CÐEZÑ[Ô[Ð[Ý25°dÔ6YÐ6_Ð]_Ñ2`Ô2`Ø'ðdñ 3ˆDÔ/ð 	×$Ò$Ñ&Ô&Ð&Øˆr.   c                 óÊ  — t          | dd¦  «        }t          | dd¦  «        }t          | dd¦  «        pi }t          | dd¦  «        }|s|st                               d¦  «         dS |�:|i k    s4t          |                     ¦   «         ¦  «                             |¦  «        s’|                     d|                     dd	¦  «        ¦  «         |                     d|¦  «         |�||d<   |d         d
v r@t          | d¦  «        r| j	        | j
        d<   nÈ| j
                             d| j        ¦  «         n§t          |¦  «        D ]—}||                              d||                              dd	¦  «        ¦  «         ||                              d|¦  «         |�|||         d<   ||         d         d
v r&| j
        |                              d| j        ¦  «         Œ˜|| _
        dS )zê
        Helper to standardize the config's rope params field by ensuring the params are defined for each
        later type. For old model the fn will duplicate a single rope param in each layer type (backward compatibility)
        rA   NrB   r    Úlayer_typeszG`standardize_rope_params` was called but no RoPE parameters were found.r   ÚtypeÚdefault)r¡   r    r7   r   )r!   ÚloggerÚwarningr´   ÚkeysÚissubsetr³   rJ   r"   r   r    rb   )r'   rA   rB   r    r¸   r   s         r,   rI   z2RotaryEmbeddingConfigMixin.standardize_rope_paramsÙ  s)  € õ ˜T <°Ñ6Ô6ˆ
Ý '¨Ð.EÀtÑ LÔ LÐÝ! $Ð(9¸4Ñ@Ô@ÐFÀBˆÝ˜d M°4Ñ8Ô8ˆð  ð  	 :ð  	å�NŠNÐdÑeÔeÐeØˆFàÐ  O°rÒ$9Ð$9ÅÀ_×EYÒEYÑE[ÔE[ÑA\ÔA\×AeÒAeÐfqÑArÔArÐ$9Ø×&Ò& {°O×4GÒ4GÈÐPYÑ4ZÔ4ZÑ[Ô[Ð[Ø×&Ò& |°ZÑ@Ô@Ð@Ø$Ð0Ø;P�Ð 7Ñ8ð ˜{Ô+Ð/MÐMÐMÝ˜4Ð!CÑDÔDð vð PTÔOt�DÔ(Ð)KÑLÐLàÔ(×3Ò3Ð4VÐX\ÔXtÑuÔuÐuøõ " +Ñ.Ô.ð 	ð 	�
Ø 
Ô+×6Ò6°{ÀOÐT^ÔD_×DcÒDcÐdjÐluÑDvÔDvÑwÔwÐwØ 
Ô+×6Ò6°|ÀZÑPÔPÐPØ(Ð4ØK`�O JÔ/Ð0GÑHà" :Ô.¨{Ô;Ð?]Ð]Ð]ØÔ(¨Ô4×?Ò?Ø:¸DÔ<Xñô ð øð  /ˆÔÐÐr.   r'   r   c                 óê  — t          | dd¦  «        }|sdS t          | dd¦  «        �:t          |                     ¦   «         ¦  «                             | j        ¦  «        rnd|i}|                     ¦   «         D ]y}|                     d|                     dd¦  «        ¦  «        }t          | d|› d	�d¦  «        }||d<   |� ||| j        ¬
¦  «         Œ[t           	                    d|› d�¦  «         ŒzdS )zY
        Validate the RoPE config arguments, given a `"PreTrainedConfig"` object
        r    Nr¸   Úfull_attentionr   r¹   rº   Ú
_validate_Ú_rope_parameters©Úignore_keyszMMissing validation function in 'RotaryEmbeddingConfigMixin' for 'rope_type'='ú')
r!   r´   r½   r¾   r¸   ÚvaluesrJ   rµ   r»   r¼   )r'   rQ   r    r   Úvalidation_fns        r,   Úvalidate_ropez(RotaryEmbeddingConfigMixin.validate_rope	  s5  € õ  ' tÐ->ÀÑEÔEÐØ#ð 	ØˆFå�4˜¨Ñ-Ô-Ð9½cÐBV×B[ÒB[ÑB]ÔB]Ñ>^Ô>^×>gÒ>gØÔñ?
ô ?
Ð9ð à$4Ð6JÐ#KÐ à3×:Ò:Ñ<Ô<ð 
	ð 
	ˆOØ'×+Ò+¨K¸×9LÒ9LÈVÐU^Ñ9_Ô9_Ñ`Ô`ˆIÝ# DÐ*R°yÐ*RÐ*RÐ*RÐTXÑYÔYˆMØ+4ˆO˜KÑ(àÐ(Ø�˜o¸4Ô;^Ð_Ñ_Ô_Ð_Ð_å—’ØpÐdmÐpÐpÐpñô ð ð ð
	ð 
	r.   Nr    rÄ   c                 ó˜   — dh}dh}t          |                     ¦   «         ¦  «        }|d         }|                      |||||¬¦  «         d S )Nr   rA   ©Úoptional_keysrÄ   )r´   r½   Ú_check_received_keys)r'   r    rÄ   Úrequired_keysrË   Úreceived_keysr   s          r,   Ú!_validate_default_rope_parametersz<RotaryEmbeddingConfigMixin._validate_default_rope_parameters&  sf   € Ø$˜ˆØ%˜ˆÝ˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ# KÔ0ˆ	Ø×!Ò!Ø�} mÀ=Ð^ið 	"ñ 	
ô 	
ð 	
ð 	
ð 	
r.   c                 ó0  — ddh}dh}t          |                     ¦   «         ¦  «        }|d         }|                      |||||¬¦  «         |d         }|�"t          |t          t
          f¦  «        r|dk     rt                               d|› �¦  «         d S d S ©Nr   r@   rA   rÊ   rC   úB`rope_parameters`'s factor field must be a float or int >= 1, got ©r´   r½   rÌ   rc   rP   rM   r»   r¼   ©r'   r    rÄ   rÍ   rË   rÎ   r   r@   s           r,   Ú _validate_linear_rope_parametersz;RotaryEmbeddingConfigMixin._validate_linear_rope_parameters/  óµ   € Ø$ hÐ/ˆØ%˜ˆÝ˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ# KÔ0ˆ	Ø×!Ò!Ø�} mÀ=Ð^ið 	"ñ 	
ô 	
ð 	
ð ! Ô*ˆØˆ>¥¨FµU½C°LÑ!AÔ!Aˆ>ÀVÈcÂ\À\Ý�NŠNÐhÐ`fÐhÐhÑiÔiÐiÐiÐið FRÀ\r.   c                 ó0  — ddh}dh}t          |                     ¦   «         ¦  «        }|d         }|                      |||||¬¦  «         |d         }|�"t          |t          t
          f¦  «        r|dk     rt                               d|› �¦  «         d S d S rÑ   rÓ   rÔ   s           r,   Ú!_validate_dynamic_rope_parametersz<RotaryEmbeddingConfigMixin._validate_dynamic_rope_parameters<  rÖ   r.   c           	      óî  — h d£}h d£}t          |                     ¦   «         ¦  «        }|d         }|                      |||||¬¦  «         |d         }|�"t          |t          t
          f¦  «        r|dk     rt                               d|› �¦  «         |                     d¦  «        }|�8t          |t          ¦  «        r|d	k     rt                               d
|› �¦  «         |                     d¦  «        }	|	�9t          |	t          t
          f¦  «        st                               d|	› �¦  «         |                     d¦  «        }
|
�9t          |
t          t
          f¦  «        st                               d|
› �¦  «         |	pd|
pdk     r!t                               d|	› d|
› d�¦  «         |d         }| j	        |z  }||k    r,|dk    r(t           
                    d|› d|› d|› d�¦  «         d S d S d S )N>   r@   r   r   >   ri   r}   rp   rr   rA   rj   rT   r   rÃ   r@   rC   rÒ   rT   r   zO`rope_parameters`'s attention_factor field must be a float greater than 0, got rp   z@`rope_parameters`'s beta_fast field must be a float or int, got rr   z@`rope_parameters`'s beta_slow field must be a float or int, got rq   r	   zR`rope_parameters`'s beta_fast field must be greater than beta_slow, got beta_fast=z( (defaults to 32 if None) and beta_slow=z (defaults to 1 if None)r   zKThe explicitly set RoPE scaling factor (config.rope_parameters['factor'] = zä) does not match the ratio implicitly set by other parameters (implicit factor = post-yarn context length / pre-yarn context length = config.max_position_embeddings / config.rope_parameters['original_max_position_embeddings'] = z). Using the explicit factor (z�) in YaRN. This may cause unexpected behaviour in model usage, please correct the 'original_max_position_embeddings' fields in the model config.)r´   r½   rÌ   rc   rP   rM   r»   r¼   rJ   rb   Úwarning_once)r'   r    rÄ   rÍ   rË   rÎ   r   r@   rT   rp   rr   r   Úimplicit_factors                r,   Ú_validate_yarn_rope_parametersz9RotaryEmbeddingConfigMixin._validate_yarn_rope_parametersI  s†  € ØSÐSÐSˆð
ð 
ð 
ˆõ ˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ# KÔ0ˆ	Ø×!Ò! )¨]¸MÈ=ÐfqÐ!ÑrÔrÐrà  Ô*ˆØˆ>¥¨FµU½C°LÑ!AÔ!Aˆ>ÀVÈcÂ\À\Ý�NŠNÐhÐ`fÐhÐhÑiÔiÐià*×.Ò.Ð/AÑBÔBÐØÐ'µÐ<LÍeÑ1TÔ1TÐ'ÐXhÐklÒXlÐXlÝ�NŠNØtÐbrÐtÐtñô ð ð $×'Ò'¨Ñ4Ô4ˆ	ØÐ ­°IÅÅs¸|Ñ)LÔ)LÐ Ý�NŠNÐiÐ^gÐiÐiÑjÔjÐjØ#×'Ò'¨Ñ4Ô4ˆ	ØÐ ­°IÅÅs¸|Ñ)LÔ)LÐ Ý�NŠNÐiÐ^gÐiÐiÑjÔjÐjàˆO˜ 	 ¨QÒ/Ð/Ý�NŠNð^Ðenð ^ð ^Ø:Cð^ð ^ð ^ñô ð ð ,;Ð;]Ô+^Ð(ØÔ6Ð9YÑYˆØ˜fÒ$Ð$¨¸AÒ)=Ð)=Ý×Òð~Ð^dð ~ð ~ð #ð	~ð ~ð CIð	~ð ~ð ~ñô ð ð ð ð %Ð$Ð)=Ð)=r.   c                 ó¨  — h d£}h d£}t          |                     ¦   «         ¦  «        }|d         }|                      |||||¬¦  «         |                     dd¦  «        }t	          | d| j        | j        z  ¦  «        }t          ||z  ¦  «        }	|                     d¦  «        }
t          |
t          ¦  «        rt          d	„ |
D ¦   «         ¦  «        st                               d
|
› �¦  «         t          |
¦  «        |	dz  k    r0t                               d|	dz  › dt          |
¦  «        › �¦  «         |                     d¦  «        }t          |t          ¦  «        rt          d„ |D ¦   «         ¦  «        st                               d|› �¦  «         t          |¦  «        |	dz  k    r0t                               d|	dz  › dt          |¦  «        › �¦  «         |                     d¦  «        }|d         }|€|�t                               d¦  «         n^|€|€t                               d¦  «         n?t          |t          t          f¦  «        r|dk     rt                               d|› �¦  «         |                     d¦  «        }|�At          |t          t          f¦  «        r|dk     r!t                               d|› �¦  «         d S d S d S )N>   r   rŒ   r�   r   >   r@   rA   rT   r   rÃ   rB   rC   rD   r�   c              3   óN   K  — | ] }t          |t          t          f¦  «        V — Œ!d S r<   ©rc   rM   rP   ©Ú.0r8   s     r,   ú	<genexpr>zPRotaryEmbeddingConfigMixin._validate_longrope_rope_parameters.<locals>.<genexpr>‰  s1   è è € Ð6iÐ6iÐWXµzÀ!ÅcÍ5À\Ñ7RÔ7RÐ6iÐ6iÐ6iÐ6iÐ6iÐ6ir.   zF`rope_parameters`'s short_factor field must be a list of numbers, got rE   z8`rope_parameters`'s short_factor field must have length z, got rŒ   c              3   óN   K  — | ] }t          |t          t          f¦  «        V — Œ!d S r<   rß   rà   s     r,   râ   zPRotaryEmbeddingConfigMixin._validate_longrope_rope_parameters.<locals>.<genexpr>‘  s1   è è € Ð5gÐ5gÐVWµjÀÅSÍ%ÀLÑ6QÔ6QÐ5gÐ5gÐ5gÐ5gÐ5gÐ5gr.   zE`rope_parameters`'s long_factor field must be a list of numbers, got z7`rope_parameters`'s long_factor field must have length r@   r   av  This model config has set a `rope_parameters['original_max_position_embeddings']` field, to be used together with `max_position_embeddings` to determine a scaling factor. Please set the `factor` field of `rope_parameters`with this ratio instead -- we recommend the use of this field over `original_max_position_embeddings`, as it is compatible with most model architectures.z4Missing required keys in `rope_parameters`: 'factor'rÒ   rT   g        zV`rope_parameters`'s attention_factor field must be a float or int greater than 0, got )r´   r½   rÌ   rJ   r!   rK   rL   rM   rc   r«   Úallr»   r¼   ÚlenrÚ   rP   )r'   r    rÄ   rÍ   rË   rÎ   r   rB   rD   rS   r�   rŒ   r@   r   rT   s                  r,   Ú"_validate_longrope_rope_parametersz=RotaryEmbeddingConfigMixin._validate_longrope_rope_parameters}  s*  € ØhÐhÐhˆØDÐDÐDˆÝ˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ# KÔ0ˆ	Ø×!Ò! )¨]¸MÈ=ÐfqÐ!ÑrÔrÐrà /× 3Ò 3Ð4KÈSÑ QÔ QÐÝ˜4 ¨TÔ-=ÀÔAYÑ-YÑZÔZˆÝ�(Ð2Ñ2Ñ3Ô3ˆà&×*Ò*¨>Ñ:Ô:ˆÝ˜<­Ñ.Ô.ð 	tµ3Ð6iÐ6iÐ\hÐ6iÑ6iÔ6iÑ3iÔ3ið 	tÝ�NŠNÐrÐdpÐrÐrÑsÔsÐsÝˆ|ÑÔ  q¡Ò(Ð(Ý�NŠNØnÈ3ÐRSÉ8ÐnÐnÕ[^Ð_kÑ[lÔ[lÐnÐnñô ð ð &×)Ò)¨-Ñ8Ô8ˆÝ˜;­Ñ-Ô-ð 	rµ#Ð5gÐ5gÐ[fÐ5gÑ5gÔ5gÑ2gÔ2gð 	rÝ�NŠNÐpÐcnÐpÐpÑqÔqÐqÝˆ{ÑÔ˜s a™xÒ'Ð'Ý�NŠNØlÈ#ÐQRÉ(ÐlÐlÕZ]Ð^iÑZjÔZjÐlÐlñô ð ð !×$Ò$ XÑ.Ô.ˆØ+:Ð;]Ô+^Ð(ð ˆ>Ð>ÐJÝ×ÒðEñô ð ð ð ˆ^Ð @Ð HÝ�NŠNÐQÑRÔRÐRÐRÝ˜F¥U­C LÑ1Ô1ð 	j°V¸c²\°\Ý�NŠNÐhÐ`fÐhÐhÑiÔiÐià*×.Ò.Ð/AÑBÔBÐØÐ'µÐ<LÍuÕVYÈlÑ1[Ô1[Ð'Ð_oÐruÒ_uÐ_uÝ�NŠNØ{ÐiyÐ{Ð{ñô ð ð ð ð (Ð'Ð_uÐ_ur.   c                 óX  — h d£}|d         }t          |                     ¦   «         ¦  «        }|                      ||||¬¦  «         |d         }|�"t          |t          t
          f¦  «        r|dk     rt                               d|› �¦  «         |d         }|d         }|�t          |t          t
          f¦  «        st                               d	|› �¦  «         |�t          |t          t
          f¦  «        st                               d
|› �¦  «         ||k    r t                               d|› d|› �¦  «         |d         }	|	�t          |	t
          ¦  «        st                               d|	› �¦  «         |	| j        k    r't                               d|	› d| j        › �¦  «         d S d S )N>   r@   r   rA   r“   r”   r   r   rÃ   r@   rC   rÒ   r“   r”   zF`rope_parameters`'s low_freq_factor field must be a float, or int got zG`rope_parameters`'s high_freq_factor field must be a float or int, got zf`rope_parameters`'s high_freq_factor field must be greater than low_freq_factor, got high_freq_factor=z and low_freq_factor=r   zS`rope_parameters`'s original_max_position_embeddings field must be an integer, got zj`rope_parameters`'s original_max_position_embeddings field must be less than max_position_embeddings, got z and max_position_embeddings=)	r´   r½   rÌ   rc   rP   rM   r»   r¼   rb   )
r'   r    rÄ   rÍ   r   rÎ   r@   r“   r”   r   s
             r,   Ú _validate_llama3_rope_parametersz;RotaryEmbeddingConfigMixin._validate_llama3_rope_parameters¯  s  € ð
ð 
ð 
ˆð $ KÔ0ˆ	Ý˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ×!Ò! )¨]¸MÐWbÐ!ÑcÔcÐcà  Ô*ˆØˆ>¥¨FµU½C°LÑ!AÔ!Aˆ>ÀVÈcÂ\À\Ý�NŠNÐhÐ`fÐhÐhÑiÔiÐià)Ð*;Ô<ˆØ*Ð+=Ô>ÐØÐ"­*°_ÅuÍcÀlÑ*SÔ*SÐ"Ý�NŠNÐuÐdsÐuÐuÑvÔvÐvØÐ#­:Ð6FÍÕPSÈÑ+UÔ+UÐ#Ý�NŠNØlÐZjÐlÐlñô ð ð ˜Ò.Ð.Ý�NŠNðLØ#ðLð LØ:IðLð Lñô ð ð
 ,;Ð;]Ô+^Ð(Ø+Ð3½:ÐFfÕhkÑ;lÔ;lÐ3Ý�NŠNð6Ø3ð6ð 6ñô ð ð ,¨tÔ/KÒKÐKÝ�NŠNðqØ3ðqð qØRVÔRnðqð qñô ð ð ð ð LÐKr.   c                 óø   — ddh}|d         }t          |                     ¦   «         ¦  «        }|                      ||||¬¦  «         |                     d¦  «        }|€t                               d¦  «         d S d S )Nr   rA   rÃ   rB   zó`rope_parameters`'s partial_rotary_factor is None. This will default to 1.0 in the computation, making this equivalent to the linear_scaling RoPE type. Provide a value in the range [0.0, 1.0) to make use of the proportional RoPE functionality.)r´   r½   rÌ   rJ   r»   r¼   )r'   r    rÄ   rÍ   r   rÎ   rB   s          r,   Ú&_validate_proportional_rope_parameterszARotaryEmbeddingConfigMixin._validate_proportional_rope_parametersÚ  s–   € Ø$ lÐ3ˆØ# KÔ0ˆ	Ý˜O×0Ò0Ñ2Ô2Ñ3Ô3ˆØ×!Ò! )¨]¸MÐWbÐ!ÑcÔcÐcà /× 3Ò 3Ð4KÑ LÔ LÐØ Ð(Ý�NŠNðCñô ð ð ð ð )Ð(r.   r   rÎ   rÍ   rË   c                 óN  — d|v r|dhz  }|                      d¦  «         |pt          ¦   «         }d|vr|                      d¦  «         |�|t          |¦  «        z  }||z
  }|rt          d| › d|› �¦  «        ‚||z
  |z
  }|r"t                               d| › d|› �¦  «         dS dS )z\Compare the received keys in `config.rope_parameters` against the expected and optional keysr¹   r   rB   Nz<Missing required keys in `rope_parameters` for 'rope_type'='z': z8Unrecognized keys in `rope_parameters` for 'rope_type'=')Úaddr´   ÚKeyErrorr»   r¼   )r   rÎ   rÍ   rË   rÄ   Úmissing_keysÚunused_keyss          r,   rÌ   z/RotaryEmbeddingConfigMixin._check_received_keysè  sú   € ð �]Ð"Ð"Ø˜f˜XÑ%ˆMØ×Ò˜kÑ*Ô*Ð*à%Ð.­©¬ˆØ"¨-Ð7Ð7Ø×ÒÐ5Ñ6Ô6Ð6ð Ð"Ø�S Ñ-Ô-Ñ-ˆMà$ }Ñ4ˆØð 	xÝÐvÐZcÐvÐvÐhtÐvÐvÑwÔwÐwà# mÑ3°mÑCˆØð 	sÝ�NŠNÐqÐV_ÐqÐqÐdoÐqÐqÑrÔrÐrÐrÐrð	sð 	sr.   )r'   r   r<   )NN)r¥   r¦   r§   r¨   r²   r´   rµ   r¶   rI   rÈ   ÚdictrÏ   rÕ   rØ   rÜ   ræ   rè   rê   Ústaticmethodrª   rÌ   r¬   r.   r,   r®   r®   º  s&  € € € € € ðð ð €MØ%( S¡U¤UÐ"ðð ð ð../ð ./ð ./ð`ð ð ð ð:
ð 
Àð 
ÐTWÐZ^ÑT^ð 
ð 
ð 
ð 
ðjð jÀð jÐSVÐY]ÑS]ð jð jð jð jðjð jÀð jÐTWÐZ^ÑT^ð jð jð jð jð2ð 2¸dð 2ÐQTÐW[ÑQ[ð 2ð 2ð 2ð 2ðh0ð 0À$ð 0ÐUXÐ[_ÑU_ð 0ð 0ð 0ð 0ðd)ð )Àð )ÐSVÐY]ÑS]ð )ð )ð )ð )ðVð Àdð ÐY\Ð_cÑYcð ð ð ð ð ð
 %)Ø"&ðsð sØðsàðsð ðsð ˜T‘zð	sð
 ˜4‘Zðsð sð sñ „\ðsð sð sr.   r®   rÄ   c                 óŠ   — t          j        dt          ¦  «         |                      ¦   «          |                      ¦   «          dS )zq
    This is a deprecated function.
    It has been kept for backward compatibility with custom code models.
    aX  `rope_config_validation` is deprecated and has been removed. Its functionality has been moved to RotaryEmbeddingConfigMixin.validate_rope method. PreTrainedConfig inherits this class, so please call self.validate_rope() instead. Also, make sure to use the new rope_parameters syntax. You can call self.standardize_rope_params() in the meantime.N)ÚwarningsÚwarnÚFutureWarningrI   rÈ   )r   rÄ   s     r,   Úrope_config_validationrö     sO   € õ
 „Mð	Gõ
 	ñô ð ð ×"Ò"Ñ$Ô$Ð$Ø
×ÒÑÔÐÐÐr.   )NNNN)NNNNrD   )NNNr<   )%rl   ró   Úcollections.abcr   Ú	functoolsr   Útypingr   r   r   Úutilsr
   r   Ú
get_loggerr¥   r»   r   Úconfiguration_utilsr   r=   rM   rª   ÚtuplerP   rU   r`   rg   rŠ   r‘   rž   r#   rð   r©   r¤   r®   r´   rö   r¬   r.   r,   ú<module>rþ      s–  ðð €€€€Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5à .Ð .Ð .Ð .Ð .Ð .Ð .Ð .ð 
ˆÔ	˜HÑ	%Ô	%€ð ÐÑÔð Ø€L€L€Làð 6Ø5Ð5Ð5Ð5Ð5Ð5ð`ð `ð `ðH ,0Ø'+ØØ!ð	3&ð 3&ØÐ'Ô(ð3&à�^Ô$ð3&ð �4‰Zð3&ð �d‘
ð	3&ð
 ˆ>˜5Ð Ô!ð3&ð 3&ð 3&ð 3&ðn ,0Ø'+ØØ!Ø"ðC&ð C&ØÐ'Ô(ðC&à�^Ô$ðC&ð �4‰ZðC&ð �d‘
ð	C&ð
 ðC&ð ˆ>˜5Ð Ô!ðC&ð C&ð C&ð C&ðN ,0Ø'+ØØ!ð	C&ð C&ØÐ'Ô(ðC&à�^Ô$ðC&ð �4‰ZðC&ð �d‘
ð	C&ð
 ˆ>˜5Ð Ô!ðC&ð C&ð C&ð C&ðP (,ØØ!ð	D&ð D&ØðD&à�^Ô$ðD&ð �4‰ZðD&ð �d‘
ð	D&ð
 ˆ>˜5Ð Ô!ðD&ð D&ð D&ð D&ðR (,ØØ!ð	U&ð U&ØðU&à�^Ô$ðU&ð �4‰ZðU&ð �d‘
ð	U&ð
 ˆ>˜5Ð Ô!ðU&ð U&ð U&ð U&ðt (,ØØ!ð	L,ð L,ØðL,à�^Ô$ðL,ð �4‰ZðL,ð �d‘
ð	L,ð
 ˆ>˜5Ð Ô!ðL,ð L,ð L,ð L,ðf 6Ø.Ø$Ø,Ø(Ø9ðOð OÐ �T˜#˜x¨¨U°>À5Ð3HÔ-IÐ(IÔJÐJÔKð ð ñ ð5#ð 5#ð 5#ð 5#ð 5#�Yñ 5#ô 5#ð 5#ðpJsð Jsð Jsð Jsð Jsñ Jsô Jsð JsðZ
ð Ð#=ð ÈCÐRVÉJð ð ð ð ð ð r.   