Files
DS-LLM-TEMPLATE-FINETUNING/unsloth_compiled_cache/__pycache__/UnslothORPOTrainer.cpython-310.pyc
T

413 lines
44 KiB
Plaintext
Raw Normal View History

2025-08-28 17:57:59 +00:00
o
2025-08-28 22:41:56 +00:00
ö×°h.,ã@sdZddlmZddlZddlmZddlmZddlmZm Z m
2025-08-28 17:57:59 +00:00
Z
m Z m Z m
Z
mZmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm
Z
mZmZmZmZm Z m!Z!m"Z"m#Z#m Z m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3mZm4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=mZm>Z>mZm
Z
mZmZm"Z"m-Z-m6Z6mZddl6Z6ddlTddl?m@Z@mAZAdd lBmCZCddlZddlDZ4dd
lEm5Z5ddlmZdd lFmGZGmHZId d
d d
d
dœZJejKd d eJdddƒZLe@GdddeƒƒZM Gddde"ƒZNGdddeNƒZOdS)z9
2025.8.9
2025.8.10
4.55.4
0.21.0
__UNSLOTH_VERSIONING__
é)ÚTensorN)Ú
functional)ÚAnyÚListÚOptionalÚTupleÚUnionÚDictÚSetÚCallable)=rÚAutoModelForCausalLMÚBaseImageProcessorr ÚDPODataCollatorWithPaddingÚ DataCollatorÚ
DataLoaderÚDatasetÚEvalLoopOutputÚFeatureExtractionMixinÚLiteralÚ
ORPOConfigÚ ORPOTrainerrÚ PartialStateÚPathÚ PeftModelÚPreTrainedModelÚPreTrainedTokenizerBaseÚProcessorMixinÚTrainerÚTrainerCallbackrÚadd_bos_token_if_neededÚadd_eos_token_if_neededÚautocastÚ defaultdictÚdisable_dropout_in_modelÚgenerate_model_cardÚget_comet_experiment_urlÚinspectÚis_comet_availableÚis_peft_availableÚis_torch_fx_proxyÚis_torch_xla_availableÚis_wandb_availableÚlog_table_to_comet_experimentÚmaybe_apply_chat_templateÚmaybe_extract_promptÚnnÚnpÚ nullcontextÚosÚ
pad_to_lengthÚpdÚpeft_module_casting_to_bf16Úprepare_model_for_kbit_trainingÚrandomÚselective_log_softmaxÚtextwrapÚtorchÚwarningsrrrrrr)r3r;)Ú*)Ú dataclassÚfield)ÚVersion)r2)ÚDataCollatorForSeq2SeqÚDataCollatorForLanguageModelingTF)Úepilogue_fusionÚ max_autotuneÚ
shape_paddingz
trace.enabledztriton.cudagraphs)ÚdynamicÚ fullgraphÚoptionsc
Ctj| d|jd¡ddd}tj| d¡ddd}g}t||ƒD](\}}| tj¡}tj|d| d¡d  d¡}tj
|dd}||} |  | ¡q! t  |¡}| |jd|jdf¡}|S)Néÿÿÿÿér)ÚchunksÚdim)rLÚindex©rLé)
r;ÚchunkÚreshapeÚshapeÚzipÚtoÚfloat32ÚgatherÚ unsqueezeÚsqueezeÚ logsumexpÚappendÚconcat)
ÚlogitsrMÚchunked_logitsÚ
chunked_indexÚall_per_token_logpsÚ chunk_logitsÚ chunk_indexÚselected_logitsÚlogsumexp_valuesÚper_token_logps©reúR/workspace/DS-LLM-TEMPLATE-FINETUNING/unsloth_compiled_cache/UnslothORPOTrainer.pyÚchunked_selective_log_softmax"s  
rgcseZdZUdZedddidZeeed<edddidZ ee
ed <eddd
idZ ee
ed <  
                            ! ! " #     $           $      % &  '         (      #    $   ) *       + ,    -  .      d1‡fd/d0„ Z Z
S)2ÚUnslothORPOConfiguB
Configuration class for the [`ORPOTrainer`].
This class includes only the parameters that are specific to ORPO training. For a full list of training arguments,
please refer to the [`~transformers.TrainingArguments`] documentation. Note that default values in this class may
differ from those in [`~transformers.TrainingArguments`].
Using [`~transformers.HfArgumentParser`] we can turn this class into
[argparse](https://docs.python.org/3/library/argparse#module-argparse) arguments that can be specified on the
command line.
Parameters:
max_length (`int` or `None`, *optional*, defaults to `1024`):
Maximum length of the sequences (prompt + completion) in the batch. This argument is required if you want
to use the default data collator.
max_prompt_length (`int` or `None`, *optional*, defaults to `512`):
Maximum length of the prompt. This argument is required if you want to use the default data collator.
max_completion_length (`int` or `None`, *optional*, defaults to `None`):
Maximum length of the completion. This argument is required if you want to use the default data collator
and your model is an encoder-decoder.
beta (`float`, *optional*, defaults to `0.1`):
Parameter controlling the relative ratio loss weight in the ORPO loss. In the
[paper](https://huggingface.co/papers/2403.07691), it is denoted by λ. In the
[code](https://github.com/xfactlab/orpo), it is denoted by `alpha`.
disable_dropout (`bool`, *optional*, defaults to `True`):
Whether to disable dropout in the model.
label_pad_token_id (`int`, *optional*, defaults to `-100`):
Label pad token id. This argument is required if you want to use the default data collator.
padding_value (`int` or `None`, *optional*, defaults to `None`):
Padding value to use. If `None`, the padding value of the tokenizer is used.
truncation_mode (`str`, *optional*, defaults to `"keep_end"`):
Truncation mode to use when the prompt is too long. Possible values are `"keep_end"` or `"keep_start"`.
This argument is required if you want to use the default data collator.
generate_during_eval (`bool`, *optional*, defaults to `False`):
If `True`, generates and logs completions from the model to W&B or Comet during evaluation.
is_encoder_decoder (`bool` or `None`, *optional*, defaults to `None`):
When using the `model_init` argument (callable) to instantiate the model instead of the `model` argument,
you need to specify if the model returned by the callable is an encoder-decoder model.
model_init_kwargs (`dict[str, Any]` or `None`, *optional*, defaults to `None`):
Keyword arguments to pass to `AutoModelForCausalLM.from_pretrained` when instantiating the model from a
string.
dataset_num_proc (`int` or `None`, *optional*, defaults to `None`):
Number of processes to use for processing the dataset.
helpzvLLM SamplingParams)ÚdefaultÚmetadataÚvllm_sampling_paramsrIz8Chunk size to reduce memory usage. -1 is most efficient.Úunsloth_num_chunksz'Maximum sequence length to truncate to.Úmax_seq_lengthFÚnorJéréúç-Cëâ6
?ç{®Gáz„?çÍÌÌÌÌÌì?ç+‡ÙÎ÷ï?ç:Œ0âŽyE>çð?çlinearçš™™™™™¹?ÚpassiveÚwarningTÚstepsrOéôéO
ÚO1ÚautoÚçÚ
adamw_8bitÚlengthÚ
every_saveÚlastééééœÿÿÿÚkeep_endc |dkr td|dƒ|dkrtd|dƒ|dur(|#dkr(|$dkr(d}d }#|Œdur:d
d lm}t|‘ƒd d
ƒ}Œtƒjdšid|d|d|d|d|d|d|d|d| “d|
d| d| d|
d|d|d|d|d|d |d!|d"|d#|d$|d%|d&|d'|d(|d)|d*|d+|d,|d-| “d.|!“d/|"“d0|#“d1|$“d2|%“d3|&“d4|'“d5|(“d6|)“d7|*“d8|+“d9|,“d:|-“d;|.“d<|/“d=|0“d>|1“d?|2“d@|3“dA|4“dB|5“dC|6“dD|7“dE|8“dF|9“dG|:“dH|;“dI|<“dJ|=“dK|>“dL|?“dM|@“dN|A“dO|B“dP|C“dQ|D“dR|E“dS|F“dT|G“dU|H“dV|I“dW|J“dX|K“dY|L“dZ|M“d[|N“d\|O“d]|P“d^|Q“d_|R“d`|S“da|T“db|U“dc|V“dd|W“de|X“df|Y“dg|Z“dh|[“di|\“dj|]“dk|^“dl|_“dm|`“dn|a“do|b“dp|c“dq|d“dr|e“ds|f“dt|g“du|h“dv|i“dw|j“dx|k“dy|l“dz|m“d{|n“d||o“d}|p“d~|q“d|r“d€|s“d|t“d|u“dƒ|v“d„|w“d…|x“d†|y“d‡|z“dˆ|{“d‰||“dŠ|}“d|~“dŒ|d|€“dŽ|d|‚“d|ƒ“d‘|„“d’|…“d“|†“d”|‡“d•|ˆ“d–|‰“d—|Š“d˜|‹“d™|Œ“|¤Ž||_|Ž|_||_ dS)NgH¯¼šò×z>z Unsloth: Your learning rate of `zi` is too small and less than 1e-7! Consider increasing it, otherwise gradient updates will be close to 0!rOza` is way too larger > 1! Consider decreasing it to 1e-1, otherwise gradient updates will explode!r}r~Úunsloth_training_checkpointsror)Ú cpu_countrJrpÚ
output_dirÚoverwrite_output_dirÚdo_trainÚdo_evalÚ
do_predictÚ
eval_strategyÚprediction_loss_onlyÚper_device_train_batch_sizeÚper_device_eval_batch_sizeÚper_gpu_train_batch_sizeÚper_gpu_eval_batch_sizeÚgradient_accumulation_stepsÚeval_accumulation_stepsÚ
eval_delayÚtorch_empty_cache_stepsÚ
learning_rateÚ weight_decayÚ
adam_beta1Ú
adam_beta2Ú adam_epsilonÚ
max_grad_normÚnum_train_epochsÚ max_stepsÚlr_scheduler_typeÚ warmup_ratioÚ warmup_stepsÚ log_levelÚlog_level_replicaÚlog_on_each_nodeÚ logging_dirÚlogging_strategyÚlogging_first_stepÚ
logging_stepsÚlogging_nan_inf_filterÚ
save_strategyÚ
save_stepsÚsave_total_limitÚsave_safetensorsÚsave_on_each_nodeÚsave_only_modelÚ'restore_callback_states_from_checkpointÚno_cudaÚuse_cpuÚuse_mps_deviceÚseedÚ data_seedÚ
jit_mode_evalÚuse_ipexÚbf16Úfp16Úfp16_opt_levelÚhalf_precision_backendÚbf16_full_evalÚfp16_full_evalÚtf32Ú
local_rankÚ ddp_backendÚ
tpu_num_coresÚtpu_metrics_debugÚdebugÚdataloader_drop_lastÚ
eval_stepsÚdataloader_num_workersÚdataloader_prefetch_factorÚ
past_indexÚrun_nameÚ disable_tqdmÚremove_unused_columnsÚ label_namesÚload_best_model_at_endÚmetric_for_best_modelÚgreater_is_betterÚignore_data_skipÚfsdpÚfsdp_min_num_paramsÚ fsdp_configÚ"fsdp_transformer_layer_cls_to_wrapÚaccelerator_configÚ deepspeedÚlabel_smoothing_factorÚoptimÚ
optim_argsÚ adafactorÚgroup_by_lengthÚlength_column_nameÚ report_toÚddp_find_unused_parametersÚddp_bucket_cap_mbÚddp_broadcast_buffersÚdataloader_pin_memoryÚdataloader_persistent_workersÚskip_memory_metricsÚuse_legacy_prediction_loopÚ push_to_hubÚresume_from_checkpointÚ hub_model_idÚ hub_strategyÚ hub_tokenÚhub_private_repoÚhub_always_pushÚ hub_revisionÚgradient_checkpointingÚgradient_checkpointing_kwargsÚinclude_inputs_for_metricsÚeval_do_concat_batchesÚ fp16_backendÚpush_to_hub_model_idÚpush_to_hub_organizationÚpush_to_hub_tokenÚ
mp_parametersÚauto_find_batch_sizeÚfull_determinismÚ torchdynamoÚ ray_scopeÚ ddp_timeoutÚ
torch_compileÚtorch_compile_backendÚtorch_compile_modeÚinclude_tokens_per_secondÚinclude_num_input_tokens_seenÚneftune_noise_alphaÚoptim_target_modulesÚbatch_eval_metricsÚ
eval_on_startÚuse_liger_kernelÚliger_kernel_configÚeval_use_gather_objectÚaverage_tokens_across_devicesÚ
max_lengthÚmax_prompt_lengthÚmax_completion_lengthÚbetaÚdisable_dropoutÚlabel_pad_token_idÚ
padding_valueÚtruncation_modeÚgenerate_during_evalÚis_encoder_decoderÚmodel_init_kwargsÚdataset_num_procre)
ÚFloatingPointErrorÚ
OverflowErrorÚmultiprocessingrŽÚmaxÚsuperÚ__init__rlrmrn)’Úselfrrrrr“r”r•r–r—r™rrr r­r¿rÿrrrrrrrrrr r
r r r
rrrrrrrrrrrrrrlrmrnÚkwargsrŽ©Ú __class__rerfr os€  ÿþýüûúùø ÷
ö õ ô
óòñðïîíìëêéèçæåäãâá à!ß"Þ#Ý$Ü%Û&Ú'Ù(Ø)×*Ö+Õ,Ô-Ó.Ò/Ñ0Ð1Ï2Î3Í4Ì5Ë6Ê7É8È9Ç:Æ;Å<Ä=Ã>Â?Á@ÀA¿B¾C½D¼E»FºG¹H¸I·JKµL´M³N²O±P°Q¯R®S­T¬U«VªW©X¨Y§Z¦[¥\¤]£^¢_¡` aŸbžcdœefšgh˜ijklmnopqrŽstŒuvŠwxˆyz{|}ƒ~ÿþýüûúùø ÷
ö õ ô
ó
zUnslothORPOConfig.__init__)NNFFFroFrJrJNNrprprrqrrrsrtrurvrwrxrIryrzrr{r|TNr}FrOFr}r~NTFFFFFFrrFFFFr€rFFNrINNFrFNrNrINNTNFNNFrrNNNNrƒr„NFFr…NNNNTFTFFNNr†NNFNFNFTrNNNrTFNr‡rˆFNNFFNNFFFNFTr‰NrzTrNrŒFNNNNrIN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r?rlrrÚ__annotations__rmÚintrnr Ú
__classcell__rerer#rfrh3s>
.þþþïrhceZdZdZddgZ            dRdeeeej e
fdee dee d ee
d
eee
ee
e
ffd eeeeeefd eegefd
eeedeejjejjjfdeeejejgejfdeedeeegefffdd
ZddZdSdeeeej fdefddZ e!    dTdee
eeej"ffde#de$de$d eej%dee
ej"ff d!d"„ƒZ&d#ej'd$ej'deej'ej'ej'ej'ej'ffd%d&„Z(e!   dUd'ej'd(ej"d)e#de$de#dej'f d*d+„ƒZ)dej dee
eeej"ffdeej'ej'ej'ej'ffd,d-„Z* .dVdee
eeej"ffd/e+d0fd1d2„Z,  dWdeeej fd3ee
eeje-ffdeejeejee
ejffffd4d5„Z.dee
ej"fde
fd6d7„Z/ dSdeeej fd3ee
eeje-ffd8e#d9eee
fd:d;„Z0dVd<ee
e1fd/e+d0ddfd=d>„Z2   ?dXd@e3dAe
d8ee#d9eee
dBe
def fdCdD„
Z4dSdEee
e1fdFee1ddffdGdH„
Z5dIdJ„Z6‡fdKdL„Z7   dYdMee
dNee
dOee
ee
dffdPdQ„Z8‡Z9S)ZÚ_UnslothORPOTrainerrÚtrlÚorpoN©NNÚmodelÚargsÚ
data_collatorÚ
train_datasetÚ eval_datasetÚprocessing_classÚ
model_initÚ callbacksÚ
optimizersÚpreprocess_logits_for_metricsÚ peft_configÚcompute_metricsc

