o
    wiM                     @  s   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
mZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZ G dd	 d	eZdS )
    )annotationsN)TensorProtohelpersubbyte)Base)expect)float32_to_float8e4m3float32_to_float8e5m2make_tensortensor_dtype_to_field)float8e4m3_to_float32float8e5m2_to_float32unpacked_float4e2m1_to_float32c                   @  s(   e Zd ZedddZedddZdS )	CastreturnNonec               	   C  s  d} g d}t t}t t}t dd }t dd }d}|D ]\}}d }	d }
|dks3|dkrt jg dt jd	}tjd
k}|jt j	d	}|rR|dd d n|dd d }|dkr|dkscJ |
ddg}|
ddg}tjttj|j}	tjttj|j}
n.|dksJ |
ddg}t jt|d ft j	d	}|r||dd d< n||dd d< |jt jd	}|
ddg}tjttj|j}	tjttj|j}
n||v s||v rt jg dt jd	}|dkr|}tdtjddg| }n|dkr|t jt j}tdtjddg| }nn|dkr5t||}tdtjddg| }nV|dkrQt||dddd}tdtjddg| }n:|dkrit||}tdtjddg| }n"|dkrt||dddddd}tdtjddg| }nt d|dkrt||}nJ|dkrt||dddd}n:|dkrt||}n.|dkrt||dddddd}n|dkr|t jt j}n|dkr|}nt dtdt!t|ddg| }|}n|dv s|dv rt "ddt j}d }|dkr|}tdtj|| }nB|dkr1|t j}tdtj|| }n,|d!krE||}tdtj#|| }n|d"krY||}tdtj$|| }nt d|d!krk||t%j&}n>|d"kry||t%j'}n0|dkr|t j}n$|dkr|}n|d#kr|t j(}n|d$kr|t j)}nt dtd%t!t||| }|}tjt!t||}	tjt!t||}
n|d&ks|d&krMt jg d't jd	}d(}|dkr|}tdtj|| }n5|dkr|t jt j}tdtj|| }n|d&krtdtj*|| }nt d)| d*| d+|d,vr9t d)| d*| d+t+t,-|}td%t!t||| }nj|d-krt j./| t0t!t|}|d-krg }|1 D ]}t2|3d.}|4d.}|5| qlt |t6
ddg}n(|t0t!t|}nt jg dt 7t6d	
ddg}|t0t!t|}tjj8d/d0gd1gt!t|d2}|	r|
rt9||g|gd3| d4 | |	g|
gd5 q"t9||g|gd3| d4 | d6 q"d S )7N)      )()FLOATFLOAT16)r   DOUBLE)r   r   )r   r   )r   r   )r   r   )r   STRING)r   r   )r   BFLOAT16)r   r   r   FLOAT8E4M3FNr   r   r   FLOAT8E4M3FNUZr   r   )r   r   )r   r   )r   r   )r   r   r   
FLOAT8E5M2r   r    r   FLOAT8E5M2FNUZr   r#   )r    r   )r    r   )r#   r   )r#   r   )r   UINT4)r   r%   )r   INT4)r   r&   )r%   r   )r%   r   )r%   UINT8)r&   r   )r&   r   )r&   INT8)
FLOAT4E2M1r   )r)   r   )r   r)   )r   r)   c                 S     t j| ddS )NFsignedr   float32_to_4bit_unpackedx r1   ]/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/backend/test/case/node/cast.py<lambda>N       zCast.export.<locals>.<lambda>c                 S  r*   )NTr+   r-   r/   r1   r1   r2   r3   Q   r4   )r   r   r    r#   r   )
0.47892547
0.48033667
0.49968487
0.81910545
0.47031248z0.816468z
0.21087195	0.7229038NaNINF+INF-INFdtypelittle      r   r   r   r   r5   r6   r7   r8   r9   r:   10000001e-7r;   r<   r=   r>   z
-0.0000001z	0.0000001z-1000000r0      r   r   r   T)uzr    r#   )fnrH   7Conversion from {from_type} to {to_type} is not tested.)r%   r&   i   )rG   rG   r%   r&   r'   r(   yr)   )z0.48z0.25z1.05z-3.5z-89rE   rF   r;   r<   r=   r>   z-4z0.01z-0.0)r   rG   zConversion from z to z is not tested.)r   r   r)   r   zutf-8r   inputoutput)inputsoutputsto
test_cast__to_)rP   rQ   nameinput_type_protosoutput_type_protosrP   rQ   rU   ):np	vectorizer   r	   arrayfloat32sys	byteorderviewuint16reshapeonnxr   make_tensor_type_protointr   r   shaper   zeroslenr
   tolistastypefloat16r   r   r   r   r   r    r#   
ValueErrorgetattraranger%   r&   customuint4int4uint8int8r)   r   r   float32_to_float4e2m1_unpackedrandomrandom_sampletensor_dtype_to_np_dtypeflattenstrencodedecodeappendobjectr@   	make_noder   )re   
test_casesvect_float32_to_float8e4m3vect_float32_to_float8e5m2vect_float32_to_uint4vect_float32_to_int4f8_types	from_typeto_typeinput_type_protooutput_type_protonp_fp32little_endisannp_uint16_viewnp_bfp16rN   rO   np_fp32_zerosnp_fp32_from_bfloatinput_valuesexpectedexpected_tensorinput_shapessissunoder1   r1   r2   export   s  
+

































	  zCast.exportc                  C  s  g d} t t}t t}| D ]\}}t jg dt jd}|dkr1|}tdtjddg|	 }n|dkrL|
t j
t j}tdtjddg|	 }ntd	|d
kr[||dd}n(|dkrg||ddd}n|dkrr||dd}n|dkr||dddd}ntd	tdd |D }	t }
tt||
_d|
_|
jddg t|
j}t|
||	 |
}tjjddgdgtt|dd}t||g|gd| d | d qd S )N)r   r   r   r   r   r!   r"   r$   rD   r?   r   r0   r   rG   r   rJ   r   F)saturater   T)rH   r   r    r#   )rI   rH   r   c                 S  s   g | ]}t |qS r1   )rd   ).0r   r1   r1   r2   
<listcomp>  s    z.Cast.export_saturate_false.<locals>.<listcomp>r   rN   rO   r   )rP   rQ   rR   r   test_cast_no_saturate_rT   rX   )rY   rZ   r   r	   r[   r\   r
   r   r   rh   ri   rj   r   rk   bytesrl   	data_typerU   dimsextendr   rb   r   r}   r   )r~   r   r   r   r   r   r   rN   r   ivalstensorfieldrO   r   r1   r1   r2   export_saturate_false  sp   



zCast.export_saturate_falseN)r   r   )__name__
__module____qualname__staticmethodr   r   r1   r1   r1   r2   r      s      ur   )
__future__r   r]   numpyrY   rb   onnx._custom_element_types_custom_element_typesrn   r   r   r   onnx.backend.test.case.baser   onnx.backend.test.case.noder   onnx.helperr   r	   r
   r   onnx.numpy_helperr   r   r   r   r1   r1   r1   r2   <module>   s   