o
    Gi=                     @   sn   d Z ddlmZ ddlZddlmZ ddlZddlm	Z	 ddl
mZ ddlmZ eeZdd	 Zd
d ZdS )z!PyTorch - Flax general utilities.    )UnpicklingErrorN)
from_bytes)flatten_dict   )loggingc                 C   s   zt |d}td | }W d    n1 sw   Y  W nA ty_ } z5zt |}| dr7tdt|1 s=w   Y  W n ttfyT   td| dw W Y d }~nd }~ww t	| |S )NrbversionzYou seem to have cloned a repository without having git-lfs installed. Please install git-lfs and run `git lfs install` followed by `git lfs pull` in the folder you cloned.zUnable to convert z  to Flax deserializable object. )
openr   readr   
startswithOSError
ValueErrorUnicodeDecodeErrorEnvironmentError"load_flax_weights_in_pytorch_model)pt_model
model_fileflax_state_f
flax_stateef r   `/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/models/modeling_pytorch_flax_utils.py%load_flax_checkpoint_in_pytorch_model%   s*   
	
r   c                 C   s  zddl }W n ty   td  w ttjdd | }t	|r3t
d tjdd |}d| _t|d	d
}|  }g }t| }| D ]\}}	|d	}
|
d dkrp|	jdkrp|
dd dg }
t|	d}	n"|
d dkr|
dd dg }
|	j}	n|
d dkr|
dd dg }
d|
vrt|
D ]0\}}|ddddddddddddddd d!d"d#d$d%|
|< qd	|
}||v r	|	j|| jkrtd&| d'|| j d(|	j d	t|	tjst|	n|	}	||	||< || qL| | qL| !| t"|}t#|dkr7t
d)| j$j% d*| d+| j$j% d,| j$j% d-	 t#|dkrLt
d.| j$j% d/| d0 | S )1z(Load flax checkpoints in a PyTorch modelr   NzLoading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation instructions.c                 S   s   | j tjkS N)dtypejnpbfloat16)xr   r   r   <lambda>H   s    z4load_flax_weights_in_pytorch_model.<locals>.<lambda>zFound ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` before loading those in PyTorch model.c                 S   s   | j tjkr| tjS | S r   )r   r   r   astypenpfloat32)paramsr   r   r   r   R   s     .)sepkernel   weight)   r   r      scaletime_embedding_0z.0_1z.1_2z.2_3z.3_4z.4_5z.5_6z.6_7z.7_8z.8_9z.9z.Flax checkpoint seems to be incorrect. Weight z was expected to be of shape z	, but is zQSome weights of the Flax model were not used when initializing the PyTorch model z: z,
- This IS expected if you are initializing z from a Flax model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).
- This IS NOT expected if you are initializing z from a Flax model that you expect to be exactly identical (e.g. initializing a BertForSequenceClassification model from a FlaxBertForSequenceClassification model).zSome weights of zE were not initialized from the Flax model and are newly initialized: zo
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.)&torchImportErrorloggererrorr   jax	tree_utiltree_mapvaluesanywarningbase_model_prefix
state_dictsetkeysitemssplitndimr   	transposeT	enumeratereplacejoinshaper   
isinstancer!   ndarrayasarray
from_numpyremoveappendload_state_dictlistlen	__class____name__)r   r   r9   is_type_bf16flax_state_dictpt_model_dictunexpected_keysmissing_keysflax_key_tupleflax_tensorflax_key_tuple_arrayiflax_key_tuple_stringflax_keyr   r   r   r   :   s   	




	r   )__doc__pickler   r=   	jax.numpynumpyr   r!   flax.serializationr   flax.traverse_utilr   utilsr   
get_loggerrZ   r;   r   r   r   r   r   r   <module>   s   
	