U
    T?h:                     @   s`   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 edddZedd	d
ZdS )    )NumpyHelper)
ModelProtoTensorProto)set_external_data)	OnnxModel)OrtValue)modelc                 C   s|   g }t | }| D ]^}|jD ]R}|j}|drt|}t|}|	||f t
|dd ||_|d qqt| S )a  
    Extract external data from model and return the external data as a list of tuples (name, value).
    Note this function does not handle external data that is not loaded into the model as raw data.

    Args:
        model (ModelProto): the model proto to extract external data from.
    Returns:
        (external_names, external_values): a tuple of two lists of external data names and values.
    raw_datazfoo.bin)location)r   graphsinitializernameHasFieldr   Zto_arrayr   Zortvalue_from_numpyappendr   Z
ClearFieldzip)r   Zexternal_data
onnx_modelgraphr   r   Znumpy_tensorZ	ort_value r   U/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/transformers/onnx_utils.pyextract_raw_data_from_model   s    




r   c                 C   sD   t | }| D ].}|jD ]"}|dr|jtjkr  dS qqdS )z
    Check if the model has external data.

    Args:
        model (ModelProto): the model proto to check for external data.
    Returns:
        bool: True if the model has external data, False otherwise.
    data_locationTF)r   r   r   r   r   r   ZEXTERNAL)r   r   r   r   r   r   r   has_external_data)   s    	
r   N)Zfusion_utilsr   Zonnxr   r   Zonnx.external_data_helperr   r   r   Zonnxruntimer   r   r   r   r   r   r   <module>   s   