
    Pi7                        d dl mZmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZ  G d d	e          Zdd
dddddededeeeef                  dedee         dee         dedeeef         defdZdS )    )AnyCallableDictListMappingOptionalN)load_dataset)Dataset)ChosenRejectedToMessagesCROSS_ENTROPY_IGNORE_IDX)	Transform)ModelTokenizerc                       e Zd ZdZddddedededee         d	e	d
e
eef         ddfdZd Zdede
eee         f         fdZdeeef         de
eee         f         fdZdS )PreferenceDataseta  
    Primary class for fine-tuning via preference modelling techniques (e.g. training
    a preference model for RLHF, or directly optimizing a model through DPO) on a
    preference dataset sourced from Hugging Face Hub, local files, or remote files. This
    class requires the dataset to have "chosen" and "rejected" model responses. These are
    typically either full conversations between user and assistant in separate columns::

        |  chosen                                |  rejected                              |
        |----------------------------------------|----------------------------------------|
        | [{"role": "user", "content": Q1},      | [{"role": "user", "content": Q1},      |
        |  {"role": "assistant", "content": A1}] |  {"role": "assistant", "content": A2}] |

    or a user prompt column with separate chosen and rejected assistant reponses::

        |  prompt  |  chosen  |  rejected  |
        |----------|----------|------------|
        |  Q1      |  A1      |  A2        |


    In the above case when the format is prompt-chosen-rejected, only single-turn interactions are supported.

    At a high level, this class will load the data from source and apply the following pre-processing steps when a
    sample is retrieved:

    1. Dataset-specific transform. This is typically unique to each dataset and extracts
       the necessary prompt and chosen/rejected columns into torchtune's :class:`~torchtune.data.Message`
       format, a standardized API for all model tokenizers.
    2. Tokenization with optional prompt template if configured


    All datasets are formatted into a list of :class:`~torchtune.data.Message`
    because preference datasets can be considered as chosen and rejected "conversations"
    with the model, or AI assistant. Thus, we can standardize all text content as messages
    in a conversation assigned to a role:

    - ``"user"`` messages contain the input prompt into the model
    - ``"assistant"`` messages are the response of the model and what you actually want
      to train for and compute loss directly against

    The :class:`~torchtune.data.Message` forms the core data unit that all tokenizer
    APIs expect. The key component of this class that ensures any dataset is transformed
    into this format is the ``message_transform``. This is a callable class that takes
    in a sample dictionary - typically a single row from the source dataset - that
    processes the sample in any configurable way to output a list of messages::

        [
            Message(
                role=<system|user|assistant|ipython>,
                content=<message>,
            ),
            ...
        ]

    For any custom dataset, use the ``message_transform`` to contain all pre-processing to
    return the list of messages.

    Args:
        source (str): path to dataset repository on Hugging Face. For local datasets,
            define source as the data file type (e.g. "json", "csv", "text") and pass
            in the filepath in ``data_files``. See `Hugging Face's
            <https://huggingface.co/docs/datasets/en/package_reference/loading_methods#datasets.load_dataset.path>`_
            ``load_dataset`` for more details.
        message_transform (Transform): callable that keys into the desired fields in the sample
            and converts text content to a list of :class:`~torchtune.data.Message`. It is expected that the final list
            of messages are stored in the ``"chosen"`` and ``"rejected"`` keys.
        tokenizer (ModelTokenizer): Tokenizer used by the model that implements the ``tokenize_messages`` method.
            Since PreferenceDataset only supports text data, it requires a
            :class:`~torchtune.modules.transforms.tokenizers.ModelTokenizer` instead of the ``model_transform`` in
            :class:`~torchtune.datasets.SFTDataset`.
        filter_fn (Optional[Callable]): callable used to filter the dataset prior to any pre-processing. See
            the Hugging Face `docs <https://huggingface.co/docs/datasets/v2.20.0/process#select-and-filter>`_ for more
            details.
        packed (bool): Whether or not to pack the dataset to ``max_seq_len`` prior to training. Default is False. Packed is
            currently not supported for ``PreferenceDataset`` and a ``ValueError`` will be raised if this is set to True.
        **load_dataset_kwargs (Dict[str, Any]): additional keyword arguments to pass to ``load_dataset``. See Hugging
            Face's `API ref <https://huggingface.co/docs/datasets/en/package_reference/loading_methods#datasets.load_dataset>`_
            for more details.

    Raises:
        ValueError: If ``packed`` is True, this feature is not supported for ``PreferenceDataset``.
    NF)	filter_fnpackedsourcemessage_transform	tokenizerr   r   load_dataset_kwargsreturnc                    |rt          d          || _        || _        t          |fi || _        |!| j                            |          | _        d S d S )Nz:Packed is currently not supported for preference datasets.)
ValueError
_tokenizer_message_transformr	   _datafilter)selfr   r   r   r   r   r   s          r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/datasets/_preference.py__init__zPreferenceDataset.__init__f   ss      	L   $"3!&@@,?@@
 **955DJJJ !     c                 *    t          | j                  S N)lenr   )r   s    r   __len__zPreferenceDataset.__len__|   s    4:r!   indexc                 F    | j         |         }|                     |          S r#   )r   _prepare_sample)r   r&   samples      r   __getitem__zPreferenceDataset.__getitem__   s"    E"##F+++r!   r)   c                 
   |                      |          }| j                            |d                   \  }}t          t	          j        |t          |                    }| j                            |d                   \  }}t          t	          j        |t          |                    }t          |          t          |          k    sJ t          |          t          |          k    sJ t          ||||          }	|	S )Nchosenrejected)chosen_input_idschosen_labelsrejected_input_idsrejected_labels)	r   r   tokenize_messageslistnpwherer   r$   dict)
r   r)   transformed_sampler.   chosen_masksr/   r0   rejected_masksr1   tokenized_dicts
             r   r(   z!PreferenceDataset._prepare_sample   s   !44V<< *.)J)Jx(*
 *
&, H\#;=MNN
 
 .2_-N-Nz*.
 .
*N H^%=?QRR
 
 #$$M(:(:::::%&&#o*>*>>>>>-'1+	
 
 
 r!   )__name__
__module____qualname____doc__strr   r   r   r   boolr   r   r    r%   intr   r*   r   r(    r!   r   r   r      s       P Pp )-6 6 6 6 %	6
 "6 H%6 6  $CH~6 
