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

비밀번호

댓글 작성자
 




... 151  [152]  153  154  155  156  157  158  159  160  161  162  163  164  165  ...
NoWriterDateCnt.TitleFile(s)
1292정성태5/24/201224880.NET Framework: 324. First-chance exception에 대해 조건에 따라 디버거가 멈추게 할 수는 없을까? [1]파일 다운로드1
1291정성태5/23/201232050VC++: 62. 배열 초기화를 위한 기계어 코드 확인 [2]
1290정성태5/18/201236953.NET Framework: 323. 관리자 권한이 필요한 작업을 COM+에 대행 [7]파일 다운로드1
1289정성태5/17/201241345.NET Framework: 322. regsvcs.exe로 어셈블리 등록 시 시스템 변경 사항 [5]파일 다운로드2
1288정성태5/17/201228264.NET Framework: 321. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (3) - Type Library파일 다운로드1
1287정성태5/17/201230975.NET Framework: 320. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (2) - .NET 4.0 + .NET 2.0 [2]
1286정성태5/17/201239849.NET Framework: 319. regasm.exe로 어셈블리 등록 시 시스템 변경 사항 (1) - .NET 2.0 + x86/x64/AnyCPU [5]
1285정성태5/16/201235030.NET Framework: 318. gacutil.exe로 어셈블리 등록 시 시스템 변경 사항파일 다운로드1
1284정성태5/15/201227434오류 유형: 155. Windows Phone 연결 상태에서 DRIVER POWER STATE FAILURE 블루 스크린 뜨는 현상
1283정성태5/12/201235071.NET Framework: 317. C# 관점에서의 Observer 패턴 구현 [1]파일 다운로드1
1282정성태5/12/201227857Phone: 6. Windows Phone 7 Silverlight에서 Google Map 사용하는 방법 [3]파일 다운로드1
1281정성태5/9/201234887.NET Framework: 316. WPF/Silverlight의 그래픽 단위와 Anti-aliasing 처리를 이해하자 [1]파일 다운로드1
1280정성태5/9/201228194오류 유형: 154. Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...'.
1279정성태5/9/201226841.NET Framework: 315. 해당 DLL이 Managed인지 / Unmanaged인지 확인하는 방법 [1]파일 다운로드1
1278정성태5/8/201228297오류 유형: 153. Visual Studio 디버깅 - Unable to break execution. This process is not currently executing the type of code that you selected to debug.
1277정성태5/8/201233038오류 유형: 152. cmd.exe - The system cannot write to the specified device. [2]
1276정성태4/28/201225086Phone: 5. 모든 Marketplace에 윈폰 앱을 등록하는 방법 [1]
1275정성태4/28/201228598개발 환경 구성: 150. 프로세스 실행으로 잠긴 파일이지만, 이름은 변경가능하다는 사실! 아셨나요? [7]
1274정성태4/17/201223334Phone: 4. "Holiday Calendar" 윈폰 응용 프로그램 등록
1273정성태4/6/201226865Phone: 3. 윈도우 폰을 위한 Holiyday Calendar 앱 개발파일 다운로드1
1272정성태4/5/201227663오류 유형: 151. ASP.NET - EcbGetUnicodeServerVariables 코드에서 System.AccessViolationException 예외 발생
1271정성태4/3/201230099Math: 6. 동전을 여러 더미로 나누는 경우의 수 세기 [1]
1270정성태3/29/201224913오류 유형: 150. Visual Studio 2010 원격 디버깅 오류 - Kerberos authentication failed
1269정성태3/27/201238844오류 유형: 149. ODP.NET 오류 - The provider is not compatible with the version of Oracle client
1268정성태3/27/201234947오류 유형: 148. WCF svc 호출 시 HTTP Error 404.17 - Not Found [1]
1267정성태3/16/201233319.NET Framework: 314. C++의 inline asm 사용을 .NET으로 포팅하는 방법 [1]파일 다운로드1
... 151  [152]  153  154  155  156  157  158  159  160  161  162  163  164  165  ...