|jduri}
n9t|tƒstdƒ|j}
|
 d¡}|durAt|tƒr+|dkr+tt|ƒ}|dkr=t|tjƒs=td|dƒ||
d<t|tƒrOtj |fi|
¤Ž}d|_
t ƒs]| dur]tdƒt ƒrÈ| durÈt|t ƒrm| 
¡}t|ddƒsyt|d dƒrt|d
ƒo‡d
tt t¡jƒv}d |ji}|r”|j|d
<t|fi|¤Ž}n|jrµt|d ƒrª| ¡n d
d}| ¡ |¡|}|jrÇt|d dƒrÇt|ƒd|_
n|jràt|d ƒrÕ| ¡n dd}| ¡ |¡|jrítƒsítƒsítdƒ|dur÷|jj|_n|jdurtdƒ|j|_|jr|jj |_ |jj!|_!|durtdƒ|j"dur+t# $dt%¡d}n|j"}|j&dur=t# $dt%¡d}n|j&}|j'durT|jrTt# $dt%¡d|_'n|j'|_'|durxt(|j!|j)|jd}|j*rtd|_*t# $dt%¡d|_+nd|_+|j,rƒt-|ƒ||_"|j|_|j)|_)|j.dur—|j.n|j!|_.||_&|j/|_/||_0|j1|_1t|jddƒ|_2t|jddƒ|_3|j2rÉ|j3dkrÉt# $dt%¡t4dd „ƒ|_5d|j6d!<t7ƒ E|j9t:|j;d"}|j9t<d#|i|j;d$}|j9|j=|j;d"}|dur|j9t:|j;d"}|j9t<d#|i|j;d$}|j9|j=|j;d"}Wdƒn 1s#wYt>ƒj?|||||||| || |
d% d|_@t|jAd&ƒrJ|jA B|jC¡t|d'ƒsTtDd(ƒdS))NzSYou passed model_kwargs to the ORPOTrainer. But your model is already instantiated.Ú torch_dtyperzoInvalid `torch_dtype` passed to the ORPOConfig. Expected a string with either `torch.dtype` or 'auto', but got Ú.FzvPEFT is not installed and you passed a `peft_config` in the trainer's kwargs, please install it to use the PEFT modelsÚis_loaded_in_8bitÚis_loaded_in_4bitrõÚuse_gradient_checkpointingÚenable_input_require_gradscSó| d¡dS©NT©Úrequires_grad_©ÚmoduleÚinputÚoutputrererfÚmake_inputs_require_gradçóz>_UnslothORPOTrainer.__init__.<locals>.make_inputs_require_gradTcSrBrCrDrFrererfrJürKz`generate_during_eval=True` requires Weights and Biases or Comet to be installed. Please install `wandb` or `comet-ml` to resolve.zMWhen no model is provided, you need to pass the parameter is_encoder_decoder.z>processing_class must be specified to tokenize a ORPO dataset.z`max_length` is not set in the ORPOConfig's init it will default to `512` by default, but you should do it yourself in the future.rŠz‰`max_prompt_length` is not set in the ORPOConfig's init it will default to `128` by default, but you should do it yourself in the future.é€z½When using an encoder decoder architecture, you should set `max_completion_length` in the ORPOConfig's init it will default to `128` by default, but you should do it yourself in the future.)Ú pad_token_idrrz²When using DPODataCollatorWithPadding, you should set `remove_unused_columns=False` in your TrainingArguments we have set it for you, but you should do it yourself in the future.Úoutput_router_logitsÚrouter_aux_loss_coefrƒa-You set `output_router_logits` to `True` in the model config, but `router_aux_loss_coef` is set to `0.0`, meaning the auxiliary loss will not be used. Either set `router_aux_loss_coef` to a value greater than `0.0`, or set `output_router_logits` to `False` if you don't want to use the auxiliary loss.cSsttƒS©N)r#ÚlistrerererfÚ<lambda>]sz._UnslothORPOTrainer.__init__.<locals>.<lambda>Úestimate_tokens)Únum_procÚ tokenizer)Ú fn_kwargsrT) r0r1r2r3r4r5r6r;r7r8r9Úadd_model_tagsÚ acceleratorzXYour `Trainer` does not have an `accelerator` object. Consider upgrading `transformers`.)ErÚ
isinstanceÚstrÚ
ValueErrorÚgetÚgetattrr;Údtyper Úfrom_pretrainedÚ_peft_has_been_casted_to_bf16r)rÚmerge_and_unloadÚhasattrrQr'Ú signaturer7Ú
parametersrôrAÚget_input_embeddingsÚregister_forward_hookr¿r6rr,r(ÚconfigrÚdecoder_start_token_idrMrr<ÚwarnÚ UserWarningrrrrÚuse_dpo_data_collatorrr$rrr5rÚaux_loss_enabledÚ
aux_loss_coefr#Ú_stored_metricsÚwarnings_issuedrÚmain_process_firstÚmapr/rr.Ú tokenize_rowrr Úmodel_accepts_loss_kwargsr0rWÚ
_tag_namesÚAttributeError)r!r0r1r2r3r4r5r6r7r8r9r:r;rr<Ú_support_gc_kwargsÚprepare_model_kwargsrJrrr#rerfr ¢s8




