{"id":15157665,"url":"https://github.com/pytorch/extension-script","last_synced_at":"2025-09-30T07:30:44.356Z","repository":{"id":47729799,"uuid":"159563524","full_name":"pytorch/extension-script","owner":"pytorch","description":"Example repository for custom C++/CUDA operators for TorchScript","archived":true,"fork":false,"pushed_at":"2022-08-28T02:26:07.000Z","size":14,"stargazers_count":113,"open_issues_count":7,"forks_count":23,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-09-27T20:03:21.589Z","etag":null,"topics":["cpp","cuda","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pytorch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-28T20:54:55.000Z","updated_at":"2024-09-19T16:17:41.000Z","dependencies_parsed_at":"2022-09-05T04:01:08.498Z","dependency_job_id":null,"html_url":"https://github.com/pytorch/extension-script","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytorch%2Fextension-script","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytorch%2Fextension-script/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytorch%2Fextension-script/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pytorch%2Fextension-script/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pytorch","download_url":"https://codeload.github.com/pytorch/extension-script/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234712845,"owners_count":18875521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cuda","pytorch"],"created_at":"2024-09-26T20:01:39.014Z","updated_at":"2025-09-30T07:30:39.085Z","avatar_url":"https://github.com/pytorch.png","language":"Python","readme":"# Custom TorchScript Operators Example\n\nThis repository contains examples for writing, compiling and using custom\nTorchScript operators. See\n[here](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html)\nfor the accompanying tutorial.\n\n## Contents\n\nThere a few monuments in this repository you can visit. They are described in\ncontext in the tutorial, which you are encouraged to read. These monuments are:\n\n- `example_app/warp_perspective/op.cpp`: The custom operator implementation,\n- `example_app/main.cpp`: An example application that loads and executes a serialized TorchScript model, which uses the custom operator, in C++,\n- `script.py`: Example of using the custom operator in a scripted model,\n- `trace.py`: Example of using the custom operator in a traced model,\n- `eager.py`: Example of using the custom operator in vanilla eager PyTorch,\n- `load.py`: Example of using `torch.utils.cpp_extension.load` to build the custom operator,\n- `load.py`: Example of using `torch.utils.cpp_extension.load_inline` to build the custom operator,\n- `setup.py`: Example of using setuptools to build the custom operator,\n- `test_setup.py`: Example of using the custom operator built using `setup.py`.\n\nTo execute the C++ application, first run `script.py` to serialize a TorchScript\nmodel to a file called `example.pt`, then pass that file to the\n`example_app/build/example_app` binary.\n\n## Setup\n\nFor the smoothest experience when trying out these examples, we recommend\nbuilding a docker container from this repository's `Dockerfile`. This will give\nyou a clean, isolated Ubuntu Linux environment in which we guarantee everything\nto work perfectly. These steps should get you started:\n\n```sh\n$ git clone https://github.com/pytorch/extension-script\n\n$ cd extension-script\n\n$ docker build -t extension-script .\n\n$ docker run -v $PWD:/home -it extension-script\n\n$ root@2f00feefe46a:/home# source /activate # Activate the Conda environment\n\n$ cd example_app \u0026\u0026 mkdir build \u0026\u0026 cd build\n\n$ cmake -DCMAKE_PREFIX_PATH=/libtorch ..\n-- The C compiler identification is GNU 5.4.0\n-- The CXX compiler identification is GNU 5.4.0\n-- Check for working C compiler: /usr/bin/cc\n-- Check for working C compiler: /usr/bin/cc -- works\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Check for working CXX compiler: /usr/bin/c++\n-- Check for working CXX compiler: /usr/bin/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Looking for pthread.h\n-- Looking for pthread.h - found\n-- Looking for pthread_create\n-- Looking for pthread_create - not found\n-- Looking for pthread_create in pthreads\n-- Looking for pthread_create in pthreads - not found\n-- Looking for pthread_create in pthread\n-- Looking for pthread_create in pthread - found\n-- Found Threads: TRUE\n-- Found torch: /libtorch/lib/libtorch.so\n-- Configuring done\n-- Generating done\n-- Build files have been written to: /home/example_app/build\n\n$ make -j\nScanning dependencies of target warp_perspective\n[ 25%] Building CXX object warp_perspective/CMakeFiles/warp_perspective.dir/op.cpp.o\n[ 50%] Linking CXX shared library libwarp_perspective.so\n[ 50%] Built target warp_perspective\nScanning dependencies of target example_app\n[ 75%] Building CXX object CMakeFiles/example_app.dir/main.cpp.o\n[100%] Linking CXX executable example_app\n[100%] Built target example_app\n```\n\nThis will create a shared library under\n`/home/example_app/build/warp_perspective/libwarp_perspective.so` containing the\ncustom operator defined in `example_app/warp_perspective/op.cpp`. Then, you can\nrun the examples, e.g.:\n\n```sh\n(base) root@2f00feefe46a:/home# python script.py\ngraph(%x.1 : Dynamic\n      %y : Dynamic) {\n  %20 : int = prim::Constant[value=1]()\n  %16 : int[] = prim::Constant[value=[0, -1]]()\n  %14 : int = prim::Constant[value=6]()\n  %2 : int = prim::Constant[value=0]()\n  %7 : int = prim::Constant[value=42]()\n  %z.1 : int = prim::Constant[value=5]()\n  %z.2 : int = prim::Constant[value=10]()\n  %13 : int = prim::Constant[value=3]()\n  %4 : Dynamic = aten::select(%x.1, %2, %2)\n  %6 : Dynamic = aten::select(%4, %2, %2)\n  %8 : Dynamic = aten::eq(%6, %7)\n  %9 : bool = prim::TensorToBool(%8)\n  %z : int = prim::If(%9)\n    block0() {\n      -\u003e (%z.1)\n    }\n    block1() {\n      -\u003e (%z.2)\n    }\n  %17 : Dynamic = aten::eye(%13, %14, %2, %16)\n  %x : Dynamic = my_ops::warp_perspective(%x.1, %17)\n  %19 : Dynamic = aten::matmul(%x, %y)\n  %21 : Dynamic = aten::add(%19, %z, %20)\n  return (%21);\n}\n\ntensor([[11.6196, 12.0056, 11.6122, 12.9298,  7.0649],\n        [ 8.5063,  9.0621,  9.9925,  6.3741,  8.9668],\n        [12.5898,  6.5872,  8.1511, 10.0806, 11.9829],\n        [ 4.9142, 11.6614, 15.7161, 17.0538, 11.7243],\n        [10.0000, 10.0000, 10.0000, 10.0000, 10.0000],\n        [10.0000, 10.0000, 10.0000, 10.0000, 10.0000],\n        [10.0000, 10.0000, 10.0000, 10.0000, 10.0000],\n        [10.0000, 10.0000, 10.0000, 10.0000, 10.0000]])\n```\n","funding_links":[],"categories":["Pytorch elsewhere ｜ Pytorch相关","Pytorch elsewhere"],"sub_categories":["Other libraries｜其他库:","Other libraries:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytorch%2Fextension-script","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpytorch%2Fextension-script","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytorch%2Fextension-script/lists"}