<!--
title: Optimize tensor memory operations
domain: llm-infra
topic: Pytorch
language: Python
source: vllm-project/vllm
updated: 2025-06-25
url: https://awesomereviewers.com/reviewers/vllm-optimize-tensor-memory-operations/
-->

When working with PyTorch tensors, use memory-efficient operations that avoid unnecessary copies. Specify memory formats directly during tensor creation instead of applying operations like `.t().contiguous()`. For C++/CUDA kernel interfacing, use `.data_ptr()` instead of `.view(dtype)` to ensure safe memory access and maintain compatibility with future PyTorch versions.

```python
# Inefficient approach with unnecessary copy:
tensor = torch.empty((n, m), device="cuda", dtype=torch.bfloat16).t().contiguous()

# Efficient approach:
tensor = torch.empty((n, m), device="cuda", dtype=torch.bfloat16, 
                     memory_format=torch.contiguous_format).t()

# Unsafe C++ kernel interfacing:
cutlass_function(w1_scale.view(torch.int32), w1.view(torch.long))

# Safe approach with explicit pointer access:
cutlass_function(w1_scale.data_ptr(), w1.data_ptr())
```
