o
    }oif                     @   s@   d dl mZmZ d dlZd dlmZ d	ddZG dd dZdS )
    )ListOptionalN)logginglargerc                    sX   |dks
|dks
J |dkrt d   fddS t d   fddS )z
        Filter image based on its resolution.
        value: filter threshold
        method: Either larger or smaller
        image_idx: idx of the image in the tuple input
    r   smallerz)Only Selecting images with resolution >= c                    s$   |   j d ko|   j d kS Nr      sizex	image_idxvalue w/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/data/imagen/augmentations/augmentations.py<lambda>      $ z)build_resolution_filter.<locals>.<lambda>z)Only Selecting images with resolution <= c                    s$   |   j d ko|   j d kS r   r	   r   r   r   r   r   "   r   )r   info)r   methodr   r   r   r   build_resolution_filter   s   r   c                	   @   sH   e Zd ZdZddee dee deee  fddZdd	 Z	d
d Z
dS )PickleTransformz
        Convert encodings stored in the pickle file to encoding and mask.
        Transform the pad and resize the embedding to match the generator config.
    Nencoding_lengthsencoding_keysout_keysc                 C   s<   t |t |ks
J || _|| _|d ur|| _d S || _d S N)lenr   r   r   )selfr   r   r   r   r   r   __init__+   s   zPickleTransform.__init__c                 C   s   t |}|jd }t j|||jt jd}|jd |kr"|d| }t | }t|jdkr9d|d|jd < t|jdkrI||d|jd < ||fS )Nr   )devicedtyper   )	torchtensorshapezerosr   float32
LongTensorzero_r   )r   arrntokens	embed_dim
arr_paddedmaskr   r   r   _pad_and_resize1   s   

zPickleTransform._pad_and_resizec                 C   s\   t  }t| j| j| jD ]\}}}| || d |\}}||| d< ||| d< q|S )N	encodings_embeddings_mask)dictzipr   r   r   r-   )r   dataout_dicttoken_lengthencoding_keyout_keyembedr,   r   r   r   __call__F   s   zPickleTransform.__call__r   )__name__
__module____qualname____doc__r   intstrr   r   r-   r9   r   r   r   r   r   %   s
    (r   )Nr   r   )typingr   r   r!   
nemo.utilsr   r   r   r   r   r   r   <module>   s
   
