o
    پiY                  	   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dl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 d dlmZ d dlmZ d dlmZ eeZdefdd	Zd
efddZddededed
efddZG dd deZeG dd dZeG dd dZdS )    N)	dataclass)Enumauto)Any)init_logger)StoreBooleanobjc                 C   sN   t | tr| jS t | trdd |  D S t | tttfr%dd | D S | S )z
    Recursively convert objects to JSON-serializable forms.
    - Enums -> their name
    - Sets/Tuples -> lists
    - Dicts/Lists -> recursively processed
    c                 S   s   i | ]	\}}|t |qS  
_json_safe).0kvr	   r	   h/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/configs/sample/sampling_params.py
<dictcomp>#   s    z_json_safe.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r	   r
   )r   r   r	   r	   r   
<listcomp>%   s    z_json_safe.<locals>.<listcomp>)
isinstancer   namedictitemslisttupleset)r   r	   r	   r   r      s   

r   returnc                   C   s   t t S N)struuiduuid4r	   r	   r	   r   generate_request_id)      r   _   r   replacement
max_lengthc                 C   st   t d| }|ddd}|dd}td||}tdd|d}|s*d	}|r8t||kr8|d
| }|S )a<  Create a filesystem- and ffmpeg-friendly filename.

    - Normalize to ASCII (drop accents and unsupported chars)
    - Replace spaces with underscores
    - Replace any char not in [A-Za-z0-9_.-] with replacement
    - Collapse multiple underscores
    - Trim leading/trailing dots/underscores and limit length
    NFKDasciiignore r    z[^A-Za-z0-9._-]z_+z._outputN)	unicodedata	normalizeencodedecodereplaceresubstriplen)r   r"   r#   
normalized
ascii_namer	   r	   r   _sanitize_filename-   s   	r4   c                   @   s&   e Zd Ze Ze ZdefddZdS )DataTyper   c                 C   s   | t jkrdS dS )Npngmp4)r5   IMAGEselfr	   r	   r   get_default_extensionF   s   
zDataType.get_default_extensionN)__name__
__module____qualname__r   r8   VIDEOr   r;   r	   r	   r	   r   r5   B   s    r5   c                   @   sN  e Zd ZU dZejZeed< dZe	dB ed< dZ
e	ee	 B dB ed< dZe	ee	 B dB ed< dZe	ed< dZe	dB ed	< dZe	dB ed
< dZe	dB ed< dZe	dB ed< dZedB ed< dZeed< dZeed< dZe	ed< dZeed< dZeed< dZedB ed< dZedB ed< dZeed< dZeed< dZeed< dZee eef  dB ed< dZ!eed< d Z"e#ed!< dZ$e#ed"< dZ%e#ed#< d$Z&e#ed%< d$Z'e#eB ed&< dZ(e#dB ed'< dZ)eed(< dZ*eed)< d*Z+eed+< dZ,eed,< dZ-eed-< dZ.e	dB ed.< d/Z/eed0< dZ0eed1< dZ1eed2< dZ2eed3< dZ3eed4< d/Z4eed5< dZ5eed6< d/Z6eed7< dZ7eed8< d9d: Z8d;d< Z9d`d>d?Z:de	ded=efd@dAZ;dBdC Z<dDdE Z=dFdG Z>dHdI Z?e@dJe	d=d fdKdLZAeBdJe	fdMdNZCd=e	fdOdPZDd=e#fdQdRZEeBdSeFd=eFfdTdUZGe@dVeHjIfdWdXZJdYdZ ZKdad\d]ZLeMd=efd^d_ZNdS )bSamplingParamsz-
    Sampling parameters for generation.
    	data_typeN
