o
    hJK                     @   sp   d dl mZmZ d dlmZmZ G dd deZG dd deZG dd dZG d	d
 d
eZ	G dd dZ
dS )    )ABCabstractmethod)ListOptionalc                   @   sv   e Zd ZdZdd Zdd Zedd Zedefd	d
Z	edefddZ
edd Zedd ZedddZdS )
Constraintay  Abstract base class for all constraints that can be applied during generation.
    It must define how the constraint can be satisfied.

    All classes that inherit Constraint must follow the requirement that

    ```py
    completed = False
    while not completed:
        _, completed = constraint.update(constraint.advance())
    ```

    will always terminate (halt).
    c                 C   s   |    d S N)testself r   |/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/generation/beam_constraints.py__init__   s   zConstraint.__init__c                 C   sz   d}d}|s1|dkr|    |  }| |std| |\}}}|d7 }|dkr/td|r|  dkr;tddS )	zJ
        Tests whether this constraint has been properly defined.
        r   F   z[Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.i'  z)update() does not fulfill the constraint.z+Custom Constraint is not defined correctly.N)resetadvancedoes_advance	Exceptionupdate	remaining)r
   counter	completedr   steppedr   r   r   r   r      s$   
zConstraint.testc                 C      t | j d)a  
        When called, returns the token(s) that would take this constraint one step closer to being fulfilled.

        Return:
            token_ids (Union[int, List[int], None]):
                - A single token ID (int) that advances the constraint, or
                - A list of token IDs that could advance the constraint
                - None if the constraint is completed or cannot be advanced
        H is an abstract class. Only classes inheriting this class can be called.NotImplementedError	__class__r	   r   r   r   r   0      
zConstraint.advancetoken_idc                 C   r   )zK
        Reads in a token and returns whether it creates progress.
        r   r   r
   r   r   r   r   r   ?      
zConstraint.does_advancec                 C   r   )a  
        Reads in a token and returns booleans that indicate the progress made by it. This function will update the
        state of this object unlikes `does_advance(self, token_id: int)`.

        This isn't to test whether a certain token will advance the progress; it's to update its state as if it has
        been generated. This becomes important if token_id != desired token (refer to else statement in
        PhrasalConstraint)

        Args:
            token_id(`int`):
                The id of a newly generated token in the beam search.
        Return:
            stepped(`bool`):
                Whether this constraint has become one step closer to being fulfuilled.
            completed(`bool`):
                Whether this constraint has been completely fulfilled by this token being generated.
            reset (`bool`):
                Whether this constraint has reset its progress by this token being generated.
        r   r   r   r   r   r   r   H   s   
zConstraint.updatec                 C   r   )z
        Resets the state of this constraint to its initialization. We would call this in cases where the fulfillment of
        a constraint is abrupted by an unwanted token.
        r   r   r	   r   r   r   r   a   s   
zConstraint.resetc                 C   r   )zl
        Returns the number of remaining steps of `advance()` in order to complete this constraint.
        r   r   r	   r   r   r   r   k   r    zConstraint.remainingFc                 C   r   )a   
        Creates a new instance of this constraint.

        Args:
            stateful(`bool`): Whether to not only copy the constraint for new instance, but also its state.

        Return:
            constraint(`Constraint`): The same constraint as the one being called from.
        r   r   )r
   statefulr   r   r   copyt   r   zConstraint.copyNF)__name__
__module____qualname____doc__r   r   r   r   intr   r   r   r   r"   r   r   r   r   r      s     

	
r   c                       sh   e Zd ZdZdee f fddZdd Zdefdd	Zdefd
dZ	dd Z
dd ZdddZ  ZS )PhrasalConstraintz
    [`Constraint`] enforcing that an ordered sequence of tokens is included in the output.

    Args:
        token_ids (`List[int]`):
            The id of the token that must be generated by the output.
    	token_idsc                    sx   t t|   t|trt|dkrtd| dtdd |D r+td| d|| _t| j| _	d| _
d| _d S )	Nr   z/`token_ids` has to be a non-empty list, but is .c                 s   $    | ]}t |t p|d k V  qdS r   N
isinstancer(   .0r   r   r   r   	<genexpr>      " z-PhrasalConstraint.__init__.<locals>.<genexpr>zGEach list in `token_ids` has to be a list of positive integers, but is F)superr   r   r/   listlen
ValueErroranyr*   seqlenfulfilled_idxr   )r
   r*   r   r   r   r      s   
