ワークフローの例¶
このページでは、Snowpark Container Services( SPCS )を使用してリアルタイム推論の機械学習モデルを展開するためのワークフローの例を紹介します。それぞれの例は、モデルの登録から展開、推論までの完全なライフサイクルを示しています。
これには次が含まれます。
HTTP エンドポイントを介してサービスを作成し、予測を行い、モデルにアクセスする方法。
異なるモデルアーキテクチャ( XGBoost 、Hugging Face transformers、 PyTorch )およびコンピューティングオプション( CPU および GPU )の使用方法。
CPU-powered推論のための XGBoost モデルの展開¶
次のコードは、次を実行します。
SPCS で推論の XGBoost モデルを展開します。
推論の展開されたモデルを使用します。
from snowflake.ml.registry import registry
from snowflake.ml.utils.connection_params import SnowflakeLoginOptions
from snowflake.snowpark import Session
from xgboost import XGBRegressor
# your model training code here output of which is a trained xgb_model
# Open model registry
reg = registry.Registry(session=session, database_name='my_registry_db', schema_name='my_registry_schema')
# Log the model in Snowflake Model Registry
model_ref = reg.log_model(
model_name="my_xgb_forecasting_model",
version_name="v1",
model=xgb_model,
conda_dependencies=["scikit-learn","xgboost"],
sample_input_data=pandas_test_df,
comment="XGBoost model for forecasting customer demand"
)
# Deploy the model to SPCS
model_ref.create_service(
service_name="forecast_model_service",
service_compute_pool="my_cpu_pool",
ingress_enabled=True)
# See all services running a model
model_ref.list_services()
# Run on SPCS
model_ref.run(pandas_test_df, function_name="predict", service_name="forecast_model_service")
# Delete the service
model_ref.delete_service("forecast_model_service")
HTTP 経由での呼び出し(外部アプリケーション)¶
このモデルはイングレスを有効にしているので( ingress_enabled=True )、パブリック HTTP エンドポイントを呼び出すことができます。次の例では、環境変数 PAT_TOKEN に格納されている PAT を使用してパブリックSnowflakeエンドポイントで認証します。
import os
import json
import numpy as np
from pprint import pprint
import requests
def get_headers(pat_token):
headers = {'Authorization': f'Snowflake Token="{pat_token}"'}
return headers
headers = get_headers(os.getenv("PAT_TOKEN"))
# Put the endpoint url with method name `predict`
# The endpoint url can be found with `show endpoints in service <service_name>`.
URL = 'https://<random_str>-<organization>-<account>.snowflakecomputing.app/predict'
# Prepare data to be sent
data = {"data": np.column_stack([range(pandas_test_df.shape[0]), pandas_test_df.values]).tolist()}
# Send over HTTP
def send_request(data: dict):
output = requests.post(URL, json=data, headers=headers)
assert (output.status_code == 200), f"Failed to get response from the service. Status code: {output.status_code}"
return output.content
# Test
results = send_request(data=data)
print(json.loads(results))
GPU-powered推論のためのHugging Face文型変換器の展開¶
次のコードは、 HTTP エンドポイントを含むHugging Face文型変換器をトレーニングおよび展開します。
この例では、 sentence-transformers パッケージ、 GPU コンピューティングプールとイメージリポジトリが必要です。
from snowflake.ml.registry import registry
from snowflake.ml.utils.connection_params import SnowflakeLoginOptions
from snowflake.snowpark import Session
from sentence_transformers import SentenceTransformer
session = Session.builder.configs(SnowflakeLoginOptions("connection_name")).create()
reg = registry.Registry(session=session, database_name='my_registry_db', schema_name='my_registry_schema')
# Take an example sentence transformer from HF
embed_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
# Have some sample input data
input_data = [
"This is the first sentence.",
"Here's another sentence for testing.",
"The quick brown fox jumps over the lazy dog.",
"I love coding and programming.",
"Machine learning is an exciting field.",
"Python is a popular programming language.",
"I enjoy working with data.",
"Deep learning models are powerful.",
"Natural language processing is fascinating.",
"I want to improve my NLP skills.",
]
# Log the model with pip dependencies
pip_model = reg.log_model(
embed_model,
model_name="sentence_transformer_minilm",
version_name="pip",
sample_input_data=input_data, # Needed for determining signature of the model
pip_requirements=["sentence-transformers", "torch", "transformers"], # If you want to run this model in the Warehouse, you can use conda_dependencies instead
)
# Force Snowflake to not try to check warehouse
conda_forge_model = reg.log_model(
embed_model,
model_name="sentence_transformer_minilm",
version_name="conda_forge_force",
sample_input_data=input_data,
# setting any package from conda-forge is sufficient to know that it can't be run in warehouse
conda_dependencies=["sentence-transformers", "conda-forge::pytorch", "transformers"]
)
# Deploy the model to SPCS
pip_model.create_service(
service_name="my_minilm_service",
service_compute_pool="my_gpu_pool", # Using GPU_NV_S - smallest GPU node that can run the model
ingress_enabled=True,
gpu_requests="1", # Model fits in GPU memory; only needed for GPU pool
max_instances=4, # 4 instances were able to run 10M inferences from an XS warehouse
)
# See all services running a model
pip_model.list_services()
# Run on SPCS
pip_model.run(input_data, function_name="encode", service_name="my_minilm_service")
# Delete the service
pip_model.delete_service("my_minilm_service")
SQL では、次のようにサービス関数を呼び出すことができます:
SELECT my_minilm_service!encode('This is a test sentence.');
同様に、 HTTP エンドポイントを次のように呼び出すことができます。
import json
from pprint import pprint
import requests
# Put the endpoint url with method name `encode`
URL='https://<random_str>-<account>.snowflakecomputing.app/encode'
# Prepare data to be sent
data = {
'data': []
}
for idx, x in enumerate(input_data):
data['data'].append([idx, x])
# Send over HTTP
def send_request(data: dict):
output = requests.post(URL, json=data, headers=headers)
assert (output.status_code == 200), f"Failed to get response from the service. Status code: {output.status_code}"
return output.content
# Test
results = send_request(data=data)
pprint(json.loads(results))
GPU-powered推論のための PyTorch モデルの展開¶
PyTorch ディープラーニング推奨モデル( DLRM )を GPU 推論のために SPCS にトレーニングしてデプロイする例については、この クイックスタート をご参照ください。
Snowpark ML モデリングモデルのデプロイ¶
Snowpark ML モデリングクラスを使用して開発されたモデルは、 GPU を持つ環境にはデプロイできません。回避策として、ネイティブモデルを抽出して、それをデプロイすることができます。例:
# Train a model using Snowpark ML
from snowflake.ml.modeling.xgboost import XGBRegressor
regressor = XGBRegressor(...)
regressor.fit(training_df)
# Extract the native model
xgb_model = regressor.to_xgboost()
# Test the model with pandas dataframe
pandas_test_df = test_df.select(['FEATURE1', 'FEATURE2', ...]).to_pandas()
xgb_model.predict(pandas_test_df)
# Log the model in Snowflake Model Registry
mv = reg.log_model(xgb_model,
model_name="my_native_xgb_model",
sample_input_data=pandas_test_df,
comment = 'A native XGB model trained from Snowflake Modeling API',
)
# Now we should be able to deploy to a GPU compute pool on SPCS
mv.create_service(
service_name="my_service_gpu",
service_compute_pool="my_gpu_pool",
image_repo="my_repo",
max_instances=1,
gpu_requests="1",
)