
    1VhaT                         d Z ddlZddlmZ ddlmZ dZdZdZdZ	d	Z
d
ZdZdZdZdZdZd&dZd Zd Zd Zd Zd Zd&dZd Zd&dZd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$ G d$ d%ejJ                        Z&y)'a  An implementation of DataProvider that serves tfdbg v2 data.

This implementation is:
  1. Based on reading data from a DebugEvent file set on the local filesystem.
  2. Implements only the relevant methods for the debugger v2 plugin, including
     - list_runs()
     - read_blob_sequences()
     - read_blob()

This class is a short-term hack. To be used in production, it awaits integration
with a more complete implementation of DataProvider such as
MultiplexerDataProvider.
    N)provider)debug_data_multiplexerzdebugger-v2alertsexecution_digestsexecution_datagraphexec_digestsgraphexec_data
graph_infograph_op_infosource_file_listsource_filestack_framesc                 `    dt         ||fz  }||d|z  z  }t        j                  | g|g      S )aQ  Create a RunTagFilter for Alerts.

    Args:
      run: tfdbg2 run name.
      begin: Beginning index of alerts.
      end: Ending index of alerts.
      alert_type: Optional alert type, used to restrict retrieval of alerts
        data to a single type of alerts.

    Returns:
      `RunTagFilter` for the run and range of Alerts.
    %s_%d_%dz_%srunstags)ALERTS_BLOB_TAG_PREFIXr   RunTagFilter)runbeginend
alert_typetags        c/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/debugger_v2/debug_data_provider.pyalerts_run_tag_filterr   2   sB     .s;
;Cuz!!  se3%88    c                     | j                  dd      \  }}|t        t              d }|j                  dd      }t        |d         }t        |d         }d}t        |      dkD  r|d   }||||fS )a  Parse the BLOB key for Alerts.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       - `${ALERTS_BLOB_TAG_PREFIX}_${begin}_${end}.${run_id}` when there is no
         alert type filter.
       - `${ALERTS_BLOB_TAG_PREFIX}_${begin}_${end}_${alert_filter}.${run_id}`
         when there is an alert type filter.

    Returns:
      - run ID
      - begin index
      - end index
      - alert_type: alert type string used to filter retrieved alert data.
          `None` if no filtering is used.
    .   N_      )splitlenr   int)blob_keykey_bodyr   	key_itemsr   r   r   s          r   _parse_alerts_blob_keyr*   E   s    " NN3*MHc2356HsA&I	!E
il
CJ
9~q\
sJ&&r   c                 H    t        j                  | gdt        ||fz  g      S )a  Create a RunTagFilter for ExecutionDigests.

    This differs from `execution_data_run_tag_filter()` in that it is for
    the small-size digest objects for execution debug events, instead of the
    full-size data objects.

    Args:
      run: tfdbg2 run name.
      begin: Beginning index of ExecutionDigests.
      end: Ending index of ExecutionDigests.

    Returns:
      `RunTagFilter` for the run and range of ExecutionDigests.
    r   r   )r   r   !EXECUTION_DIGESTS_BLOB_TAG_PREFIXr   r   r   s      r   execution_digest_run_tag_filterr.   a   s/       U=ucJJK r   c                     | j                  dd      \  }}|t        t              d }t        |j                  d      d         }t        |j                  d      d         }|||fS )a  Parse the BLOB key for ExecutionDigests.

    This differs from `_parse_execution_data_blob_key()` in that it is for
    the small-size digest objects for execution debug events, instead of the
    full-size data objects.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${EXECUTION_DIGESTS_BLOB_TAG_PREFIX}_${begin}_${end}.${run_id}`

    Returns:
      - run ID
      - begin index
      - end index
    r   r    Nr!   r#   )r$   r%   r,   r&   r'   r(   r   r   r   s        r    _parse_execution_digest_blob_keyr1   v   sg    " NN3*MHc=>@AHs#A&'E