request_id
image_pathprompta  Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwardsnegative_promptprompt_pathoutput_pathoutput_file_namedefaultoutput_qualityoutput_compression   num_outputs_per_prompt*   seedcudagenerator_device
num_framesFnum_frames_round_downheightwidthheight_not_providedwidth_not_provided   fpssupported_resolutionsnum_inference_steps      ?guidance_scaleguidance_scale_2true_cfg_scale        guidance_rescalecfg_normalizationboundary_ratioenable_teacacheprofile   num_profiled_timestepsprofile_all_stagesdebugperf_dump_pathTsave_outputreturn_framesreturn_trajectory_latentsreturn_trajectory_decodedno_override_protected_fieldsadjust_framessuppress_logsreturn_file_paths_onlyenable_sequence_shardc                    s6   t  fdddD s j d j   _d S d S )Nc                 3   s    | ]	} j |V  qd S r   )rH   endswith)r   extr9   r	   r   	<genexpr>   s
    

z6SamplingParams._set_output_file_ext.<locals>.<genexpr>)z.mp4z.jpgz.pngz.webp.)anyrH   rA   r;   r9   r	   r9   r   _set_output_file_ext   s   z#SamplingParams._set_output_file_extc                 C   s   | j d u rK| jrKt| jtrKt| }d|d< tjt|dd}t	
 }||d | d d }td}| jd d  d	| d	| }|| _ | j d u r[td}d
| | _ t| j | _ |   d S )N rH   T)	sort_keyszutf-8   z%Y%m%d-%H%M%Sd   r    output_)rH   rD   r   r   dataclassesasdictjsondumpsr   hashlibsha256updater+   	hexdigesttimestrftimer4   ry   )r:   params_dict
params_strhasher
param_hash	timestampbaser	   r	   r   _set_output_file_name   s&   





z$SamplingParams._set_output_file_namer   c                 C   s   | j dksJ | jd u rd| _| jd u rd| _| jd u r*| jd ur*| | j| j| _| 	  t
jd}|d urD| jd urFt|| _d S d S d S )NrL   TSGLANG_TEST_NUM_INFERENCE_STEPS)rR   rU   rW   rT   rV   rK   rJ   _adjust_output_qualityrA   	_validateosenvirongetr[   int)r:   	env_stepsr	   r	   r   __post_init__   s   

zSamplingParams.__post_init__c                 C   s2   ddddd}|dkr|t jkrdS dS ||S )	z3Convert output_quality string to compression level.r}   Z   7   #   )maximumhighmediumlowrI   2   K   )r5   r?   r   )r:   rJ   rA   output_quality_mapperr	   r	   r   r      s   
z%SamplingParams._adjust_output_qualityc              	   C   s  | j r| j dstd| j t| jtr| jdkr$td| jt| jtr/| jdkr7td| jt| jtrB| jdkrJtd| j| jdurbt| jtrZ| jdkrbtd| j		dd
t	dt
dtddfdd}|d| jd	d |d| jd	d |d| jd	d |d| jdd | jdu rd| _nt| jtr| jrdnd| _| jdurt| jtst| jttfstd| jtt| jstd| jdt| j  krdksn td| jdS dS )zC
        check if the sampling params is correct by itself
        z.txtz$prompt_path must be a txt file, got r   z3num_outputs_per_prompt must be a positive int, got z fps must be a positive int, got z'num_frames must be a positive int, got Nz0num_inference_steps must be a positive int, got Tr   value
allow_noner   c                 S   s|   |d u r|rd S t |tst |ttfst|  d|tt|s-t|  d|t|dk r<t|  d|d S )Nz must be a number, got z must be finite, got r`   z must be non-negative, got )r   boolr   float
ValueErrormathisfinite)r   r   r   r	   r	   r   _finite_non_negative_float  s   z<SamplingParams._validate.<locals>._finite_non_negative_floatr]   )r   r^   r_   ra   Fr`   r\   z%boundary_ratio must be a number, got z#boundary_ratio must be finite, got z*boundary_ratio must be within [0, 1], got )T)rF   rt   r   r   rM   r   rY   rR   r[   r   r   r   r]   r^   r_   ra   rb   rc   r   r   r   )r:   r   r	   r	   r   r      s   















zSamplingParams._validatec                 C   s   |    d S r   )r   r9   r	   r	   r   check_sampling_param>  s   z#SamplingParams.check_sampling_paramc                 C   s0   |j  r| jdu rtd|j j ddS dS )zW
        check if the sampling params is compatible and valid with server_args
        NzServed model with task type 'z7' requires an 'image_path' input, but none was provided)	task_typerequires_image_inputrC   r   r   )r:   pipeline_configr	   r	   r   _validate_with_pipeline_configB  s   

z-SamplingParams._validate_with_pipeline_configc                 C   sh  |j }t| jtstdt| j |j j | _| jdu r1|jdur1|j| _t	
d| j  | jdurA| j sA| j | _| jdkrVtd| j d| j d| j | jdu ry| jdu ry| jdury| jd \| _| _t	d| jd   | jdur| jdur| jdur| j| jf| jvrd	d
d | jD }d| j d| j d| }t	| | jrd| _t	d |j r|j  st	
d d| _n\| jr(|j | j| _|jj}| j}|j}|jjj}|r|d | d }	|	|j dkr(| j rt!d|	| | }
n	t"#|	| | }
|r|
d | d }t	d| j||j || _|j$s2| %  dS dS )zH
        final adjustment, called after merged with user params
        z#`prompt` must be a string, but got Nz2Overriding output_path with server configuration: r   zDheight, width, and num_frames must be positive integers, got height=z, width=z, num_frames=z'Resolution unspecified, using default: z, c                 S   s   g | ]\}}| d | qS )xr	   )r   whr	   r	   r   r   z  s    z*SamplingParams._adjust.<locals>.<listcomp>zUnsupported resolution: r   z4, output quality may suffer. Supported resolutions: Fz?Sequence dimension shard is enabled, disabling frame adjustmentz4Setting `num_frames` to 1 for image generation modelrL   zEAdjusting number of frames from %s to %s based on number of GPUs (%s))&r   r   rD   r   	TypeErrortyper   rA   rG   loggerri   rE   isspacer0   rR   r   rT   rU   rZ   infojoinwarningrs   rp   is_image_genallow_set_num_framesadjust_num_frames
vae_configuse_temporal_scaling_framesnum_gpusarch_configtemporal_compression_ratiorS   maxr   ceilcomfyui_moder   )r:   server_argsr   supported_str	error_msgr   rR   r   temporal_scale_factororig_latent_num_framesnew_latent_num_framesnew_num_framesr	   r	   r   _adjustM  s   







zSamplingParams._adjust
model_pathc                 K   s8   ddl m} |dd }|||d}|jdi |}|S )Nr   )get_model_infobackendr   r	   )sglang.multimodal_gen.registryr   popsampling_param_cls)clsr   kwargsr   r   
model_infosampling_paramsr	   r	   r   from_pretrained  s
   zSamplingParams.from_pretrainedc                 O   sN  z
t j| |jd}W ny ttfy } zktj| rx| drxt	|dd }ddl
m} |r1||nd }|d urq|\}	}
z|
 }td|
j d| d|  W n* typ } ztd	|
j d
| d t  }W Y d }~nd }~ww td|  W Y d }~nd }~ww t|}|dd  t |i |}|| || ||j |S )Nr   z.safetensorspipeline_class_namer   )get_pipeline_config_classeszUsing z for z+ safetensors file (no model_index.json): %szFailed to instantiate z: z. Using default SamplingParamsz*Could not get pipeline config classes for diffusers_kwargs)r@   r   r   AttributeErrorr   r   pathisfilert   getattrr   r   r   r   r<   	Exceptionr   r   r   _merge_with_user_paramsr   r   r   )r   r   argsr   r   er   r   config_classesr    sampling_params_clsimport_erroruser_kwargsuser_sampling_paramsr	   r	   r   from_user_sampling_params_args  sP   

&

