o
    Xi                     @  s   d dl mZ d dlZd dlmZmZ d dlZd dlZd dl	Z
d dlmZ 				d!d"ddZdd Zdd Zd#ddZdd Zdd  ZdS )$    )annotationsN)OptionalSequence)tensornamestr	data_typeintdimsSequence[int]locationoffsetOptional[int]lengthchecksumOptional[str]basepathreturnonnx.TensorProtoc           	        s   t   |  _| _ j| t jj _ fdd}|d| |dur+|dt| |dur6|dt| |dur?|d| |durH|d|  S )	a`  Create a TensorProto referencing externally stored tensor-data.

    Args:
        name: name of the tensor
        data_type: data type of tensor element
        dims: shape of the tensor
        location: location of the external file (relative path)
        offset: offset in the file where the tensor-data starts
        length: number of bytes containing the data
        checksum: SHA1 digest of the file
        basepath: basepath combined with location to form the full path

    Returns:
        TensorProto

    See https://github.com/onnx/onnx/blob/main/docs/ExternalData.md for more details.
    c                   s    j  }| |_t||_d S N)external_dataaddkeyr   value)kventrytensor_proto N/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/_internal/utils.pyr   0   s   
zexternal_tensor.<locals>.addr   Nr   r   r   r   )	onnxTensorProtor   r   r
   extendEXTERNALdata_locationr	   )	r   r   r
   r   r   r   r   r   r   r   r   r    external_tensor   s    



r&   c                 C  s   t | tjtjfrtj| j}| j	}tj
||S t | tr(tj
tjjg S t | ttjfr9tj
tjjg S t | tr[t| dkrNtjt| d S tjtj
tjjdS t | tjrtt| }tj|j}tj
|g S tdt|  d)z'Return the ONNX type of a python-value.r   NValue of type $ is invalid as an ONNX input/output.)
isinstancenpndarrayr   Tensorr!   helpernp_dtype_to_tensor_dtypedtypeshapemake_tensor_type_protor	   r"   INT32floatfloat32FLOATlistlenmake_sequence_type_protovalue_to_type_protonumbersNumberarray
ValueErrortype)val	elem_typer0   nparrayr   r   r    r9   A   s&   


r9   c                 C  s  t | tjtjfrtj| j}| j	}t
||fS t | tr,tjj}g }t
||fS t | ttjfrAtjj}g }t
||fS t | trgt| dkr[t| d \}}t
||fS t
t
tjjdfS t | tjrt| }tj|j}t
|g fS td||  d)4Return an ir.Value representation of a python-value.r   Nr'   r(   )r)   r*   r+   r   r,   r!   r-   r.   r/   r0   ir
TensorTyper	   r"   r2   r3   r4   r5   r6   r7   value_to_typeSequenceTyper:   r;   r<   r=   )r?   r@   r0   r>   rA   r   r   r    rE   [   s,   


rE   ir.Valuec                 C  s   t |\}}tj| ||dS )rB   )r   r>   r0   )rE   rC   Value)r   r?   r>   r0   r   r   r    value_to_ir_valuex   s   rI   c                 C     dd | D S )zfCreate a list of ValueInfoProto from a list of (name, value) pairs,
    skipping any None values.
    c                 S  s*   g | ]\}}|d urt j|t|qS r   )r!   r-   make_value_infor9   .0r   r?   r   r   r    
<listcomp>   s
    z)values_to_value_infos.<locals>.<listcomp>r   name_valuesr   r   r    values_to_value_infos~   s   rQ   c                 C  rJ   )z`Create a list of ir.Value from a list of (name, value) pairs,
    skipping any None values.
    c                 S  s"   g | ]\}}|d urt ||qS r   )rI   rL   r   r   r    rN      s   " z'values_to_ir_values.<locals>.<listcomp>r   rO   r   r   r    values_to_ir_values   s   rR   )NNNN)r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   rG   )
__future__r   r:   typingr   r   numpyr*   r!   onnx_irrC   
onnxscriptr   r&   r9   rE   rI   rQ   rR   r   r   r   r    <module>   s"   2
