CoolFace
Modelpublic

xtuner/internlm-7b-qlora-msagent-react

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes7downloads
xtuner_config.py201 linesDownload Raw Back to root
1# Copyright (c) OpenMMLab. All rights reserved.2import torch3from bitsandbytes.optim import PagedAdamW32bit4from mmengine.dataset import DefaultSampler5from mmengine.hooks import (CheckpointHook, DistSamplerSeedHook, IterTimerHook,6                            LoggerHook, ParamSchedulerHook)7from mmengine.optim import AmpOptimWrapper, CosineAnnealingLR8from modelscope.msdatasets import MsDataset9from peft import LoraConfig10from transformers import (AutoModelForCausalLM, AutoTokenizer,11                          BitsAndBytesConfig)12 13from xtuner.dataset import process_ms_dataset14from xtuner.dataset.collate_fns import default_collate_fn15from xtuner.dataset.map_fns import (msagent_react_map_fn,16                                    template_map_fn_factory)17from xtuner.engine import DatasetInfoHook, EvaluateChatHook18from xtuner.model import SupervisedFinetune19from xtuner.utils import PROMPT_TEMPLATE20 21#######################################################################22#                          PART 1  Settings                           #23#######################################################################24# Model25pretrained_model_name_or_path = 'internlm/internlm-7b'26 27# Data28data_path = 'damo/MSAgent-Bench'29prompt_template = PROMPT_TEMPLATE.default30max_length = 204831pack_to_max_length = False32 33# Scheduler & Optimizer34batch_size = 8  # per_device35accumulative_counts = 136dataloader_num_workers = 237max_epochs = 338optim_type = PagedAdamW32bit39lr = 2e-440betas = (0.9, 0.999)41weight_decay = 042max_norm = 1  # grad clip43 44# Evaluate the generation performance during the training45evaluation_freq = 50046SYSTEM = (47    '你是一个可以调用外部工具的助手,可以使用的工具包括:\n'48    "{{\'GoogleSearch\': \'一个可以从谷歌搜索结果的API。\\n"49    '当你需要对于一个特定问题找到简短明了的回答时,可以使用它。\\n'50    "输入应该是一个搜索查询。\\n\\n\',"51    "\'PythonInterpreter\': \"用来执行Python代码。代码必须是一个函数,\\n"52    "函数名必须得是 \'solution\',代码对应你的思考过程。代码实例格式如下:\\n"53    '```python\\n# import 依赖包\\nimport xxx\\ndef solution():'54    '\\n    # 初始化一些变量\\n    variable_names_with_real_meaning = xxx'55    '\\n    # 步骤一\\n    mid_variable = func(variable_names_with_real_meaning)'56    '\\n    # 步骤 x\\n    mid_variable = func(mid_variable)\\n    # 最后结果'57    '\\n    final_answer =  func(mid_variable)\\n    return final_answer'58    "\\n```\\n\"}}\n"59    '如果使用工具请遵循以下格式回复:\n```\n'60    'Thought:思考你当前步骤需要解决什么问题,是否需要使用工具\n'61    "Action:工具名称,你的工具必须从 [[\'GoogleSearch\', \'PythonInterpreter\']] 选择"62    '\nAction Input:工具输入参数\n```\n工具返回按照以下格式回复:\n'63    '```\nResponse:调用工具后的结果\n```'64    '\n如果你已经知道了答案,或者你不需要工具,请遵循以下格式回复\n```'65    '\nThought:给出最终答案的思考过程\nFinal Answer:最终答案\n```\n开始!\n')66evaluation_inputs = ['上海明天天气怎么样?']67 68#######################################################################69#                      PART 2  Model & Tokenizer                      #70#######################################################################71tokenizer = dict(72    type=AutoTokenizer.from_pretrained,73    pretrained_model_name_or_path=pretrained_model_name_or_path,74    trust_remote_code=True,75    padding_side='right')76 77model = dict(78    type=SupervisedFinetune,79    llm=dict(80        type=AutoModelForCausalLM.from_pretrained,81        pretrained_model_name_or_path=pretrained_model_name_or_path,82        trust_remote_code=True,83        torch_dtype=torch.float16,84        quantization_config=dict(85            type=BitsAndBytesConfig,86            load_in_4bit=True,87            load_in_8bit=False,88            llm_int8_threshold=6.0,89            llm_int8_has_fp16_weight=False,90            bnb_4bit_compute_dtype=torch.float16,91            bnb_4bit_use_double_quant=True,92            bnb_4bit_quant_type='nf4')),93    lora=dict(94        type=LoraConfig,95        r=64,96        lora_alpha=16,97        lora_dropout=0.1,98        bias='none',99        task_type='CAUSAL_LM'))100 101#######################################################################102#                      PART 3  Dataset & Dataloader                   #103#######################################################################104train_dataset = dict(105    type=process_ms_dataset,106    dataset=dict(type=MsDataset.load, dataset_name=data_path),107    tokenizer=tokenizer,108    max_length=max_length,109    dataset_map_fn=msagent_react_map_fn,110    template_map_fn=dict(111        type=template_map_fn_factory, template=prompt_template),112    remove_unused_columns=True,113    shuffle_before_pack=True,114    pack_to_max_length=pack_to_max_length)115 116train_dataloader = dict(117    batch_size=batch_size,118    num_workers=dataloader_num_workers,119    dataset=train_dataset,120    sampler=dict(type=DefaultSampler, shuffle=True),121    collate_fn=dict(type=default_collate_fn))122 123#######################################################################124#                    PART 4  Scheduler & Optimizer                    #125#######################################################################126# optimizer127optim_wrapper = dict(128    type=AmpOptimWrapper,129    optimizer=dict(130        type=optim_type, lr=lr, betas=betas, weight_decay=weight_decay),131    clip_grad=dict(max_norm=max_norm, error_if_nonfinite=False),132    accumulative_counts=accumulative_counts,133    loss_scale='dynamic',134    dtype='float16')135 136# learning policy137# More information: https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/param_scheduler.md  # noqa: E501138param_scheduler = dict(139    type=CosineAnnealingLR,140    eta_min=lr * 0.1,141    by_epoch=True,142    T_max=max_epochs,143    convert_to_iter_based=True)144 145# train, val, test setting146train_cfg = dict(by_epoch=True, max_epochs=max_epochs, val_interval=1)147 148#######################################################################149#                           PART 5  Runtime                           #150#######################################################################151# Log the dialogue periodically during the training process, optional152custom_hooks = [153    dict(type=DatasetInfoHook, tokenizer=tokenizer),154    dict(155        type=EvaluateChatHook,156        tokenizer=tokenizer,157        every_n_iters=evaluation_freq,158        evaluation_inputs=evaluation_inputs,159        system=SYSTEM,160        prompt_template=prompt_template)161]162 163# configure default hooks164default_hooks = dict(165    # record the time of every iteration.166    timer=dict(type=IterTimerHook),167    # print log every 100 iterations.168    logger=dict(type=LoggerHook, interval=10),169    # enable the parameter scheduler.170    param_scheduler=dict(type=ParamSchedulerHook),171    # save checkpoint per epoch.172    checkpoint=dict(type=CheckpointHook, interval=1),173    # set sampler seed in distributed evrionment.174    sampler_seed=dict(type=DistSamplerSeedHook),175)176 177# configure environment178env_cfg = dict(179    # whether to enable cudnn benchmark180    cudnn_benchmark=False,181    # set multi process parameters182    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0),183    # set distributed parameters184    dist_cfg=dict(backend='nccl'),185)186 187# set visualizer188visualizer = None189 190# set log level191log_level = 'INFO'192 193# load from which checkpoint194load_from = None195 196# whether to resume training from the loaded checkpoint197resume = False198 199# Defaults to use random seed and disable `deterministic`200randomness = dict(seed=None, deterministic=False)201