You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import torch
import cutex
M, N, K = 4, 4, 1
a = torch.rand((M, K), dtype=torch.float32).cuda()
b = torch.rand((K, N), dtype=torch.float32).cuda()
c = torch.empty((M, N), dtype=torch.float32).cuda()
kernels = cutex.SourceModule("""
//cuda
__global__ void matmul(Tensor<float, 2> a, Tensor<float, 2> b, Tensor<float, 2> c, int M, int N, int K) {
int m = blockIdx.y * blockDim.y + threadIdx.y;
int n = blockIdx.x * blockDim.x + threadIdx.x;
float v = 0.f;
if (m >= M || n >= N) return; // you can also write `a.size(0)` instead of `M`, `b.size(1)` instead of `N`
for (int k = 0; k < K; ++k) { // you can also write `a.size(1)` instead of `K`
v += a[m][k] * b[k][n]; // you can access tensor elements just like operating a multi-level array, with optional out-of-bound check.
}
c[m][n] = v; // the modification will be reflected in the torch tensor in place, no redundant data copying.
}
//!cuda
""",
float_bits=32, # change to 16 to use half precision as `float` type in the above source code.
boundscheck=True, # turning on for debug and off for performance (to use full threads of a block), default is on.
)
kernels.matmul( # automatically discover the kernel function by its name (e.g. 'matmul'), just like a normal python module.
a, b, c, M, N, K, # directly pass tensors and scalars as arguments
grid=(N // 16 + 1, M // 16 + 1), # grid size (number of blocks to be executed)
block=(16, 16, 1), # block size (number of threads in each block)
)
assert torch.allclose(c, torch.mm(a, b))
It triggers an OSError: [Errno 2] No such file or directory: '' at line 242 in previous_frame_arg_lineno (src_module.py) source = open(inspect.getfile(frame)).read()
The text was updated successfully, but these errors were encountered:
When executing the following sample code:
It triggers an OSError: [Errno 2] No such file or directory: '' at line 242 in previous_frame_arg_lineno (src_module.py)
source = open(inspect.getfile(frame)).read()
The text was updated successfully, but these errors were encountered: