fix: add tensormatmul and tensortranspose to iengine for proper integration
Summary
Verified and completed IEngine integration for tensor matrix operations. Added missing TensorMatMul and TensorTranspose methods to the IEngine interface to enable future GPU acceleration in autodiff computation graphs.
Key Findings:
- TensorOperations.MatrixMultiply currently uses
Tensor.MatrixMultiply()(not IEngine) - TensorOperations.Transpose currently uses
Tensor.Transpose()(not IEngine) - ComputationNode lacks an Engine property, preventing direct IEngine usage in TensorOperations
- DenseLayer ExportComputationGraph method not present in this branch
Changes
IEngine Interface (src/Engines/IEngine.cs)
- Added
TensorMatMul<T>(Tensor<T> a, Tensor<T> b)method - Added
TensorTranspose<T>(Tensor<T> tensor)method - Both methods documented with full XML comments
CpuEngine (src/Engines/CpuEngine.cs)
- Implemented
TensorMatMulwith standard matrix multiplication algorithm - Implemented
TensorTransposewith row/column swap logic - Both methods validate 2D tensor inputs and dimensions
GpuEngine (src/Engines/GpuEngine.cs)
- Implemented
TensorMatMuldelegating to Matrix operations with adaptive thresholds - Implemented
TensorTransposedelegating to Matrix operations - Added
ToMatrix<T>andToTensor<T>helper methods for conversion - Uses GPU acceleration for large tensors, CPU fallback for small ones
Architecture Impact
While TensorOperations doesn't use these methods yet (due to ComputationNode lacking an Engine reference), this PR:
- Completes the IEngine interface for tensor operations
- Provides infrastructure for future optimization
- Maintains consistency between Matrix and Tensor operation APIs
- Enables GPU acceleration pathways when autodiff is refactored
Testing
- Build succeeds for all target frameworks (net462, net471, net8.0)
- No compilation errors
- IEngine interface now complete for tensor operations
Next Steps
Future work could include:
- Add Engine property to ComputationNode
- Update TensorOperations to use IEngine methods directly
- Update Tensor.MatrixMultiply and Tensor.Transpose to delegate to IEngine
Generated with Claude Code
Summary by CodeRabbit
New Features
- Added 2D tensor matrix multiplication operation with CPU and GPU backend support
- Added 2D tensor transpose operation with CPU and GPU backend support
- Both operations include automatic backend routing with fallback capabilities
✏️ Tip: You can customize this high-level summary in your review settings.
Walkthrough
Adds two new tensor operation methods—TensorMatMul for matrix multiplication and TensorTranspose for tensor transposition—to the IEngine interface and both CpuEngine and GpuEngine implementations. GpuEngine includes adaptive routing to CPU for small shapes, while CpuEngine provides direct CPU implementations.
Changes
| Cohort / File(s) | Summary |
|---|---|
Interface Definitionssrc/Engines/IEngine.cs |
Declares two new public generic methods: TensorMatMul<T> and TensorTranspose<T> with comprehensive XML documentation describing 2D tensor operation behavior. Note: Methods appear duplicated in the interface definition. |
CPU Implementationsrc/Engines/CpuEngine.cs |
Implements TensorMatMul<T> with input validation (null checks, rank verification, inner-dimension compatibility) and triple-nested loop multiplication logic; implements TensorTranspose<T> with validation and transpose loop. |
GPU Implementationsrc/Engines/GpuEngine.cs |
Implements TensorMatMul<T> and TensorTranspose<T> with adaptive routing (small shapes fall back to CPU, otherwise use GPU if available and healthy). Adds private helper methods ToMatrix<T> and ToTensor<T> to convert between Tensor and Matrix representations for GPU processing. Includes type-specialized handling for float and double. |
Sequence Diagram
sequenceDiagram
actor Client
participant GpuEngine
participant CpuEngine
participant GPU
Client->>GpuEngine: TensorMatMul(a, b)
alt Shape is Small
GpuEngine->>CpuEngine: TensorMatMul(a, b)
CpuEngine-->>GpuEngine: result
else Shape is Large & GPU Healthy
GpuEngine->>GpuEngine: ToMatrix(a), ToMatrix(b)
GpuEngine->>GPU: MatrixMultiply
GPU-->>GpuEngine: matrix_result
GpuEngine->>GpuEngine: ToTensor(matrix_result)
else GPU Unavailable
GpuEngine->>CpuEngine: TensorMatMul(a, b)
CpuEngine-->>GpuEngine: result
end
GpuEngine-->>Client: Tensor<T> result
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
- GpuEngine adaptive routing logic: Verify correct shape threshold for CPU fallback and GPU health checks
- Type specialization in GpuEngine: Confirm float/double handling and type casting in conversion helpers
- Interface duplication: Investigate why TensorMatMul and TensorTranspose appear twice in IEngine
- Input validation consistency: Cross-check null checks and rank validation across CpuEngine and GpuEngine implementations
- Tensor-Matrix conversion helpers: Validate correctness of ToMatrix and ToTensor bidirectional conversions
Poem
🐰 Two tensors dance, seeking dimensions bright,
Transposed and multiplied with CPU might!
When GPU's ready, with adaptive grace,
Small shapes hop back to the safe CPU space. ✨
Pre-merge checks and finishing touches
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title accurately summarizes the main change: adding TensorMatMul and TensorTranspose methods to the IEngine interface for proper integration. |
| Description check | ✅ Passed | The description is well-structured and directly related to the changeset, providing context about the changes to IEngine, CpuEngine, and GpuEngine with clear explanations of implementation approaches. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 84.62% which is sufficient. The required threshold is 80.00%. |
✨ Finishing touches
- [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
- [ ] Commit unit tests in branch
feat/iengine-verification
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.