
    &`ip                         d dl mZ d dlmZmZmZmZ d dlZd dl	Z	d dl
mZmZ d dlmZ d dlmZmZ e	 	 	 ddeee                  d	ee         d
edeeee         f         fd            ZdS )    )defaultdict)DefaultDictListOptionalSetN)DEFAULT_POLICY_IDSampleBatch)DeveloperAPI)Suspect _test_some_code_for_memory_leaks   to_checkrepeatsmax_num_trialsreturnc                 f  	
 | j         |ph d}t          t                    }d|v rj        
J d            j                                        d         

j        }|                                
fd}t          dd||pd|	          }|r|d                             |           d
|v rj	        t                   t          j        d j                                                  t          d           fd}t          dd||pd|	          }|r|d
                             |           j                            dd          sM                    d          	t          dd	fd|pd|	          }|r|d
                             |           d|v rj	        t                   t          j        d j                                                  t          d           t          ddfd|pd|	          }|r|d                             |           d|v rGt          d           fd}t          dd||pd |	          }|r|d                             |           d!|v r| j                            dd          r| j        j        s
J d"            |                                                     d                                          	t          d#           	fd$}t          d%d||pd|	          }|r|d!                             |           |S )&a  Diagnoses the given Algorithm for possible memory leaks.

    Isolates single components inside the Algorithm's local worker, e.g. the env,
    policy, etc.. and calls some of their methods repeatedly, while checking
    the memory footprints and keeping track of which lines in the code add
    un-GC'd items to memory.

    Args:
        algorithm: The Algorithm instance to test.
        to_check: Set of strings to indentify components to test. Allowed strings
            are: "env", "policy", "model", "rollout_worker". By default, check all
            of these.
        repeats: Number of times the test code block should get executed (per trial).
            If a trial fails, a new trial may get started with a larger number of
            repeats: actual_repeats = `repeats` * (trial + 1) (1st trial == 0).
        max_num_trials: The maximum number of trials to run each check for.

    Raises:
        A defaultdict(list) with keys being the `to_check` strings and values being
        lists of Suspect instances that were found.
    >   envmodelpolicyrollout_workerr   NzERROR: Cannot test 'env' since given Algorithm does not have one in its local worker. Try setting `create_local_env_runner=True`.r   c                  |    d}                                   	                               \  }}}}}| dz  } |rd S %)Nr   T   )resetstep)ts_doneaction_sampler   s      p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/utils/debug/memory.pycodez check_memory_leaks.<locals>.code<   sS    BIIKKK $'88M#:#: 1dAqa E    z3Looking for leaks in env, running through episodes.   )descinitr    r   r   r   c                 6    t          j        | gdz  d          S )N
   r   )axis)npstackss    r   <lambda>z$check_memory_leaks.<locals>.<lambda>Y   s    bhsRxa000 r!   zLooking for leaks in Policyc                  6                         d i           d S )Nobs)compute_actions_from_input_dictr.   r   s   r   r    z check_memory_leaks.<locals>.code^   s/    223    r!   z,Calling `compute_actions_from_input_dict()`.i  enable_rl_module_and_learnerF   )
batch_sizezCalling `learn_on_batch()`.c                  .                                    S N)learn_on_batch)dummy_batchr   s   r   r,   z$check_memory_leaks.<locals>.<lambda>{   s    V22;?? r!   d   r   c                     | d          S r5    r*   s    r   r,   z$check_memory_leaks.<locals>.<lambda>   s
    1T7 r!   zLooking for leaks in ModelzCalling `[model]()`.c                  F                         t          j         i          S r5   )r   r	   OBSr0   s   r   r,   z$check_memory_leaks.<locals>.<lambda>   s    {&<== r!   r   z(Looking for leaks in local RolloutWorkerc                  X                                                                         d S r5   )sampleget_metrics)local_workers   r   r    z check_memory_leaks.<locals>.code   s-    !!!$$&&&&&r!   z'Calling `sample()` and `get_metrics()`.2   learnerzThis test will miss leaks hidden in remote workers. Please make sure that there is a local learner inside the learner group for this test.zLooking for leaks in Learnerc                  2                                     d S r5   )update)r7   learner_groups   r   r    z check_memory_leaks.<locals>.code   s      -----r!   z Calling `LearnerGroup.update()`.)
env_runnerr   list	async_envget_sub_environmentsaction_spacer>   r   extend
policy_mapr   treemap_structureobservation_spaceprintconfigget'_get_dummy_batch_from_view_requirementsrE   	_is_local
get_policyas_multi_agent)	algorithmr   r   r   results_per_categoryrJ   r    testr   r7   r   rE   r@   r.   r   s           @@@@@@@r   check_memory_leaksrZ      s   8 'L GGGGH&t,, %11O 211 $99;;A>'$++--
	 
	 
	 
	 
	 
	 0FNs)
 
 
  	5 '..t444 8():;  00&2J2Q2Q2S2S
 
 	+,,,	 	 	 	 	 	 0?Ns *	
 	
 	
  	8 *11$777 }  !?GG 	< HHTVHWWK32?????3-  D  <$X.55d;;; (():;  !2!2F4L4S4S4U4UVV*+++ 0'=====Ns *	
 	
 	
  	7 )00666 8##8999	' 	' 	' 	' 	'
 0:Mr *	
 	
 	
  	@ !1299$???H!1!5!5&" " "/& 	
 	
	
 	
&   ""444CC^ 	 	,---	. 	. 	. 	. 	. 	. 03Ns *	
 	
 	
  	9 +224888r!   )NNr   )collectionsr   typingr   r   r   r   numpyr(   rM   ray.rllib.policy.sample_batchr   r	   ray.rllib.utils.annotationsr
   ray.util.debugr   r   strintrZ   r:   r!   r   <module>rc      s   # # # # # # 3 3 3 3 3 3 3 3 3 3 3 3      H H H H H H H H 4 4 4 4 4 4 D D D D D D D D  $(!	G  G s3x G  c]G  	G 
 d7m#$G  G  G  G  G  G r!   