Compare commits

..

2 Commits

Author SHA1 Message Date
9dfd0e4d77 Merge pull request 'fix(worker): real image 加 tensorflow 支援 tflite' (#2) from fix/tflite-tensorflow into main 2026-07-02 07:32:53 +00:00
21f863ae1e fix(worker): real image 加 tensorflow 支援 tflite 輸入格式
staging real worker 轉 tflite 失敗:tflite→onnx 需要 import tensorflow,但
Dockerfile.real 當初為省 image 大小沒裝 TF(只驗過 onnx/bie/nef 輸入)。

改 services/workers/Dockerfile.real:
- 加 tensorflow-cpu==2.13.1(Python 3.9 相容 + numpy<2 相容 + ktc TF compat
  patch 預期範圍的交集;-cpu 省 ~1.5GB CUDA)
- 加 igraph==1.0.0(tree_structure.py 硬 import、pin 對齊本檔版本鎖哲學)
- 補 typing-extensions>=4.8.0(TF 會把它降版打爆 fastapi import)
- numpy<2 精確化為 numpy==1.23.5(落在原 <2 範圍 + TF 接受窗口、語意子集非破鎖)

本機驗證:image 4.36GB→5.71GB;tflite→onnx 通(產 188 節點合法 ONNX);既有
BIE→NEF 回歸產出同一顆真 NEF(621,956 bytes)、prebuild ldd 全 resolved。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 15:31:59 +08:00

View File

@ -50,8 +50,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
# 0.7.0 import 乾淨。
# - onnxsim==0.4.36--only-binarypy3.9/bullseye 上新版 onnxsim 無 wheel、需
# cmake 從源碼編0.4.36 有 prebuilt wheel避免裝 build toolchain。
# 4) numpy<2 最後裝prebuild 鏈的 C-extension 以 numpy 1.x 編譯numpy 2.x 會出
# "numpy.core.multiarray failed to import"。最後安裝確保不被其他套件升級回 2.x。
# 4) tflite 輸入格式轉檔tflite→onnx第一步 ktc.onnx_optimizer.tflite2onnx_flow
# 需要 tensorflow + python-igraphconverter: libs/ONNX_Convertor/tflite-onnx
# - tensorflow-cpu==2.13.1:挑此版的三重理由——
# (a) Python 3.9 相容2.13.x 支援 py3.83.11;再新的 TF2.16+drop py3.9。
# (b) numpy<2 相容最關鍵TF 2.13.1 要求 numpy>=1.22,<=1.24.3(全 1.x
# 與既有 prebuild 鏈的「numpy 1.x」硬需求一致再新的 TF2.16+)會拉
# numpy>=2 → 直接撞爆 prebuild 的 "numpy.core.multiarray failed to import"。
# (c) ktc 需求ktc/onnx_optimizer_1_13.py 的 _get_tensor_details_compat patch
# 是為「較新 TFtf.lite.Interpreter._get_tensor_details 需 subgraph_index
# TF 2.5+ 引入)」寫的 → TF 2.13.1 落在此範圍、patch 正常生效。
# 選 -cpu 版:不需 GPU、省下 CUDA 相關 ~1.5GB。
# - igraph==1.0.0converter 的 tree_structure.py:13 `from igraph import Graph` 硬
# import 需要不裝→tflite→onnx 第一步 ModuleNotFoundError。pin 死是為了
# reproducible build——igraph 是 tflite 鏈的硬依賴、裸裝 latest 未來重 build 可能
# 裝到行為改變的新版1.0.0 是本次 build 實裝並驗證可跑的版本。
# - typing-extensions>=4.8.0TF 2.13.1 會把 typing-extensions 降到 4.5.0
# (其 pin 是 <4.6),導致 fastapi 0.104.1 `import fastapi` 掛
# `cannot import name 'TypeAliasType'`。TF 對 typing-extensions 的上限只是
# *soft warning*(實測升到 4.15 後 tf 2.13.1 + tf.lite.Interpreter 仍正常),
# 故在 TF 之後把 typing-extensions 補回 >=4.8.0,修復 fastapi/pydantic。
# 5) numpy==1.23.5 最後裝並「釘死」prebuild 鏈的 C-extension 以 numpy 1.x 編譯,
# numpy 2.x 會出 "numpy.core.multiarray failed to import"。1.23.5 同時滿足
# TF 2.13.1 的 numpy>=1.22,<=1.24.3 窗口 + 既有 prebuild/onnx 的 1.x 需求。
# 最後安裝確保不被 TF/其他套件連帶升級。
COPY services/workers/nef/requirements.txt /tmp/worker-requirements.txt
RUN pip install --no-cache-dir \
redis>=5.0 boto3>=1.28 \
@ -63,7 +85,9 @@ RUN pip install --no-cache-dir \
pyzipper dict_recursive_update beautifulsoup4 jinja2 matplotlib tabulate \
commentjson IPython adjustText "onnx_tool==0.7.0" \
&& pip install --no-cache-dir --only-binary :all: "onnxsim==0.4.36" \
&& pip install --no-cache-dir "numpy<2"
&& pip install --no-cache-dir "tensorflow-cpu==2.13.1" "igraph==1.0.0" \
&& pip install --no-cache-dir "typing-extensions>=4.8.0" \
&& pip install --no-cache-dir "numpy==1.23.5"
# --- Application + toolchain source ------------------------------------------
# 精準 COPY避免把 libs/dynasty(2.0G) + libs/compiler(1.2G) 帶進來。