-
Notifications
You must be signed in to change notification settings - Fork 302
SNOW-2367850: task integration example update #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0291c04
update the task SDK
sfc-gh-ajiang 375d3ab
revert changes
sfc-gh-ajiang 703488f
resolve the comments
sfc-gh-ajiang f96fd9b
revert unnecessary changes
sfc-gh-ajiang ad5d13b
revert unnecessary changes
sfc-gh-ajiang fd6a7dc
resolve the comments
sfc-gh-ajiang 5524f9a
resolve the comments
sfc-gh-ajiang 3015500
resolve the comments
sfc-gh-ajiang 94e941a
resolve the comments
sfc-gh-ajiang 74a1edb
resolve the comments
sfc-gh-ajiang 7f992c4
reformat the script
sfc-gh-ajiang 01c160f
update the sample
sfc-gh-ajiang 9fb7478
update the sample
sfc-gh-ajiang 071eb1c
update the sample
sfc-gh-ajiang f6ab75c
update the sample
sfc-gh-ajiang 16b0b42
update the samples
sfc-gh-ajiang 9fe2b7a
update the samples
sfc-gh-ajiang 6b7c373
add more information for ML Job Definition
sfc-gh-ajiang cf1e70f
remove session creation at module level
sfc-gh-ajiang 1b5f9b1
resolve the comments
sfc-gh-ajiang d6a9bd0
update the image
sfc-gh-ajiang 591d89a
resolve the comments
sfc-gh-ajiang 2412da7
add the latest screenshots
sfc-gh-ajiang cd46e72
resolve the comments
sfc-gh-ajiang ca1e332
add a screenshot for the job link
sfc-gh-ajiang 0735559
resolve the comments
sfc-gh-ajiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| from snowflake.ml.data import DataConnector, DatasetInfo, DataSource | ||
| from snowflake.core.task.context import TaskContext | ||
| from snowflake.snowpark import Session | ||
| from xgboost import XGBClassifier | ||
| import os | ||
| import json | ||
| import cloudpickle as cp | ||
| import io | ||
| from pipeline_dag import RunConfig | ||
| from modeling import evaluate_model | ||
|
|
||
| session = Session.builder.getOrCreate() | ||
sfc-gh-ajiang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def train_model(session: Session, input_data: DataSource) -> XGBClassifier: | ||
sfc-gh-ajiang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Train a model on the training dataset and evaluate it on the test dataset. | ||
|
|
||
| This function trains an XGBoost classifier on the provided training data. It extracts | ||
| features and labels from the input data, configures the model with predefined parameters, | ||
| and trains the model. This function is executed remotely on Snowpark Container Services. | ||
|
|
||
| Args: | ||
| session (Session): Snowflake session object | ||
| input_data (DataSource): Data source containing training data with features and labels | ||
|
|
||
| Returns: | ||
| XGBClassifier: Trained XGBoost classifier model | ||
| """ | ||
| input_data_df = DataConnector.from_sources(session, [input_data]).to_pandas() | ||
|
|
||
| assert isinstance(input_data, DatasetInfo), "Input data must be a DatasetInfo" | ||
| exclude_cols = input_data.exclude_cols | ||
| label_col = exclude_cols[0] | ||
|
|
||
| X_train = input_data_df.drop(exclude_cols, axis=1) | ||
| y_train = input_data_df[label_col].squeeze() | ||
|
|
||
| model_params = dict( | ||
| max_depth=50, | ||
| n_estimators=3, | ||
| learning_rate=0.75, | ||
| objective="binary:logistic", | ||
| booster="gbtree", | ||
| ) | ||
|
|
||
| # Retrieve the number of nodes from environment variable | ||
| if int(os.environ.get("SNOWFLAKE_JOBS_COUNT", 1)) > 1: | ||
| # Distributed training - use ML Runtime distributor APIs | ||
| from snowflake.ml.modeling.distributors.xgboost.xgboost_estimator import ( | ||
| XGBEstimator, | ||
| XGBScalingConfig, | ||
| ) | ||
| estimator = XGBEstimator( | ||
| params=model_params, | ||
| scaling_config=XGBScalingConfig(num_workers=2), | ||
| ) | ||
| else: | ||
| # Single node training - can use standard XGBClassifier | ||
| estimator = XGBClassifier(**model_params) | ||
|
|
||
| estimator.fit(X_train, y_train) | ||
|
|
||
| # Convert distributed estimator to standard XGBClassifier if needed | ||
| return getattr(estimator, '_sklearn_estimator', estimator) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| ctx = TaskContext(session) | ||
| config = RunConfig.from_task_context(ctx) | ||
|
|
||
| # Load the datasets | ||
| serialized = json.loads(ctx.get_predecessor_return_value("PREPARE_DATA")) | ||
| dataset_info = { | ||
| key: DatasetInfo(**obj_dict) for key, obj_dict in serialized.items() | ||
| } | ||
| artifact_dir = config.artifact_dir | ||
| model_obj = train_model(session, dataset_info["train"]) | ||
| train_metrics = evaluate_model( | ||
| session, model_obj, dataset_info["train"], prefix="train" | ||
| ) | ||
| test_metrics = evaluate_model( | ||
| session, model_obj, dataset_info["test"], prefix="test" | ||
| ) | ||
| metrics = {**train_metrics, **test_metrics} | ||
|
|
||
| model_pkl = cp.dumps(model_obj) | ||
| model_path = os.path.join(config.artifact_dir, "model.pkl") | ||
| put_result = session.file.put_stream( | ||
| io.BytesIO(model_pkl), model_path, overwrite=True | ||
| ) | ||
| result_dict = { | ||
| "model_path": os.path.join(config.artifact_dir, put_result.target), | ||
| "metrics": metrics, | ||
| } | ||
| ctx.set_return_value(json.dumps(result_dict)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.