-
Notifications
You must be signed in to change notification settings - Fork 109
Added new pinv() operator and updated Reduced SVD #740
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 all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .. _pinv_func: | ||
|
|
||
| pinv | ||
| #### | ||
|
|
||
| Compute the Moore-Penrose pseudo-inverse of a matrix. | ||
|
|
||
| .. doxygenfunction:: pinv(const OpA &a, float rcond = get_default_rcond<typename OpA::value_type>()) | ||
|
|
||
| Examples | ||
| ~~~~~~~~ | ||
|
|
||
| .. literalinclude:: ../../../../test/00_solver/Pinv.cu | ||
| :language: cpp | ||
| :start-after: example-begin pinv-test-1 | ||
| :end-before: example-end pinv-test-1 | ||
| :dedent: | ||
|
|
||
|
|
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,18 @@ | ||
| .. _det_func: | ||
|
|
||
| det | ||
| ===== | ||
|
|
||
| Compute the determinant of a tensor. | ||
|
|
||
| .. doxygenfunction:: det(const OpA &a) | ||
|
|
||
| Examples | ||
| ~~~~~~~~ | ||
|
|
||
| .. literalinclude:: ../../../../test/00_solver/Det.cu | ||
| :language: cpp | ||
| :start-after: example-begin det-test-1 | ||
| :end-before: example-end det-test-1 | ||
| :dedent: | ||
|
|
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
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,144 @@ | ||
| //////////////////////////////////////////////////////////////////////////////// | ||
| // BSD 3-Clause License | ||
| // | ||
| // COpBright (c) 2021, NVIDIA Corporation | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
| // | ||
| // 1. Redistributions of source code must retain the above cOpBright notice, this | ||
| // list of conditions and the following disclaimer. | ||
| // | ||
| // 2. Redistributions in binary form must reproduce the above cOpBright notice, | ||
| // this list of conditions and the following disclaimer in the documentation | ||
| // and/or other materials provided with the distribution. | ||
| // | ||
| // 3. Neither the name of the cOpBright holder nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COpBRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| // DISCLAIMED. IN NO EVENT SHALL THE COpBRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| ///////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #pragma once | ||
|
|
||
|
|
||
| #include "matx/core/type_utils.h" | ||
| #include "matx/operators/base_operator.h" | ||
| #include "matx/transforms/pinv.h" | ||
|
|
||
| namespace matx { | ||
| namespace detail { | ||
| template<typename OpA> | ||
| class PinvOp : public BaseOp<PinvOp<OpA>> | ||
| { | ||
| private: | ||
| OpA a_; | ||
| float rcond_; | ||
| cuda::std::array<index_t, OpA::Rank()> out_dims_; | ||
| mutable detail::tensor_impl_t<typename remove_cvref_t<OpA>::value_type, OpA::Rank()> tmp_out_; | ||
| mutable typename remove_cvref_t<OpA>::value_type *ptr; | ||
|
|
||
| public: | ||
| using matxop = bool; | ||
| using value_type = typename OpA::value_type; | ||
| using matx_transform_op = bool; | ||
| using pinv_xform_op = bool; | ||
|
|
||
| __MATX_INLINE__ std::string str() const { return "pinv()"; } | ||
| __MATX_INLINE__ PinvOp(OpA a, float rcond) : a_(a), rcond_(rcond) { | ||
| for (int r = 0; r < Rank(); r++) { | ||
| if (r >= Rank() - 2) { | ||
| out_dims_[r] = (r == Rank() - 1) ? a_.Size(Rank() - 2) : a_.Size(Rank() - 1); | ||
| } | ||
| else { | ||
| out_dims_[r] = a_.Size(r); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <typename... Is> | ||
| __MATX_INLINE__ __MATX_DEVICE__ __MATX_HOST__ decltype(auto) operator()(Is... indices) const | ||
| { | ||
| return tmp_out_(indices...); | ||
| } | ||
|
|
||
| static __MATX_INLINE__ constexpr __MATX_HOST__ __MATX_DEVICE__ int32_t Rank() | ||
| { | ||
| return OpA::Rank(); | ||
| } | ||
|
|
||
| constexpr __MATX_INLINE__ __MATX_HOST__ __MATX_DEVICE__ index_t Size(int dim) const | ||
| { | ||
| return out_dims_.Size(dim); | ||
| } | ||
|
|
||
| template <typename Out, typename Executor> | ||
| void Exec(Out &&out, Executor &&ex) const{ | ||
| pinv_impl(cuda::std::get<0>(out), a_, ex, rcond_); | ||
| } | ||
|
|
||
| template <typename ShapeType, typename Executor> | ||
| __MATX_INLINE__ void InnerPreRun([[maybe_unused]] ShapeType &&shape, Executor &&ex) const noexcept | ||
| { | ||
| if constexpr (is_matx_op<OpA>()) { | ||
| a_.PreRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); | ||
| } | ||
| } | ||
|
|
||
| template <typename ShapeType, typename Executor> | ||
| __MATX_INLINE__ void PreRun([[maybe_unused]] ShapeType &&shape, Executor &&ex) const noexcept | ||
| { | ||
| InnerPreRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); | ||
|
|
||
| detail::AllocateTempTensor(tmp_out_, std::forward<Executor>(ex), out_dims_, &ptr); | ||
|
|
||
| Exec(cuda::std::make_tuple(tmp_out_), std::forward<Executor>(ex)); | ||
| } | ||
|
|
||
| template <typename ShapeType, typename Executor> | ||
| __MATX_INLINE__ void PostRun(ShapeType &&shape, Executor &&ex) const noexcept | ||
| { | ||
| if constexpr (is_matx_op<OpA>()) { | ||
| a_.PostRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); | ||
| } | ||
| } | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Perfom a generalized inverse of a matrix using its singular-value decomposition (SVD). | ||
| * It automatically removes small singular values for stability. | ||
| * | ||
| * For tensors of rank > 2, batching is performed. | ||
| * | ||
| * @tparam OpA | ||
| * Tensor or operator type of input A | ||
| * | ||
| * @param a | ||
| * Input tensor or operator of shape `... x m x n` | ||
| * @param rcond | ||
| * Cutoff for small singular values. For stability, singular values | ||
| * smaller than `rcond * largest_singular_value` are set to 0 for each matrix | ||
| * in the batch. By default, `rcond` is approximately the machine epsilon of the tensor dtype. | ||
aayushg55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @return | ||
| * An operator that gives a tensor of size `... x n x m` representing the pseudo-inverse of the input | ||
| */ | ||
| template<typename OpA> | ||
| __MATX_INLINE__ auto pinv(const OpA &a, float rcond = get_default_rcond<typename OpA::value_type>()) { | ||
| return detail::PinvOp(a, rcond); | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.