
    i                     H   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
Z
d dlZd dlmZ d dlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZ  ej:                  dddd       edd       eddd       eddddd       eddd       eddd d!       ed"d#d$d%&       ed'd(d)d d*       edd+d,d-      fd.ej<                  d/ed0ed1e	e    d2e!d3e d4e d5e d6e	e   fd7       Z" G d8 d9      Z# G d: d;      Z$d<ed=ee   d1e	e    d>e
jJ                  fd?Z&d<ed=ee   d4e d1e d@e!d>e
jJ                  fdAZ'e
jP                  dBfd>e
jJ                  fdCZ)d=ee   d>e fdDZ*dEe
jJ                  fdFZ+dEe
jJ                  fdGZ,d<ed=ee   d5e d1e	e    d>e
jJ                  f
dHZ-y)I    N)islice)Path)IterableListOptional)tqdm)msg   )util)Language)Doc)Corpus   )ArgOptbenchmark_cliimport_code	setup_gpuspeedT)allow_extra_argsignore_unknown_options)context_settings.zModel name or path)helpz3Location of binary evaluation data in .spacy format)r   existsz--batch-sizez-bz Override the pipeline batch size)minr   Fz--no-shufflezDo not shuffle benchmark dataz--gpu-idz-gzGPU ID or -1 for CPU2   z	--batchesz&Minimum number of batches to benchmark   )r   r      z--warmupz-wz-Number of iterations over the data for warmupz--codez-czNPath to Python file with additional code (registered functions) to be importedctxmodel	data_path
batch_size
no_shuffleuse_gpu	n_batcheswarmup_epochs	code_pathc	                    t        |       t        |d       t        j                  |      }	||n|	j                  }t        |      }
 |
|	      D cg c]  }|j                   }}t        |      dk(  rt        j                  dd       t        d| d	       t        |	|||       t                t        d
| d       t        |	||||       }t                t        |       t        |       yc c}w )zu
    Benchmark a pipeline. Expects a loadable spaCy pipeline and benchmark
    data in the binary .spacy format.
    F)r%   silentNr   z-Cannot benchmark speed using an empty corpus.r   )exitszWarming up for z
 epochs...zBenchmarking z batches...)r   r   r   
load_modelr#   r   	predictedlenr	   failprintwarmup	benchmarkprint_outliersprint_mean_with_ci)r    r!   r"   r#   r$   r%   r&   r'   r(   nlpcorpusegdocswpss                 j/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/spacy/cli/benchmark_speed.pybenchmark_speed_clir;      s    J 	ge,
//%
 C)53>>JIF#)#;/RBLL/D/
4yA~@J	OM?*
56
3mZ0	G	M)K
01
Cy**n
EC	G3s 0s   C4c                       e Zd ZdZd Zd Zy)time_contextz'Register the running time of a context.c                 8    t        j                         | _        | S N)timeperf_counterstart)selfs    r:   	__enter__ztime_context.__enter__S   s    &&(
    c                 P    t        j                         | j                  z
  | _        y r?   )r@   rA   rB   elapsed)rC   typevalue	tracebacks       r:   __exit__ztime_context.__exit__W   s    ((*TZZ7rE   N)__name__
__module____qualname____doc__rD   rK    rE   r:   r=   r=   P   s    18rE   r=   c                   ^    e Zd ZU dZeed<   eed<   eed<   eed<   dej                  ddfd	Zy)
	QuartileszVCalculate the q1, q2, q3 quartiles and the inter-quartile range (iqr)
    of a sample.q1q2q3iqrsamplereturnNc                     t        j                  |d      | _        t        j                  |d      | _        t        j                  |d      | _        | j                  | j                  z
  | _        y )Ng      ?g      ?g      ?)numpyquantilerS   rT   rU   rV   )rC   rW   s     r:   __init__zQuartiles.__init__d   sL    .....-...77TWW$rE   )	rL   rM   rN   rO   float__annotations__rZ   ndarrayr\   rP   rE   r:   rR   rR   [   s4     	III	J%u}} % %rE   rR   r5   r8   rX   c                 f   | j                  t        |dd       |      }g }	 t               5 }t        t	        ||r|n| j
                              }d d d        t              dk(  rn*t        |      }|j                  |j                  z         ot        j                  |      S # 1 sw Y   WxY w)Ndoc)unitdisable)r#   r   )piper   r=   listr   r#   r.   count_tokensappendrG   rZ   array)r5   r8   r#   r9   rG   
