3
H4W^r  ã               @   sŒ   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	 d dl
Z
d dlZd dlmZ e	jjZd	d
„ Zdd„ Zdd„ ZdS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Ú
caffe2_pb2)ÚcoreÚ	workspaceN)ÚZipFilec             C   sš  t | ƒ\}}}t|ƒ}t|ƒ}t|ƒ}|| }ttjj|dƒdƒ��L}	|	jdƒ |	jdƒ |	jdƒ |	jdj||t|| d ƒd ƒƒ |	jd	ƒ |	jd
ƒ |	jdj|ƒƒ x"t	|ƒD ]}
|	jd|
 d ƒ q¶W |	jdƒ |	jdƒ |	jd
ƒ |	jdj|ƒƒ x$t	|ƒD ]}
|	jd|
 d ƒ �qW |	jdƒ |	jdƒ |	jdƒ |	jd
ƒ |	jdj|ƒƒ x$t	|ƒD ]}
|	jd|
 d ƒ �qfW |	jdƒ W d Q R X d S )NzSerializedTestCoverage.mdzw+z"# Serialized Test Coverage Report
z©This is an automatically generated file. Please see `caffe2/python/serialized_test/README.md` for details. In the case of merge conflicts, please rebase and regenerate.
z## Summary
z5Serialized tests have covered {}/{} ({}%) operators

iè  é
   z## Not covered operators
z
<details>
z7<summary>There are {} not covered operators</summary>

z* Ú
z</details>

z## Covered operators
z3<summary>There are {} covered operators</summary>

z%## Excluded from coverage statistics
z### Schemaless operators
z6<summary>There are {} schemaless operators</summary>

)
Úgen_coverage_setsÚlenÚopenÚosÚpathÚjoinÚwriteÚformatÚintÚsorted)Ú
source_dirÚ
output_dirÚcoveredÚnot_coveredZ
schemalessZnum_coveredZnum_not_coveredZnum_schemalessZ	total_opsÚfÚn© r   úG/tmp/pip-build-e66j97b2/torch/caffe2/python/serialized_test/coverage.pyÚgen_serialized_test_coverage   sL    











r   c             C   sx   t | ƒ}tƒ }g }xZtjƒ D ]N}tj|ƒ}|d k	r:|jr:q|rR||krj|j|ƒ q|jdƒdkr|j	|ƒ qW |||fS )NZ_ENGINE_é   éÿÿÿÿ)
Úgen_covered_opsÚsetr   Z_GetRegisteredOperatorsÚOpSchemaÚgetZprivateÚaddÚfindÚappend)r   Zcovered_opsZnot_covered_opsZschemaless_opsZop_nameÚsr   r   r   r   D   s    
r   c             C   s&  dd„ }t ƒ }�xtj| ƒD �] }tjj| |ƒ}tjj|ƒs>qtjƒ }t|ƒ�}|j	|ƒ W d Q R X tjj|dƒ}t
|dƒ�}|jƒ }W d Q R X ||ƒ}	|j|	jƒ d}
tjj|dj|
ƒƒ}x`tjj|ƒ�rt
|dƒ�}|jƒ }W d Q R X ||ƒ}|j|jƒ |
d7 }
tjj|dj|
ƒƒ}q¾W qW |S )Nc             S   s   t jƒ }|j| ƒ |S )N)r   ZOperatorDefZParseFromString)ÚxÚprotor   r   r   Úparse_protoX   s    
z$gen_covered_ops.<locals>.parse_protozop.pbÚrbr   z
grad_{}.pbr   )r"   r   Úlistdirr   r   ÚisfileÚtempfileÚmkdtempr	   Ú
extractallr   Úreadr%   Útyper   )r   r+   r   r   ÚzipfileÚtemp_dirÚzZop_pathZ	loaded_opZop_protoÚindexZ	grad_pathZloaded_gradZ
grad_protor   r   r   r!   W   s0    
r!   )Ú
__future__r   r   r   r   Zcaffe2.protor   Zcaffe2.pythonr   r   r   r/   r4   r	   ÚCr#   r   r   r!   r   r   r   r   Ú<module>   s   /