MCPcopy Create free account
hub / github.com/InternScience/SciReason / _inference

Method _inference

opencompass/tasks/openicl_attack.py:91–182  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

89 self._inference()
90
91 def _inference(self):
92 self.logger.info(
93 f'Start inferencing {task_abbr_from_cfg(self.sub_cfg)}')
94
95 assert hasattr(self.infer_cfg, 'ice_template') or hasattr(self.infer_cfg, 'prompt_template'), \
96 'Both ice_template and prompt_template cannot be None simultaneously.' # noqa: E501
97 ice_template = None
98 if hasattr(self.infer_cfg, 'ice_template'):
99 ice_template = ICL_PROMPT_TEMPLATES.build(
100 self.infer_cfg['ice_template'])
101
102 prompt_template = None
103 if hasattr(self.infer_cfg, 'prompt_template'):
104 prompt_template = ICL_PROMPT_TEMPLATES.build(
105 self.infer_cfg['prompt_template'])
106
107 retriever_cfg = self.infer_cfg['retriever'].copy()
108 retriever_cfg['dataset'] = self.dataset
109 retriever = ICL_RETRIEVERS.build(retriever_cfg)
110
111 # set inferencer's default value according to model's config'
112 inferencer_cfg = self.infer_cfg['inferencer']
113 inferencer_cfg['model'] = self.model
114 self._set_default_value(inferencer_cfg, 'max_out_len',
115 self.max_out_len)
116 self._set_default_value(inferencer_cfg, 'batch_size', self.batch_size)
117 inferencer_cfg['max_seq_len'] = self.model_cfg['max_seq_len']
118 inferencer_cfg['dataset_cfg'] = self.dataset_cfg
119 inferencer = ICL_INFERENCERS.build(inferencer_cfg)
120
121 out_path = get_infer_output_path(self.model_cfg, self.dataset_cfg,
122 osp.join(self.work_dir, 'attack'))
123 out_dir, out_file = osp.split(out_path)
124 mkdir_or_exist(out_dir)
125
126 from config import LABEL_SET
127 from prompt_attack.attack import create_attack
128 from prompt_attack.goal_function import PromptGoalFunction
129
130 inferencer.retriever = retriever
131 inferencer.prompt_template = prompt_template
132 inferencer.ice_template = ice_template
133 inferencer.output_json_filepath = out_dir
134 inferencer.output_json_filename = out_file
135 goal_function = PromptGoalFunction(
136 inference=inferencer,
137 query_budget=self.cfg['attack'].query_budget,
138 logger=self.logger,
139 model_wrapper=None,
140 verbose='True')
141 if self.cfg['attack']['dataset'] not in LABEL_SET:
142 # set default
143 self.cfg['attack']['dataset'] = 'mmlu'
144 attack = create_attack(self.cfg['attack'], goal_function)
145
146 prompts = self.infer_cfg['inferencer']['original_prompt_list']
147 sorted_prompts = self.prompt_selection(inferencer, prompts)
148 if True:

Callers 1

runMethod · 0.95

Calls 8

_set_default_valueMethod · 0.95
prompt_selectionMethod · 0.95
task_abbr_from_cfgFunction · 0.90
get_infer_output_pathFunction · 0.90
openFunction · 0.85
buildMethod · 0.80
encodeMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected