You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2023/01/17 19:08:15 UTC
[GitHub] [spark] leewyang opened a new pull request, #39628: [SPARK-40264][ML] followup pydoc edits
leewyang opened a new pull request, #39628:
URL: https://github.com/apache/spark/pull/39628
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
Followup edits to pydoc from #37734 per [request](https://github.com/apache/spark/pull/37734#issuecomment-1384092385)
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Adds examples of multiple output models (using StructType return_type) for:
- returning multiple fields of array type
- returning a list of dict for row-oriented
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
Yes, updated pydocs for `predict_batch_udf` function.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
Documentation only.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a diff in pull request #39628: [SPARK-40264][ML] followup pydoc edits
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on code in PR #39628:
URL: https://github.com/apache/spark/pull/39628#discussion_r1073022795
##########
python/pyspark/ml/functions.py:
##########
@@ -486,6 +486,7 @@ def predict(inputs: np.ndarray) -> np.ndarray:
data = np.arange(0, 1000, dtype=np.float64).reshape(-1, 4)
Review Comment:
Can we follow https://numpydoc.readthedocs.io/en/latest/format.html and add the examples as a proper doctest?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] leewyang commented on pull request #39628: [SPARK-40264][ML] followup pydoc edits
Posted by GitBox <gi...@apache.org>.
leewyang commented on PR #39628:
URL: https://github.com/apache/spark/pull/39628#issuecomment-1386053573
@WeichenXu123 Here's the followup PR w/ additional examples in the pydoc.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] AmplabJenkins commented on pull request #39628: [SPARK-40264][ML][DOCS] Supplement docstring in pyspark.ml.functions.predict_batch_udf
Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on PR #39628:
URL: https://github.com/apache/spark/pull/39628#issuecomment-1398038997
Can one of the admins verify this patch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a diff in pull request #39628: [SPARK-40264][ML] followup pydoc edits
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on code in PR #39628:
URL: https://github.com/apache/spark/pull/39628#discussion_r1073023166
##########
python/pyspark/ml/functions.py:
##########
@@ -486,6 +486,7 @@ def predict(inputs: np.ndarray) -> np.ndarray:
data = np.arange(0, 1000, dtype=np.float64).reshape(-1, 4)
Review Comment:
`predict_batch_udf` should also be added to https://github.com/apache/spark/blob/master/python/docs/source/reference/pyspark.ml.rst#functions
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on pull request #39628: [SPARK-40264][ML][DOCS] Supplement docstring in pyspark.ml.functions.predict_batch_udf
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #39628:
URL: https://github.com/apache/spark/pull/39628#issuecomment-1399197002
Merged to master.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a diff in pull request #39628: [SPARK-40264][ML] followup pydoc edits
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on code in PR #39628:
URL: https://github.com/apache/spark/pull/39628#discussion_r1073041810
##########
python/pyspark/ml/functions.py:
##########
@@ -637,6 +641,62 @@ def predict_columnar(x1: np.ndarray, x2: np.ndarray) -> Mapping[str, np.ndarray]
# |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|54.0|39.0|
# |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|70.0|51.0|
# +--------------------+------------------+----+----+
+
+ def make_multi_sum_fn():
+ def predict_row(x1: np.ndarray, x2: np.ndarray) -> list[Mapping[str, float]]:
+ # x1.shape = [batch_size, 4]
+ # x2.shape = [batch_size, 3]
+ return [{'sum1': np.sum(x1[i]), 'sum2': np.sum(x2[i])} for i in range(len(x1))]
+ return predict_row
+
+ multi_sum_udf = predict_batch_udf(
+ make_multi_sum_fn,
+ return_type=StructType([
+ StructField("sum1", FloatType(), True),
+ StructField("sum2", FloatType(), True)
+ ]),
+ batch_size=5,
+ input_tensor_shapes=[[4], [3]],
+ )
+
+ df.withColumn("sum", multi_sum_udf("t1", "t2")).select("t1", "t2", "sum.*").show(5)
+ # +--------------------+------------------+----+----+
+ # | t1| t2|sum1|sum2|
+ # +--------------------+------------------+----+----+
+ # |[0.0, 1.0, 2.0, 3.0]| [0.0, 1.0, 2.0]| 6.0| 3.0|
+ # |[4.0, 5.0, 6.0, 7.0]| [4.0, 5.0, 6.0]|22.0|15.0|
+ # |[8.0, 9.0, 10.0, ...| [8.0, 9.0, 10.0]|38.0|27.0|
+ # |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|54.0|39.0|
+ # |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|70.0|51.0|
+ # +--------------------+------------------+----+----+
+
+ def make_multi_times_two_fn():
+ def predict(x1: np.ndarray, x2: np.ndarray) -> Mapping[str, np.ndarray]:
+ # x1.shape = [batch_size, 4]
+ # x2.shape = [batch_size, 3]
+ return {"t1x2": x1 * 2, "t2x2": x2 * 2}
+ return predict
+
+ multi_times_two_udf = predict_batch_udf(
+ make_multi_times_two_fn,
+ return_type=StructType([
+ StructField("t1x2", ArrayType(FloatType()), True),
+ StructField("t2x2", ArrayType(FloatType()), True)
+ ]),
+ batch_size=5,
+ input_tensor_shapes=[[4], [3]],
+ )
+
+ df.withColumn("x2", multi_times_two_udf("t1", "t2")).select("t1", "t2", "x2.*").show(5)
+ # +--------------------+------------------+--------------------+------------------+
+ # | t1| t2| t1x2| t2x2|
+ # +--------------------+------------------+--------------------+------------------+
+ # |[0.0, 1.0, 2.0, 3.0]| [0.0, 1.0, 2.0]|[0.0, 2.0, 4.0, 6.0]| [0.0, 2.0, 4.0]|
+ # |[4.0, 5.0, 6.0, 7.0]| [4.0, 5.0, 6.0]|[8.0, 10.0, 12.0,...| [8.0, 10.0, 12.0]|
+ # |[8.0, 9.0, 10.0, ...| [8.0, 9.0, 10.0]|[16.0, 18.0, 20.0...|[16.0, 18.0, 20.0]|
+ # |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|[24.0, 26.0, 28.0...|[24.0, 26.0, 28.0]|
+ # |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|[32.0, 34.0, 36.0...|[32.0, 34.0, 36.0]|
+ # +--------------------+------------------+--------------------+------------------+
Review Comment:
backtick doesn't work in sphinx IIRC. I meant ```` ``` ````
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a diff in pull request #39628: [SPARK-40264][ML][DOCS] Supplement docstring in pyspark.ml.functions.predict_batch_udf
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on code in PR #39628:
URL: https://github.com/apache/spark/pull/39628#discussion_r1082211081
##########
python/pyspark/ml/functions.py:
##########
@@ -647,37 +386,369 @@ def predict_columnar(x1: np.ndarray, x2: np.ndarray) -> Mapping[str, np.ndarray]
Function which is responsible for loading a model and returning a
:py:class:`PredictBatchFunction` which takes one or more numpy arrays as input and returns
one of the following:
- - a numpy array (for a single output)
- - a dictionary of named numpy arrays (for multiple outputs)
- - a row-oriented list of dictionaries (for multiple outputs).
+
+ * a numpy array (for a single output)
+ * a dictionary of named numpy arrays (for multiple outputs)
+ * a row-oriented list of dictionaries (for multiple outputs).
+
For a dictionary of named numpy arrays, the arrays can only be one or two dimensional, since
- higher dimension arrays are not supported. For a row-oriented list of dictionaries, each
+ higher dimensional arrays are not supported. For a row-oriented list of dictionaries, each
element in the dictionary must be either a scalar or one-dimensional array.
- return_type : :class:`pspark.sql.types.DataType` or str.
+ return_type : :py:class:`pyspark.sql.types.DataType` or str.
Spark SQL datatype for the expected output:
- - Scalar (e.g. IntegerType, FloatType) --> 1-dim numpy array.
- - ArrayType --> 2-dim numpy array.
- - StructType --> dict with keys matching struct fields.
- - StructType --> list of dict with keys matching struct fields, for models like the
- [Huggingface pipeline for sentiment analysis](https://huggingface.co/docs/transformers/quicktour#pipeline-usage] # noqa: E501
+
+ * Scalar (e.g. IntegerType, FloatType) --> 1-dim numpy array.
+ * ArrayType --> 2-dim numpy array.
+ * StructType --> dict with keys matching struct fields.
+ * StructType --> list of dict with keys matching struct fields, for models like the
+ `Huggingface pipeline for sentiment analysis
+ <https://huggingface.co/docs/transformers/quicktour#pipeline-usage>`_.
+
batch_size : int
- Batch size to use for inference, note that this is typically a limitation of the model
- and/or the hardware resources and is usually smaller than the Spark partition size.
- input_tensor_shapes: List[List[int] | None] | Mapping[int, List[int]] | None
- Optional input tensor shapes for models with tensor inputs. This can be a list of shapes,
+ Batch size to use for inference. This is typically a limitation of the model
+ and/or available hardware resources and is usually smaller than the Spark partition size.
+ input_tensor_shapes : List[List[int] | None] | Mapping[int, List[int]] | None, optional
+ Input tensor shapes for models with tensor inputs. This can be a list of shapes,
where each shape is a list of integers or None (for scalar inputs). Alternatively, this
can be represented by a "sparse" dictionary, where the keys are the integer indices of the
inputs, and the values are the shapes. Each tensor input value in the Spark DataFrame must
be represented as a single column containing a flattened 1-D array. The provided
- input_tensor_shapes will be used to reshape the flattened array into expected tensor shape.
- For the list form, the order of the tensor shapes must match the order of the selected
- DataFrame columns. The batch dimension (typically -1 or None in the first dimension) should
- not be included, since it will be determined by the batch_size argument. Tabular datasets
- with scalar-valued columns should not provide this argument.
+ `input_tensor_shapes` will be used to reshape the flattened array into the expected tensor
+ shape. For the list form, the order of the tensor shapes must match the order of the
+ selected DataFrame columns. The batch dimension (typically -1 or None in the first
+ dimension) should not be included, since it will be determined by the batch_size argument.
+ Tabular datasets with scalar-valued columns should not provide this argument.
Returns
-------
- A pandas_udf for predicting a batch.
+ :py:class:`UserDefinedFunctionLike`
+ A Pandas UDF for model inference on a Spark DataFrame.
+
+ Examples
+ --------
+ For a pre-trained TensorFlow MNIST model with two-dimensional input images represented as a
+ flattened tensor value stored in a single Spark DataFrame column of type `array<float>`.::
+
+ from pyspark.ml.functions import predict_batch_udf
Review Comment:
If this example cannot be ran, should better do it with `.. code-block:: python`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon closed pull request #39628: [SPARK-40264][ML][DOCS] Supplement docstring in pyspark.ml.functions.predict_batch_udf
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon closed pull request #39628: [SPARK-40264][ML][DOCS] Supplement docstring in pyspark.ml.functions.predict_batch_udf
URL: https://github.com/apache/spark/pull/39628
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org