K i*#0ddlZddlZddlZddlZddlmZddlmZddlm Z m Z m Z ddl m Z mZmZddlmZddlmZmZd Zd efd Zd ed efd Zd edeeeefd efdZd ed dfdZ d!dddddddeedeedeed eedeeeefdeef dZd!deed dfdZd!deed dfdZd"deedeed dfdZ d#deedeedeed eedeedeef dZ d!ddddddddeedeedeed eedeedeef d Z!y)$N)knobs)proton)set_profiling_offset_profiling_onis_command_line) HookManager LaunchHookInstrumentationHook)BaseMode)OptionalUnionrreturnctjjjj j }|dk(ry|dk(ryt d)Ncudacuptihip roctracerz/No backend is available for the current target.)tritonruntimedriveractiveget_current_targetbackend ValueError)rs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/triton/profiler/profile.py_select_backendrsHnn##**==?GGG& E JKKrcd}|dk(rltjj}|Ptt j t jjjdz dz dz dz }|S)Nrbackendsnvidialib) rr cupti_dirstrpathlibPath__file__parentabsolute)rlib_paths r_get_backend_default_pathr,sjH'<<))  7<<188??HHJZWZbbejjmttuH Ormodec|dk(rGtjjjj j }|r|d|S|S|r t |SdS)Ninstrumentation:r )rrrrrrr%)rr-prefixs r _get_mode_strr2&sX##&&--@@BJJ%)&4&!5v53t9$"$rcp|dk(r1ddg}|D]'}tj|dtd|dyy)NrHIP_VISIBLE_DEVICESCUDA_VISIBLE_DEVICESz3Proton does not work when the environment variable zK is set on AMD GPUs. Please unset it and use `ROCR_VISIBLE_DEVICES` instead)osgetenvr)rhip_device_envsenvs r _check_envr:-s[+02HI" Cyyd#/ I#OZ[ rshadowtreecontextdatarr-hooknamer>r?r@cftryt|tn|}| tn|}t |}t ||}t |tj||||||}|dk(rtjt||dk(rtjt|||S)a Start profiling with the given name and backend. Usage: ```python proton.start("my_profile") # do something proton.finalize() ``` Args: name (str, optional): The name (with path) of the profiling session. If not provided, the default name is "~/proton.", where suffix is the default format according to the data type. For example, if data is "tree", the default name is "~/proton.hatchet". context (str, optional): The context to use for profiling. Available options are ["shadow", "python"]. Defaults to "shadow". data (str, optional): The data structure to use for profiling. Available options are ["tree", "trace"]. Defaults to "tree". backend (str, optional): The backend to use for profiling. Available options are [None, "cupti", "roctracer", "instrumentation"]. Defaults to None, which automatically selects the backend matching the current active runtime. mode (Union[str, BaseMode], optional): The "mode" to use for profiling, which is specific to the backend. Can be a string or an instance of BaseMode (or any subclass thereof). Defaults to None. For "cupti", available options are [None, "pcsampling"]. For "roctracer", available options are [None]. For "instrumentation", available options are [None]. Each mode has a set of control knobs following with the mode name. For example, "pcsampling" has an "interval" control knob, expressed as "pcsampling:interval=1000". hook (str, optional): The hook to use for profiling. Available options are [None, "launch"]. Defaults to None. Returns: session (int): The session ID of the profiling session. Nrr/) rrDEFAULT_PROFILE_NAMErr,r2r: libprotonstartr registerr r ) rAr>r?rr-r@ backend_pathmode_strsessions rrErE7s^#'< TD#*?oG,W5LWd+HwoodGT7HlSG xZ\73##06@ NrrIctr|dk7r tdtj||t j yt j|y)z Activate the specified session. The profiling session will be active and data will be recorded. Args: session (int): The session ID of the profiling session. Defaults to None (all sessions) Returns: None rzEOnly one session can be activated when running from the command line.N)rrr activaterD activate_allrIs rrKrK~sIW\`aa! 7#rctr|dk7r tdtj||t j yt j|y)a  Stop the specified session. The profiling session's data will still be in the memory, but no more data will be recorded. Args: session (int): The session ID of the profiling session. Defaults to None (all sessions) Returns: None rzGOnly one session can be deactivated when running from the command line.N)rrr deactivaterDdeactivate_allrMs rrOrOsIW\bcc7#  "W%r output_formatctj|| ttj|yt r|dk7r t dtj||y)a Finalizes a profiling session. Flush and write the profiling data to the file specified by the session name. Args: session (int, optional): The session ID to finalize. If None, all sessions are finalized. Defaults to None. output_format (str, optional): The output format for the profiling results. Available options are ["hatchet", "chrome_trace"]. Returns: None NrzEOnly one session can be finalized when running from the command line.)r unregisterrrD finalize_allrrfinalize)rIrQs rrUrUsS7#}-  Ade e7M2rcZtjfd}|S)z Context manager for profiling. Internally use only. Args: See start() for the arguments. Returns: wrapper (function): The wrapped function. cPt  }|i|}t||S)Nr=)rErO) argskwargsrIretrr>r?funcr@r-rAs rwrapperz_profiling..wrappers5gD'PT[_`D#F#7 r) functoolswraps)r[rAr>r?rr-r@r\s``````` r _profilingr_s,&__T NrrAr>r?rr-r@c N| fd}|St|S)z Decorator for profiling. Usage: ```python @proton.profile def foo(): pass ``` Args: See start() for the arguments. Returns: decorator (function): The decorator function. c (t|S)Nr`r_)frr>r?r@r-rAs r decoratorzprofile..decoratorsadG$PW^bimn nrr`rc)r[rAr>r?rr-r@res `````` rprofilerfs96 | o o$T7w]ahlmmr)N)Nr )Nr;r<NNN)"r]rr6r&rtriton._C.libprotonrrDflagsrrrhooksr r r r-r typingr rrCr%rr,r2r:rEintrKrOrUr_rfrrrmsK 3GG??"LL s s %3%huS(]/C&D%%D& !+/D 3-Dc]D 3- D c] D 5h' ( D 3-DN$hsm$t$,& &&,3hsm38C=3RV34% ! 3-c] 3-  c]  3-  3-< #n% !#n 3-#nc] #n 3- #n c] #n 3-#n 3-#nr