zPhrasalConstraint.__init__c                 C   s   | j rd S | j| jd  S Nr   )r   r*   r;   r	   r   r   r   r      s   zPhrasalConstraint.advancer   c                 C   s@   t |tstd| dt| | jrdS || j| jd  kS )N&`token_id` has to be an `int`, but is 	 of type Fr   )r/   r(   	TypeErrortyper   r*   r;   r   r   r   r   r      s
   
zPhrasalConstraint.does_advancec                 C   s|   t |tstd| dt| d}d}d}| |r3|  jd7  _d}| j| jd kr/d}|| _nd}|   |||fS )Nr>   r?   Fr   T)	r/   r(   r@   rA   r   r;   r:   r   r   r
   r   r   r   r   r   r   r   r      s   


zPhrasalConstraint.updatec                 C   s   d| _ d| _d S )NFr   )r   r;   r	   r   r   r   r         
zPhrasalConstraint.resetc                 C   s   | j | jd  S r=   )r:   r;   r	   r   r   r   r      s   zPhrasalConstraint.remainingFc                 C   *   t | j}|r| j|_| j|_| j|_|S r   )r)   r*   r:   seq_lenr;   r   r
   r!   new_constraintr   r   r   r"         
zPhrasalConstraint.copyr#   r$   r%   r&   r'   r   r(   r   r   r   r   r   r   r"   __classcell__r   r   r<   r   r)      s    	r)   c                   @   sD   e Zd Zddeee  fddZdd Zdd Zd	d
 Zdd Z	dS )DisjunctiveTrieTnested_token_idsc                 C   sz   t dd |D | _i }|D ]}|}t|D ]\}}||vr"i ||< || }qq|r8| ||r8td| d|| _dS )ze
        A helper class that builds a trie with the words represented in `nested_token_ids`.
        c                 S   s   g | ]}t |qS r   )r7   )r1   oner   r   r   
<listcomp>       z,DisjunctiveTrie.__init__.<locals>.<listcomp>zSEach list in `nested_token_ids` can't be a complete subset of another list, but is r+   N)max
max_height	enumeratehas_subsetsr8   trie)r
   rL   
no_subsetsrootr*   leveltidxr   r   r   r   r      s    

zDisjunctiveTrie.__init__c                 C   s(   | j }|D ]}|| }qt| }|S )z~
        The next possible tokens that will progress the trie, given the current sequence of tokens in `current_seq`.
        )rT   r6   keys)r
   current_seqstartcurrent_tokennext_tokensr   r   r   r]      s
   
zDisjunctiveTrie.next_tokensc                 C   s   |  |}t|dkS Nr   )r]   r7   )r
   rZ   r]   r   r   r   reached_leaf   s   
zDisjunctiveTrie.reached_leafc                    s2   t | }t|dkrdS t fdd|D S )Nr   r   c                    s   g | ]}  |qS r   )count_leaves)r1   nnr	   r   r   rN      s    z0DisjunctiveTrie.count_leaves.<locals>.<listcomp>)r6   valuesr7   sum)r
   rV   
next_nodesr   r	   r   r`      s   zDisjunctiveTrie.count_leavesc                 C   s   |  |}t||kS )zh
        Returns whether # of leaves == # of words. Otherwise some word is a subset of another.
        )r`   r7   )r
   rT   rL   
leaf_countr   r   r   rS      s   
zDisjunctiveTrie.has_subsetsNT)
r$   r%   r&   r   r(   r   r]   r_   r`   rS   r   r   r   r   rK      s    rK   c                       sl   e Zd ZdZdeee  f fddZdd Zdefdd	Zdefd
dZ	dd Z
dd ZdddZ  ZS )DisjunctiveConstrainta3  
    A special [`Constraint`] that is fulfilled by fulfilling just one of several constraints.

    Args:
        nested_token_ids (`List[List[int]]`):
            A list of words, where each word is a list of ids. This constraint is fulfilled by generating just one from
            the list of words.
    rL   c                    s   t t|   t|trt|dkrtd| dtdd |D r+td| dtdd |D r<td| dt|| _	|| _
| j	j| _g | _d	| _d S )
Nr   z6`nested_token_ids` has to be a non-empty list, but is r+   c                 s   s    | ]	}t |t V  qd S r   )r/   r6   r1   r*   r   r   r   r2     s    z1DisjunctiveConstraint.__init__.<locals>.<genexpr>z5`nested_token_ids` has to be a list of lists, but is c                 s   s"    | ]}t d d |D V  qdS )c                 s   r,   r-   r.   r0   r   r   r   r2     r3   z;DisjunctiveConstraint.__init__.<locals>.<genexpr>.<genexpr>N)r9   rh   r   r   r   r2     s
    
zNEach list in `nested_token_ids` has to be a list of positive integers, but is F)r5   r   r   r/   r6   r7   r8   r9   rK   rT   r*   rQ   r:   rZ   r   )r
   rL   r<   r   r   r     s    



zDisjunctiveConstraint.__init__c                 C   s"   | j | j}t|dkrd S |S r^   )rT   r]   rZ   r7   )r
   
token_listr   r   r   r   (  s   zDisjunctiveConstraint.advancer   c                 C   s8   t |tstd| dt| | j| j}||v S )N0`token_id` is supposed to be type `int`, but is r?   )r/   r(   r@   rA   rT   r]   rZ   )r
   r   r]   r   r   r   r   0  s   
z"DisjunctiveConstraint.does_advancec                 C   st   t |tstd| dt| d}d}d}| |r%| j| d}nd}|   | j	| j}|| _
|||fS )Nrj   r?   FT)r/   r(   r@   rA   r   rZ   appendr   rT   r_   r   rB   r   r   r   r   8  s   


zDisjunctiveConstraint.updatec                 C   s   d| _ g | _d S )NF)r   rZ   r	   r   r   r   r   L  rC   zDisjunctiveConstraint.resetc                 C   s   | j rdS | jt| j S r^   )r   r:   r7   rZ   r	   r   r   r   r   P  s   zDisjunctiveConstraint.remainingFc                 C   rD   r   )rg   r*   r:   rE   rZ   r   rF   r   r   r   r"   W  rH   zDisjunctiveConstraint.copyr#   rI   r   r   r<   r   rg     s    	rg   c                   @   sh   e Zd ZdZdee fddZdd Zdd Zd	d
 Z	de
ee  fddZdefddZdddZdS )ConstraintListStatez
    A class for beam scorers to track its progress through a list of constraints.

    Args:
        constraints (`List[Constraint]`):
            A list of [`Constraint`] objects that must be fulfilled by the beam scorer.
    constraintsc                 C   s6   || _ tdd |D | _t|| _d| _|   d S )Nc                 S   s   g | ]}|j qS r   )r:   )r1   cr   r   r   rN   o  s    z0ConstraintListState.__init__.<locals>.<listcomp>F)rm   rP   
max_seqlenr7   n_constraintsr   
init_state)r
   rm   r   r   r   r   k  s
   
zConstraintListState.__init__c                 C   s"   g | _ d | _dd | jD | _d S )Nc                 S      g | ]}|j d dqS )Fr!   r"   r1   
constraintr   r   r   rN   x  s    z2ConstraintListState.init_state.<locals>.<listcomp>)complete_constraintsinprogress_constraintrm   pending_constraintsr	   r   r   r   rq   u  s   zConstraintListState.init_statec                 C   s2   d}| j r|| j| j   7 }t| j| j | S r^   )rx   ro   r   r7   rw   )r
   addr   r   r   get_bankz  s   zConstraintListState.get_bankc                 C   s   g }| j du r'| jD ]}| }t|tr|| q
t|tr%|| q
n| j  }t|tr7|| n
t|trA|| t|dkrIdS |S )a  The list of tokens to generate such that we can make progress.
        By "list" we don't mean the list of token that will fully fulfill a constraint.

        Given constraints `c_i = {t_ij | j == # of tokens}`, If we're not in the middle of progressing through a
        specific constraint `c_i`, we return:

        `[t_k1 for k in indices of unfulfilled constraints]`

        If we are in the middle of a constraint, then we return:
            `[t_ij]`, where `i` is the index of the inprogress constraint, `j` is the next step for the constraint.

        Though we don't care which constraint is fulfilled first, if we are in the progress of fulfilling a constraint,
        that's the only one we'll return.
        Nr   )	rx   ry   r   r/   r(   rk   r6   extendr7   )r
   ri   rv   r   r   r   r   r     s$   








zConstraintListState.advancer*   c                 C   s<   |    |dur|D ]}| |\}}| jr dS q
dS dS )zr
        token_ids: the tokens generated thus far to reset the state of the progress through constraints.
        N)rq   rz   r   )r
   r*   tokencompleter   r   r   r   r     s   zConstraintListState.resetr   c                 C   s`  t |tstd| dd\}}| jrd}d}||fS | jd urT| j|\}}}|r:| j| jjdd d | _|rP| j	| j d | _t
| jdkrPd| _||fS t| jD ]R\}}||r||\}}}|sptd|r{| j	| d | _|s|r|| _|s|r| jd | | j|d	 d   | _t
| jdkr| jd u rd| _ ||fS qY||fS )
Nz'`token_id` should be an `int`, but is `z`.)FFTFrs   r   z|`constraint.update(token_id)` is not yielding incremental progress, even though `constraint.does_advance(token_id)` is true.r   )r/   r(   r@   r   rx   r   ry   rk   r"   rw   r7   rR   r   r   )r
   r   r~   r   r   cidxpending_constraintr   r   r   rz     sN   

&
 zConstraintListState.addTc                 C   sP   t | j}|r&dd | jD |_| jd ur| jjdd|_dd | jD |_|S )Nc                 S   rr   )Trs   rt   ru   r   r   r   rN     s    z,ConstraintListState.copy.<locals>.<listcomp>Trs   c                 S   s   g | ]}|  qS r   rt   ru   r   r   r   rN   
  rO   )rl   rm   rw   rx   r"   ry   )r
   r!   	new_stater   r   r   r"      s   

zConstraintListState.copyNrf   )r$   r%   r&   r'   r   r   r   rq   r{   r   r   r(   r   rz   r"   r   r   r   r   rl   b  s    
#Lrl   N)abcr   r   typingr   r   r   r)   rK   rg   rl   r   r   r   r   <module>   s    K9Z