U
    db                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlZd dl mZ d dlZd dl	Z
d dlmZ d dlZd dlZd dlZd dlmZ d dlmZmZ d dlmZ d dlm  mZ eeZejZej Z!ej"Z#ej$Z%ej&Z'ej(Z)ej*Z+ej,Z-ej.Z/ej0Z1ej2Z3ej4Z5ej6Z7ej8Z9ej:Z;ej<Z=ee>Z?ej@Z@ejAZAejBZBejCZCejDZDeBrtejEZFejGZHejGZIejJZKejLZMd	d
 ZNejOZPejQZRndd ZHdd ZKdd ZMeCrejSZFejTZIejUZVdd
 ZNejOZPejQZReDsejEZFdd ZIdd ZPdd ZNdd ZRejWZXejYZZej[Z\ej]Z^dd Z_dd Z`d|ddZadd Zbd}ddZcd~dd Zdd!d" Zed#d$ Zfd%d& Zgd'd( Zhd)d* Zid+d, Zjd-d. Zkd/d0 Zldd2d3Zmd4d5 Znd6d7 Zodd8d9Zpd:d; Zqd<d= Zrd>d? Zsd@dA ZtddBdCZudDdE ZvdFdG ZwdHdI Zxe ydJdKdLdMgZzdNdO Z{dPdQ Z|dRdS Z}e}ej~_dTdU ZdVdW ZdXdY ZG dZd[ d[eZe Zdad\Zd]ad^d_ Zejd`da ZddbdcZddde Zdfdg Zdhdi Zdjdk Zdldm ZddndoZdpdq ZddrdsZdtdu Zej~Z~ee~_ee~_ee~_ee~_ddvdwZeej_dxdy Zeej_dzd{ Zeej_dS )    N)Message)Process)defaultdict)
basestring)
caffe2_pb2)scopeutils)TriggerLazyImportc                   C   s   t t S N)npasarrayCZget_cuda_peer_access_pattern r   r   ;/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/workspace.pyGetGpuPeerAccessPattern=   s    r   c                   C   s   dS Nr   r   r   r   r   r   <lambda>D       r   c                   C   s   dS r   r   r   r   r   r   r   F   r   c                   C   s   dS r   r   r   r   r   r   r   H   r   c                   C   s   t t S r
   )r   r   r   Zget_hip_peer_access_patternr   r   r   r   r   P   s    c                   C   s   dS r   r   r   r   r   r   r   Z   r   c                 C   s   d S r
   r   )xr   r   r   r   [   r   c                   C   s
   t g S r
   )r   arrayr   r   r   r   r   \   r   c                   C   s   d S r
   r   r   r   r   r   r   ^   r   c                 C   s   t |   || d S r
   )r   Zfill_random_network_inputsProtoSerializeToString)netZ
input_dimsZinput_typesr   r   r   FillRandomNetworkInputsf   s    r   c                  C   sT   t  t jt j} | d}|dkr&dS t   }|d | d }|  |S dS )zGet a free flask port.)z	127.0.0.1  r   r   ) r      N)socketAF_INETSOCK_STREAM
connect_exbindgetsocknameclose)sockresultsportr   r   r   _GetFreeFlaskPortj   s    

r(   c                 C   sf   ddl m} | dkrt } |dkr*t }t|jdt|d| gfd}|  t	d
t | |S )zStart a mint instance.

    TODO(Yangqing): this does not work well under ipython yet. According to
        https://github.com/ipython/ipython/issues/5862
    writing up some fix is a todo item.
    r   )appNz-pz-r)targetargszMint running at http://{}:{})Zcaffe2.python.mintr)   r   root_folderr(   r   mainstrstartprintformatr   getfqdn)r,   r'   r)   processr   r   r   	StartMint|   s    r4   c                 C   sL   t | tr| S t | tr |  S t| dr6|   S tdt| j dS )a  Stringify a protocol buffer object.

  Inputs:
    obj: a protocol buffer object, or a Pycaffe2 object that has a Proto()
        function.
  Outputs:
    string: the output protobuf string.
  Raises:
    AttributeError: if the passed in object does not have the right attribute.
  r   z.Unexpected argument to StringifyProto of type N)	
isinstancer   r   r   hasattrr   
ValueErrortype__name__)objr   r   r   StringifyProto   s    


r;   c                 C   s:   | d krt t  S tj| s,t|  t | S d S r
   )r   Zreset_workspacer,   ospathexistsmakedirs)r,   r   r   r   ResetWorkspace   s
    
r@   Fc                 C   sF   t   |d krg }|D ]}t| qttjtjjjt| t	| |S r
   )
r	   r   create_blobCallWithExceptionIntercept
create_net	Workspacecurrent_last_failed_op_net_position
GetNetNamer;   )r   	overwriteZinput_blobsZ
input_blobr   r   r   	CreateNet   s     rI   c                 C   s   t t| t|S r
   )r   	Predictorr;   )init_netZpredict_netr   r   r   rJ      s    rJ   c                 C   s   t t| |S r
   )r   Zget_operator_costr;   )operatorblobsr   r   r   GetOperatorCost   s    rN   c                 C   s   t t| S r
   )r   Zrun_operator_oncer;   )rL   r   r   r   RunOperatorOnce   s    rO   c                 C   s   t t| |S r
   )r   Zrun_operator_multipler;   )rL   Znum_runsr   r   r   RunOperatorMultiple   s    rP   c                 C   s    | D ]}t |}|s dS qdS )NFT)rO   )	operatorsopsuccessr   r   r   RunOperatorsOnce   s
    rT   c                   C   s   t  S r
   )r   Zclear_global_net_observerr   r   r   r   ClearGlobalNetObserver   s    rU   c           	      O   s   z| ||W S  t k
r   | }t|d }td|| |r||kr|| }t|D ]$}td|d |d |d  q\ Y nX d S )NzgOriginal python traceback for operator `{}` in network `{}` in exception above (most recent call last):z  File "{}", line {}, in {}r   r      )	Exceptionoperator_tracebacksgetloggerwarningr1   reversed)	funcZop_id_fetcherZnet_namer+   kwargsZop_idZnet_tracebackstbliner   r   r   rB      s(       rB   c                 C   s   t tjtjjjt| t| S r
   )rB   r   Zrun_net_oncerD   rE   rF   rG   r;   r   r   r   r   
RunNetOnce   s    rb   r   c                 C   s"   t tjtjjjt| t| ||S )a  Runs a given net.

    Inputs:
      name: the name of the net, or a reference to the net.
      num_iter: number of iterations to run
      allow_fail: if True, does not assert on net exec failure but returns False
    Returns:
      True or an exception.
    )rB   r   Zrun_netrD   rE   rF   rG   StringifyNetName)nameZnum_iterZ
allow_failr   r   r   RunNet   s    
  re   c                 C   s6   dd l m  m} t| |jr(|| } tt| S r   )	caffe2.python.corepythoncorer5   ExecutionStepPlanr   Zrun_planr;   Zplan_or_steprh   r   r   r   RunPlan  s    
rl   c                 C   s6   dd l m  m} t| |jr(|| } tt| S r   )	rf   rg   rh   r5   ri   rj   r   Zrun_plan_in_backgroundr;   rk   r   r   r   RunPlanInBackground  s    
rm   c           
      C   s   |rdd | D }ndd | D }|dkrB|dks6t t|}n$|dkrXt||}nt|||}t }|| i }i }|jD ]&}	|	jst	|	j
||	j< |	j||	j< q||fS )a  Infers the shapes and types for the specified nets.

    Inputs:
      nets: the list of nets
      blob_dimensions (optional): a dictionary of blobs and their dimensions.
          If not specified, the workspace blobs are used.
      nets_proto (optional): a boolean flag indicating whether the protobuffer
          representation is passed to the routine.
    Returns:
      A tuple of (shapes, types) dictionaries keyed by blob name.
    c                 S   s   g | ]}t |qS r   )r;   .0nr   r   r   
<listcomp>,  s     z'InferShapesAndTypes.<locals>.<listcomp>c                 S   s   g | ]}t | qS r   )r;   r   rn   r   r   r   rq   .  s     N)AssertionErrorr   Z%infer_shapes_and_types_from_workspaceZinfer_shapes_and_types_from_mapr   ZTensorShapesParseFromStringshapesZunknown_shapelistZdimsrd   Z	data_type)
netsZblob_dimensionsZ
nets_protoZ
blob_typesZ
net_protosZblobdesc_prototxtZblobdesc_protort   typestsr   r   r   InferShapesAndTypes  s2       

