MCPcopy Create free account
hub / github.com/OpenMOSS/MOSS / Inference

Class Inference

moss_inference.py:44–343  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42 }
43
44class Inference:
45 def __init__(
46 self,
47 model: Optional[MossForCausalLM] = None,
48 model_dir: Optional[str] = None,
49 parallelism: bool = True,
50 device_map: Optional[Union[str, List[int]]] = None,
51 ) -> None:
52 """
53 Initializes the MossModel with a given model or loads a model from the specified directory.
54
55 Args:
56 model (Optional[MossForCausalLM], optional): An existing model to use. Defaults to None.
57 model_dir (Optional[str], optional): The directory containing the pre-trained model files. Defaults to None.
58 parallelism (bool, optional): Whether to initialize model parallelism. Defaults to True.
59 device_map (Optional[Union[str, List[int]]], optional): The list of GPU device indices for model parallelism or "auto" to use the default device map. Defaults to None.
60 """
61 self.model_dir = "OpenMOSS-Team/moss-moon-003-sft" if not model_dir else model_dir
62
63 if model:
64 self.model = model
65 else:
66 self.model = (
67 self.Init_Model_Parallelism(raw_model_dir=self.model_dir, device_map=device_map)
68 if parallelism
69 else MossForCausalLM.from_pretrained(self.model_dir)
70 )
71
72 self.tokenizer = MossTokenizer.from_pretrained(self.model_dir)
73
74 self.prefix = PREFIX
75 self.default_paras = DEFAULT_PARAS
76 self.num_layers, self.heads, self.hidden, self.vocab_size = 34, 24, 256, 107008
77
78 self.moss_startwords = torch.LongTensor([27, 91, 44, 18420, 91, 31175])
79 self.tool_startwords = torch.LongTensor([27, 91, 6935, 1746, 91, 31175])
80 self.tool_specialwords = torch.LongTensor([6045])
81
82 self.innerthought_stopwords = torch.LongTensor([self.tokenizer.convert_tokens_to_ids("<eot>")])
83 self.tool_stopwords = torch.LongTensor([self.tokenizer.convert_tokens_to_ids("<eoc>")])
84 self.result_stopwords = torch.LongTensor([self.tokenizer.convert_tokens_to_ids("<eor>")])
85 self.moss_stopwords = torch.LongTensor([self.tokenizer.convert_tokens_to_ids("<eom>")])
86
87 def Init_Model_Parallelism(self, raw_model_dir: str, device_map: Union[str, List[int]] = "auto") -> MossForCausalLM:
88 """
89 Initializes model parallelism for the given model and device map.
90
91 Args:
92 raw_model_dir (str): The directory containing the pre-trained model files.
93 device_map (Union[str, List[int]], optional): The list of GPU device indices for model parallelism, or "auto" to use the default device map. Defaults to "auto".
94
95 Returns:
96 MossForCausalLM: The model with model parallelism initialized.
97
98 References:
99 https://github1s.com/huggingface/accelerate/blob/HEAD/src/accelerate/big_modeling.py#L407
100 """
101 # Print the number of CUDA devices available

Callers 1

moss_inference.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected