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)
1063정성태6/10/201131046웹: 21. Sysnet 웹 사이트의 CSS 2.1 변환 기록 [1]
1062정성태6/9/201131340웹: 20. Sysnet 웹 사이트의 HTML5 변환 기록 [1]
1061정성태6/8/201129305오류 유형: 125. 인터넷 익스플로러 - 개발자 도구에서 정지점(BP: Breakpoint) 설정이 안 되는 경우 [1]
1060정성태6/8/201125986VC++: 51. PHP 모듈의 F5 디버깅
1059정성태6/6/201131262VC++: 50. PHP 모듈 - php_mysql 빌드하는 방법파일 다운로드1
1058정성태6/5/201134766개발 환경 구성: 124. .NET 개발자가 처음 해보는 PHP + MySQL 연동 [2]
1057정성태6/4/201132207VC++: 49. 소스 코드로부터 php5apache2_2.dll 생성하는 방법파일 다운로드1
1056정성태6/2/201130441VC++: 48. 윈도우에서 Apache Module - Content Handler 컴파일파일 다운로드1
1055정성태6/1/201127720오류 유형: 124. MVC 프로젝트의 Site.Master 관련 오류 정리
1054정성태5/31/201131672.NET Framework: 220. ASP.NET MVC Web Site 프로젝트 - 단위 테스트 작성파일 다운로드1
1053정성태5/31/201134333VC++: 47. Apache Module에 대한 'F5 디버그 (Start with debugging)' [2]
1052정성태5/30/201131877.NET Framework: 219. ASP.NET MVC Web Site 프로젝트 구성하기파일 다운로드1
1051정성태5/28/201140429VC++: 46. 윈도우에서 Apache Module 컴파일 (VC++)파일 다운로드1
1050정성태5/28/201126621오류 유형: 123. Firebird - Exception of type 'FirebirdSql.Data.Common.IscException' was thrown.
1049정성태5/28/201132307.NET Framework: 218. WCF REST 서비스 - 웹 브라우저 측 Ajax 호출 캐시 [1]
1048정성태5/27/201133822개발 환경 구성: 123. Apache 소스를 윈도우 환경에서 빌드하기
1047정성태5/27/201128084.NET Framework: 217. Firebird ALinq Provider - 날짜 필드에 대한 낙관적 동시성 쿼리 오류
1046정성태5/26/201132764.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [5]
1045정성태5/24/201133820.NET Framework: 215. 닷넷 System.ComponentModel.LicenseManager를 이용한 라이선스 적용 [1]파일 다운로드1
1044정성태5/24/201134434오류 유형: 122. zlib 빌드 오류 - inflate.obj : error LNK2001: unresolved external symbol _inflate_fast
1043정성태5/24/201133812.NET Framework: 214. 무료 Linq Provider - DbLinq를 이용한 Firebird 접근파일 다운로드1
1042정성태5/23/201139687개발 환경 구성: 122. PHP 소스를 윈도우 환경에서 빌드하기
1041정성태5/22/201130549.NET Framework: 213. Linq To SQL - ALinq Provider를 이용하여 Firebird 사용파일 다운로드1
1040정성태5/21/201141061개발 환경 구성: 121. .NET 개발자가 처음 설치해 본 Apache + PHP [2]
1039정성태5/17/201133834.NET Framework: 212. Firebird 데이터베이스와 ADO.NET [2]파일 다운로드1
1038정성태5/16/201135676개발 환경 구성: 120. .NET 프로그래머에게도 유용한 Firebird 무료 데이터베이스 [2]
... 151  152  153  154  155  156  157  158  159  160  [161]  162  163  164  165  ...