tjyuyao / cutex Goto Github PK
View Code? Open in Web Editor NEWPyCUDA based PyTorch Extension Made Easy
License: MIT License
PyCUDA based PyTorch Extension Made Easy
License: MIT License
When executing the following sample code:
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()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.