ry   c                 C   s0   t | tr| S t| j|ks(td| t| S )NzExpected a string or %s)r5   r   r8   r9   rr   r.   )rd   Zexpected_typer   r   r   _StringifyNameF  s    
rz   c                 C   s
   t | dS )NZBlobReferencerz   rd   r   r   r   StringifyBlobNameN  s    r}   c                 C   s
   t | dS )NNetr{   r|   r   r   r   rc   R  s    rc   c                 C   sZ   t | tr| S t| jdks*t| jdkr2|  S t | tjrD| jS td	t
| d S )Nr~   ZNetWithShapeInferencezNot a Net object: {})r5   r   r8   r9   Namer   NetDefrd   rW   r1   r.   ra   r   r   r   rG   V  s    
rG   c                 C   s   t jj}t|| ||S )aN  Feeds a blob into the workspace.

    Inputs:
      name: the name of the blob.
      arr: either a TensorProto object or a numpy array object to be fed into
          the workspace.
      device_option (optional): the device option to feed the data with.
    Returns:
      True or False, stating whether the feed is successful.
    )r   rD   rE   _Workspace_feed_blob)rd   arrdevice_optionwsr   r   r   FeedBlob`  s    r   c                 C   s   dd | D S )zFetches a list of blobs from the workspace.

    Inputs:
        names: list of names of blobs - strings or BlobReferences
    Returns:
        list of fetched blobs
    c                 S   s   g | ]}t |qS r   	FetchBlob)ro   rd   r   r   r   rq   w  s     zFetchBlobs.<locals>.<listcomp>r   )namesr   r   r   
FetchBlobso  s    r   c                 C   s.   t t| }t|tr*tdt| |S )zFetches a blob from the workspace.

    Inputs:
      name: the name of the blob - a string or a BlobReference
    Returns:
      Fetched blob (numpy array or string) if successful
    z'Use FetchInt8Blob to fetch Int8 Blob {})r   
fetch_blobr}   r5   tuple	TypeErrorr1   rd   r%   r   r   r   r   z  s    
r   c                 C   s   t jj}|j|   S r
   )r   rD   rE   rM   to_torch)rd   r   r   r   r   
FetchTorch  s    r   
Int8Tensordatascale
zero_pointc                 C   s2   t t| }t|ts*tdt| t| S )m  Fetches an Int8 blob from the workspace. It shared backend implementation
    with FetchBlob but it is recommended when fetching Int8 Blobs

    Inputs:
      name: the name of the Int8 blob - a string or a BlobReference
    Returns:
      data: int8 numpy array, data
      scale: float, fake quantization scale
      zero_point: int, fake quantization offset
    9You are not fetching an Int8Blob {}. Please use FetchBlob)r   r   r}   r5   r   rr   r1   r   r   r   r   r   FetchInt8Blob  s    r   c                 C   sX   t t| }t|ts*tdt| t| }|j	t
jt|j 	t
j|j S )zFetches an Int8 blob from the workspace and return its real value representation.

    Inputs:
      name: the name of the Int8 blob - a string or a BlobReference
    Returns:
      real value representation of int8 numpy array
    r   )r   r   r}   r5   r   rr   r1   r   r   astyper   Zint32intr   Zfloat32r   )rd   r%   Z	int8_blobr   r   r   FetchInt8BlobRealVal  s    r   c                 C   s.   |  |}t|ts&tdt|t| S )r   z:You are not fetching an Int8Blob {}. Please use fetch_blob)r   r5   r   rr   r1   r}   r   )r   rd   r%   r   r   r   _Workspace_fetch_int8_blob  s    
r   c                 C   s0   t  }tt| d| }|| |S )a  Apply a Transform to a NetDef protobuf object, and returns the new
    transformed NetDef.

    Inputs:
      transform_key: the name of the transform, as it is stored in the registry
      net: a NetDef protobuf object
    Returns:
      Transformed NetDef protobuf object.
    utf-8)r   r   r   Zapply_transformr.   encoder   rs   )transform_keyr   transformed_nettransformed_strr   r   r   ApplyTransform  s    

r   c           	   	   K   s|   d|kr|d nd}d|kr$|d nd}d|kr8|d nd}t  }tt| d| | ||t|}|| |S )a  Apply a Transform to a NetDef protobuf object, and returns the new
    transformed NetDef, only if it runs faster than the original.

    The runs are performed on the current active workspace (gWorkspace).
    You should initialize that workspace before making a call to this function.

    Inputs:
      transform_key: the name of the transform, as it is stored in the registry
      net: a NetDef protobuf object
      init_net: The net to initialize the workspace.
      warmup_runs (optional):
        Determines how many times the net is run before testing.
        Will be 5 by default.
      main_runs (optional):
        Determines how many times the net is run during testing.
        Will be 10 by default.
      improvement_threshold (optional):
        Determines the factor which the new net needs to be faster
        in order to replace the old. Will be 1.01 by default.

    Returns:
      Either a Transformed NetDef protobuf object, or the original netdef.
    warmup_runs   	main_runs
   improvement_thresholdg)\(?r   )	r   r   r   Zapply_transform_if_fasterr.   r   r   floatrs   )	r   r   rK   r^   r   r   r   r   r   r   r   r   ApplyTransformIfFaster  s"    

r   c                   C   s   t  S )z>Return the current namescope string. To be used to fetch blobs)r   ZCurrentNameScoper   r   r   r   GetNameScope  s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	_BlobDictz>Provides python dict compatible way to do fetching and feedingc                 C   s   t |S r
   r   )selfkeyr   r   r   __getitem__  s    z_BlobDict.__getitem__c                 C   s
   t ||S r
   )r   )r   r   valuer   r   r   __setitem__  s    z_BlobDict.__setitem__c                 C   s   t t S r
   )lenr   rM   r   r   r   r   __len__  s    z_BlobDict.__len__c                 C   s   t   S r
   )r   rM   __iter__r   r   r   r   r     s    z_BlobDict.__iter__c                 C   s
   t |S r
   )r   has_blob)r   itemr   r   r   __contains__   s    z_BlobDict.__contains__N)	r9   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r     s   r   Z_CAFFE2_IMMEDIATEr   c                   C   s   t S r
   )_immediate_moder   r   r   r   IsImmediate@  s    r   c                 c   s"   t  }t| d d V  t| d S )NT)CurrentWorkspaceSwitchWorkspace)Zworkspace_namerE   r   r   r   WorkspaceGuardD  s    
r   c              	   C   sH   t  rt  datt t att W 5 Q R X | r<d S t	d d S )NTaF  
    Enabling immediate mode in caffe2 python is an EXTREMELY EXPERIMENTAL
    feature and may very easily go wrong. This is because Caffe2 uses a
    declarative way of defining operators and models, which is essentially
    not meant to run things in an interactive way. Read the following carefully
    to make sure that you understand the caveats.

    (1) You need to make sure that the sequences of operators you create are
    actually runnable sequentially. For example, if you create an op that takes
    an input X, somewhere earlier you should have already created X.

    (2) Caffe2 immediate uses one single workspace, so if the set of operators
    you run are intended to be under different workspaces, they will not run.
    To create boundaries between such use cases, you can call FinishImmediate()
    and StartImmediate() manually to flush out everything no longer needed.

    (3) Underlying objects held by the immediate mode may interfere with your
    normal run. For example, if there is a leveldb that you opened in immediate
    mode and did not close, your main run will fail because leveldb does not
    support double opening. Immediate mode may also occupy a lot of memory esp.
    on GPUs. Call FinishImmediate() as soon as possible when you no longer
    need it.

    (4) Immediate is designed to be slow. Every immediate call implicitly
    creates a temp operator object, runs it, and destroys the operator. This
    slow-speed run is by design to discourage abuse. For most use cases other
    than debugging, do NOT turn on immediate mode.

    (5) If there is anything FATAL happening in the underlying C++ code, the
    immediate mode will immediately (pun intended) cause the runtime to crash.

    Thus you should use immediate mode with extra care. If you still would
    like to, have fun [https://xkcd.com/149/].
    )
r   StopImmediater   r   _immediate_workspace_nametempfilemkdtemp_immediate_root_folderr@   r0   )Zi_knowr   r   r   StartImmediateL  s    
r   c                	   C   s:   t  s
dS tt t  W 5 Q R X tt dadadS )zStops an immediate mode run.Nr   F)r   r   r   r@   shutilrmtreer   r   r   r   r   r   r   ~  s    

r   c                
   C   s&   t t t W  5 Q R  S Q R X d S r
   )r   r   Blobsr   r   r   r   ImmediateBlobs  s    
r   c              	   C   s    t t t|  W 5 Q R X d S r
   )r   r   rO   )rR   r   r   r   RunOperatorImmediate  s    
r   c               
   O   s*   t t t| |W  5 Q R  S Q R X d S r
   )r   r   r   r+   r^   r   r   r   FetchImmediate  s    
r   c               
   O   s*   t t t| |W  5 Q R  S Q R X d S r
   )r   r   r   r   r   r   r   FeedImmediate  s    
r   c                 C   s   t | j| jt|t||S r
   )rB   Z_create_netrF   rG   r;   )r   r   rH   r   r   r   ._Workspace_create_net_with_exception_intercept  s     r   c                 C   s   t |dr| }t|tjr,| | S t|tjrRt| j	| j
t|| S t|tjrl| | S tdt|d S )Nr   z*Don't know how to do Workspace.run() on {})r6   r   r5   r   ZPlanDefZ	_run_planr   r   rB   Z_run_netrF   rG   ZOperatorDefZ_run_operatorr7   r1   r8   )r   r:   r   r   r   _Workspace_run  s     
r   c                 C   s   t |tjkrt|}t |tjkr>|jjdkr>|	tj
}|d krNt }|r|jtjkr|jtdkrtdd| dt|j  t|}|d k	r| |||S | ||S d S )NZSUZfloat64zeCUDA operators do not support 64-bit doubles, please use arr.astype(np.float32) or np.int32 for ints.z	 Blob: {}z	 type: {})r8   r   ZTensorProtor   ZCaffe2TensorToNumpyArrayr   ZndarrayZdtypekindr   objectr   ZCurrentDeviceScopeZdevice_typeCUDArZ   r[   r1   r.   r}   rA   feed)r   rd   r   r   r   r   r   r     s&    
r   c                 C   s   |  t| d S r
   )Z_remove_blobr.   )r   blobr   r   r   _Workspace_remove_blob  s    r   c                 C   sv   t |jdkrZt |jdkrZdd l}t||jrZ|d ks@td|j|}| 	| dS |d k	rjt
|}| ||S )NTensortorchr   z5device_option doesn't make sense with PyTorch tensorsT)r8   r9   r   r   r5   r   rr   _C_tensor_impl_raw_handle_wrap_tensor_implr;   Z_feed)r   argr   r   handler   r   r   
_Blob_feed  s    

r   c                 C   s   ddl }|  }|j|S )z
    PyTorch tensor interop (TensorCPU methods)

    Can be accessed as:
      workspace.Workspace.current.blobs['foo'].tensor().to_torch()
    r   N)r   r   r   r   )Ztensorr   r   r   r   r   _Tensor_to_torch  s    r   c                 C   s   |   std|   S )NzBlob has to be a tensor)Z	is_tensorRuntimeErrorZ	as_tensorr   )r   r   r   r   _Blob_to_torch  s    r   )NN)N)FN)r   F)NFN)N)F)F)N)N)collections
contextlibZgoogle.protobuf.messager   multiprocessingr   r<   r   loggingZnumpyr   Zpast.builtinsr   r   r   r   Zcaffe2.protor   Zcaffe2.pythonr   r   Zcaffe2.python.lazyr	   Z!caffe2.python._import_c_extensionrg   Z_import_c_extensionr   	getLoggerr9   rZ   rM   r   Z
reset_blobZ	ResetBlobrA   Z
CreateBlobZcurrent_workspacer   Zdeserialize_blobZDeserializeBlobZglobal_initZ
GlobalInitr   ZHasBlobZregistered_operatorsZRegisteredOperatorsZserialize_blobZSerializeBlobZswitch_workspacer   r,   Z
RootFolderZ
workspacesZ
WorkspacesZbenchmark_netZBenchmarkNetZbenchmark_net_onceZBenchmarkNetOnceZ	get_statsZGetStatsZcreate_offline_tensorZCreateOfflineTensordictrX   Zis_asanZ
has_fbgemmZhas_cuda_supportZhas_hip_supportZhas_gpu_supportr   ZGpuDeviceTypeZnum_cuda_devicesZNumCudaDevicesZNumGpuDevicesZget_cuda_versionZGetCUDAVersionZget_cudnn_versionZGetCuDNNVersionr   Zget_device_propertiesZGetDevicePropertiesZget_gpu_memory_infoZGetGPUMemoryInfoZHIPZnum_hip_devicesZget_hip_versionZGetHIPVersionZis_numa_enabledZIsNUMAEnabledZget_num_numa_nodesZGetNumNUMANodesZget_blob_numa_nodeZGetBlobNUMANodeZget_blob_size_bytesZGetBlobSizeBytesr   r(   r4   r;   r@   rI   rJ   rN   rO   rP   rT   rU   rB   rb   re   rl   rm   ry   rz   r}   rc   rG   r   r   r   r   
namedtupler   r   r   r   rD   Zfetch_int8_blobr   r   r   r   r   r   r   r   r   contextmanagerr   r   r   r   r   r   r   r   r   r   r   rC   runZ	feed_blobZremove_blobr   ZBlobr   r   Z	TensorCPUr   r   r   r   r   r   <module>   s  




	
  
(

 +

2
	

