Skip to main content

A Simple Pytorch version for CFU

Project description

torch-cfu

This project is for adding custom function unit to accelerate AI algorithm.

How to install

You can install it by pip for the latest version. And assume that you have installed torch before.

pip install torch-cfu

How to use

First you need to 'import torch', then 'import torch_cfu'. And you are able to rename default device name to whatever you want.

$ python
Python 3.8.18 (default, Sep 11 2023, 13:40:15) 
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torch_cfu
>>> torch_cfu.custom_device(0)
device(type='privateuseone', index=0)
>>> torch.utils.rename_privateuse1_backend("cfu")
>>> torch_cfu.custom_device(0)
device(type='cfu', index=0)
>>>
>>> x = torch.ones(4, 4, device='cfu:0')
Custom aten::empty.memory_format() called!
Custom allocator's allocate() called!
>>> x.device
device(type='cfu', index=0)
>>> x.is_cpu
False
>>> exit()
Custom allocator's delete() called!

x = torch.randn(3, 3) y = torch.randn(3, 3)

Assuming the custom operation is registered in a library called 'myops'

result = myops.myadd(x, y) print(result)

How to add new custom kernels in C++

  1. Writing custom kernels in C++, and registering them to the PyTorch dispatcher
  2. Providing a user API for your custom device, so users can invoke the custom code using torch.foo(..., device="custom_device")
  3. Registering a custom memory allocator
  4. Registering a custom device guard

Defining schema and backend implementations

TORCH_LIBRARY_IMPL(myops, CPU, m) {
  m.impl("myadd", myadd_cpu);
}

Reference

python setup.py sdist bdist_wheel
twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_cfu-0.0.8.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file torch_cfu-0.0.8.tar.gz.

File metadata

  • Download URL: torch_cfu-0.0.8.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/65.3.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for torch_cfu-0.0.8.tar.gz
Algorithm Hash digest
SHA256 f37dcf6f9b438066484a331426f7f46689bdccb2a53a7cc186d66ba39daffffd
MD5 ed12a2a65165a8edc0770cf8bd12f7f0
BLAKE2b-256 dd638ccc874315535cf0c3ff1e3b76ce7a5f67b788c46177c7f1736e35a2162f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page