Я получаю сообщение об ошибке «RuntimeError: BlobWriter не загружен» при экспорте модели PyTorch в CoreML. Как это исправить?
Та же проблема с Python 3.11 и Python 3.10. Та же проблема с факелом 2.3.1 и 2.2.0. Протестировано в Windows 10.
Скрипт экспорта:
# -*- coding: utf-8 -*-
"""Core ML Export
pip install transformers torch coremltools nltk
"""
import os
from transformers import AutoModelForTokenClassification, AutoTokenizer
import torch
import torch.nn as nn
import nltk
import coremltools as ct
nltk.download('punkt')
# Load the model and tokenizer
model_path = os.path.join('model')
model = AutoModelForTokenClassification.from_pretrained(model_path, local_files_only=True)
tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
# Modify the model's forward method to return a tuple
class ModifiedModel(nn.Module):
def __init__(self, model):
super(ModifiedModel, self).__init__()
self.model = model
self.device = model.device # Add the device attribute
def forward(self, input_ids, attention_mask, token_type_ids=None):
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
return outputs.logits
modified_model = ModifiedModel(model)
# Export to Core ML
def convert_to_coreml(model, tokenizer):
# Define a dummy input for tracing
dummy_input = tokenizer("A French fan", return_tensors="pt")
dummy_input = {k: v.to(model.device) for k, v in dummy_input.items()}
# Trace the model with the dummy input
traced_model = torch.jit.trace(model, (
dummy_input['input_ids'], dummy_input['attention_mask'], dummy_input.get('token_type_ids')))
# Convert to Core ML
inputs = [
ct.TensorType(name="input_ids", shape=dummy_input['input_ids'].shape),
ct.TensorType(name="attention_mask", shape=dummy_input['attention_mask'].shape)
]
if 'token_type_ids' in dummy_input:
inputs.append(ct.TensorType(name="token_type_ids", shape=dummy_input['token_type_ids'].shape))
mlmodel = ct.convert(traced_model, inputs=inputs)
# Save the Core ML model
mlmodel.save("model.mlmodel")
print("Model exported to Core ML successfully")
convert_to_coreml(modified_model, tokenizer)
Стек ошибок:
C:\Users\dernoncourt\anaconda3\envs\coreml\python.exe C:\Users\dernoncourt\PycharmProjects\coding\export_model_to_coreml6_fopr_SE_q.py
Failed to load _MLModelProxy: No module named 'coremltools.libcoremlpython'
Fail to import BlobReader from libmilstoragepython. No module named 'coremltools.libmilstoragepython'
Fail to import BlobWriter from libmilstoragepython. No module named 'coremltools.libmilstoragepython'
[nltk_data] Downloading package punkt to
[nltk_data] C:\Users\dernoncourt\AppData\Roaming\nltk_data...
[nltk_data] Package punkt is already up-to-date!
C:\Users\dernoncourt\anaconda3\envs\coreml\lib\site-packages\transformers\modeling_utils.py:4565: FutureWarning: `_is_quantized_training_enabled` is going to be deprecated in transformers 4.39.0. Please use `model.hf_quantizer.is_trainable` instead
warnings.warn(
When both 'convert_to' and 'minimum_deployment_target' not specified, 'convert_to' is set to "mlprogram" and 'minimum_deployment_target' is set to ct.target.iOS15 (which is same as ct.target.macOS12). Note: the model will not run on systems older than iOS15/macOS12/watchOS8/tvOS15. In order to make your model run on older system, please set the 'minimum_deployment_target' to iOS14/iOS13. Details please see the link: https://apple.github.io/coremltools/doc ... rmats.html
Model is not in eval mode. Consider calling '.eval()' on your model prior to conversion
Converting PyTorch Frontend ==> MIL Ops: 0%| | 0/127 [00:00
Подробнее здесь: https://stackoverflow.com/questions/786 ... odel-to-co
Ошибка «RuntimeError: BlobWriter не загружен» при экспорте модели PyTorch в CoreML. Как это исправить? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение