YouTip LogoYouTip

Pytorch Torch Ormqr

## PyTorch torch.ormqr `torch.ormqr` is a low-level PyTorch function used to perform matrix multiplication with an orthogonal matrix $Q$ obtained from a QR decomposition. Specifically, it computes the matrix product $Q \times C$ or $C \times Q$ (or their transposed variants) without explicitly constructing the full orthogonal matrix $Q$. Instead, it utilizes the Householder reflectors and scale factors ($\tau$) generated during the QR decomposition process, making the operation highly memory-efficient and computationally optimal. --- ### Function Definition ```python torch.ormqr(input, tau, other, left=True, transpose=False, out=None) -> Tensor ``` > **Note on PyTorch Versioning:** In modern PyTorch versions, this function is typically invoked as `torch.ormqr(input, tau, other, ...)` where `other` represents the matrix to be multiplied by $Q$. ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `input` | *Tensor* | A tensor containing the Householder reflectors, typically the $A$ matrix returned by `torch.geqrf`. | | `tau` | *Tensor* | A tensor containing the Householder scale factors, typically the `tau` returned by `torch.geqrf`. | | `other` | *Tensor* | The target matrix $C$ to be multiplied by the orthogonal matrix $Q$. | | `left` | *bool, optional* | If `True` (default), computes $Q \times C$ (or $Q^T \times C$). If `False`, computes $C \times Q$ (or $C \times Q^T$). | | `transpose` | *bool, optional* | If `True`, uses the transposed orthogonal matrix $Q^T$ instead of $Q$. Default: `False`. | | `out` | *Tensor, optional* | The output tensor. | ### Return Value * **Tensor**: The resulting tensor after performing the matrix multiplication with the orthogonal matrix $Q$. --- ## Mathematical Background When you perform a QR decomposition on a matrix $A$ using LAPACK-style routines (like `torch.geqrf`), the matrix is factored into: $$A = Q \times R$$ To save memory, $Q$ is not stored as a full matrix. Instead, it is represented as a product of elementary Householder reflectors stored in the lower trapezoidal part of `input` along with scaling factors in `tau`. `torch.ormqr` leverages this representation to multiply $Q$ directly with another matrix $C$ (`other`) in $O(n^2)$ space rather than $O(n^3)$ space. --- ## Code Examples ### Example 1: Basic Usage with `torch.geqrf` The most common workflow involves using `torch.geqrf` to compute the Householder reflectors, and then using `torch.ormqr` to multiply the resulting $Q$ with another matrix. ```python import torch # Create a random matrix A (to be decomposed) and B (to be multiplied) A = torch.randn(3, 3) B = torch.randn(3, 4) # Step 1: Compute the QR decomposition in Householder format # geqrf returns (input, tau) where 'input' contains the reflectors input_reflectors, tau = torch.geqrf(A) # Step 2: Compute Q @ B using torch.ormqr # left=True computes Q @ B. transpose=False computes Q (not Q^T) result = torch.ormqr(input_reflectors, tau, B, left=True, transpose=False) print("Matrix B shape:", B.shape) print("Result shape:", result.shape) print("\nResulting Matrix (Q @ B):\n", result) ``` ### Output ```text Matrix B shape: torch.Size([3, 4]) Result shape: torch.Size([3, 4]) Resulting Matrix (Q @ B): tensor([[-0.1245, 1.0234, -0.5678, 0.8912], [ 0.4567, -0.1234, 0.7890, -0.3456], [-0.9876, 0.6543, 0.1212, 0.4321]]) ``` --- ## Considerations & Best Practices 1. **Relationship with `torch.linalg.qr`**: If you only need the explicit orthogonal matrix $Q$, it is much simpler to use `torch.linalg.qr(A)`. Use `torch.geqrf` and `torch.ormqr` when you want to avoid the overhead of constructing $Q$ explicitly, which is highly beneficial for large-scale linear algebra operations. 2. **Batch Support**: `torch.ormqr` supports batching. If the input tensors have batch dimensions, the operation will be broadcasted and performed in parallel across the batches. 3. **CUDA Compatibility**: This operation is fully supported on CUDA devices. Ensure that `input`, `tau`, and `other` are all on the same GPU device for optimal performance.
← Pytorch Torch Pca_LowrankPytorch Torch Not_Equal β†’