File size: 9,852 Bytes
fedd8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# MedGemma 医学推理运行器
# 类似 Protenix 的 BiologyInferenceRunner
import sys
from pathlib import Path

DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(DIR))

import logging
import os
import json
from typing import Any, Dict, List, Optional
import torch

from models.medgemma import MedGemma
from models.config import parse_configs, load_config

logger = logging.getLogger(__name__)

class AttrDict(dict):
    """同时支持 config.key 和 config['key']。"""

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError as exc:
            raise AttributeError(key) from exc

    def __setattr__(self, key, value):
        self[key] = value

def to_attr_dict(value):
    if isinstance(value, dict):
        return AttrDict({
            key: to_attr_dict(item)
            for key, item in value.items()
        })

    if isinstance(value, list):
        return [to_attr_dict(item) for item in value]

    if isinstance(value, tuple):
        return tuple(to_attr_dict(item) for item in value)

    return value


class MedicalInferenceRunner:
    """
    MedGemma 医学推理运行器
    提供统一的推理接口
    """

    def __init__(self, configs: Any) -> None:
        """
        初始化推理运行器

        Args:
            configs: 配置对象
        """
        #self.configs = configs
        self.configs = to_attr_dict(configs)
        self.init_env()
        self.init_basics()
        self.init_model()
        self.init_dumper()

        logger.info("MedicalInferenceRunner initialized")

    def init_env(self) -> None:
        """初始化环境"""
        self.use_cuda = torch.cuda.is_available()
        if self.use_cuda:
            self.device = torch.device("cuda:0")
            torch.cuda.set_device(self.device)
            logger.info(f"Using GPU: {torch.cuda.get_device_name(0)}")
        else:
            self.device = torch.device("cpu")
            logger.info("Using CPU")

    def init_basics(self) -> None:
        """初始化基础设置"""
        self.dump_dir = self.configs.output.dump_dir
        self.error_dir = os.path.join(self.dump_dir, "errors")
        os.makedirs(self.dump_dir, exist_ok=True)
        os.makedirs(self.error_dir, exist_ok=True)
        logger.info(f"Output directory: {self.dump_dir}")

    def init_model(self) -> None:
        """初始化 MedGemma 模型"""
        logger.info("Loading MedGemma model...")
        try:
            self.model = MedGemma(self.configs)
            logger.info("Model loaded successfully")
        except Exception as e:
            logger.error(f"Failed to load model: {e}")
            raise

    def init_dumper(self) -> None:
        """初始化结果保存器"""
        self.output_format = self.configs.output.output_format
        self.save_predictions = self.configs.output.save_predictions
        logger.info(f"Output format: {self.output_format}")

    @torch.no_grad()
    def predict(
        self,
        messages: List[Dict[str, Any]],
        max_tokens: Optional[int] = None,
        temperature: Optional[float] = None,
    ) -> Dict[str, Any]:
        """
        运行推理

        Args:
            messages: 消息列表
            max_tokens: 最大生成 token 数
            temperature: 采样温度

        Returns:
            预测结果
        """
        if max_tokens is None:
            max_tokens = self.configs.inference.default_max_tokens
        if temperature is None:
            temperature = self.configs.inference.temperature

        try:
            result = self.model.forward(
                messages=messages,
                max_tokens=max_tokens,
                temperature=temperature,
            )
            return result
        except Exception as e:
            logger.error(f"Prediction failed: {e}")
            return {"error": str(e)}

    def run_from_file(self, input_path: str) -> None:
        """
        从文件运行推理

        Args:
            input_path: 输入文件路径(JSON 或 JSONL)
        """
        logger.info(f"Loading input from: {input_path}")

        # 读取输入数据
        if input_path.endswith('.jsonl'):
            samples = self._load_jsonl(input_path)
        elif input_path.endswith('.json'):
            samples = self._load_json(input_path)
        else:
            raise ValueError(f"Unsupported file format: {input_path}")

        logger.info(f"Loaded {len(samples)} samples")

        # 处理每个样本
        results = []
        for idx, sample in enumerate(samples):
            logger.info(f"Processing sample {idx + 1}/{len(samples)}")

            try:
                # 提取消息
                if "messages" in sample:
                    messages = sample["messages"]
                elif "text" in sample:
                    messages = [{"role": "user", "content": sample["text"]}]
                elif "question" in sample:
                    messages = [{"role": "user", "content": sample["question"]}]
                else:
                    logger.warning(f"Sample {idx} has no valid input")
                    continue

                # 运行推理
                result = self.predict(messages)

                # 添加样本 ID
                result["sample_id"] = sample.get("id", idx)

                # 保存结果
                if self.save_predictions:
                    self._save_result(result, idx)

                results.append(result)

            except Exception as e:
                logger.error(f"Error processing sample {idx}: {e}")
                self._save_error(sample, idx, str(e))

        logger.info(f"Completed processing {len(results)} samples")

        # 保存汇总结果
        self._save_summary(results)

    def _load_json(self, filepath: str) -> List[Dict[str, Any]]:
        """加载 JSON 文件"""
        with open(filepath, 'r', encoding='utf-8') as f:
            data = json.load(f)

        if isinstance(data, list):
            return data
        else:
            return [data]

    def _load_jsonl(self, filepath: str) -> List[Dict[str, Any]]:
        """加载 JSONL 文件"""
        samples = []
        with open(filepath, 'r', encoding='utf-8') as f:
            for line in f:
                if line.strip():
                    samples.append(json.loads(line))
        return samples

    def _save_result(self, result: Dict[str, Any], idx: int) -> None:
        """保存单个结果"""
        output_path = os.path.join(
            self.dump_dir,
            f"prediction_{idx}.{self.output_format}"
        )

        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(result, f, indent=2, ensure_ascii=False)

    def _save_error(self, sample: Dict[str, Any], idx: int, error: str) -> None:
        """保存错误信息"""
        error_path = os.path.join(
            self.error_dir,
            f"error_{idx}.json"
        )

        error_data = {
            "sample": sample,
            "error": error,
        }

        with open(error_path, 'w', encoding='utf-8') as f:
            json.dump(error_data, f, indent=2, ensure_ascii=False)

    def _save_summary(self, results: List[Dict[str, Any]]) -> None:
        """保存汇总结果"""
        summary_path = os.path.join(self.dump_dir, "summary.json")

        summary = {
            "total_samples": len(results),
            "successful": sum(1 for r in results if "error" not in r),
            "failed": sum(1 for r in results if "error" in r),
            "results": results,
        }

        with open(summary_path, 'w', encoding='utf-8') as f:
            json.dump(summary, f, indent=2, ensure_ascii=False)

        logger.info(f"Summary saved to: {summary_path}")

    def run_interactive(self) -> None:
        """交互式推理"""
        logger.info("Starting interactive mode. Type 'quit' to exit.")

        while True:
            try:
                user_input = input("\nUser: ")
                if user_input.lower() in ['quit', 'exit', 'q']:
                    break

                messages = [{"role": "user", "content": user_input}]
                result = self.predict(messages)

                # 提取响应
                if "choices" in result and result["choices"]:
                    response = result["choices"][0]["message"]["content"]
                    print(f"\nAssistant: {response}")
                else:
                    print(f"\nError: {result}")

            except KeyboardInterrupt:
                break
            except Exception as e:
                logger.error(f"Error: {e}")

        logger.info("Exiting interactive mode")


def main():
    """主函数"""
    import argparse

    parser = argparse.ArgumentParser(description="MedGemma Medical Inference Runner")
    parser.add_argument("--config", type=str, required=True, help="Config file path")
    parser.add_argument("--input", type=str, help="Input file path (JSON/JSONL)")
    parser.add_argument("--interactive", action="store_true", help="Interactive mode")
    parser.add_argument("--model_path", type=str, help="Override model path")
    parser.add_argument("--dump_dir", type=str, help="Override output directory")

    args = parser.parse_args()

    # 加载配置
    configs = load_config(args.config)

    # 覆盖配置
    if args.model_path:
        configs.model.model_path = args.model_path
    if args.dump_dir:
        configs.output.dump_dir = args.dump_dir

    # 创建运行器
    runner = MedicalInferenceRunner(configs)

    # 运行推理
    if args.interactive:
        runner.run_interactive()
    elif args.input:
        runner.run_from_file(args.input)
    else:
        logger.error("Either --input or --interactive must be specified")


if __name__ == "__main__":
    main()