o
    5tiB                     @   s~   d dl Z d dlmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZ eG dd deZeG dd deZG d	d
 d
e jZdS )    N)asdict	dataclass)	getsource)AnyCallableListOptionalUnionc                   @   s^   e Zd ZU dZee ed< dZee ed< dZee ed< dZ	ee
eef  ed< d	d
 ZdS )AggMetricConfigNmetricmeanaggregationFweight_by_sizenonefilter_listc                 C   sD   | j dkrt| j std| j  dt| jtr | jg| _d S d S )Nr   zTCurrently, 'mean' is the only pre-defined aggregation across groups' subtasks. Got 'z'.)r   callable
ValueError
isinstancer   strself r   E/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/api/group.py__post_init__   s   zAggMetricConfig.__post_init__)__name__
__module____qualname__r   r   r   __annotations__r   r   r   r	   listr   r   r   r   r   r
      s   
 r
   c                   @   s   e Zd ZU dZee ed< dZee ed< dZee	ee
f  ed< dZee	ee eef  ed< dZee ed< dd Zd	d
 Zdd ZddedefddZ	dde	eef de	eef fddZdS )GroupConfigNgroupgroup_aliastaskaggregate_metric_listmetadatac                 C   s
   t | |S N)getattr)r   itemr   r   r   __getitem__%   s   
zGroupConfig.__getitem__c                 C   s   t | ||S r%   )setattr)r   r'   valuer   r   r   __setitem__(   s   zGroupConfig.__setitem__c                 C   s:   | j d urt| j tr| j g| _ dd | j D | _ d S d S )Nc                 S   s(   g | ]}t |trtd i |n|qS )r   )r   dictr
   ).0r'   r   r   r   
<listcomp>0   s    z-GroupConfig.__post_init__.<locals>.<listcomp>)r#   r   r,   r   r   r   r   r   +   s   

zGroupConfig.__post_init__Fkeep_callablereturnc                 C   s<   t | }t| D ]\}}t|r| j||d||< q
|S )ad  dumps the current config as a dictionary object, as a printable format.
        null fields will not be printed.
        Used for dumping results alongside full task configuration

        :return: dict
            A printable dictionary version of the TaskConfig object.

        # TODO: should any default value in the TaskConfig not be printed?
        )r/   )r   r   itemsr   serialize_function)r   r/   cfg_dictkvr   r   r   to_dict5   s   
zGroupConfig.to_dictr*   c              	   C   s2   |r|S zt |W S  ttfy   t| Y S w )zSerializes a given function or string.

        If 'keep_callable' is True, the original callable is returned.
        Otherwise, attempts to return the source code of the callable using 'getsource'.
        )r   	TypeErrorOSErrorr   )r   r*   r/   r   r   r   r2   F   s   
zGroupConfig.serialize_function)F)r   r   r   r    r   r   r   r!   r"   r	   r   r#   r   r
   r,   r$   r(   r+   r   boolr6   r   r2   r   r   r   r   r      s(   
 


r   c                   @   sp   e Zd Z	ddee ddfddZedd Zedd	 Zed
d Z	edd Z
edefddZdd ZdS )ConfigurableGroupNconfigr0   c                 C   s   t di || _d S )Nr   )r   _config)r   r;   r   r   r   __init__X   s   zConfigurableGroup.__init__c                 C      | j jS r%   r<   r    r   r   r   r   r    ^      zConfigurableGroup.groupc                 C   r>   r%   )r<   r!   r   r   r   r   r!   b   r@   zConfigurableGroup.group_aliasc                 C   r>   r%   )r<   versionr   r   r   r   rA   f   r@   zConfigurableGroup.versionc                 C   s
   | j  S r%   )r<   r6   r   r   r   r   r;   j   s   
zConfigurableGroup.configc                 C   r>   r%   r?   r   r   r   r   
group_namen   r@   zConfigurableGroup.group_namec                 C   s   d| j  d| j dS )NzConfigurableGroup(group=z,group_alias=))r    r!   r   r   r   r   __repr__r   s   zConfigurableGroup.__repr__r%   )r   r   r   r   r,   r=   propertyr    r!   rA   r;   r   rB   rD   r   r   r   r   r:   W   s$    




r:   )abcdataclassesr   r   inspectr   typingr   r   r   r   r	   r,   r
   r   ABCr:   r   r   r   r   <module>   s    =