o
    پi                     @   s   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Zd dl	m
Z
 dee dee dee fdd	Zdee dee dee fd
dZ		ddedeee  deee  deeeee f fddZG dd de
ZdS )    N)Path)	GeneratorOptionalTuple)BaseFileConnectorpathspatternsreturnc                        fdd| D S )Nc                    s&   g | ] t  fd dD r qS )c                 3       | ]	}t   |V  qd S Nfnmatch.0patternpath K/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/connector/s3.py	<genexpr>       z+_filter_allow.<locals>.<listcomp>.<genexpr>anyr   r   r   r   
<listcomp>       z!_filter_allow.<locals>.<listcomp>r   r   r   r   r   r   _filter_allow      
r   c                    r
   )Nc                    s&   g | ] t  fd dD s qS )c                 3   r   r   r   r   r   r   r   r      r   z,_filter_ignore.<locals>.<listcomp>.<genexpr>r   r   r   r   r   r      r   z"_filter_ignore.<locals>.<listcomp>r   r   r   r   r   _filter_ignore   r    r!   r   allow_patternignore_patternc           	      C   s   | dd}d|dd }|d }| j||d}dd |d	g D }t|d
g}|dur6t||}|dur?t||}|||fS )aZ  
    List files from S3 path and filter by pattern.

    Args:
        s3: S3 client to use.
        path: The S3 path to list from.
        allow_pattern: A list of patterns of which files to pull.
        ignore_pattern: A list of patterns of which files not to pull.

    Returns:
        tuple[str, str, list[str]]: A tuple where:
            - The first element is the bucket name
            - The second element is string represent the bucket
              and the prefix as a dir like string
            - The third element is a list of files allowed or
              disallowed by pattern
    s3:///   Nr   )BucketPrefixc                 S   s   g | ]}|d  qS )Keyr   )r   objr   r   r   r   9   s    zlist_files.<locals>.<listcomp>Contentsz*/)removeprefixsplitjoinlist_objects_v2getr!   r   )	s3r   r"   r#   partsprefixbucket_nameobjectsr   r   r   r   
list_files   s   


r6   c                       s   e Zd Zdeddf fddZddeee  dee fddZ		ddeee  d	eee  ddfd
dZ	dde	de
eeejf ddf fddZ fddZ  ZS )S3Connectorurlr	   Nc                    s$   dd l }t | |d| _d S )Nr   r1   )boto3super__init__client)selfr8   r9   	__class__r   r   r;   G   s   zS3Connector.__init__r"   c                    s*   t | j| j|d\ }} fdd|D S )N)r   r"   c                    s   g | ]
}d   d| qS )r$   r%   r   )r   r   r4   r   r   r   Q   s    z$S3Connector.glob.<locals>.<listcomp>)r6   r<   r8   )r=   r"   _r   r   r@   r   globM   s   
zS3Connector.globr#   c           	      C   st   t | j| j||\}}}t|dkrdS |D ]!}tj| j||}t	|j
}tj|dd | j||| qdS )a   
        Pull files from S3 storage into the temporary directory.

        Args:
            s3_model_path: The S3 path of the model.
            allow_pattern: A list of patterns of which files to pull.
            ignore_pattern: A list of patterns of which files not to pull.

        r   NT)exist_ok)r6   r<   r8   lenosr   r.   	local_dirr,   r   parentmakedirsdownload_file)	r=   r"   r#   r4   base_dirfilesfiledestination_filerF   r   r   r   
pull_filesS   s   

zS3Connector.pull_filesr   rankc                 C   s"   ddl m} | jdgd}||S )Nr   )"runai_safetensors_weights_iteratorz*.safetensors)r"   )$sglang.srt.model_loader.weight_utilsrP   rB   )r=   rO   rP   hf_weights_filesr   r   r   weight_iteratorm   s   zS3Connector.weight_iteratorc                    s   | j   t   d S r   )r<   closer:   )r=   r>   r   r   rT   x   s   
zS3Connector.closer   NN)r   )__name__
__module____qualname__strr;   r   listrB   rN   intr   r   torchTensorrS   rT   __classcell__r   r   r>   r   r7   E   s&     



r7   rU   )r   rE   pathlibr   typingr   r   r   r\   sglang.srt.connectorr   rZ   rY   r   r!   tupler6   r7   r   r   r   r   <module>   s(   ""


(