4.11. 新增模型

vLLM-gcu中,可以按照vLLM官方手册所示进行模型新增。相比于对vLLM-gcu进行侵入式修改,推荐采用out-of-tree方式进行模型新增。

使用out-of-tree方式进行模型新增且需要多卡推理时,为了方便新增模型可以注册到所有的worker,可按照下述方法实现示例模型cusllama的新增。

  • 本地新建文件夹A,在其下创建OOT_utils.py,内部填写下述内容:

      import os
      import sys
      import importlib
    
      def register_OOT_models(OOT_models_path: str):
          for model in os.listdir(OOT_models_path):
              model_path = os.path.join(OOT_models_path, model)
              if not os.path.isdir(model_path) or \
                  not os.path.exists(os.path.join(model_path,'register_'+model+'.py')):
                  continue
    
              sys.path.append(model_path)
              module = importlib.import_module('register_'+model)
              attrs = dir(module)
              for attr in attrs:
                  if 'register' in attr:
                      func = getattr(module, attr)
                      func()
    
  • 在A下创建模型名称文件夹,需与模型名称一致,本例中为cusllama

  • cusllama下新建存储模型推理过程的py文件,并在新建的register_cusllama.py中实现register_cusllama函数,在其中调用ModelRegistry.register_model完成模型注册。如下给出register_cusllama.py的示例:

      import os
      current_dir = os.path.dirname(os.path.abspath(__file__))
      import sys
      sys.path.append(current_dir)
      import contextlib
      from transformers import AutoConfig
    
      def register_cusllama():
          from cusllama import CustomerLlaMAForCausalLM
          from cusllama_config import CustomerLlaMAConfig
    
          from vllm import ModelRegistry
          ModelRegistry.register_model('CustomerLlaMAForCausalLM', CustomerLlaMAForCausalLM)
    
          with contextlib.suppress(ValueError):
              AutoConfig.register("cusllama", CustomerLlaMAConfig)
    
  • 推理时,添加环境变量VLLM_OOT_MODEL_PATH指向A的路径。