hnnS!!$
%Cs?r   c                 H    t        j                  | gdt        ||fz  g      S )a  Create a RunTagFilter for Execution data objects.

    This differs from `execution_digest_run_tag_filter()` in that it is
    for the detailed data objects for execution, instead of the digests.

    Args:
      run: tfdbg2 run name.
      begin: Beginning index of Execution.
      end: Ending index of Execution.

    Returns:
      `RunTagFilter` for the run and range of ExecutionDigests.
    r   r   )r   r   EXECUTION_DATA_BLOB_TAG_PREFIXr-   s      r   execution_data_run_tag_filterr4      s/       U:E3GGH r   c                     | j                  dd      \  }}|t        t              d }t        |j                  d      d         }t        |j                  d      d         }|||fS )a  Parse the BLOB key for Execution data objects.

    This differs from `_parse_execution_digest_blob_key()` in that it is
    for the deatiled data objects for execution, instead of the digests.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${EXECUTION_DATA_BLOB_TAG_PREFIX}_${begin}_${end}.${run_id}`

    Returns:
      - run ID
      - begin index
      - end index
    r   r    Nr!   r#   )r$   r%   r3   r&   r0   s        r   _parse_execution_data_blob_keyr6      sg     NN3*MHc:;=>Hs#A&'E
hnnS!!$
%Cs?r   c                 b    |t        d      t        j                  | gdt        ||fz  g      S )a  Create a RunTagFilter for GraphExecutionTraceDigests.

    This differs from `graph_execution_data_run_tag_filter()` in that it is for
    the small-size digest objects for intra-graph execution debug events, instead
    of the full-size data objects.

    Args:
      run: tfdbg2 run name.
      begin: Beginning index of GraphExecutionTraceDigests.
      end: Ending index of GraphExecutionTraceDigests.

    Returns:
      `RunTagFilter` for the run and range of GraphExecutionTraceDigests.
    zTtrace_id support for graph_execution_digest_run_tag_filter() is not implemented yet.r   r   )NotImplementedErrorr   r   'GRAPH_EXECUTION_DIGESTS_BLOB_TAG_PREFIXr   r   r   trace_ids       r   %graph_execution_digest_run_tag_filterr<      sK    " !#
 	
   UA5#NN
 r   c                     | j                  dd      \  }}|t        t              d }t        |j                  d      d         }t        |j                  d      d         }|||fS )a  Parse the BLOB key for GraphExecutionTraceDigests.

    This differs from `_parse_graph_execution_data_blob_key()` in that it is for
    the small-size digest objects for intra-graph execution debug events,
    instead of the full-size data objects.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${GRAPH_EXECUTION_DIGESTS_BLOB_TAG_PREFIX}_${begin}_${end}.${run_id}`

    Returns:
      - run ID
      - begin index
      - end index
    r   r    Nr!   r#   )r$   r%   r9   r&   r0   s        r   &_parse_graph_execution_digest_blob_keyr>      sg    " NN3*MHcCDFGHs#A&'E