ÿ
ÿ
ÿ
ÿþ





ÿ  


 ý ýý
ýýû
 ÿ
ýòõ ÿÿz_UnslothORPOTrainer.__init__c Cs |j||dd}|j|ddd}|dt|ƒd}|dt|ƒd}t ||g¡}t |d¡}t|ƒt|ƒkr@tdƒt|ƒ} ||dd| …krR| d8} |dd| …}|dd| …}
t|ƒt|
ƒkrntdƒ|d| d}|d| d}t||
||d S)
a
Llama tokenizer does satisfy `enc(a + b) = enc(a) + enc(b)`. It does ensure `enc(a + b) = enc(a) + enc(a +
b)[len(enc(a)):]`. Reference:
https://github.com/EleutherAI/lm-evaluation-harness/pull/531#issuecomment-1595586257
Úadd_special_tokensÚ input_idsNÚattention_maskzBPrompt input ids and answer input ids should have the same length.rOz@Prompt input ids and attention mask should have the same length.)Úprompt_input_idsÚprompt_attention_maskrzr{)r5Úlenr1Ú concatenateÚarrayr[Údict) r!ÚpromptÚanswerÚfull_tokenizedr|Úanswer_input_idsÚanswer_attention_maskÚfull_concat_input_idsÚfull_input_idsÚresponse_token_ids_start_idxr}rererfÚbuild_tokenized_answers.üz*_UnslothORPOTrainer.build_tokenized_answerÚreturncsi}|d}|d}|d}|js°t|tƒs tdt|ƒƒ|j|dd}dd| ¡Dƒ}t|tƒs>td t|ƒƒ| ||¡t|tƒsRtd
t|ƒƒ| ||¡t|d ƒ}tˆd ƒ} tˆd ƒ}
t | |
ƒ}| ¡D] \} } | d ||| <qst
d
dt ˆd ˆd ƒDƒƒ}
t | |
ƒ}|
dksž|dkr¢tdƒt
|jj||| ˆ|
ˆƒ\}t|jjˆˆƒ\ttˆdƒtˆdƒƒ}ˆˆ|fD]D}t|d ƒ||jkr|jdkrñdD]
} || d |j|| <qâqÍ|jdkr dD]} || |j d || <qùqÍtd|jƒq͈ˆfD]#}t|d ƒ||jkr8dD]} || d |j|j|| <q&qfdddDƒ}fdddDƒ}|dd d |d<|jgtˆd ƒ|dd tˆd ƒ<|dd d |d<|jgtˆd ƒ|dd tˆd ƒ<|||dœ ¡D]\} }| ¡D]\}}|dkq˜||| |<q˜qn[|j|d|jdd|j|d|jdd|j|d|jdd}ˆd|d<ˆd|d<|d|d <|d |d!<|d ur t|d"ƒr |jt |d¡d#|d$<|jt |d¡d#|d%<tƒrF|D]4} d| vs|jr |j}n|  d&¡r*|j}n|  d'¡r2d(}|| |g|jt|| ƒ|| <q|S))a/Tokenize a single row from a ORPO specific dataset.
At this stage, we don't convert to PyTorch tensors yet; we just handle the truncation in case the prompt +
chosen or prompt + rejected responses is/are too long. First we truncate the prompt; if we're still too long,
we truncate the chosen/rejected.
We also create the labels for the chosen/rejected responses, which are of length equal to the sum of the length
of the prompt and the chosen/rejected response, with label_pad_token_id for the prompt tokens.
rÚchosenÚrejectedz prompt should be an str but got FrxcSsi|]
\}}d||qS©Úprompt_re©Ú.0ÚvrererfÚ
<dictcomp>ßóz4_UnslothORPOTrainer.tokenize_row.<locals>.<dictcomp>z chosen should be an str but got z"rejected should be an str but got r|NcSsg|]\}}||kqSrere)rÚbrererfÚ
<listcomp>÷sz4_UnslothORPOTrainer.tokenize_row.<locals>.<listcomp>rOzdChosen and rejected prompt_input_ids might only differ on the last token due to tokenizer merge ops.rzÚ
keep_start)r|r}zUnknown truncation mode: )rzr{có$i|]}|ˆd|ˆ|qSre©rr)Ú
chosen_tokensrerfr”%óÿcrer)Úrejected_tokensrerfr”(rÚlabels)Úchosen_Ú rejected_rÚtoken_type_idsT)Ú
truncationrryÚ
chosen_labelsÚrejected_labelsr{r}Ú%prepare_decoder_input_ids_from_labels)Úrejected_decoder_input_idsÚchosen_decoder_input_idsÚ
_input_idsÚ_attention_maskr)rrYrZr[Útyper5ÚitemsrŠr~ÚminÚsumrSÚabsr Ú bos_token_idr!Ú eos_token_idrrrrrrrbr;Útensorr+Úendswithr)r!Úfeaturer0ÚbatchrrÚ
prompt_tokensÚprompt_len_input_idsÚchosen_prompt_len_input_idsÚrejected_prompt_len_input_idsrr“Únum_diff_tokensÚ num_diff_lenÚlonger_response_lengthÚ
answer_tokensÚchosen_sequence_tokensÚrejected_sequence_tokensÚtoksÚtype_keyÚtokensÚ pad_valuere)rfrrÇ


 
    
ÿ ÿ
ù
ÿ
ÿ ÿø  
ÿ
ÿÿ
þÿ
þý
ü
ýû
ÿ
ÿ
ÿ     
ÿ 
ÿ  (z _UnslothORPOTrainer.tokenize_rowFrrrrrÚdevicec
Ci}|rt|djd|djdƒ}nt|djd|djdƒ}|D]8}| d¡r]t||tjƒr]d|vs:|r=|}n| d¡rE|}n| d ¡rLd
}| dd ¡} t||||d || <q%|D]E}| d
¡r¥t||tjƒr¥d|vsu|rx|}n| d¡r€|}n| d ¡r‡d
}| d
d ¡} tj || t||||d fd
dj
|d|| <q`|rÄ|d  dd¡j
|d|d<|d  dd¡j
|d|d<|S)Concatenate the chosen and rejected inputs into a single tensor.
Args:
batch:
A batch of data. Must contain the keys 'chosen_input_ids' and 'rejected_input_ids', which are tensors
of shape (batch_size, sequence_length).
is_encoder_decoder:
Whether the model is an encoder-decoder model.
label_pad_token_id:
The label pad token id.
padding_value:
The padding value to use for the concatenated inputs_ids.
device:
The device for the concatenated inputs.
Returns:
A dictionary containing the concatenated inputs under the key 'concatenated_input_ids'.
rOÚchosen_input_idsÚrejected_input_idsrŒrÚ concatenated)rrN©r|rpÚconcatenated_input_idsr}Úconcatenated_attention_mask) rrRÚ
startswithrYr;rÚreplacer4ÚcatrTÚrepeat)
rrrÚconcatenated_batchrrÚconcatenated_keyrererfÚconcatenated_inputsbsL  

  

 þû
úÿz'_UnslothORPOTrainer.concatenated_inputsÚpolicy_chosen_logpsÚpolicy_rejected_logpscC||t t |¡ ¡t t |¡ ¡}t |¡}|j|}|j| |jj¡  ¡}|j| |jj¡  ¡}|||t 
|¡t 
|¡fS)anCompute ORPO's odds ratio (OR) loss for a batch of policy and reference model log probabilities.
Args:
policy_chosen_logps:
Log probabilities of the policy model for the chosen responses. Shape: (batch_size,)
policy_rejected_logps:
Log probabilities of the policy model for the rejected responses. Shape: (batch_size,)
Returns:
A tuple of three tensors: (losses, chosen_rewards, rejected_rewards). The losses tensor contains the ORPO
loss for each example in the batch. The chosen_rewards and rejected_rewards tensors contain the rewards for
the chosen and rejected responses, respectively. The log odds ratio of the chosen responses over the
rejected responses ratio for logging purposes. The `log(sigmoid(log_odds_chosen))` for logging purposes.
) r;Úlog1pÚexprÚ
logsigmoidrrTrXÚdetachÚmean)r!Úlog_oddsÚratioÚlossesÚchosen_rewardsÚrejected_rewardsrererfÚodds_ratio_loss¦s"ÿ

z#_UnslothORPOTrainer.odds_ratio_lossr\Úaverage_log_probcCs |jdd|jkrtdƒ|s)|ddddf ¡}|ddddddf}||k}t ||kd|¡}t||ƒ}|rI|| d¡| d¡S|| d¡S)Compute the log probabilities of the given labels under the given logits.
Args:
logits: Logits of the model (unnormalized). Shape: (batch_size, sequence_length, vocab_size)
labels:
Labels for which to compute the log probabilities. Label tokens with a value of label_pad_token_id are
ignored. Shape: (batch_size, sequence_length)
average_log_prob:
If True, return the average log probability per (non-masked) token. Otherwise, return the sum of the
log probabilities of the (non-masked) tokens.
label_pad_token_id: The label pad token id.
is_encoder_decoder: Whether the model is an encoder-decoder model.
Returns:
A tensor of shape (batch_size,) containing the average/sum log probabilities of the given labels under the
given logits.
NrIzKLogits (batch and sequence length dim) and labels must have the same shape.rOr)rRr[Úcloner;Úwherer9)r\rrÚ loss_maskrdrererfÚget_batch_logpsÆs
z#_UnslothORPOTrainer.get_batch_logpscˆj|ˆjˆjˆjˆjjd}|djd}ˆjr"dˆ |d¡ini}ˆjr+d|d<||df|d d
d œ|¤Ž}|j }fd d
}ˆjrM|d 
¡} n|d 
¡} |d }
t   |
dk| ˆj¡} ||d|| d|ƒ} ˆj
||ddˆjˆjd} | d|}
| |d}ˆjs¦|d|ddddf}||dddddf}n |d|}||d}ˆjr¾|
|||| |jfS|
|||| fS)zÆRun the given model on the given batch of inputs, concatenating the chosen and rejected inputs together.
We do this to avoid doing two forward passes, because it's faster for FSDP.
)rrrrÚdecoder_input_idsÚconcatenated_labelsTrNF)r{Ú use_cachecsrˆjs|dddddf ¡}|dddf ¡}t ¡}| d|jd¡}| d¡}| |j¡}|||ƒ}|S)N.rIrO)rÚ
contiguousr0ÚCrossEntropyLossÚviewrRrT)r\Úloss_fctÚloss©r!rerfÚcross_entropy_losss
 
zD_UnslothORPOTrainer.concatenated_forward.<locals>.cross_entropy_lossrON)rrrI)rrrrXrRÚ _shift_rightrlr\r;Úaux_loss)r!r0Ú
len_chosenÚ model_kwargsÚoutputsÚ
all_logitsrír{Úchosen_nll_lossÚ all_logpsÚ chosen_logpsÚrejected_logpsÚ
chosen_logitsÚrejected_logitsrerfÚconcatenated_forwardñsbûýÿûÿýü  û    z(_UnslothORPOTrainer.concatenated_forwardÚtrainÚ
train_eval)ÚevalcCsi}| ||¡}|dd\}}}} }
|jr|d} | ||¡\} }
}}}|
|  ¡}|
|k ¡}|dkr7dnd}|j |
¡ ¡||d<|j |¡ ¡||d<|j |¡ ¡||d<|j |
|¡ ¡||d <|j |¡ ¡ ¡||d
<|j |¡ ¡ ¡||d <|j |  ¡ ¡¡ ¡||d <|j | ¡ ¡¡ ¡||d
<|j |
¡ ¡ ¡||d<|j |¡ ¡ ¡||d<|j |¡ ¡ ¡||d<tƒrãt  
¡|  ¡D]
\}}|  ¡||<qç|jrü||j
| 7}||fS)zXCompute the ORPO loss and other metrics for the given batch of inputs for train or test.NéÚeval_rzrewards/chosenzrewards/rejectedzrewards/accuracieszrewards/marginszlogps/rejectedz logps/chosenzlogits/rejectedz
logits/chosenÚnll_lossÚlog_odds_ratioÚlog_odds_chosen)rlÚfloatrXÚgather_for_metricsr×r+ÚxmÚ mark_stepr¬Úitemrm)r!r0ÚmetricsÚforward_outputrÒÚpolicy_chosen_logitsÚpolicy_rejected_logitsÚpolicy_nll_lossrïrrÚreward_accuraciesÚprefixrr“rererfÚget_batch_loss_metricsBs^ 
úÿ  ÿ þ
ÿ þ
ÿ þz*_UnslothORPOTrainer.get_batch_loss_metricsÚinputscCs~|jr
t|jjjƒntƒ}||j||dd\}}Wdƒn1s$wY| |jj¡}|j |dd|r=||fS|S)Nrû©)
r`r"rXr2rrTr1Ú
store_metrics)r!r0rÚreturn_outputsÚnum_items_in_batchÚcompute_loss_context_managerrërrererfÚ compute_lossysÿÿz _UnslothORPOTrainer.compute_losscCs†|jr
t|jjjƒntƒ}||j|d|d|jd|jj d}Wdƒn1s+wYt
||j|jj ƒ}|jj |dd}|S)zRGenerate samples from the model and reference model for the given batch of inputs.r|r}T)rzr{rÚ do_samplerMN)Úskip_special_tokens) r`r"rXr2Úgeneraterr5rMr4Ú batch_decode)r!r0Úgenerate_context_managerÚ
policy_outputÚpolicy_output_decodedrererfÚgenerate_from_modelsÿûÿ z'_UnslothORPOTrainer.generate_from_modelr•Ú ignore_keysc s0|jst d¡ˆdurt|dƒrt|jdgƒng|jr%t|jj j
ƒnt ƒ}t  
¡$||j||dd\}}Wdƒn1sDwYWdƒn1sSwY|j|dd|rh| ¡ddfS|d|ddœ}fd d
| ¡Dƒ} t j| |jj d } t j| jd |jj d }
| ¡| |
fS)
Na!prediction_step is only implemented for DPODataCollatorWithPadding, and you passed a datacollator that is different than DPODataCollatorWithPadding - you might see unexpected behavior. Alternatively, you can implement your own prediction_step method if you are using a custom data collatorrgÚkeys_to_ignore_at_inferencerýrúeval_logits/chosenúeval_logits/rejected)r!r"csg|]
\}}|ˆvr|qSrerer©rrerfr˜Ìr•z7_UnslothORPOTrainer.prediction_step.<locals>.<listcomp>rÈr)rkr<rirbr]rgr`r"rXr2r;Úno_gradrrr×ÚzerosrR) r!r0rr•rÚprediction_context_managerrërÚ logits_dictr\rer#rfÚprediction_step¨s2ÿ
ÿÿþz#_UnslothORPOTrainer.prediction_steprcCs*| ¡D]\}}|j|| |¡qdSrP)rnrZ)r!rÚkeyÚvaluerererfrÒsÿz!_UnslothORPOTrainer.store_metricsrýÚ
dataloaderÚ descriptionÚmetric_key_prefixc
|jrZt|jƒ}tjt|ƒ|jjd}|j |¡}|  |¡} | 
| ¡} |  |j | ¡}
t
jddgddt| d|
ƒDƒd} d|jjvrNt d tj| d
i¡d |jjvrZtd | d
tƒ |||||¡} | S)
Overriding built-in evaluation loop to store metrics for each batch. Prediction/evaluation loop, shared by
`Trainer.evaluate()` and `Trainer.predict()`.
Works both with or without labels.
)rÚPromptÚPolicycSs$g|]\}}||t|ƒdgqSrP)r~)rrÚpolrererfr˜ôrz7_UnslothORPOTrainer.evaluation_loop.<locals>.<listcomp>r)ÚcolumnsÚdataÚwandbÚgame_log)r2Úcomet_mlz game_log.csv)ÚnameÚtable)rr~Údatasetr8ÚsampleÚranger1Úeval_batch_sizeÚselectr2Ú_prepare_inputsrr0r5Ú DataFramerSr3ÚlogÚTabler-rÚevaluation_loop)
r!r+r,r•rr-Ú num_samplesÚrandom_indicesÚrandom_batch_datasetÚ random_batchrr7Úinitial_outputr#rerfrAÖs0
 

 ÿþ  þ
ÿz#_UnslothORPOTrainer.evaluation_loopÚlogsÚ
start_timecsTd|vrdnd}|j| ¡D]\}}t |¡ ¡ ¡||<q|j|=tƒ ||¡S)a1
Log `logs` on the various objects watching training, including stored metrics.
Args:
logs (`dict[str, float]`):
The values to log.
start_time (`float` or `None`, *optional*, defaults to `None`):
Start time of the training.
)rnr;rrr?)r!rGrHr)rr#rerfr?s
 z_UnslothORPOTrainer.logcCs´|jdur tdƒt|ƒr+t |jddd|j¡}tj||dddfgdd}n| |j¡}|dddf ¡|dddf<|j|d<|j durOtdƒ| 
|d k|j ¡|S)
Nz]model.config.decoder_start_token_id has to be defined. It is usually set to the pad_token_id.rI)rO.rNrO).rz,model.config.pad_token_id has to be defined.r) rhr[r*r;ÚfullrRÚ new_zerosràrMÚ masked_fill_)r!rzÚshifted_input_idsrererfs
ÿ   

z _UnslothORPOTrainer._shift_rightcsL|jjdurt|jjƒj}n |jj d¡d}|j|dtƒ ||¡dS)/rI)Ú
model_name) r1rrr6ÚsplitÚcreate_model_cardrÚ_save_checkpoint)r!r0ÚtrialrNr#rerfrQ2s
 z$_UnslothORPOTrainer._save_checkpointrNÚ dataset_nameÚtagsc
C| ¡sdSt|jjdƒrtj |jjj¡s|jjj}nd}|dur&tƒ}n
t |t
ƒr/|h}nt|ƒ}t|jjdƒr?|  d¡|  |j
¡t d¡}t|||j||tƒr]tjdur]tjjndtƒd|ddd }| tj |jjd
¡¡dS) 
Creates a draft of a model card using the information available to the `Trainer`.