feat: add blas/ext/axpb#12861
Conversation
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: passed
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: passed
- task: lint_repl_help
status: passed
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: passed
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: passed
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: passed
- task: lint_license_headers
status: passed
---
Coverage Report
The above coverage report was generated for the changes in this PR. |
|
Total implementation time: 202 minutes 😅 I finally was able to implement this, @kgryte! 😮💨 |
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: na
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: na
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
| var bench = require( '@stdlib/bench' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); |
There was a problem hiding this comment.
| var uniform = require( '@stdlib/random/array/uniform' ); | |
| var uniform = require( '@stdlib/random/uniform' ); |
You should be using the API to generate random ndarrays directly.
There was a problem hiding this comment.
I see that we are doing this in blas/ext/cusum, but that should likely be updated in order to use top-level APIs. Feel free to make a METR task to update the top-level blas/ext/* APIs (where appropriate).
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
There was a problem hiding this comment.
| var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| * ## Notes | ||
| * | ||
| * - The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**). | ||
| * - By default, the function multiplies each element by `1.0` and adds `0.0` (i.e., performs a no-op). |
There was a problem hiding this comment.
Why are alpha and beta optional parameters? This seems odd to me. I would expect them to be mandatory, as that is the entire point of axpb.
| type Alpha = ndarray | number | ComplexLike; | ||
|
|
||
| /** | ||
| * Beta parameter. | ||
| */ | ||
| type Beta = ndarray | number | ComplexLike; |
There was a problem hiding this comment.
Instead of separate types, I would suggest doing something like type ScalarArgument = ndarray | ... and then, in your signatures, do alpha: ScalarArgument, beta: ScalarArgument, etc.
| @@ -0,0 +1,50 @@ | |||
|
|
|||
| {{alias}}( x[, alpha][, beta][, options] ) | |||
There was a problem hiding this comment.
Yeah, this sort of signature is going to lead to all sorts of argument juggling, especially as you have no way of distinguishing between alpha and beta when wanting to only provide beta.
TL;DR: make alpha and beta mandatory arguments, similar to have the searchElement argument in index-of is mandatory.
| // Create a data buffer: | ||
| var xbuf = discreteUniform( 10, -100, 100, { | ||
| 'dtype': 'generic' | ||
| }); | ||
|
|
||
| // Define the shape of the input array: | ||
| var shape = [ 5, 2 ]; | ||
|
|
||
| // Define the array strides: | ||
| var sx = [ 2, 1 ]; | ||
|
|
||
| // Define the index offset: | ||
| var ox = 0; | ||
|
|
||
| // Create an input ndarray: | ||
| var x = new ndarray( 'generic', xbuf, shape, sx, ox, 'row-major' ); |
There was a problem hiding this comment.
Use the top-level random/discrete-uniform package. Then you can avoid all this manual ndarray creation noise.
| 'default': gaxpb | ||
| }; | ||
| var options = { | ||
| 'strictTraversalOrder': true |
kgryte
left a comment
There was a problem hiding this comment.
Left initial comments. TL;DR: this needs to be refactored to make alpha and beta mandatory. This should simplify your argument juggling logic significantly.
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: passed
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: passed
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: passed
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: passed
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: passed
- task: lint_license_headers
status: passed
---
Co-authored-by: Athan <kgryte@gmail.com> Signed-off-by: Athan <kgryte@gmail.com>
Co-authored-by: Athan <kgryte@gmail.com> Signed-off-by: Athan <kgryte@gmail.com>
Co-authored-by: Athan <kgryte@gmail.com> Signed-off-by: Athan <kgryte@gmail.com>
Co-authored-by: Athan <kgryte@gmail.com> Signed-off-by: Athan <kgryte@gmail.com>
|
|
||
| alpha: ndarray|number | ||
| Scalar constant to multiply. May be either a scalar value, or an ndarray | ||
| having a floating-point or "generic" data type. If provided an ndarray, |
There was a problem hiding this comment.
| having a floating-point or "generic" data type. If provided an ndarray, | |
| having a numeric or "generic" data type. If provided an ndarray, |
Given this change, you'll need to rewrap this description.
|
|
||
| beta: ndarray|number | ||
| Scalar constant to add. May be either a scalar value, or an ndarray | ||
| having a floating-point or "generic" data type. Broadcasting semantics |
There was a problem hiding this comment.
| having a floating-point or "generic" data type. Broadcasting semantics | |
| having a numeric or "generic" data type. Broadcasting semantics |
Rewrap.
| beta: ndarray|number | ||
| Scalar constant to add. May be either a scalar value, or an ndarray | ||
| having a floating-point or "generic" data type. Broadcasting semantics | ||
| are the same as for alpha. |
There was a problem hiding this comment.
I would just copy, paste, and modify the broadcasting semantics description from alpha. This makes very explicit that broadcasting is purely related to x and options.dims, and that there are no broadcasting semantics between alpha and beta.
| Function options. | ||
|
|
||
| options.dims: Array<integer> (optional) | ||
| List of dimensions over which to perform operation. If not provided, the |
There was a problem hiding this comment.
This means "flattening" during the operation, which I think is the right behavior, by default, for this API.
Co-authored-by: Athan <kgryte@gmail.com> Signed-off-by: Athan <kgryte@gmail.com>
| var idtypes0 = dtypes( 'floating_point_and_generic' ); // alpha ndarray | ||
| var idtypes1 = dtypes( 'floating_point_and_generic' ); // beta ndarray | ||
| var odtypes = dtypes( 'floating_point_and_generic' ); |
There was a problem hiding this comment.
Why are we limited to floating-point data types here?
Couldn't I reasonably have, say, an int32 input array x, alpha = 5, and beta = 3 and have that delegate to gaxpb?
| var idtypes0 = dtypes( 'floating_point_and_generic' ); // alpha ndarray | |
| var idtypes1 = dtypes( 'floating_point_and_generic' ); // beta ndarray | |
| var odtypes = dtypes( 'floating_point_and_generic' ); | |
| var idtypes0 = dtypes( 'numeric_and_generic' ); // alpha ndarray | |
| var idtypes1 = dtypes( 'numeric_and_generic' ); // beta ndarray | |
| var odtypes = dtypes( 'numeric_and_generic' ); |
| throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); | ||
| } | ||
| if ( nargs < 3 ) { | ||
| throw new TypeError( 'invalid invocation. Insufficient arguments. Must provide an ndarray, alpha, and beta.' ); |
There was a problem hiding this comment.
| throw new TypeError( 'invalid invocation. Insufficient arguments. Must provide an ndarray, alpha, and beta.' ); | |
| throw new TypeError( 'invalid invocation. Insufficient arguments. Must provide at least three arguments.' ); |
There was a problem hiding this comment.
Minimize future copy-paste mistakes.
| // Broadcast alpha and beta to match the appropriate shape... | ||
|
|
||
| // Case: axpb( x, alpha_scalar, beta_scalar, ??? ) | ||
| if ( isScalar( alpha ) && isScalar( beta ) ) { |
There was a problem hiding this comment.
You do a lot of repeated isScalar( alpha ) and isScalar( beta ) here. Call once for both and assign the result to internal variables (e.g., isScalarAlpha = isScalar( alpha ), etc).
There was a problem hiding this comment.
And then can do explicit alpha and beta validation.
if ( !isScalarAlpha && !isndarrayLike( alpha ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray, a number, or a complex number. Value: `%s`.', alpha ) );
}And you can do all this before you hit L129. Meaning, you can take care of your validation up front, rather than have it intermingled with delegation and broadcasting logic.
| } | ||
| } | ||
| // Case: axpb( x, alpha_ndarray, beta_ndarray, ??? ) | ||
| else if ( isndarrayLike( alpha ) && isndarrayLike( beta ) ) { |
There was a problem hiding this comment.
Looking at the various conditionals, you have a decent amount of repeated logic. You should have a series of four conditionals after caching whether alpha and beta are scalars:
- if
isScalarAlpha, broadcastScalar - else maybeBroadcastArray
- if
isScalarBeta, broadcastScalar - else maybeBroadcastArray
That should be it. No combined conditions (e.g., if scalar alpha AND ndarray beta, do x and y and if ndarray alpha and ndarray beta, do z and y, etc etc). If you do things up front, you can handle each argument on its own.
| beta = maybeBroadcastArray( beta, sh ); | ||
| } | ||
| } else { | ||
| if ( !isScalar( alpha ) && !isndarrayLike( alpha ) ) { |
There was a problem hiding this comment.
Also, recognize that isndarrayLike checks are relatively expensive, as they involve materializing various meta data. If we do them, we want to only do them once, and, if the knowledge is needed again, make sure we cache the result in a variable.
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. |
There was a problem hiding this comment.
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. | |
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. |
| The function has the following parameters: | ||
|
|
||
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. | ||
| - **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] alpha must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] alpha must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
There was a problem hiding this comment.
| - **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] alpha must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] alpha must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. | |
| - **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] `alpha` must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] `alpha` must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
|
|
||
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. | ||
| - **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] alpha must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] alpha must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. | ||
| - **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha. |
There was a problem hiding this comment.
| - **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha. | |
| - **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha. |
I would also be more explicit regarding the broadcasting behavior, rather than just referring to alpha. If you want to avoid duplication, consider moving the broadcast discussion to the ## Notes section below and then saying "Must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by options.dims. For more discussion, see the notes below." for both alpha and beta.
| Both `alpha` and `beta` can be complex numbers: | ||
|
|
||
| ```javascript | ||
| var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
| var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' ); | ||
|
|
||
| var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0 ] ); | ||
|
|
||
| var alpha = new Complex128( 2.0, 1.0 ); | ||
| var beta = new Complex128( 1.0, 0.0 ); | ||
|
|
||
| var y = axpb( x, alpha, beta ); | ||
| // returns <ndarray>[ <Complex128>[ 1.0, 5.0 ], <Complex128>[ 3.0, 11.0 ] ] | ||
| ``` | ||
|
|
||
| The function also supports complex-valued floating-point ndarrays: | ||
|
|
||
| ```javascript | ||
| var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' ); | ||
|
|
||
| var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0 ] ); | ||
|
|
||
| var y = axpb( x, 2.0, 1.0 ); | ||
| // returns <ndarray>[ <Complex128>[ 3.0, 4.0 ], <Complex128>[ 7.0, 8.0 ] ] | ||
| ``` |
There was a problem hiding this comment.
These examples are a bit weak. Instead, I suggest consolidating into a single example which demonstrates the behavior when alpha and/or beta are/is real-valued and x is complex-valued. This highlights both that complex arrays are accepted and that real-values are casted to the dtype of x.
| }); | ||
|
|
||
| // Print the input ndarray: |
There was a problem hiding this comment.
| }); | |
| // Print the input ndarray: | |
| }); |
| } | ||
| throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray, a number, or a complex number. Value: `%s`.', beta ) ); | ||
| } | ||
| // Case: axpb( x, alpha, beta ) |
There was a problem hiding this comment.
One thing which is not handled here is when alpha and beta are real-valued ndarrays and x is complex-valued. For real scalars, they are converted to complex-valued ndarrays. However, it doesn't seem that is the case for alpha and beta which are already ndarrays.
However, this also applies to blas/ext/cusum where we do real-to-complex conversion for scalar initial values, but not ndarray initial values.
Not sure the best approach, as we will want to consistently apply the same logic across relevant packages.
Resolves stdlib-js/metr-issue-tracker#639
Description
This pull request:
blas/ext/base/axpbRelated Issues
This pull request has the following related issues:
blas/ext/axpbmetr-issue-tracker#639Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
Used Cursor+VS Code code-generation tools to help assist with the feature.
@stdlib-js/reviewers