tessl install tessl/pypi-cupy-cuda101@9.6.0CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)
Agent Success
Agent success rate when using this tile
87%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.19x
Baseline
Agent success rate without this tile
73%
You need to implement a system for processing multiple matrices simultaneously using GPU acceleration. The system should perform batch matrix operations efficiently, handling multiple independent matrix computations in parallel.
Implement a module matrix_batch_processor.py that provides functionality for:
Batch Matrix Multiplication: Given two batches of matrices (batch A and batch B), compute the matrix product for each corresponding pair. The batches are represented as 3D arrays where:
Batch Matrix Inversion: Given a batch of square invertible matrices, compute their inverses. The batch is represented as a 3D array where:
Performance Comparison: Provide a function that compares batch operations against sequential (loop-based) operations to demonstrate the performance benefits of batched operations.
Your implementation should include:
batch_matrix_multiply(batch_a, batch_b): Performs batched matrix multiplication
batch_matrix_inverse(batch_matrices): Computes inverses for a batch of matrices
compare_batch_vs_sequential(batch_size, matrix_size): Benchmarks batch vs sequential operations
Input:
import cupy as cp
# Batch of 3 matrices (3x2) multiplied by batch of 3 matrices (2x4)
batch_a = cp.array([
[[1, 2], [3, 4], [5, 6]],
[[2, 1], [4, 3], [6, 5]],
[[1, 1], [2, 2], [3, 3]]
])
batch_b = cp.array([
[[1, 0, 1, 0], [0, 1, 0, 1]],
[[1, 1, 0, 0], [0, 0, 1, 1]],
[[2, 0, 0, 2], [0, 2, 2, 0]]
])
result = batch_matrix_multiply(batch_a, batch_b)Expected Output:
# Shape: (3, 3, 4)
# First matrix result: [[1, 2, 1, 2], [3, 4, 3, 4], [5, 6, 5, 6]]
# Second matrix result: [[2, 2, 1, 1], [4, 4, 3, 3], [6, 6, 5, 5]]
# Third matrix result: [[2, 2, 2, 2], [4, 4, 4, 4], [6, 6, 6, 6]]Input:
import cupy as cp
# Batch of 2 identity-like matrices
batch_matrices = cp.array([
[[2.0, 0.0], [0.0, 2.0]],
[[4.0, 0.0], [0.0, 4.0]]
])
result = batch_matrix_inverse(batch_matrices)Expected Output:
# Shape: (2, 2, 2)
# First inverse: [[0.5, 0.0], [0.0, 0.5]]
# Second inverse: [[0.25, 0.0], [0.0, 0.25]]Input:
result = compare_batch_vs_sequential(batch_size=100, matrix_size=50)Expected Output:
# Dictionary with keys 'batch_time' and 'sequential_time'
# batch_time should be significantly lower than sequential_time
# Example: {'batch_time': 0.05, 'sequential_time': 0.45}
assert result['batch_time'] < result['sequential_time']Provides GPU-accelerated array operations and numerical computing capabilities.
matrix_batch_processor.py: Main implementation moduletest_matrix_batch_processor.py: Test file containing the test cases above