Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tensor and Operator Basics

jhelsby edited this pageAug 31, 2024 ·6 revisions

Scope

  • Understand what a tensor is (including stride, dtype, and layout)
  • Understand what an operator is in PyTorch
  • Understand what views are
  • Understand how to author an operator in PyTorch
  • Understand how to test operators in PyTorch
  • Understand what TensorIterator is

What is a tensor?

Tensors are a specialized data structure that are very similar to arrays and matrices. In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters. Tensors are similar toNumPy’s ndarrays, except that tensors can run on GPUs or other hardware accelerators. In fact, tensors and NumPy arrays can often share the same underlying memory, eliminating the need to copy data. Tensors are also optimized for automatic differentiation. (Source:PyTorch Basics Tutorial.)

A Tensorconsists of:

  • data_ptr, a pointer to a chunk of memory
  • somesizes metadata
  • somestrides metadata
  • a storage offset

What is an operator?

A kernel is a function that accepts Tensors and/or raw pointers to memory and performs a useful computation (for example, matrix multiplication, attention, etc).

An operator is glue code for the PyTorch runtime that tells it about the computation. A single operator can be associated with multiple kernels (for example, torch.add has a kernel for CPU and a kernel for CUDA). The glue code is necessary to get PyTorch subsystems (like torch.compile and torch.autograd) to compose with the computation.

Standalone kernels may work directly with PyTorch but will not compose with the majority of PyTorch subsystems. In order to get them to compose, please register an operator for them.

(Source:The Custom Operator Manual, provided in the officialPyTorch Custom Operators documentation.)

How to author an operator

Comprehensive guide

TensorIterator

  • Read through the colab notebook (link)
  • Listen to the podcast (link)

Learn about view operations

  • Read through the colab notebook (link)
  • Try out the Tensor Views lab (link)

Bonus Lab

  • After completing the views lab if you have extra time feel free to work through Sasha Rush'sTensor Puzzles

Next

Unit 2: Tensors, Operators, and Testing -Running and writing tests

I would love to contribute to PyTorch!

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp