Skip to content

⚡️ Speed up function point_line_distance by 28%#29

Open
codeflash-ai[bot] wants to merge 2 commits into
mainfrom
codeflash/optimize-point_line_distance-m8oc2zz0
Open

⚡️ Speed up function point_line_distance by 28%#29
codeflash-ai[bot] wants to merge 2 commits into
mainfrom
codeflash/optimize-point_line_distance-m8oc2zz0

Conversation

@codeflash-ai
Copy link
Copy Markdown

@codeflash-ai codeflash-ai Bot commented Mar 25, 2025

📄 28% (0.28x) speedup for point_line_distance in kornia/geometry/linalg.py

⏱️ Runtime : 5.39 milliseconds 4.22 milliseconds (best of 104 runs)

📝 Explanation and details

Changes Made.

  1. In-place Operations: Used abs_() to modify numerator in-place to save time by not creating a new object.
  2. Pre-calculation: Calculated denom_norm only once instead of using norm to reduce function calls.
  3. Simplified Norm Calculation: Calculated the norm manually without calling norm method for efficiency. This avoids potential overheads from additional checks or support for other configurations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 45 Passed
🌀 Generated Regression Tests 14 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 88.9%
⚙️ Existing Unit Tests Details
- geometry/test_linalg.py
🌀 Generated Regression Tests Details
from __future__ import annotations

from typing import Optional, TypeVar

# imports
import pytest  # used for our unit tests
import torch  # used for tensor operations
from kornia.core import Tensor  # used for type checking
from kornia.geometry.linalg import point_line_distance
from typing_extensions import TypeGuard

# function to test
# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""The testing package contains testing-specific utilities."""

__all__ = [
    "KORNIA_CHECK_IS_TENSOR",
]

T = TypeVar("T", bound=type)
from kornia.geometry.linalg import point_line_distance


# unit tests
def test_point_line_distance_basic_2d():
    # Single 2D point and line
    point = torch.tensor([[1.0, 2.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distance = torch.tensor([1.0 / (2**0.5)])

def test_point_line_distance_multiple_points_single_line():
    # Multiple 2D points and a single line
    point = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distances = torch.tensor([1.0 / (2**0.5), 1.0 / (2**0.5)])

def test_point_line_distance_3d_homogeneous_points():
    # Single 3D homogeneous point and line
    point = torch.tensor([[1.0, 2.0, 1.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distance = torch.tensor([1.0 / (2**0.5)])


def test_point_line_distance_points_on_line():
    # Points on the line
    point = torch.tensor([[1.0, 1.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distance = torch.tensor([0.0])

def test_point_line_distance_large_coefficients():
    # Very large coefficients
    point = torch.tensor([[1.0, 2.0]])
    line = torch.tensor([[1e10, -1e10, 1e10]])
    expected_distance = torch.tensor([1.0 / (2**0.5)])

def test_point_line_distance_invalid_input_shape():
    # Invalid shape for points
    point = torch.tensor([[1.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    with pytest.raises(ValueError):
        point_line_distance(point, line)

def test_point_line_distance_large_batch():
    # Large batch of points and lines
    point = torch.rand(1000, 2)
    line = torch.rand(1000, 3)
    codeflash_output = point_line_distance(point, line)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import pytest  # used for our unit tests
import torch  # used to create tensors for testing
from kornia.core import Tensor  # import the Tensor type from Kornia
from kornia.geometry.linalg import point_line_distance


# unit tests
def test_single_point_line_2d():
    # Test a single point and line in 2D
    point = torch.tensor([[1.0, 1.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])  # Line: x - y = 0
    expected_distance = 0.0

def test_single_point_line_3d():
    # Test a single point and line in 3D (homogeneous coordinates)
    point = torch.tensor([[1.0, 1.0, 1.0]])
    line = torch.tensor([[1.0, -1.0, 0.0]])  # Line: x - y = 0
    expected_distance = 0.0

def test_vertical_horizontal_lines():
    # Test vertical and horizontal lines
    point = torch.tensor([[1.0, 2.0]])
    line = torch.tensor([[1.0, 0.0, -1.0]])  # Vertical line x = 1
    expected_distance = 0.0

    point = torch.tensor([[2.0, 1.0]])
    line = torch.tensor([[0.0, 1.0, -1.0]])  # Horizontal line y = 1
    expected_distance = 0.0


def test_invalid_inputs():
    # Test non-tensor inputs
    point = [[1.0, 1.0]]
    line = [1.0, -1.0, 0.0]
    with pytest.raises(TypeError):
        point_line_distance(point, line)

def test_large_batch():
    # Test large batch of points and lines
    points = torch.rand((1000, 1000, 2))
    lines = torch.rand((1000, 1000, 3))
    codeflash_output = point_line_distance(points, lines)

def test_boundary_values():
    # Test points with large coordinates
    point = torch.tensor([[1e9, 1e9]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distance = 0.0

    # Test lines with large coefficients
    point = torch.tensor([[1.0, 1.0]])
    line = torch.tensor([[1e9, -1e9, 0.0]])
    expected_distance = 0.0

def test_precision():
    # Test very close points and lines for precision
    point = torch.tensor([[1.000000001, 1.000000001]])
    line = torch.tensor([[1.0, -1.0, 0.0]])
    expected_distance = 0.0
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-point_line_distance-m8oc2zz0 and push.

Codeflash

Ubuntu and others added 2 commits March 13, 2025 00:39
### Changes Made.
1. **In-place Operations**: Used `abs_()` to modify `numerator` in-place to save time by not creating a new object.
2. **Pre-calculation**: Calculated `denom_norm` only once instead of using `norm` to reduce function calls.
3. **Simplified Norm Calculation**: Calculated the norm manually without calling `norm` method for efficiency. This avoids potential overheads from additional checks or support for other configurations.
@codeflash-ai codeflash-ai Bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Mar 25, 2025
@codeflash-ai codeflash-ai Bot requested a review from dasarchan March 25, 2025 10:09
@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@github-actions github-actions Bot added the stale label Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants