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

Cpp API Quick Walkthrough

Manuel edited this pageJul 3, 2024 ·2 revisions

C++ API quick code walkthrough

PyTorch, but in C++

#include <torch/csrc/autograd/variable.h>#include <torch/csrc/autograd/function.h>torch::Tensor a = torch::ones({2, 2}, torch::requires_grad());torch::Tensor b = torch::randn({2, 2});auto c = a + b;c.sum().backward(); // a.grad() will now hold the gradient of c w.r.t. a.

Operators

Come straight from the at:: namespace.There is ausing namespace at somewhere.

E.g., at::add, torch::add are the same thing

Modules

Mnist example: https://pytorch.org/cppdocs/frontend.html#end-to-end-example

C++ Modules are not implemented the same way as they are in Python but we try to reproduce their behavior/APIs as much as possible.

Optimizers

Optimizer interfaceSGD as example

Other utilities exist...

DataLoader:https://github.com/pytorch/pytorch/blob/5d82311f0d6411fd20f1ce59b80f8fd569a26a67/torch/csrc/api/include/torch/data/dataloader.h#L19-L56. But I’m not sure how different this is from the Python dataloader.

C++ Extensions

Read through:https://pytorch.org/tutorials/advanced/cpp_extension.html

Why?

  • Let’s say you wanted to write a custom CPU or CUDA kernel for some operation in C++, and hook it up to the PyTorch frontend.
  • You can write your own setuptools Python extension, or you can use the PyTorch C++ extensions API.

There are two types of extensions, really:

Things like TorchVision use C++ extensions to add new kernels in their packages.

Next

Unit 5: torch.nn -Modules Lab

I would love to contribute to PyTorch!

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp