B
    !`O                 @   s   d 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 dd	lmZ dd
lmZ dd Zdd ZG dd dejZdS )z#The TensorBoard Debugger V2 plugin.    )absolute_import)division)print_function)wrappers)errors)plugin_util)base_plugin)debug_data_provider)	http_utilc             C   s   t j| d|idddS )Nerrorzapplication/jsoni  )code)r
   Respond)requesterror_message r   a/home/dcms/DCMS/lib/python3.7/site-packages/tensorboard/plugins/debugger_v2/debugger_v2_plugin.py_error_response   s    r   c             C   s
   t | dS )Nzrun parameter is not provided)r   )r   r   r   r   _missing_run_error_response$   s    r   c                   s   e Zd ZdZejZ fddZdd Zdd Z	dd	 Z
ejjd
d Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Z  ZS ) DebuggerV2Pluginz#Debugger V2 Plugin for TensorBoard.c                s*   t t| | |j| _t| j| _dS )zInstantiates Debugger V2 Plugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        N)superr   __init__ZlogdirZ_logdirr	   ZLocalDebuggerV2DataProvider_data_provider)selfcontext)	__class__r   r   r   -   s    zDebuggerV2Plugin.__init__c             C   s2   | j | j| j| j| j| j| j| j| j| j	| j
dS )N)z/runsz/alertsz/execution/digestsz/execution/dataz/graph_execution/digestsz/graph_execution/dataz/graphs/graph_infoz/graphs/op_infoz/source_files/listz/source_files/filez/stack_frames/stack_frames)
serve_runsserve_alertsserve_execution_digestsserve_execution_dataserve_graph_execution_digestsserve_graph_execution_dataserve_graph_infoserve_graph_op_infoserve_source_files_listserve_source_fileserve_stack_frames)r   r   r   r   get_plugin_apps;   s    z DebuggerV2Plugin.get_plugin_appsc             C   s   t | jjddS )ah  Check whether the Debugger V2 Plugin is always active.

        When no data in the tfdbg v2 format is available, a custom information
        screen is displayed to instruct the user on how to generate such data
        to be able to use the plugin.

        Returns:
          `True` if and only if data in tfdbg v2's DebugEvent format is available.
         )experiment_id)boolr   	list_runs)r   r   r   r   	is_activeK   s    
zDebuggerV2Plugin.is_activec             C   s   t jddddS )NTzDebugger V2F)Zis_ng_componentZtab_nameZdisable_reload)r   ZFrontendMetadata)r   r   r   r   frontend_metadataW   s    z"DebuggerV2Plugin.frontend_metadatac             C   sL   t |j}| jj|d}t }x|D ]}d|ji||j< q&W t	||dS )N)r(   
start_timezapplication/json)
r   r(   environr   r*   dictr-   Zrun_idr
   r   )r   r   
experimentrunsZrun_listingrunr   r   r   r   \   s    
zDebuggerV2Plugin.serve_runsc          
   C   s   t |j}|jd}|d kr(t|S t|jdd}t|jdd}|jdd }tj||||d}| j	j
|| j|d}tt|j}	y&t|| j	j|| |	 d	 jd
dS  tjk
r }
 zt|t|
S d }
~
X Y nX d S )Nr2   begin0endz-1
alert_type)r6   )r(   plugin_namerun_tag_filterr   )blob_keyzapplication/json)r   r(   r.   argsgetr   intr	   Zalerts_run_tag_filterr   read_blob_sequencesr7   nextitertagsr
   r   	read_blobr9   r   InvalidArgumentErrorr   str)r   r   r0   r2   r3   r5   r6   r8   blob_sequencestager   r   r   r   e   s,    zDebuggerV2Plugin.serve_alertsc       
   
   C   s   t |j}|jd}|d kr(t|S t|jdd}t|jdd}t|||}| j	j
|| j|d}tt|j}y&t|| j	j|| | d jdd	S  tjk
r }	 zt|t|	S d }	~	X Y nX d S )
Nr2   r3   r4   r5   z-1)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r<   r	   Zexecution_digest_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rB   r   rC   )
r   r   r0   r2   r3   r5   r8   rD   rE   rF   r   r   r   r      s*    
z(DebuggerV2Plugin.serve_execution_digestsc       
   
   C   s   t |j}|jd}|d kr(t|S t|jdd}t|jdd}t|||}| j	j
|| j|d}tt|j}y&t|| j	j|| | d jdd	S  tjk
r }	 zt|t|	S d }	~	X Y nX d S )
Nr2   r3   r4   r5   z-1)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r<   r	   Zexecution_data_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rB   r   rC   )
r   r   r0   r2   r3   r5   r8   rD   rE   rF   r   r   r   r      s*    
z%DebuggerV2Plugin.serve_execution_datac       
   
   C   s   t |j}|jd}|dkr(t|S t|jdd}t|jdd}t|||}| j	j
|| j|d}tt|j}y&t|| j	j|| | d jd	d
S  tjk
r }	 zt|t|	S d}	~	X Y nX dS )a  Serve digests of intra-graph execution events.

        As the names imply, this route differs from `serve_execution_digests()`
        in that it is for intra-graph execution, while `serve_execution_digests()`
        is for top-level (eager) execution.
        r2   Nr3   r4   r5   z-1)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r<   r	   Z%graph_execution_digest_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rB   r   rC   )
r   r   r0   r2   r3   r5   r8   rD   rE   rF   r   r   r   r      s*    
z.DebuggerV2Plugin.serve_graph_execution_digestsc       
   
   C   s   t |j}|jd}|dkr(t|S t|jdd}t|jdd}t|||}| j	j
|| j|d}tt|j}y&t|| j	j|| | d jd	d
S  tjk
r }	 zt|t|	S d}	~	X Y nX dS )a  Serve detailed data objects of intra-graph execution events.

        As the names imply, this route differs from `serve_execution_data()`
        in that it is for intra-graph execution, while `serve_execution_data()`
        is for top-level (eager) execution.

        Unlike `serve_graph_execution_digests()`, this method serves the
        full-sized data objects for intra-graph execution events.
        r2   Nr3   r4   r5   z-1)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r<   r	   Z#graph_execution_data_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rB   r   rC   )
r   r   r0   r2   r3   r5   r8   rD   rE   rF   r   r   r   r       s*    
z+DebuggerV2Plugin.serve_graph_execution_datac       	   
   C   s   t |j}|jd}|dkr(t|S |jd}t||}| jj	|| j
|d}tt|j}y&t|| jj|| | d jddS  tjk
r } zt|t|S d}~X Y nX dS )a  Serve basic information about a TensorFlow graph.

        The request specifies the debugger-generated ID of the graph being
        queried.

        The response contains a JSON object with the following fields:
          - graph_id: The debugger-generated ID (echoing the request).
          - name: The name of the graph (if any). For TensorFlow 2.x
            Function Graphs (FuncGraphs), this is typically the name of
            the underlying Python function, optionally prefixed with
            TensorFlow-generated prefixed such as "__inference_".
            Some graphs (e.g., certain outermost graphs) may have no names,
            in which case this field is `null`.
          - outer_graph_id: Outer graph ID (if any). For an outermost graph
            without an outer graph context, this field is `null`.
          - inner_graph_ids: Debugger-generated IDs of all the graphs
            nested inside this graph. For a graph without any graphs nested
            inside, this field is an empty array.
        r2   Ngraph_id)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r	   Zgraph_info_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   NotFoundErrorr   rC   )	r   r   r0   r2   rG   r8   rD   rE   rF   r   r   r   r!     s(    z!DebuggerV2Plugin.serve_graph_infoc       
   
   C   s   t |j}|jd}|dkr(t|S |jd}|jd}t|||}| jj	|| j
|d}tt|j}y&t|| jj|| | d jddS  tjk
r }	 zt|t|	S d}	~	X Y nX dS )	a@  Serve information for ops in graphs.

        The request specifies the op name and the ID of the graph that
        contains the op.

        The response contains a JSON object with the following fields:
          - op_type
          - op_name
          - graph_ids: Stack of graph IDs that the op is located in, from
            outermost to innermost. The length of this array is always >= 1.
            The length is 1 if and only if the graph is an outermost graph.
          - num_outputs: Number of output tensors.
          - output_tensor_ids: The debugger-generated number IDs for the
            symbolic output tensors of the op (an array of numbers).
          - host_name: Name of the host on which the op is created.
          - stack_trace: Stack frames of the op's creation.
          - inputs: Specifications of all inputs to this op.
            Currently only immediate (one level of) inputs are provided.
            This is an array of length N_in, where N_in is the number of
            data inputs received by the op. Each element of the array is an
            object with the following fields:
              - op_name: Name of the op that provides the input tensor.
              - output_slot: 0-based output slot index from which the input
                tensor emits.
              - data: A recursive data structure of this same schema.
                This field is not populated (undefined) at the leaf nodes
                of this recursive data structure.
                In the rare case wherein the data for an input cannot be
                retrieved properly (e.g., special internal op types), this
                field will be unpopulated.
            This is an empty list for an op with no inputs.
          - consumers: Specifications for all the downstream consuming ops of
            this. Currently only immediate (one level of) consumers are provided.
            This is an array of length N_out, where N_out is the number of
            symbolic tensors output by this op.
            Each element of the array is an array of which the length equals
            the number of downstream ops that consume the corresponding symbolic
            tensor (only data edges are tracked).
            Each element of the array is an object with the following fields:
              - op_name: Name of the op that receives the output tensor as an
                input.
              - input_slot: 0-based input slot index at which the downstream
                op receives this output tensor.
              - data: A recursive data structure of this very schema.
                This field is not populated (undefined) at the leaf nodes
                of this recursive data structure.
                In the rare case wherein the data for a consumer op cannot be
                retrieved properly (e.g., special internal op types), this
                field will be unpopulated.
            If this op has no output tensors, this is an empty array.
            If one of the output tensors of this op has no consumers, the
            corresponding element is an empty array.
        r2   NrG   op_name)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r	   Zgraph_op_info_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rH   r   rC   )
r   r   r0   r2   rG   rI   r8   rD   rE   rF   r   r   r   r"   /  s*    7
z$DebuggerV2Plugin.serve_graph_op_infoc             C   sz   t |j}|jd}|dkr(t|S t|}| jj	|| j
|d}tt|j}t|| jj|| | d jddS )zCServes a list of all source files involved in the debugged program.r2   N)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r	   Zsource_file_list_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   )r   r   r0   r2   r8   rD   rE   r   r   r   r#     s     z(DebuggerV2Plugin.serve_source_files_listc       	   
   C   s   t |j}|jd}|dkr(t|S |jd}|dkrFt|dS t|}t	||}| j
j|| j|d}tt|j}y&t|| j
j|| | d jddS  tjk
r } zt|t|S d}~X Y nX dS )	aw  Serves the content of a given source file.

        The source file is referred to by the index in the list of all source
        files involved in the execution of the debugged program, which is
        available via the `serve_source_files_list()`  serving route.

        Args:
          request: HTTP request.

        Returns:
          Response to the request.
        r2   Nindexz-index is not provided for source file content)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r   r<   r	   Zsource_file_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rH   rC   )	r   r   r0   r2   rJ   r8   rD   rE   rF   r   r   r   r$     s0    z"DebuggerV2Plugin.serve_source_filec       	   
   C   s   t |j}|jd}|dkr(t|S |jd}|dkrFt|dS |sTt|dS |d}t	||}| j
j|| j|d}tt|j}y&t|| j
j|| | d jd	d
S  tjk
r } zt|t|S d}~X Y nX dS )a  Serves the content of stack frames.

        The source frames being requested are referred to be UUIDs for each of
        them, separated by commas.

        Args:
          request: HTTP request.

        Returns:
          Response to the request.
        r2   Nstack_frame_idsz!Missing stack_frame_ids parameterzEmpty stack_frame_ids parameter,)r(   r7   r8   r   )r9   zapplication/json)r   r(   r.   r:   r;   r   r   splitr	   Zstack_frames_run_tag_filterr   r=   r7   r>   r?   r@   r
   r   rA   r9   r   rH   rC   )	r   r   r0   r2   rK   r8   rD   rE   rF   r   r   r   r%     s2    


z#DebuggerV2Plugin.serve_stack_frames)__name__
__module____qualname____doc__r	   ZPLUGIN_NAMEr7   r   r&   r+   r,   r   RequestZapplicationr   r   r   r   r   r    r!   r"   r#   r$   r%   __classcell__r   r   )r   r   r   (   s"   	"%.Q.r   N)rQ   
__future__r   r   r   Zwerkzeugr   Ztensorboardr   r   Ztensorboard.pluginsr   Ztensorboard.plugins.debugger_v2r	   Ztensorboard.backendr
   r   r   ZTBPluginr   r   r   r   r   <module>   s   