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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13661정성태7/3/20248600Linux: 72. g++ - 다른 버전의 GLIBC로 소스코드 빌드
13660정성태7/3/20249636오류 유형: 912. Visual C++ - Linux 프로젝트 빌드 오류
13659정성태7/1/20249394개발 환경 구성: 715. Windows - WSL 2 환경의 Docker Desktop 네트워크
13658정성태6/28/202410348개발 환경 구성: 714. WSL 2 인스턴스와 호스트 측의 Hyper-V에 운영 중인 VM과 네트워크 연결을 하는 방법 - 두 번째 이야기
13657정성태6/27/20249151닷넷: 2270. C# - Hyper-V Socket 통신(AF_HYPERV, AF_VSOCK)을 위한 EndPoint 사용자 정의
13656정성태6/27/202410119Windows: 264. WSL 2 VM의 swap 파일 위치
13655정성태6/24/20249552닷넷: 2269. C# - Win32 Resource 포맷 해석파일 다운로드1
13654정성태6/24/20249196오류 유형: 911. shutdown - The entered computer name is not valid or remote shutdown is not supported on the target computer.
13653정성태6/22/20249342닷넷: 2268. C# 코드에서 MAKEINTREOURCE 매크로 처리
13652정성태6/21/202411255닷넷: 2267. C# - Linux 환경에서 (Reflection 없이) DLL AssemblyFileVersion 구하는 방법파일 다운로드2
13651정성태6/19/202410594닷넷: 2266. C# - (Reflection 없이) DLL AssemblyFileVersion 구하는 방법파일 다운로드1
13650정성태6/18/202410744개발 환경 구성: 713. "WSL --debug-shell"로 살펴보는 WSL 2 VM의 리눅스 환경
13649정성태6/18/20249480오류 유형: 910. windbg - !py 확장 명령어 실행 시 "failed to find python interpreter" (2)
13648정성태6/17/202410586오류 유형: 909. C# - DynamicMethod 사용 시 System.TypeAccessException
13647정성태6/16/202411829개발 환경 구성: 712. Windows - WSL 2의 네트워크 통신 방법 - 세 번째 이야기 (같은 IP를 공유하는 WSL 2 인스턴스) [1]
13646정성태6/14/20249831오류 유형: 908. Process Explorer - "Error configuring dump resources: The system cannot find the file specified."
13645정성태6/13/20249692개발 환경 구성: 711. Visual Studio로 개발 시 기본 등록하는 dev tag 이미지로 Docker Desktop k8s에서 실행하는 방법
13644정성태6/12/202410924닷넷: 2265. C# - System.Text.Json의 기본적인 (한글 등에서의) escape 처리 [1]
13643정성태6/12/20249798오류 유형: 907. MySqlConnector 사용 시 System.IO.FileLoadException 오류
13642정성태6/11/20249484스크립트: 65. 파이썬 - asgi 버전(2, 3)에 따라 달라지는 uvicorn 호스팅
13641정성태6/11/202410541Linux: 71. Ubuntu 20.04를 22.04로 업데이트
13640정성태6/10/202411069Phone: 21. C# MAUI - Android 환경에서의 파일 다운로드(DownloadManager)
13639정성태6/8/202410415오류 유형: 906. C# MAUI - Android Emulator에서 "Waiting For Debugger"로 무한 대기
13638정성태6/8/202410578오류 유형: 905. C# MAUI - 추가한 layout XML 파일이 Resource.Layout 멤버로 나오지 않는 문제
13637정성태6/6/20249661Phone: 20. C# MAUI - 유튜브 동영상을 MediaElement로 재생하는 방법
13636정성태5/30/20249255닷넷: 2264. C# - 형식 인자로 인터페이스를 갖는 제네릭 타입으로의 형변환파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...