z-SamplingParams.from_user_sampling_params_argsc                 C   s   | j  d| j S )Nr   )rU   rT   r9   r	   r	   r   output_size_str      zSamplingParams.output_size_strc                 C   s   | j | j S r   )rR   rY   r9   r	   r	   r   seconds  r   zSamplingParams.secondsparserc                 C   sH  | j dtdtjd | j ddtjd | j ddtjd | j ddtjd	d
 | j dttj	dd | j dddtj
dd | j ddtjdd
 | j dttjdd | j dttjdd | j dttjdd | j dttjdd | j dttjdd | j dttjdd | j d ttjd!d | j d"ttjd#d | j d$ttjg d%d&d' | j d(ttjd)d | j d*ttjd+d | j d,ttjd-d | j d.dd/d0d1 | j d2dd3d4d1 | j d5dd6d7d1 | j d8dd9d:d1 | j d;ttjd<d | j d=ttjd>d | j d?ttjd@d | j dAttjdBdCdD | j dEttjdFd | j dGttjdHdIdD | j dJttjdKd | j dLdtj dMd
 | j dNdOdPdQd1 | j dRdtj!dSd
 | j dTtdtj"dUdV | j dWtdXdYd | j dZdtj#d[d
 | j d\dtj$d]d
 | j d^tdXd_d | j d`dtj%dad
 | j dbt&tj'dcd
 | j ddt&tj(ded
 | j dft&tj)dgd
 | S )hz*Add CLI arguments for SamplingParam fieldsz--data-type+)r   nargsrI   z--num-frames-round-down
store_true)actionrI   z--enable-teacachez	--profilez)Enable torch profiler for denoising stage)r   rI   helpz--num-profiled-timestepsz+Number of timesteps to profile after warmup)r   rI   r   z--profile-all-stagesrh   z0Used with --profile, profile all pipeline stages)r   destrI   r   z--debugrz   z--promptzText prompt for generationz--negative-promptz#Negative text prompt for generationz--prompt-pathz)Path to a text file containing the promptz--output-file-namezName of the output filez--output-qualityz<Output quality setting (default, low, medium, high, maximum)z--output-compressionzROutput compression level (0-100, higher means better quality but larger file size)z--num-outputs-per-promptz(Number of outputs to generate per promptz--seedzRandom seed for generationz--generator-device)rP   musacpuz>Device for random generator (cuda, musa or cpu). Default: cuda)r   rI   choicesr   z--num-frameszNumber of frames to generatez--heightzHeight of generated outputz--widthzWidth of generated outputz--4kresolution_4kz Set resolution to 4K (3840x2160))r   r   r   z--2kresolution_2kz Set resolution to 2K (2560x1440)z--1080presolution_1080pz#Set resolution to 1080p (1920x1080)z--720presolution_720pz!Set resolution to 720p (1280x720)z--fpsz"Frames per second for saved outputz--num-inference-stepszNumber of denoising stepsz--guidance-scalezClassifier-free guidance scalez--guidance-scale-2r^   zNSecondary guidance scale for dual-guidance models (e.g., Wan low-noise expert))r   rI   r   r   z--guidance-rescalezGuidance rescale factorz--cfg-normalizationrb   z*CFG renormalization factor (for Z-Image). z--boundary-ratiozBoundary timestep ratioz--save-outputz"Whether to save the output to diskz--no-save-outputstore_falserk   zDon't save the output to diskz--return-framesz Whether to return the raw framesz--image-pathzPath(s) to input image(s) for image-to-image / image-to-video generation. For multiple images, pass them as space-separated values, e.g.: --image-path "img1.png" "img2.png")r   r   rI   r   z--moba-config-pathNz>Path to a JSON file containing V-MoBA specific configurations.z--return-trajectory-latentsz Whether to return the trajectoryz--return-trajectory-decodedz(Whether to return the decoded trajectoryz--diffusers-kwargszoJSON string of extra kwargs to pass to diffusers pipeline. Example: '{"output_type": "latent", "clip_skip": 2}'z--no-override-protected-fieldszFIf set, disallow user params to override fields defined in subclasses.z--adjust-frameszEnable/disable adjusting num_frames to evenly split latent frames across GPUs and satisfy model temporal constraints. If disabled, tokens might be padded for SP.Default: true. Examples: --adjust-frames, --adjust-frames true, --adjust-frames false.z--return-file-paths-onlyznIf set, output file will be saved early to get a performance boost, while output tensors will not be returned.z--enable-sequence-shardz:Enable sequence dimension shard with sequence parallelism.)*add_argumentr   r5   r?   r@   rS   rd   re   r   rg   rh   ri   rD   rE   rF   rH   rJ   rK   rM   rO   rQ   rR   rT   rU   rY   r[   r   r]   r^   ra   rb   rc   rk   rl   rC   rm   rn   ro   r   rp   rr   rs   )r   r	   r	   r   add_cli_args  s  
zSamplingParams.add_cli_argsr   c                    s   t  dr jrd _d _n,t  dr jrd _d _nt  dr- jr-d _d	 _nt  d
r; jr;d _d _dd t| D }t	t
  }||@ }d _d _ fdd|D S )Nr   i   ip  r   i 
  i  r   i  i8  r   i   i  c                 S   s   h | ]}|j qS r	   )r   r   attrr	   r	   r   	<setcomp>'  s    z.SamplingParams.get_cli_args.<locals>.<setcomp>Fc                    s"   i | ]}t  |r|t |qS r	   )hasattrr   r   r   r	   r   r   ,  s   " z/SamplingParams.get_cli_args.<locals>.<dictcomp>)r  r   rU   rT   r   r   r   r   fieldsr   varskeysrV   rW   )r   r   sampling_params_fields
args_attrsattrsr	   r  r   get_cli_args  s$   zSamplingParams.get_cli_argsc                 C   s   t j| j| jS r   )r   r   r   rG   rH   r9   r	   r	   r   output_file_path.  r   zSamplingParams.output_file_pathuser_paramsc           
      C   s   |du rdS t t| j }|j }t|D ]#}|j}t||}tt	|}||k}||v }	|r;|s5|	s;t
| || q|j| _|j| _|   dS )zO
        Merges parameters from a user-provided SamplingParams object.
        N)r   r   __annotations__r  ro   r   r  r   r   r@   setattrrV   rW   r   )
r:   r  predefined_fieldsallow_override_protectedfield
field_name
user_valuedefault_class_valueis_user_modifiedis_protected_fieldr	   r	   r   r   1  s(   

z&SamplingParams._merge_with_user_paramsc                 C   sT   | j r&| jr&| jd d d | j d | jd g}|d |d  |d  }|S d}|S )NrL      r|   r      )rT   rU   rR   )r:   latents_sizen_tokensr	   r	   r   r  L  s   zSamplingParams.n_tokens)r   N)r  r@   )Or<   r=   r>   __doc__r5   r?   rA   r  rB   r   rC   r   rD   rE   rF   rG   rH   rJ   rK   r   rM   rO   rQ   rR   rS   r   rT   rU   rV   rW   rY   rZ   r   r[   r]   r   r^   r_   ra   rb   rc   rd   re   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   ry   r   r   r   r   r   r   r   classmethodr   staticmethodr   r   r   r   r   argparse	Namespacer	  r
  r   propertyr  r	   r	   r	   r   r@   M   s   
 



!Vs6  
r@   c                   @   s   e Zd ZU dZeed< dS )CacheParamsnone
cache_typeN)r<   r=   r>   r#  r   r  r	   r	   r	   r   r!  [  s   
 r!  )r    r!   )r  r   r   r   r   r   os.pathr.   r   r)   r   r   enumr   r   typingr   1sglang.multimodal_gen.runtime.utils.logging_utilsr   sglang.multimodal_gen.utilsr   r<   r   r   r   r   r   r4   r5   r@   r!  r	   r	   r	   r   <module>   s>         