hnnS!!$
%Cs?r   c                 b    |t        d      t        j                  | gdt        ||fz  g      S )a  Create a RunTagFilter for GraphExecutionTrace.

    This method differs from `graph_execution_digest_run_tag_filter()` in that
    it is for full-sized data objects for intra-graph execution events.

    Args:
      run: tfdbg2 run name.
      begin: Beginning index of GraphExecutionTrace.
      end: Ending index of GraphExecutionTrace.

    Returns:
      `RunTagFilter` for the run and range of GraphExecutionTrace.
    zRtrace_id support for graph_execution_data_run_tag_filter() is not implemented yet.r   r   )r8   r   r   $GRAPH_EXECUTION_DATA_BLOB_TAG_PREFIXr:   s       r   #graph_execution_data_run_tag_filterrA      sI      !#
 	
   U@%MMN r   c                     | j                  dd      \  }}|t        t              d }t        |j                  d      d         }t        |j                  d      d         }|||fS )a  Parse the BLOB key for GraphExecutionTrace.

    This method differs from `_parse_graph_execution_digest_blob_key()` in that
    it is for full-sized data objects for intra-graph execution events.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${GRAPH_EXECUTION_DATA_BLOB_TAG_PREFIX}_${begin}_${end}.${run_id}`

    Returns:
      - run ID
      - begin index
      - end index
    r   r    Nr!   r#   )r$   r%   r@   r&   r0   s        r   $_parse_graph_execution_data_blob_keyrC   	  sg      NN3*MHc@ACDHs#A&'E
hnnS!!$
%Cs?r   c                 f    |st        d      t        j                  | gt        d|d|g      S )aw  Create a RunTagFilter for graph op info.

    Args:
      run: tfdbg2 run name.
      graph_id: Debugger-generated ID of the graph. This is assumed to
        be the ID of the graph that immediately encloses the op in question.
      op_name: Name of the op in question. (e.g., "Dense_1/MatMul")

    Returns:
      `RunTagFilter` for the run and range of graph op info.
    #graph_id must not be None or empty.r!   r   )
ValueErrorr   r   GRAPH_OP_INFO_BLOB_TAG_PREFIX)r   graph_idop_names      r   graph_op_info_run_tag_filterrJ      s8     >??  U98WMN r   c                     | j                  d      }| |dz   d }| d| }|t        t              d }|j                  dd      \  }}}|||fS )a  Parse the BLOB key for graph op info.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${GRAPH_OP_INFO_BLOB_TAG_PREFIX}_${graph_id}_${op_name}.${run_name}`,
      wherein
        - `graph_id` is a UUID
        - op_name conforms to the TensorFlow spec:
          `^[A-Za-z0-9.][A-Za-z0-9_.\/>-]*$`
        - `run_name` is assumed to contain no dots (`'.'`s).

    Returns:
      - run name
      - graph_id
      - op name
    r   r    Nr!   r#   )rindexr%   rG   r$   )r'   last_dot_indexr   r(   r!   rH   rI   s          r   _parse_graph_op_info_blob_keyrN   4  sg    & __S)N
