| @@ -9,12 +9,11 @@ Kodierung von Stellung und Zügen nach CSSLab/maia3 (``dataset.py``, ``utils.py``, |
| 9 | 9 | ``uci.py``; AGPL-3.0). Modell: ONNX-Export von bqrio (AGPL-3.0, Ableitung von |
| 10 | 10 | UofTCSSLab/Maia3-79M), auf einen Commit festgelegt. |
| 11 | 11 | |
| 12 | | -Modell: fp16 herunterladen (156 MB), einmal nach fp32 umschreiben (braucht das Paket |
| 13 | | -``onnx``, optional) – auf der CPU rund 35 statt 100 ms je Zug, gleiche Züge. |
| 12 | +Modell: fp16 (156 MB), einmal heruntergeladen – auf der CPU rund 100 ms je Zug. |
| 14 | 13 | |
| 15 | 14 | GPU: Ist ``onnxruntime-ep-webgpu`` installiert, rechnet Maia über WebGPU auf der Grafikkarte |
| 16 | 15 | (Vulkan unter Linux, D3D12 unter Windows, Metal unter macOS) – rund 13 ms je Durchgang, |
| 17 | | -dort auch mit fp16. Sonst, oder mit ``--device cpu`` / ``SCHACH_MAIA_DEVICE=cpu``, auf der CPU. |
| 16 | +Sonst, oder mit ``--device cpu`` / ``SCHACH_MAIA_DEVICE=cpu``, auf der CPU. |
| 18 | 17 | |
| 19 | 18 | Start: ``python -m schach.maia_onnx`` (lädt das Modell beim ersten ``isready``) oder |
| 20 | 19 | ``--model-path DATEI``. Das Programm selbst ruft vorher ``prepare()`` mit Fortschritts- |
| @@ -96,30 +95,15 @@ def _softmax(x): |
| 96 | 95 | # --- Modell ------------------------------------------------------------------ |
| 97 | 96 | |
| 98 | 97 | def prepare(progress=None, local_only=False): |
| 99 | | - """Modell bereitstellen und Pfad liefern: die umgewandelte fp32-Fassung, sonst fp16. |
| 98 | + """Modell bereitstellen (aus dem Cache oder herunterladen) und Pfad liefern. |
| 100 | 99 | |
| 101 | | - fp16 ist halb so groß zum Herunterladen, rechnet auf der CPU aber langsamer (auf x86 |
| 102 | | - hat ONNX Runtime kaum fp16-Rechenwege; ein Durchgang 350 statt 50 ms). Ist das Paket |
| 103 | | - ``onnx`` da, wird es deshalb einmal nach fp32 umgeschrieben und im Cache abgelegt. |
| 100 | + Das Modell ist fp16. Auf der CPU rechnet ONNX Runtime damit langsamer als mit fp32 |
| 101 | + (x86 hat kaum fp16-Rechenwege), mit rund 100 ms je Zug aber schnell genug – eine |
| 102 | + fp32-Umwandlung brächte 65 ms, kostete aber das Paket ``onnx`` und 312 MB Platte. |
| 104 | 103 | |
| 105 | | - ``progress(stufe, fertig, gesamt)`` – stufe "download" (Bytes) oder "convert" (None, None). |
| 104 | + ``progress(stufe, fertig, gesamt)`` – stufe "download", fertig/gesamt in Bytes. |
| 106 | 105 | """ |
| 107 | | - fp32 = _cache_path(MODEL_FILE.replace(".fp16.", ".fp32.")) |
| 108 | | - if os.path.isfile(fp32): |
| 109 | | - return fp32 |
| 110 | | - fp16 = _fp16_path(progress, local_only) |
| 111 | | - try: |
| 112 | | - import onnx # noqa: F401 – nur prüfen, ob umgewandelt werden kann |
| 113 | | - except ImportError: |
| 114 | | - print("Hinweis: Paket „onnx“ fehlt – Maia rechnet mit fp16 (auf der CPU langsamer).", |
| 115 | | - file=sys.stderr, flush=True) |
| 116 | | - return fp16 |
| 117 | | - if progress: |
| 118 | | - progress("convert", None, None) |
| 119 | | - to_fp32(fp16, fp32) |
| 120 | | - if fp16 == _cache_path(MODEL_FILE): |
| 121 | | - os.remove(fp16) # unsere eigene Kopie wird nicht mehr gebraucht (den HF-Cache fassen wir nicht an) |
| 122 | | - return fp32 |
| 106 | + return _fp16_path(progress, local_only) |
| 123 | 107 | |
| 124 | 108 | |
| 125 | 109 | model_path = prepare # alter Name (tools/maia_onnx_vergleich.py) |
| @@ -190,38 +174,6 @@ def _cache_path(filename): |
| 190 | 174 | return os.path.join(base, "schach", filename.replace(".onnx", f".{MODEL_REVISION[:7]}.onnx")) |
| 191 | 175 | |
| 192 | 176 | |
| 193 | | -def to_fp32(source, target): |
| 194 | | - """fp16-Modell → fp32: Gewichte, Konstanten, Umwandlungen und Typangaben. Gleiche Gewichte, |
| 195 | | - nur breiter gerechnet. Schreibt erst in eine Zwischendatei, damit kein halbes Modell liegen bleibt.""" |
| 196 | | - import onnx |
| 197 | | - from onnx import TensorProto, numpy_helper |
| 198 | | - |
| 199 | | - f16, f32 = TensorProto.FLOAT16, TensorProto.FLOAT |
| 200 | | - |
| 201 | | - def widen(tensor): |
| 202 | | - return numpy_helper.from_array(numpy_helper.to_array(tensor).astype(np.float32), tensor.name) |
| 203 | | - |
| 204 | | - model = onnx.load(source) |
| 205 | | - graph = model.graph |
| 206 | | - for i, tensor in enumerate(graph.initializer): |
| 207 | | - if tensor.data_type == f16: |
| 208 | | - graph.initializer[i].CopyFrom(widen(tensor)) |
| 209 | | - for node in graph.node: |
| 210 | | - for attr in node.attribute: |
| 211 | | - if node.op_type == "Cast" and attr.name == "to" and attr.i == f16: |
| 212 | | - attr.i = f32 |
| 213 | | - elif attr.type == onnx.AttributeProto.TENSOR and attr.t.data_type == f16: |
| 214 | | - attr.t.CopyFrom(widen(attr.t)) |
| 215 | | - for info in [*graph.value_info, *graph.input, *graph.output]: |
| 216 | | - if info.type.tensor_type.elem_type == f16: |
| 217 | | - info.type.tensor_type.elem_type = f32 |
| 218 | | - onnx.checker.check_model(model) |
| 219 | | - os.makedirs(os.path.dirname(target), exist_ok=True) |
| 220 | | - partial = target + ".part" |
| 221 | | - onnx.save(model, partial) |
| 222 | | - os.replace(partial, target) |
| 223 | | - |
| 224 | | - |
| 225 | 177 | def _options(ort): |
| 226 | 178 | options = ort.SessionOptions() |
| 227 | 179 | options.log_severity_level = 3 # keine Warnungen über ungenutzte Initialisierer |