batch_docsn_tokenss          r:   annotaterk   k   s     88DE48Z8PD
C
^ 	wt:Z3>>JJ	 z?a
+

8goo-.  ;;s	 	s   $B''B0shufflec           
      J   |rHt        ||z        D cg c]0  }| j                  t        j                  |      j                        2 }}nCt        ||z        D cg c],  }| j                  ||t        |      z     j                        . }}t        | ||      S c c}w c c}w r?   )rangemake_docrandomchoicetextr.   rk   )r5   r8   r&   r#   rl   _
bench_docsis           r:   r2   r2   }   s      9z12
 LLt,112

 
 9z12
 LLa#d)m,112

 

 CZ00


s   5B1B i'  c                 t     t        j                   fdt        |      D        t         j                        S )z9Apply a statistic to repeated random samples of an array.c           	   3      K   | ]5  } t         j                  j                  t              d              7 yw)T)replaceN)rZ   rp   rq   r.   ).0rs   	statisticxs     r:   	<genexpr>zbootstrap.<locals>.<genexpr>   s8      	
 ell))!SVT)BC	
s   ;>)rZ   fromiterrn   float64)r{   rz   
iterationss   `` r:   	bootstrapr      s/    >>	
:&	
 	 rE   c                 &    t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr?   )r.   )ry   ra   s     r:   r|   zcount_tokens.<locals>.<genexpr>   s     (Cs3x(s   )sum)r8   s    r:   rf   rf      s    (4(((rE   rW   c           	         t        j                  |       }t        |       }|j                          |t	        t        |      dz           }|t	        t        |      dz           }t        d|dd||z
  dd||z
  dd       y )Ng?g333333?zMean: .1fz words/s (95% CI: z +))rZ   meanr   sortintr.   r0   )rW   r   bootstrap_meanslowhighs        r:   r4   r4      s    ::fD'O #c/2U:;
<C3s?3e;<=D	F4*.sTz#.>bS@QQR
STrE   c           	         t        |       }t        j                  | |j                  d|j                  z  z
  k  | |j
                  d|j                  z  z   kD  z        }t        j                  | |j                  d|j                  z  z
  k  | |j
                  d|j                  z  z   kD  z        }t        dd|z  t        |       z  ddd|z  t        |       z   d       y )Ng      ?g      @z
Outliers: d   r   z%, extreme outliers: %)rR   rZ   r   rS   rV   rU   r0   r.   )rW   	quartiles
n_outliersn_extreme_outlierss       r:   r3   r3      s    &!I	9<<#	"55	6Y\\C)--$778	:J 	9<<#	"55	6Y\\C)--$778	: 

cJ&#f+5c::OQTWiQimpqwmxPxOyyz{rE   c                 f    ||z  D cg c]  }|j                          }}t        | ||      S c c}w r?   )copyrk   )r5   r8   r'   r#   ra   s        r:   r1   r1      s6     #'"673CHHJ7D7Cz** 8s   .).rp   r@   	itertoolsr   pathlibr   typingr   r   r   rZ   typerr   wasabir	    r   languager   tokensr   trainingr   _utilr   r   r   r   r   commandContextstrr   boolr;   r=   rR   r_   rk   r2   r   r   rf   r4   r3   r1   rP   rE   r:   <module>r      so       + +         B B *.$O S34GPT !$nd0R! 5.7VWr:t2HI
5	 	:t)X !$]	!+5	5 5 	5 5 5 5 5$ %5* ~+5	5r8 8% % 	c08
]]$1	1
s)1 1 	1
 1 ]]1* !::% EMM )x} ) )	Uu}} 	U5==  +	+c+36+DLSM+
]]+rE   