Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

파이썬 - onnxruntime_genai에서 지원하지 않는 모델 사용

기본적으로 "Foundry Local"에서 지원하는 모델들은 onnx 포맷을 지원할 뿐만 아니라 olive 도구를 이용한 변환까지 지원합니다. 이를 통해 Hugging Face에 공개된 다양한 모델을 사용할 수도 있는데요, 아쉽게도 이게 100% 지원되는 것은 아닙니다.

예를 들어 볼까요? ^^ 테스트를 위해 gemma-3n-E2B-it-ONNX 모델을 다운로드한 후,

(huggingface-build) C:\foundry_cache\models\gemma-3n-E2B-it-ONNX> huggingface-cli download onnx-community/gemma-3n-E2B-it-ONNX --include * --local-dir .

onnxruntime_genai 패키지를 이용해 해당 모델을 로드하려고 시도하면,

import onnxruntime_genai as og

model = og.Model('gemma-3n-E2B-it-ONNX/onnx')

이런 오류가 발생합니다.

[윈도우]
(huggingface-build) C:\foundry_cache\models> python onnx_genai.py
Traceback (most recent call last):
  File "C:\foundry_cache\models\onnx_genai.py", line 3, in <module>
    model = og.Model('gemma-3n-E2B-it-ONNX/onnx')
RuntimeError: Error opening gemma-3n-E2B-it-ONNX/onnx\genai_config.json

[리눅스]
(huggingface-build) testusr@TestPC:/mnt/c/foundry_cache/models$ python onnx_genai.py

실제로 gemma-3n-E2B-it-ONNX로부터 다운로드한 디렉터리에는 (지난 글에서 실습한 "Qwen/Qwen2.5-Math-1.5B-Instruct" 모델과는 다르게) genai_config.json이 없습니다.




다른 예를 하나 들어볼까요? onnxruntime_genai 공식 문서에는 deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B 모델에 대한 예제가 있는데요,

Reasoning in Python with DeepSeek-R1-Distill models
; https://onnxruntime.ai/docs/genai/tutorials/deepseek-python.html

이 모델 역시,

deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
; https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B/tree/main

파일을 보면 genai_config.json이 없습니다. 하지만, olive 명령어로 이것을 onnx 포맷으로 변환하면,

olive auto-opt --model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B --output_path ./deepseek-r1-distill-qwen-1.5B --device gpu --provider CUDAExecutionProvider --precision int4 --use_model_builder --log_level 1

로컬에 모델을 다운로드 및 onnx 포맷으로 변환이 완료된 시점에 genai_config.json 파일이 함께 생성됩니다. 그러니까, gemma-3n-E2B-it-ONNX도 원래는 genai_config.json 파일이 있어야 하는데... 그걸 repo에서 담고 있지 않는 것입니다. 문제는, (아마도 이 분야로 잘 아시는 분이라면 개별 parameter를 수작업으로 구성할 수 있겠지만) 저 같은 ^^ 초보가 genai_config.json을 임의로 만드는 것이 어렵다는 점입니다.

사실 가장 쉬운 방법은, 처음부터 다시 gemma-3n-E2B-it 모델을 onnx 포맷으로 olive를 이용해 다운로드/변환하면 되는데,

olive auto-opt --model_name_or_path google/gemma-3n-E2B-it --output_path ./gemma-3n-E2B-it --device gpu --provider CUDAExecutionProvider --precision int4 --use_model_builder --log_level 1

실제로 해보면 이렇게 오류가 발생합니다.

