Skip to content

Conversation

@bar-qodo
Copy link

@bar-qodo bar-qodo commented Nov 20, 2025

User description

Splits each torch library registration in the 2.10 folder into its own file -- I had a script that parsed kernel.cpp to do this but I felt like forcing this responsibility on the user might be less error prone

Compiles each file targetting 2.9 and asserts that compilation fails. (There are 2 2.9 kernels we use as negative tests where compilation is expected to succeed)

Stack from ghstack (oldest at bottom):


PR Type

Tests, Enhancement


Description

  • Split monolithic kernel.cpp into individual function files for better modularity

  • Created comprehensive test suite verifying TORCH_FEATURE_VERSION guards for 2.10+ APIs

  • Added negative tests (mv_tensor_accessor files) to validate test infrastructure

  • Dynamically generates test methods for each .cpp and .cu file in csrc directory


Diagram Walkthrough

flowchart LR
  A["kernel.cpp<br/>monolithic file"] -->|"split into"| B["Individual function files<br/>my__foreach_mul.cpp<br/>test_device_*.cpp<br/>etc."]
  B -->|"tested by"| C["test_version_compatibility.py<br/>compiles with 2.9.0"]
  C -->|"expects failure"| D["2.10+ specific code<br/>fails compilation"]
  C -->|"expects success"| E["Negative tests<br/>mv_tensor_accessor_*."]
Loading

File Walkthrough

Relevant files
Tests
3 files
test_version_compatibility.py
New test suite for version compatibility verification       
+300/-0 
mv_tensor_accessor_cpu.cpp
Added negative test file for CPU tensor accessor                 
+40/-0   
mv_tensor_accessor_cuda.cu
Added negative test file for CUDA tensor accessor               
+47/-0   
Refactoring
16 files
kernel.cpp
Removed monolithic kernel file split into separate files 
+0/-205 
my__foreach_mul.cpp
Extracted foreach_mul function into dedicated file             
+20/-0   
my__foreach_mul_.cpp
Extracted foreach_mul_ function into dedicated file           
+19/-0   
make_tensor_clones_and_call_foreach.cpp
Extracted make_tensor_clones_and_call_foreach function into dedicated
file
+41/-0   
my_empty.cpp
Extracted my_empty function into dedicated file                   
+25/-0   
my_reshape.cpp
Extracted my_reshape function into dedicated file               
+17/-0   
my_view.cpp
Extracted my_view function into dedicated file                     
+20/-0   
test_tensor_device.cpp
Extracted test_tensor_device function into dedicated file
+17/-0   
test_device_constructor.cpp
Extracted test_device_constructor function into dedicated file
+37/-0   
test_device_equality.cpp
Extracted test_device_equality function into dedicated file
+14/-0   
test_device_set_index.cpp
Extracted test_device_set_index function into dedicated file
+17/-0   
test_device_index.cpp
Extracted test_device_index function into dedicated file 
+14/-0   
test_device_is_cuda.cpp
Extracted test_device_is_cuda function into dedicated file
+14/-0   
test_device_is_cpu.cpp
Extracted test_device_is_cpu function into dedicated file
+14/-0   
test_parallel_for.cpp
Extracted test_parallel_for function into dedicated file 
+49/-0   
test_get_num_threads.cpp
Extracted test_get_num_threads function into dedicated file
+14/-0   
Enhancement
1 files
tensor_accessor_kernel.h
Added shared tensor accessor kernel header file                   
+28/-0   

@qodo-merge-pro
Copy link

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status:
No audit logs: The tests and helper compilation routines perform actions without any auditing or logging
of critical actions, but as test code this may be acceptable and not subject to audit
requirements.

Referred Code
import os
import subprocess
import tempfile
from pathlib import Path

from torch.testing._internal.common_utils import run_tests, TestCase
from torch.utils.cpp_extension import CUDA_HOME, include_paths as torch_include_paths


class FunctionVersionCompatibilityTest(TestCase):
    """Test that all function files require PyTorch 2.10+."""

    @classmethod
    def setUpClass(cls):
        """Set up test environment once for all tests."""
        cls.csrc_dir = Path(__file__).parent / "libtorch_agnostic_2_10" / "csrc"
        cls.build_dir = Path(tempfile.mkdtemp(prefix="version_check_"))

        cls.pytorch_includes = [
            f"-I{path}" for path in torch_include_paths(device_type="cpu")
        ]


 ... (clipped 194 lines)

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Limited error handling: Subprocess compilation errors are only asserted on success/failure with optional printing
under an env var and fixed timeout, without retries or granular handling of edge cases
like missing compilers or include paths which may be acceptable for tests.

Referred Code
Compile a C++ file with TORCH_TARGET_VERSION=2.9.0.
Returns (success, error_message).
"""
torch_version_2_9 = "0x0209000000000000"

cmd = [
    "g++",
    "-c",
    "-std=c++17",
    f"-DTORCH_TARGET_VERSION={torch_version_2_9}",
    f"-I{source_file.parent}",  # For includes in same directory
    *self.pytorch_includes,
]

# Add CUDA flags if available
if self.cuda_available:
    cmd.extend(self.cuda_includes)

cmd.extend([str(source_file), "-o", str(output_file)])

result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)


 ... (clipped 61 lines)

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-merge-pro
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix function signature in schema

Update the schema for mv_tensor_accessor_cpu to match its function signature by
removing the extra Tensor res argument.

test/cpp_extensions/libtorch_agnostic_2_10_extension/libtorch_agnostic_2_10/csrc/mv_tensor_accessor_cpu.cpp [34-36]

 STABLE_TORCH_LIBRARY_FRAGMENT(libtorch_agnostic_2_10, m) {
-  m.def("mv_tensor_accessor_cpu(Tensor res, Tensor m, Tensor v) -> Tensor");
+  m.def("mv_tensor_accessor_cpu(Tensor m, Tensor v) -> Tensor");
 }
  • Apply / Chat
Suggestion importance[1-10]: 9

__

Why: The suggestion correctly identifies a critical bug where the function signature of mv_tensor_accessor_cpu mismatches its registered schema, which would cause a compilation failure.

High
  • More

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants