o
    7?e                     @   s   d Z ddlZddlZddlZddlZddlZddlm  m	Z
 ddlmZ ddlmZ dd ZG dd de
jjZedkrDe
j  dS dS )	z0Benchmark for Keras hashing preprocessing layer.    N)hashingc                  c   s0    t dD ]} ddd tdD V  qd S )N    c                 s   s    | ]	}t tjV  qd S N)randomchoicestringascii_letters).0i r   |/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/keras/src/layers/preprocessing/benchmarks/hashing_benchmark.py	<genexpr>!   s    zword_gen.<locals>.<genexpr>   )	itertoolscountjoinrange)_r   r   r   word_gen   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	BenchmarkLayerz!Benchmark the layer forward pass.c           
      C   s   d}g }g }t |D ]B}tjjttjtg }||d }|	|}d}|
|}||}|t  |D ]
}tjj|dd}q:|t  q
tt|t| | }	|	S )N   d   r   )Znum_buckets)r   tfdataDatasetfrom_generatorr   r   TensorShapeshufflebatchtakeprefetchappendtimestringsZto_hash_bucketnpmeanarray)
self
batch_sizenum_repeatsstartsendsr   dsnum_batchesr   avg_timer   r   r   run_dataset_implementation'   s$   


z)BenchmarkLayer.run_dataset_implementationc                 C   s  t jdtjdd}tjdd}||}d}g }g }t|D ]>}tjj	t
tjtg }||d }||}d}	||	}||	}|t  |D ]}
||
}qM|t  qtt|t| |	 }d| }| |}||| || | d d	}| j||||d
 d S )Nr   word)shapeZdtypenamer   )Znum_binsr   r   zhashing|batch_)zdataset implementation baselinezdelta secondszdelta percent)ZitersZ	wall_timeextrasr3   )kerasZInputr   r   r   ZHashingr   r   r   r   r   r   r   r   r    r!   r"   r#   r%   r&   r'   r0   Zreport_benchmark)r(   r)   Zinput_1layerr   r*   r+   r,   r-   r.   r   r/   r3   Zbaseliner4   r   r   r   bm_layer_implementation>   s:   






z&BenchmarkLayer.bm_layer_implementationc                 C   s   dD ]}| j |d qd S )N)    @      )r)   )r7   )r(   r   r   r   r   benchmark_vocab_size_by_batchb   s   z,BenchmarkLayer.benchmark_vocab_size_by_batchN)__name__
__module____qualname____doc__r0   r7   r;   r   r   r   r   r   $   s
    $r   __main__)r?   r   r   r   r#   numpyr%   Ztensorflow.compat.v2compatv2r   Z	keras.srcsrcr5   Zkeras.src.layers.preprocessingr   r   testZ	Benchmarkr   r<   mainr   r   r   r   <module>   s   C