>A%'
(C(H9:<=H#>>#q1Ax'!!r   c                 `    |st        d      t        j                  | gt        d|g      S )zCreate a RunTagFilter for graph info.

    Args:
      run: tfdbg2 run name.
      graph_id: Debugger-generated ID of the graph in question.

    Returns:
      `RunTagFilter` for the run and range of graph info.
    rE   r!   r   )rF   r   r   GRAPH_INFO_BLOB_TAG_PREFIX)r   rH   s     r   graph_info_run_tag_filterrQ   O  s6     >??  U3X>? r   c                 \    | j                  d      \  }}|t        t              dz   d }||fS )zParse the BLOB key for graph info.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${GRAPH_INFO_BLOB_TAG_PREFIX}_${graph_id}.${run_name}`,

    Returns:
      - run name
      - graph_id
    r   r    N)r$   r%   rP   )r'   r(   r   rH   s       r   _parse_graph_info_blob_keyrS   a  s8     NN3'MHc67!;=>H=r   c                 <    t        j                  | gt        g      S )zCreate a RunTagFilter for listing source files.

    Args:
      run: tfdbg2 run name.

    Returns:
      `RunTagFilter` for listing the source files in the tfdbg2 run.
    r   )r   r   SOURCE_FILE_LIST_BLOB_TAG)r   s    r   source_file_list_run_tag_filterrV   q  s       se3L2MNNr   c                 0    | | j                  d      dz   d S )zParse the BLOB key for source file list.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${SOURCE_FILE_LIST_BLOB_TAG}.${run_id}`

    Returns:
      - run ID
    r   r    N)indexr'   s    r    _parse_source_file_list_blob_keyrZ   }  s      HNN3'!+-..r   c                 F    t        j                  | gdt        |fz  g      S )a  Create a RunTagFilter for listing source files.

    Args:
      run: tfdbg2 run name.
      index: The index for the source file of which the content is to be
        accessed.

    Returns:
      `RunTagFilter` for accessing the content of the source file.
    z%s_%dr   )r   r   SOURCE_FILE_BLOB_TAG_PREFIX)r   rX   s     r   source_file_run_tag_filterr]     s-       U4e<<= r   c                 p    | j                  dd      \  }}t        |t        t              dz   d       }||fS )a  Parse the BLOB key for accessing the content of a source file.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${SOURCE_FILE_BLOB_TAG_PREFIX}_${index}.${run_id}`

    Returns:
      - run ID, as a str.
      - File index, as an int.
    r   r    N)r$   r&   r%   r\   )r'   r(   r   rX   s       r   _parse_source_file_blob_keyr_     s?     NN3*MHc89A=?@AE:r   c                 f    t        j                  | gt        dz   dj                  |      z   g      S )zCreate a RunTagFilter for querying stack frames.

    Args:
      run: tfdbg2 run name.
      stack_frame_ids: The stack_frame_ids being requested.

    Returns:
      `RunTagFilter` for accessing the content of the source file.
    r!   r   )r   r   STACK_FRAMES_BLOB_TAG_PREFIXjoin)r   stack_frame_idss     r   stack_frames_run_tag_filterrd     s7       U +S0388O3LLM	 r   c                     | j                  dd      \  }}|t        t              dz   d }|j                  d      }||fS )aB  Parse the BLOB key for source file list.

    Args:
      blob_key: The BLOB key to parse. By contract, it should have the format:
       `${STACK_FRAMES_BLOB_TAG_PREFIX}_` +
       `${stack_frame_id_0}_..._${stack_frame_id_N}.${run_id}`

    Returns:
      - run ID
      - The stack frame IDs as a tuple of strings.
    r   r    Nr!   )r$   r%   ra   )r'   r(   r   rc   s       r   _parse_stack_frames_blob_keyrf     sI     NN3*MHc89A=?@HnnS)Or   c                        e Zd ZdZ fdZddZd Z	 ddddZ	 ddddd	Z	 dddd
Z		 ddddZ
	 dddddZddZ xZS )LocalDebuggerV2DataProviderzA DataProvider implementation for tfdbg v2 data on local filesystem.

    In this implementation, `experiment_id` is assumed to be the path to the
    logdir that contains the DebugEvent file set.
    c                 V    t         |           t        j                  |      | _        y)zConstructor of LocalDebuggerV2DataProvider.

        Args:
          logdir: Path to the directory from which the tfdbg v2 data will be
            loaded.
        N)super__init__r   DebuggerV2EventMultiplexer_multiplexer)selflogdir	__class__s     r   rk   z$LocalDebuggerV2DataProvider.__init__  s&     	2MM
r   Nc          
          | j                   j                         D cg c])  }t        j                  ||| j	                  |            + c}S c c}w )zList runs available.

        Args:
          experiment_id: currently unused, because the backing
            DebuggerV2EventMultiplexer does not accommodate multiple experiments.

        Returns:
          Run names as a list of str.
        )run_idrun_name
start_time)rm   Runsr   Run_get_first_event_timestamp)rn   ctxexperiment_idr   s       r   	list_runsz%LocalDebuggerV2DataProvider.list_runs  sR      ((--/
  LL::3?
 	
 
s   .Ac                 f    	 | j                   j                  |      S # t        $ r
}Y d }~y d }~ww xY wN)rm   FirstEventTimestamprF   )rn   rs   es      r   rw   z6LocalDebuggerV2DataProvider._get_first_event_timestamp  s1    	$$88BB 		s    	00)run_tag_filterc                    ~~~t        d      Nz1Debugger V2 DataProvider doesn't support scalars.	TypeErrorrn   rx   ry   plugin_namer   s        r   list_scalarsz(LocalDebuggerV2DataProvider.list_scalars  s     ;KLLr   )
downsampler   c                     ~~~~t        d      r   r   )rn   rx   ry   r   r   r   s         r   read_scalarsz(LocalDebuggerV2DataProvider.read_scalars  s     ;
NKLLr   c                    ~~~t        d      r   r   r   s        r   read_last_scalarsz-LocalDebuggerV2DataProvider.read_last_scalars  s     ;KLLr   c                    ~~~t               r|   )r8   r   s        r   list_blob_sequencesz/LocalDebuggerV2DataProvider.list_blob_sequences  s     ;!##r   c                   ~~|t         k7  rt        d|z        |j                  t        d      |j                  t        d      t	               }| j
                  j                         }|j                  D ]  }||vrt	               ||<   |j                  D ]l  }	|	j                  t        t        t        t        t        t        t        t        t         f	      s
|	t"        fv sKt%        j&                  |	d|      g||   |	<   n  |S )NzUnsupported plugin_name: %sz<run_tag_filter.runs is expected to be specified, but is not.z<run_tag_filter.tags is expected to be specified, but is not.r   rY   )PLUGIN_NAMErF   r   r   dictrm   ru   
startswithr   r,   r3   r9   r@   rP   rG   r\   ra   rU   r   BlobReference)
rn   rx   ry   r   r   r   outputexisting_runsr   r   s
             r   read_blob_sequencesz/LocalDebuggerV2DataProvider.read_blob_sequences  s    :+%:[HII&N  &N  ))..0!&& 	C-'&F3K%** >>.96?<2534
 688 ..38LM(F3K$		* r   c                   |j                  t              rBt        |      \  }}}}t        j                  | j
                  j                  ||||            S |j                  t              r?t        |      \  }}}t        j                  | j
                  j                  |||            S |j                  t              r?t        |      \  }}}t        j                  | j
                  j                  |||            S |j                  t              r?t        |      \  }}}t        j                  | j
                  j                  |||            S |j                  t               r?t#        |      \  }}}t        j                  | j
                  j%                  |||            S |j                  t&              r=t)        |      \  }}t        j                  | j
                  j+                  ||            S |j                  t,              r?t/        |      \  }}}t        j                  | j
                  j1                  |||            S |j                  t2              r9t5        |      }t        j                  | j
                  j7                  |            S |j                  t8              r=t;        |      \  }}	t        j                  | j
                  j=                  ||	            S |j                  t>              r=tA        |      \  }}
t        j                  | j
                  jC                  ||
            S tE        d|z        )N)alert_type_filterzUnrecognized blob_key: %s)#r   r   r*   jsondumpsrm   Alertsr,   r1   ExecutionDigestsr3   r6   ExecutionDatar9   r>   GraphExecutionDigestsr@   rC   GraphExecutionDatarP   rS   	GraphInforG   rN   GraphOpInforU   rZ   SourceFileListr\   r_   SourceLinesra   rf   StackFramesrF   )rn   rx   r'   r   r   r   r   rH   rI   rX   rc   s              r   	read_blobz%LocalDebuggerV2DataProvider.read_blobM  s   56*@*J'CZ::!!((z )  
   !BC>xHOC::!!223sC    !?@<XFOC::d//==c5#NOO  !HIDXNOC::!!77UCH    !EFB8LOC::!!44S%E    !;<6x@MC::d//99#xHII  !>?%B8%L"C7::!!--c8WE    !:;28<C::d//>>sCDD  !<=4X>JC::d//;;CGHH  !=>#?#I C::!!--c?C  88CDDr   r|   )__name__
__module____qualname____doc__rk   rz   rw   r   r   r   r   r   r   __classcell__)rp   s   @r   rh   rh     s    


& MFJM 
M 
M 	M 	M $FJ$ , ,\.Er   rh   r|   )'r   r   tensorboard.datar   tensorboard.plugins.debugger_v2r   r   r   r,   r3   r9   r@   rP   rG   rU   r\   ra   r   r*   r.   r1   r4   r6   r<   r>   rA   rC   rJ   rN   rQ   rS   rV   rZ   r]   r_   rd   rf   DataProviderrh    r   r   <module>r      s     % B ! $7 !!1 *= ''7 $)  / . + - 9&'8*0(,<06.("6$ 	O
/" $ $lE("7"7 lEr   