Loading HuggingFace model from google/gemma-3n-E2B-it
[... 13:24:56,566] [INFO] [run.py:138:run_engine] Running workflow default_workflow
[... 13:24:56,650] [INFO] [cache.py:138:__init__] Using cache directory: C:\foundry_cache\models\.olive-cache\default_workflow
[... 13:24:56,664] [INFO] [accelerator_creator.py:224:create_accelerators] Running workflow on accelerator specs: gpu-cuda
[... 13:24:56,667] [INFO] [engine.py:246:run] Running Olive on accelerator: gpu-cuda
[... 13:24:56,667] [INFO] [engine.py:888:_create_system] Creating target system ...
[... 13:24:56,668] [INFO] [engine.py:891:_create_system] Target system created in 0.000000 seconds
[... 13:24:56,668] [INFO] [engine.py:902:_create_system] Creating host system ...
[... 13:24:56,668] [INFO] [engine.py:905:_create_system] Host system created in 0.000000 seconds
[... 13:24:56,935] [INFO] [engine.py:709:_run_pass] Running pass model_builder:ModelBuilder {}
[... 13:24:58,063] [ERROR] [engine.py:776:_run_pass] Pass run failed.
Traceback (most recent call last):
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\engine\engine.py", line 764, in _run_pass
    output_model_config = host.run_pass(p, input_model_config, output_model_path, pass_search_point)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\systems\local.py", line 30, in run_pass
    output_model = the_pass.run(model, output_model_path, point)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\passes\olive_pass.py", line 245, in run
    output_model = self._run_for_config(model, config, output_model_path)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\passes\onnx\model_builder.py", line 184, in _run_for_config
    create_model(
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\onnxruntime_genai\models\builder.py", line 3794, in create_model
    raise NotImplementedError(f"The {hf_name} model is not currently supported.")
NotImplementedError: The google/gemma-3n-E2B-it model is not currently supported.
[... 13:24:58,069] [WARNING] [engine.py:334:run_accelerator] Failed to run Olive on gpu-cuda.
Traceback (most recent call last):
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\engine\engine.py", line 330, in run_accelerator
    output_footprint = self.run_no_search(input_model_config, input_model_id, accelerator_spec, output_dir)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\engine\engine.py", line 400, in run_no_search
    should_prune, signal, model_ids = self._run_passes(
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\engine\engine.py", line 664, in _run_passes
    model_config, model_id = self._run_pass(
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\engine\engine.py", line 764, in _run_pass
    output_model_config = host.run_pass(p, input_model_config, output_model_path, pass_search_point)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\systems\local.py", line 30, in run_pass
    output_model = the_pass.run(model, output_model_path, point)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\passes\olive_pass.py", line 245, in run
    output_model = self._run_for_config(model, config, output_model_path)
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\olive\passes\onnx\model_builder.py", line 184, in _run_for_config
    create_model(
  File "C:\Users\testusr\anaconda3\envs\huggingface-build\lib\site-packages\onnxruntime_genai\models\builder.py", line 3794, in create_model
    raise NotImplementedError(f"The {hf_name} model is not currently supported.")
NotImplementedError: The google/gemma-3n-E2B-it model is not currently supported.
[... 13:24:58,070] [INFO] [engine.py:265:run] Run history for gpu-cuda:
[... 13:24:58,070] [INFO] [engine.py:519:dump_run_history] Please install tabulate for better run history output
Command failed. Please set the log_level to 1 for more detailed logs.

아직 지원을 안 하고 있다는 건데요, 반면 (유사하지만 다른 모델인) 예를 들어 gemma-2-2b-it 모델을 입력으로 하면 변환이 잘됩니다.

olive auto-opt --model_name_or_path google/gemma-2-2b-it --output_path ./gemma-2-2b-it --device gpu --provider CUDAExecutionProvider --precision int4 --use_model_builder --log_level 1

공식 문서에 보면,

microsoft/onnxruntime-genai
; https://github.com/microsoft/onnxruntime-genai

* DeepSeek
* Gemma
* Llama *
* Mistral +
* Phi (language + vision)
* Qwen
* Nemotron
* Granite
* AMD OLMo

Gemma 모델 지원이 있긴 하지만, 아직 3n-E2B-it 모델은 지원하지 않는 것 같습니다. 현재 olive 패키지에서 HuggingFace의 특정 모델을 onnx 포맷으로 변환할 수 있는지에 대한 확인은 builder.py 파일을 직접 봐야 합니다. 제 경우에 다음의 경로에 있는데요,

// pip로 설치한 경우
"%USERPROFILE%\anaconda3\envs\huggingface-build\Lib\site-packages\onnxruntime_genai\models\builder.py"

// github 최신 소스 코드
https://github.com/microsoft/onnxruntime-genai/blob/main/src/python/py/models/builder.py

코드를 보면,

...[생략]...

    if "config_only" not in extra_options:
        # List architecture options in alphabetical order
        if config.architectures[0] == "ChatGLMForConditionalGeneration" or config.architectures[0] == "ChatGLMModel":
            # Quantized ChatGLM model has ChatGLMForConditionalGeneration as architecture whereas HF model as the latter
            config.hidden_act = "swiglu"
            onnx_model = ChatGLMModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "GemmaForCausalLM":
            onnx_model = GemmaModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Gemma2ForCausalLM":
            if precision == "fp16":
                print("WARNING: This model loses accuracy with float16 precision. Setting `--precision bf16` by default.")
                precision = "bf16"
            onnx_model = Gemma2Model(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Gemma3ForCausalLM":
            if precision == "fp16":
                print("WARNING: This model loses accuracy with float16 precision. Setting `--precision bf16` by default.")
                precision = "bf16"
            onnx_model = Gemma3Model(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
            onnx_model.model_type = "gemma3_text"
        elif config.architectures[0] == "Gemma3ForConditionalGeneration":
            print("WARNING: This is only generating the text component of the model. Setting `--extra_options exclude_embeds=true` by default.")
            text_config = config.text_config
            for key in text_config:
                if not hasattr(config, key):
                    setattr(config, key, getattr(text_config, key))
            if precision == "fp16":
                print("WARNING: This model loses accuracy with float16 precision. Setting `--precision bf16` by default.")
                precision = "bf16"
            onnx_model = Gemma3Model(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "GraniteForCausalLM":
            onnx_model = GraniteModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "LlamaForCausalLM":
            onnx_model = LlamaModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "MistralForCausalLM":
            onnx_model = MistralModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "NemotronForCausalLM":
            onnx_model = NemotronModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "OlmoForCausalLM":
            onnx_model = OLMoModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "PhiForCausalLM":
            onnx_model = PhiModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi3ForCausalLM" and config.max_position_embeddings == config.original_max_position_embeddings:
            onnx_model = Phi3MiniModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi3ForCausalLM" and config.max_position_embeddings != config.original_max_position_embeddings:
            onnx_model = Phi3MiniLongRoPEModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "PhiMoEForCausalLM" and config.max_position_embeddings != config.original_max_position_embeddings:
            print("WARNING: This model only works for CUDA currently because `MoE` is only supported for CUDA in ONNX Runtime. Setting `--execution_provider cuda` by default.")
            print("WARNING: This model currently only supports the quantized version. Setting `--precision int4` by default.")
            execution_provider = "cuda"
            precision = "int4"
            onnx_model = Phi3MoELongRoPEModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi3SmallForCausalLM" and config.max_position_embeddings == config.original_max_position_embeddings:
            onnx_model = Phi3SmallModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi3SmallForCausalLM" and config.max_position_embeddings != config.original_max_position_embeddings:
            onnx_model = Phi3SmallLongRoPEModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi3VForCausalLM":
            print("WARNING: This is only generating the text component of the model. Setting `--extra_options exclude_embeds=true` by default.")
            extra_options["exclude_embeds"] = True
            onnx_model = Phi3VModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Phi4MMForCausalLM":
            print("WARNING: This is only generating the text component of the model. Setting `--extra_options exclude_embeds=true` by default.")
            extra_options["exclude_embeds"] = True
            onnx_model = Phi4MMModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        elif config.architectures[0] == "Qwen2ForCausalLM":
            onnx_model = QwenModel(config, io_dtype, precision, execution_provider, cache_dir, extra_options)
        else:
            print('model:', hf_name, config.architectures[0])
            raise NotImplementedError(f"The {hf_name} model is not currently supported.")
...[생략]...

여기서 config 변수는 HuggingFace의 모델 페이지에서 공개한 config.json 파일의 내용에 해당합니다. 가령 gemma-3n-E2B-it의 config.json에는,

{
  "architectures": [
    "Gemma3nForConditionalGeneration"
  ],
  ...[생략]...
}

이렇게 architectures[0]의 값이 "Gemma3nForConditionalGeneration"라고 나오는데요, 바로 이 문자열이 builder.py 파일의 if 문에 포함돼 있어야 하는 것입니다.

// gemma-3n-E2B-it의 경우 지원하는 목록에 없는 경우
c:\temp> type "%USERPROFILE%\anaconda3\envs\huggingface-build\Lib\site-packages\onnxruntime_genai\models\builder.py" | findstr "Gemma3nForConditionalGeneration"

// meta-llama/Llama-3.2-1B-Instruct의 경우 지원 목록에 포함한 경우
c:\temp> type "%USERPROFILE%\anaconda3\envs\huggingface-build\Lib\site-packages\onnxruntime_genai\models\builder.py" | findstr "LlamaForCausalLM"
        elif config.architectures[0] == "LlamaForCausalLM":

// 또는, 가끔씩 onnxruntime_genai 패키지가 업데이트되면 github의 소스 코드를 방문해 "Gemma3nForConditionalGeneration" 문자열을 포함하고 있는지 확인해 봐야 합니다. ^^




아쉽지만, 위와 같은 이유로 인해 onnxruntime_genai 패키지로는 gemma-3n-E2B-it를 사용할 수 없고 (README.md 파일에서 설명하듯이) pipeline을 직접 다루는 것으로 우회할 수 있습니다.

import datetime


def log(message):
    print(f"{datetime.datetime.now().strftime('%X')} {message}")


log('import pipeline')

from transformers import pipeline
import torch

log('loading...')

pipe = pipeline(
    "image-text-to-text",
    model="google/gemma-3n-e2b-it",
    device="cuda",
    torch_dtype=torch.bfloat16,
)

log('loaded.')

messages = [
   {
       "role": "user",
       "content": [
           {"type": "image", "url": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/airplane.jpg"},
           {"type": "text", "text": "Describe this image"}
       ]
   }
]

output = pipe(text=messages, max_new_tokens=32)

log('chat-completed.')

log(output[0]["generated_text"][-1]["content"])

Windows 환경이라면 WSL을 이용해 다음과 같이 실행할 수 있는데,

// 미리 윈도우 환경에서 다운로드한 모델 파일을 재사용하기 위해 HF_HOME 환경 변수를 설정했습니다.

$ export HF_HOME=/mnt/e/python310/cache/huggingface
$ cd /mnt/c/foundry_cache/models
$ python test.py
10:24:48 import pipeline
10:25:15 loading...
...[생략]...
Device set to use cuda
10:32:35 loaded.
...[생략]...
10:34:45 chat-completed.
10:34:45 The image shows a futuristic, sleek airplane soaring through the sky. It has a very streamlined, almost alien design with a long, narrow body, swept-back

WSL과 Windows 시스템 간의 전송 오버헤드가 있다 보니 모델 로딩에 시간이 꽤 걸리는 것을 볼 수 있습니다. (패키지를 import하는 것만 해도 27초가 걸렸고 모델 로딩에는 약 7분이 넘게 걸렸습니다.)

어쨌든 Gemma 3n 모델을 성공적으로 로딩했고, 이미지에 대한 설명이 적절하게 나오는 것을 볼 수 있습니다.




그나저나, 혹시나 싶어서 OCR 기능도 되지 않을까... 하는 바람으로 다음과 같이 이미지 경로를 바꿔서 질의해 보았는데요,

messages = [
   {
       "role": "user",
       "content": [
           {"type": "image", "url": "https://sysnetblobaccount.blob.core.windows.net/sysnetimages/hf_request_model_access_1.png"},
           {"type": "text", "text": "extract all texts from this image"}
       ]
   }
]

결과가 이렇게 나옵니다. ^^;

13:52:48 Here's the text extracted from the image:

**Top Left:**

* .B- 3- 3M- .S-S-

이 정도면, 이미지 서술과 OCR 기능은 완전 별개임을 짐작게 합니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/7/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 16  17  [18]  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13536정성태1/23/202411905닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/202413128닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
13534정성태1/21/202411419닷넷: 2207. C# - SQL Server DB를 bacpac으로 Export/Import파일 다운로드1
13533정성태1/18/202411471닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/202411714닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/202412048닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/202411625닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/202411481닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/202412154Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/202412475오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/202412802닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/202411481오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/202410960오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/202412063오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/202412936닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/202411533닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/202411536오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....' [1]
13519정성태1/10/202410856닷넷: 2198. C# - Reflection을 이용한 ClientWebSocket의 Ping 호출파일 다운로드1
13518정성태1/9/202412129닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/202410665스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/202411833닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
13515정성태1/6/202410818닷넷: 2195. async 메서드 내에서 C# 7의 discard 구문 활용 사례 [1]
13514정성태1/5/202410797개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/202411720닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/202412350개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
13511정성태1/4/202411827닷넷: 2193. C# - ASP.NET Web Application + OpenAPI(Swashbuckle) 스펙 제공
... 16  17  [18]  19  20  21  22  23  24  25  26  27  28  29  30  ...