A Visual Studio Code extension that provides syntax highlighting for C++ and CUDA code within Python string literals used with PyTorch's load_inline()
function.
It's this guy https://marketplace.visualstudio.com/items?itemName=msaroufim.pytorch-load-inline-highlighter
-
Automatic Detection: Recognizes C++/CUDA code in various contexts:
cpp_sources="..."
andcuda_sources="..."
parameters- Variables ending with
_cpp
,_cuda
,_kernel
- Variables starting with
cpp_
orcuda_
- Content-based detection (e.g.,
torch::Tensor
,__global__
,blockIdx
)
-
Multi-String Support: Works with single quotes, double quotes, and triple quotes
-
Context-Aware: Only activates within Python files
-
Lightweight: Uses TextMate grammar injection for optimal performance
- Clone this repository
- Install dependencies:
npm install
- Compile:
npm run compile
- Install the extension in VS Code:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Click "..." → "Install from VSIX..."
- Select the generated
.vsix
file
- Open this project in VS Code
- Press F5 to launch Extension Development Host
- Open a Python file with
load_inline()
code - Verify syntax highlighting works
The extension automatically highlights C++/CUDA code in these patterns:
# Variable name patterns
cpp_code = """
torch::Tensor my_function(torch::Tensor input) {
return input * 2.0;
}
"""
cuda_kernel_code = """
__global__ void my_kernel(float* data, int size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
data[idx] *= 2.0f;
}
}
"""
# Function parameters
load_inline(
name="my_module",
cpp_sources="""
#include <torch/extension.h>
torch::Tensor process(torch::Tensor x) { return x; }
""",
cuda_sources="""
__global__ void kernel(float* x, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) x[i] *= 2.0f;
}
"""
)
*_cpp
,*_cuda
,*_kernel
cpp_*
,cuda_*
cpp_code
,cuda_kernel_code
,kernel_code
cpp_sources=
cuda_sources=
- C++:
torch::Tensor
declarations - CUDA:
__global__
,__device__
,__host__
,blockIdx
,threadIdx
- Visual Studio Code 1.74.0 or higher
- Existing C++ and CUDA extensions for full language support
Thank you to Steven Arellano for giving me the idea to just go ahead and do this and thank you Claude for doing it.
MIT