我有以下代碼來部署我的模型:
model = PyTorchModel(
entry_point='inference.py',
source_dir='code',
role=role,
model_data=model_data,
framework_version="1.12.0",
py_version='py38',
code_location='s3://staging',
name='Staging-Model'
)
instance_type = 'ml.g4dn.xlarge'
predictor = model.deploy(
initial_instance_count=1,
instance_type=instance_type,
serializer=JSONSerializer(),
deserializer=JSONDeserializer(),
)
在我的推理代碼中,我有:
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
logger.info('Model will be loaded into:{}'.format(DEVICE))
記錄器說它正在將模型加載到 cpu 中,并且實體有可用的 GPU。如何將我的模型加載到 cuda 中?
uj5u.com熱心網友回復:
正如評論中所確定的,模型運行的實體是基于 CPU 的。
發生這種情況是因為在部署模型時,它已經假定模型是使用精確配置創建的。
我們可以嘗試像這樣使模型的容器顯式:
import sagemaker
from sagemaker.model import Model
# this retrieves 'pytorch-inference:1.12.0-gpu-py38'
inf_img_uri = sagemaker.image_uris.retrieve(
framework='pytorch',
region=region,
image_scope='inference',
version="1.12.0",
instance_type='ml.g4dn.xlarge',
py_version='py38'
)
pytorch_model = Model(
image_uri=inf_img_uri,
model_data=model_data,
role=role,
entry_point='inference.py',
source_dir='code',
code_location='s3://staging',
name='Staging-Model'
)
如果您在管道中執行此操作,則可能需要在部署之前執行模型創建步驟。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533073.html
下一篇:如何在swift中將結構模型中的資料從第二個ViewController附加到第一個ViewController?并重新加載firstVC上存在的tableView?
