
    )`i_>              	          d Z ddlZddlZddlmZ ddlmZmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ d Zd Z ej        d           ej        dddd          ej        d                                     Zeeej         e            e e            dZ	  e            ed<   ej         !                    ej         "                    ed         dd                    Z#n# e$$ r
 ded<   dZ#Y nw xY we%                    d          d             Z&e%                    d          d             Z'e%                    d          d              Z(e%                    d!          d"             Z)e%                    d#          d$             Z*e%                    d%           ej        d&dd'           ej        d( ej+        g d)          d*d+,          d-                                     Z,e%                    d.           ej-        d/d0          d1                         Z.e%                    d2           ej-        d3dd45           ej        d6d7dd89          d:                                     Z/e%                    d;           ej        d<d=dd>?          d@                         Z0e1dAk    r e             dS dS )Ba3  
Copyright (c) 2025 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)tabulate   )ArtifactPathdownload_artifactsclear_cubinget_artifacts_status)clear_cache_dirjit_spec_registry)FLASHINFER_CUBINS_REPOSITORY)FLASHINFER_CACHE_DIRFLASHINFER_CUBIN_DIR)current_compilation_context)get_cuda_pathget_cuda_version)__version__c                      	 t                       t          j        dd           dS # t          $ r$} t          j        d|  d           Y d} ~ dS d} ~ ww xY w)z!Helper function to download cubinu4   ✅ All cubin download tasks completed successfully.greenfgu   ❌ Cubin download failed: redN)r   clicksecho	Exceptiones    g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/__main__.py_download_cubinr   &   s    AJwWWWWWW A A A5!55%@@@@@@@@@@A   $( 
AAAc                  :   t          j                    } | st          j        dd           	 ddlm}  |            }t          j        d| dd           t          j                    } n0# t          $ r#}t          j        d	| d
           Y d}~nd}~ww xY w| S )z0Helper function to ensure modules are registeredz0No modules found. Registering default modules...yellowr   r   )register_default_modulesu   ✅ Registered z modulesr   u    ❌ Module registration failed: r   N)r
   get_all_statusesr   r   aotr!   r   )statusesr!   num_registeredr   s       r   _ensure_modules_registeredr&   /   s     133H 	JF8TTTT	J5555555577NKB.BBBwOOOO(9;;HH 	J 	J 	JK>1>>5IIIIIIIII	JOs   =A+ +
B5BBT)invoke_without_commandz--download-cubindownload_cubin_flagDownload artifacts)is_flaghelpc                     |rt                       dS | j        (t          j        |                                            dS dS )zFlashInfer CLIN)r   invoked_subcommandr   echoget_help)ctxr(   s     r   clir1   ?   sM      #			'
3<<>>""""" 
(	'    )r   r   FLASHINFER_CUDA_ARCH_LISTFLASHINFER_CUDA_VERSIONr   CUDA_VERSION	CUDA_HOMEbinnvcc Fzshow-configc                  	   t          j        dd           t          j        ddd           t          j        dt           d	           	 d
dl} 	 | j                            d          }t          j        ddd           t          j        d| d	           nB# | j        j        $ r0 t          j        ddd           t          j        dd           Y nw xY w	 | j                            d          }t          j        ddd           t          j        d| d	           nB# | j        j        $ r0 t          j        ddd           t          j        dd           Y nw xY wn0# t          $ r#}t          j        d| d           Y d}~nd}~ww xY wd
dl}t          j        dd           t          j        ddd           t          j        d|j         d	           t          j        ddd           |j	        
                                rt          j        dd           nt          j        dd           t          j        dd           t          j        dd           t                                          D ]8\  }}t          j        | ddd           t          j        d| d	           9t          j        ddd           t          rt          j        dd           nt          j        dd           t          j        dd           t          j        d d           t          j                                        D ]M\  }}|                    d!          s3t          j        | ddd           t          j        d| d	           Nt          j        dd           t          j        d"d           t#                      }t%          d# |D                       }	t'          |          }
t          j        d$|	 d%|
 d&d	           t          j        dd           t          j        d'd           t)                      }|rrt+          j                    }t          j        d(|d)          d	           t          j        d*|d+          d           t          j        d,|d-          d           dS dS ).zShow configurationz=== Version Info ===r    r   zFlashInfer version:magentaF)r   nl cyanr   Nzflashinfer-cubinzflashinfer-cubin version:zflashinfer-cubin:z Not installedr   zflashinfer-jit-cachezflashinfer-jit-cache version:zflashinfer-jit-cache:z!Error checking package versions: z=== Torch Version Info ===zTorch version:zCUDA runtime available:z Yesr   z Nor9   whitez=== Environment Variables ===:zNVCC found:z=== Artifact Path ===__z=== Downloaded Cubins ===c              3   $   K   | ]\  }}|d V  dS )r   N ).0_existss      r   	<genexpr>z"show_config_cmd.<locals>.<genexpr>   s+      ==yq&f=======r2   zDownloaded /z cubinsz=== Module Status ===Total modules: totalz
compiled: compiledNot compiled: not_compiled)r   r   r   importlib.metadatametadataversionPackageNotFoundErrorr   torchcudais_availableenv_variablesitems
found_nvccr   __dict__
startswithr   sumlenr&   r
   	get_stats)	importlibcubin_versionjit_cache_versionr   rR   namevaluepathstatusnum_downloadedtotal_cubinsmodule_statusesstatss                r   show_config_cmdrh   ]   s    
K&84444	K%)>>>>	K!K!!f----J!!!!	4%.667IJJMK3	eLLLLK+M++77777!6 	4 	4 	4K+	eDDDDK(U333333	4	4 ) 2 : :;Q R RK7I%PPPPK/-//F;;;;;!6 	4 	4 	4K/I%HHHHK(U333333	4  J J J;;;IIIIIIIIIJ LLL	K,::::	K Y59999	K'E%''F3333	K)iEBBBBz   %Fw'''''Ee$$$$	Kw 
K/H====$**,, , ,etJJJ97777KKKF+++++	K)6666 %Fw'''''Ee$$$$	Kw 
K'H5555"+1133 / /
dt$$ 	/K4


yU;;;;K
D

v....	Kw 
K+9999!##F==F=====Nv;;L	KDnDD|DDDPPPP	Kw 
K'H5555022O H!+--6eGn666BBBB4z!244CCCC<U>%:<<GGGGGG	H Hsb   E. A
B E. <CE. CE.  A
D+ *E. +<E*'E. )E**E. .
F8FFzlist-cubinsc                     t                      } g }| D ]<\  }}|rdnd}|rdnd}|                    |t          j        ||          g           =t          j        t          |ddgd	                     t          j        d
d           dS )zList downloaded cubins
DownloadedMissingr   r   r   CubinStatusgithubheaderstablefmtr9   r?   N)r   appendr   styler.   r   r   )rc   
table_data	file_namerF   
status_strcolors         r   list_cubins_cmdrx      s     "##FJ# J J	6%+:\\
!,u9ek*&G&G&GHIIII	Jx
Wh,?(SSSTTT	Kwr2   zdownload-cubinc                  "    t                       dS )r)   N)r   rC   r2   r   download_cubin_cmdrz      s     r2   zclear-cachec                      	 t                       t          j        dd           dS # t          $ r$} t          j        d|  d           Y d} ~ dS d} ~ ww xY w)zClear cacheu   ✅ Cache cleared successfully.r   r   u   ❌ Cache clear failed: r   N)r	   r   r   r   r   s    r   clear_cache_cmdr|      s~    >5'BBBBBB > > >2q22u==========>r   zclear-cubinc                      	 t                       t          j        dd           dS # t          $ r$} t          j        d|  d           Y d} ~ dS d} ~ ww xY w)zClear cubinu   ✅ Cubin cleared successfully.r   r   u   ❌ Cubin clear failed: r   N)r   r   r   r   r   s    r   clear_cubin_cmdr~      s{    >5'BBBBBB > > >2q22u==========>r   zmodule-statusz
--detailedzShow detailed informationz--filter)allr#   jitrK   not-compiledr   z,Filter modules by compilation type or status)typedefaultr+   c                    t                      }|sdS d d dv rfd|D             }|                    d            | r|D ]}t          j        d|j         d	d
           t          j        dt          j        |j        |j        rdnd                      |j        rt          j        d|j         d           t          j        dt          |j
                   dd           |j        rt          j        dd           t          j        d|j                            d           d           t          j                     ng }|D ]_}|j        rdnd}|                    |j        t          j        |j        |          t          |j
                  |j        rdndg           `g d}t          j        t!          ||d                     t#          j                    }t          j                     t          j        dd           t          j        d|d          d	           t          j        d |d!          d"           t          j        d#|d$          d           dS )%zShow module compilation statusNc                     | j         S Nis_compiledss    r   <lambda>z#module_status_cmd.<locals>.<lambda>   s    am r2   c                     | j          S r   r   r   s    r   r   z#module_status_cmd.<locals>.<lambda>   s    am"3 r2   )rK   r   c                 6    g | ]}          |          |S rC   rC   )rD   r   filter
filter_maps     r   
<listcomp>z%module_status_cmd.<locals>.<listcomp>   s.    AAA!+=:f+=a+@+@AAAAAr2   c                     | j         S r   r`   xs    r   r   z#module_status_cmd.<locals>.<lambda>   s     r2   keyModule: r>   Tr   boldz
  Status: r   r   r   z  Library: r?   z  Sources: z file(s)z  Device Linking: Requiredr    z  Created: %Y-%m-%d %H:%M:%SYesNo)zModule NameTyperm   SourceszDevice Linkingrn   ro   z=== Summary ===rI   rJ   z
Compiled: rK   r;   rL   rM   )r&   sortr   r   r`   rs   rc   r   library_pathr[   sourcesneeds_device_linking
created_atstrftimer.   rr   r   r
   r\   )	detailedr   r$   rc   rt   status_colorrp   rg   r   s	    `      @r   module_status_cmdr      s    *++H  ,+33 J AAAAAxAAA MM&&M'''  M 	 	FK06;00V$GGGGKfU[fFX;c77^cdddff   " M?&*=??GLLLLKCc&.&9&9CCCPPPP* G8XFFFFKOf/889LMMOO    JLLLL	" 
 		 		F&,&8C77eLKK,???''#8BEEd	    QPP
8J(KKKLLL '))E	JLLL	K!h////	K2%.22v>>>>	K0U:.00Y????	K8~!688UCCCCCCr2   zlist-modulesmodule_name)requiredc           
         t                      }|sdS | rQt          j        |           }|st          j        d|  dd           dS t          j        d|j         dd	           t          j        d
t          j        |j        |j        rdnd                      |j	        rt          j        d|j	         d           t          j        d|j
                            d           d           t          j        d|j        rdnd d           t          j        dd           t          |j        d          D ]!\  }}t          j        d| d| d           "dS t                      }|sdS |                    d            t          j        ddd	           |D ]E}|j        rdnd}t          j        d|j         dt          j        |j        |                      FdS )z#List or inspect compilation modulesNzModule 'z' not found.r   r   r   r>   Tr   zStatus: r   zLibrary Path: r?   z	Created: r   zDevice Linking: RequiredzNot requiredzSource Files:r   z  z. c                     | j         S r   r   r   s    r   r   z"list_modules_cmd.<locals>.<lambda>E  s    AF r2   r   zAvailable compilation modules:z - )r&   r
   get_spec_statusr   r   r`   rs   rc   r   r   r   r   r   	enumerater   r   )r   r$   rc   isourcer   s         r   list_modules_cmdr     sy   
 *++H  #"2;?? 	K<;<<<GGGGF,v{,,dCCCC`u{6=@R5]WWX]^^^``	
 	
 	
  	LK>)<>>7KKKKI)223FGGIIg	
 	
 	
 	
 	^V-H\zzn^^	
 	
 	
 	
 	O0000"6>155 	8 	8IAvK*Q**&**w77777	8 	8 .// 	F**+++4dKKKK 	 	F&,&8C77eLKRV[RRU[<%P%P%PRR   	 	r2   zexport-compile-commandsrb   zcompile_commands.json)r   r   z--outputz-oz*Output file path (overrides PATH argument))r   r+   c                    ddl }||n| }t                       t          j                    }|st	          j        dd           dS g }|                                D ]d}	 |                                }|                    |           -# t          $ r+}t	          j        d|j
         d| d           Y d}~]d}~ww xY w	 t          |d          5 }	|                    ||	d	
           ddd           n# 1 swxY w Y   t	          j        dt          |           d| d           dS # t          $ r$}t	          j        d| d           Y d}~dS d}~ww xY w)zrExport compile commands to compile_commands.json

    PATH: Output file path (default: compile_commands.json)
    r   NzNo modules found to export.r    r   z1Warning: Failed to generate compile commands for z: w   )indentu   ✅ Successfully exported z compile commands to r   u&   ❌ Failed to write compile commands: r   )jsonr&   r
   get_all_specsr   r   valuesget_compile_commandsextendr   r`   opendumpr[   )
rb   outputr   output_path	all_specsall_compile_commandsspeccompile_commandsr   fs
             r   export_compile_commands_cmdr   N  s3    KKK #.&&DK     "/11I 1h????   ""  	#88:: ''(89999 	 	 	KTDITTQRTT        	L+s## 	9qII*AaI888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9f-A)B)BffYdff	
 	
 	
 	
 	
 	
  L L L@Q@@UKKKKKKKKKKLsT   )B		
B>!B99B>D- C7+D- 7C;;D- >C;?,D- -
E7EEreplayz--dirdump_dirz>Directory containing dump files (or root directory of session))r   r+   c           
      \   ddl m}m} d}t          j                            |           st          j        d|  d           dS t          j                            t          j                            | d                    }	 |rt          j        d	|  d
d            || d|d          }|	                    d          rt          j        dd           dS |	                    d          r!t          j        d|d          d           dS t          j        dd           dS t          j        d|  d
d            || |          }d}d}t          |          D ]\  }	}
d|
v r3t          j                            |
	                    dd                    nd|	dz    }d|
v r d|
d         v r|
d         d         }| d| d}d|
v r.t          j        d |	dz    d!| d"|
d          d           |dz  }|
	                    d          r&t          j        d |	dz    d!| d#d           |dz  }t          j        d |	dz    d!| d$d           |dz  }t          j        d%| d&| d'd(           dS # t          $ r$}t          j        d)| d           Y d}~dS d}~ww xY w)*z$Replay API calls from dump directoryr   )replay_sequencereplay_from_dumprS   u   ❌ Directory not found: r   r   Nzmetadata.jsonlzReplaying single dump from z...r>   T)compare_outputsdeviceruncomparison_matchu#   ✅ Replay passed (outputs matched)r   execution_erroru   ❌ Execution failed: u1   ⚠️  Replay finished but outputs did not matchr    zReplaying session from )r   r   r   r9   call_rO   function_namez ()error[z] u   : ❌ Error: u   : ✅ Passedu   : ⚠️  Mismatchz

Summary: z	 passed, z failed/mismatchr?   u   ❌ Replay failed: )api_loggingr   r   osrb   rF   r   r   joingetr   basenamer   )r   r   r   r   is_single_dumpresultresultspassedfailedr   res	dump_name	func_namer   s                 r   
replay_cmdr     s    ?>>>>>>>F7>>(## :::uEEEE W^^BGLL;K$L$LMMN29 .	KChCCCOOOO%%$v4  F zz,-- ^AgNNNNNN-.. ^HV4E-FHHU      OT\]]]]]] K?(???FKKKK%ohv>>>GFF#G,,    3 "S(( G$$SWWZ%<%<===(Q  $$C
O)K)K #J @I#, < <	 < < <Ic>>KKAEKKYKKS\KKPU    aKFFWW/00  K DAE D DY D D DQQQQaKFFK JAE J JY J J JxXXXXaKFFKGfGGvGGGG       9 9 9-!--%88888888889s,   AI= 4I= I= )EI= =
J+J&&J+__main__)2__doc__r   r   r   	artifactsr   r   r   r   r   r	   r
   jit.cubin_loaderr   jit.envr   r   jit.corer   jit.cpp_extr   r   rP   r   r   r&   groupoptionpass_contextr1   TARGET_CUDA_ARCHSrU   rb   isfiler   rW   r   commandrh   rx   rz   r|   r~   Choicer   argumentr   r   r   __name__rC   r2   r   <module>r      s   " 
			                   4 3 3 3 3 3 3 3 : : : : : : ? ? ? ? ? ? ? ? 1 1 1 1 1 1 8 8 8 8 8 8 8 8 !          A A A    D)))-tBV   # #   *)
# 10!<!N//11$@$$&& !.M+];-GPV W WXXJJ   !#M+JJJ
 ]QH QH QHh ]        
 ]> > > ]> > > _lD/JKKK	GGG	H	H	7	  9D 9D  LK 9Dx ^...* * /. *Z &''0GHHH	5	  )L )L  IH (')LX X	I	  ?9 ?9  ?9D zCEEEEE s   -AC5 5DD