MCPcopy Index your code
hub / github.com/modelscope/modelscope / __init__

Method __init__

modelscope/pipelines/base.py:72–138  ·  view source on GitHub ↗

Base class for pipeline. If config_file is provided, model and preprocessor will be instantiated from corresponding config. Otherwise, model and preprocessor will be constructed separately. Args: config_file(str, optional): Filepath to configuration fil

(self,
                 config_file: str = None,
                 model: Union[InputModel, List[InputModel]] = None,
                 preprocessor: Union[Preprocessor, List[Preprocessor]] = None,
                 device: str = 'gpu',
                 auto_collate=True,
                 device_map=None,
                 **kwargs)

Source from the content-addressed store, hash-verified

70 return models
71
72 def __init__(self,
73 config_file: str = None,
74 model: Union[InputModel, List[InputModel]] = None,
75 preprocessor: Union[Preprocessor, List[Preprocessor]] = None,
76 device: str = 'gpu',
77 auto_collate=True,
78 device_map=None,
79 **kwargs):
80 """ Base class for pipeline.
81
82 If config_file is provided, model and preprocessor will be
83 instantiated from corresponding config. Otherwise, model
84 and preprocessor will be constructed separately.
85
86 Args:
87 config_file(str, optional): Filepath to configuration file.
88 model: (list of) Model name or model object
89 preprocessor: (list of) Preprocessor object
90 device (str): device str, should be either cpu, cuda, gpu, gpu:X or cuda:X
91 auto_collate (bool): automatically to convert data to tensor or not.
92 compile (bool, optional): Compile the model with torch 2.0, default False
93 compile_options (dict, optional): The compile options if compile=True,
94 default None to use the default params of 'TorchModel.compile'.
95 """
96 if device_map is not None:
97 assert device == 'gpu', '`device` and `device_map` cannot be input at the same time!'
98 self.device_map = device_map
99 verify_device(device)
100 self.device_name = device
101 self.trust_remote_code = kwargs.get('trust_remote_code', False)
102
103 if not isinstance(model, List):
104 self.model = self.initiate_single_model(model, **kwargs)
105 self.models = [self.model]
106 else:
107 self.model = None
108 self.models = self.initiate_multiple_models(model)
109
110 self.has_multiple_models = len(self.models) > 1
111
112 if config_file is not None:
113 self.cfg = Config.from_file(config_file)
114 model_dir = os.path.dirname(config_file)
115 elif not self.has_multiple_models:
116 if isinstance(self.model, str):
117 model_dir = self.model
118 else:
119 model_dir = self.model.model_dir
120 self.cfg = read_config(model_dir)
121
122 if preprocessor is None and not self.has_multiple_models:
123 self.preprocessor = Preprocessor.from_pretrained(model_dir)
124 else:
125 self.preprocessor = preprocessor
126
127 if self.model or (self.has_multiple_models and self.models[0]):
128 self.framework = self._get_framework()
129 else:

Callers

nothing calls this directly

Calls 9

initiate_single_modelMethod · 0.95
_get_frameworkMethod · 0.95
verify_deviceFunction · 0.90
read_configFunction · 0.90
create_deviceFunction · 0.90
from_fileMethod · 0.80
getMethod · 0.45
from_pretrainedMethod · 0.45

Tested by

no test coverage detected