6 6 6 6,  , ,c49n)= , , , ,gc3h&7 Dd3i<P      r!   r   Ftrain)
column_maptrain_on_inputnew_system_promptr   splitr   r   rD   rE   rF   r   rG   r   r   c          	      J    t          |||          }t          d||| ||d|S )a  
    Configures a custom preference dataset comprising interactions between user and
    model assistant.

    This builder function can be used to configure a custom preference dataset directly from the yaml config
    as an alternative to :class:`~torchtune.datasets.PreferenceDataset`, as it is made to be config friendly.

    This function requires the dataset to have "chosen" and "rejected" columns. A single sample will share an
    identical system +/ user prompt between both "chosen" and "rejected" columns, followed by one or multiple
    turns of user and assistant messages::

        |  chosen                                |  rejected                              |
        |----------------------------------------|----------------------------------------|
        | [{"role": "user", "content": Q1},      | [{"role": "user", "content": Q1},      |
        |  {"role": "assistant", "content": C1}] |  {"role": "assistant", "content": R1}] |


    This example will be converted to:

    .. code-block:: python

        chosen_messages = [
            Message(role="user", content="Q1"),
            Message(role="assistant", content="C1"),
        ]

        rejected_messages = [
            Message(role="user", content="Q1"),
            Message(role="assistant", content="R1"),
        ]


    These lists of messages are then tokenized for model training. Currently, this function only supports
    conversations identical to :class:`~torchtune.data.OpenAIToMessages`, and does not support custom
    message formats.

    If your dataset does not follow this format, we recommend creating a custom message transform similar to
    :class:`~torchtune.data.ChosenRejectedToMessages` and using it in a custom dataset builder function similar
    to :class:`~torchtune.datasets.preference_dataset`.

    Masking of the prompt during training is controlled by the ``train_on_input`` flag, which is:
    set to ``False`` by default.

    - If ``train_on_input`` is True, the prompt is used during training and
      contributes to the loss.
    - If ``train_on_input`` is False, the prompt is masked out (tokens replaced with -100).

    Args:
        tokenizer (ModelTokenizer): Tokenizer used by the model that implements the ``tokenize_messages`` method.
        source (str): path to dataset repository on Hugging Face. For local datasets,
            define source as the data file type (e.g. "json", "csv", "text"), pass
            in the filepath in ``data_files``, and set ``split="train"``. See `Hugging Face's
            <https://huggingface.co/docs/datasets/en/package_reference/loading_methods#datasets.load_dataset.path>`_
            ``load_dataset`` for more details.
        column_map (Optional[Dict[str, str]]): a mapping from the expected columns "chosen" and "rejected"
            in the message transform :class:`~torchtune.data.ChosenRejectedToMessages` to the new column names in
            the dataset. Keys should be "chosen" and "rejected" and values should be the actual column names.
            If None, keep the default columns "chosen" and "rejected".
        train_on_input (bool): Whether the model is trained on the prompt or not. Default is False.
        new_system_prompt (Optional[str]): if specified, prepend a system message to every sample for both chosen
            and rejected. This can serve as instructions to guide the model response. Setting this will OVERRIDE
            any system messages already present in the dataset. Default is None.
        filter_fn (Optional[Callable]): callable used to filter the dataset prior to any pre-processing. See
            the Hugging Face `docs <https://huggingface.co/docs/datasets/v2.20.0/process#select-and-filter>`_ for more
            details.
        split (str): ``split`` argument for ``datasets.load_dataset``. You can use this argument to load a subset
            of a given split, e.g. ``split="train[:10%]"``. Default is "train".
        **load_dataset_kwargs (Dict[str, Any]): additional keyword arguments to pass to ``load_dataset``.

    Examples:

    ::

        my_preference_dataset.json
        [
            {
                "chosen_conversations": [
                    {
                        "content": "What do I do when I have a hole in my trousers?",
                        "role": "user"
                    },
                    { "content": "Fix the hole.", "role": "assistant" }
                ],
                "rejected_conversations": [
                    {
                        "content": "What do I do when I have a hole in my trousers?",
                        "role": "user"
                    },
                    { "content": "Take them off.", "role": "assistant" }
                ]
            }
        ]

    ::

        >>> from torchtune.datasets import preference_dataset
        >>> column_map = {
        ...     "chosen": "chosen_conversations",
        ...     "rejected": "rejected_conversations"
        >>> }
        >>> dataset = preference_dataset(
        ...     tokenizer=tokenizer,
        ...     source="json",
        ...     column_map=column_map,
        ...     data_files="my_preference_dataset.json",
        ...     train_on_input=False,
        ...     split="train",
        >>> )
        >>> tokenizer.decode(dataset[0]["chosen_input_ids"], skip_special_tokens=True)
        What do I do when I have a hole in my trousers?Fix the hole.
        >>> tokenizer.decode(dataset[0]["rejected_input_ids"], skip_special_tokens=True)
        What do I do when I have a hole in my trousers?Take them off.

    This can also be accomplished via the yaml config:

    .. code-block:: yaml

        dataset:
          _component_: torchtune.datasets.preference_dataset
          source: json
          data_files: my_preference_dataset.json
          column_map:
            chosen: chosen_conversations
            rejected: rejected_conversations
          train_on_input: False
          split: train


    Returns:
        PreferenceDataset: The preference dataset built from source paired data.
    )rE   rD   rF   )r   r   r   r   rG   rB   )r   r   )	r   r   rD   rE   rF   r   rG   r   r   s	            r   preference_datasetrI      sZ    ^ 1%+    +    r!   )typingr   r   r   r   r   r   numpyr4   datasetsr	   torch.utils.datar
   torchtune.datar   r   torchtune.modules.transformsr   'torchtune.modules.transforms.tokenizersr   r   r?   r@   rI   rB   r!   r   <module>rQ      s   @ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?     ! ! ! ! ! ! $ $ $ $ $ $ M M M M M M M M 2 2 2 2 2 2 B B B B B BM M M M M M M Mh ,0 '+$(\ \ \\ \ c3h(	\
 \  }\ !\ \  S>\ \ \ \ \ \ \r!   