From torch.utils.ffi import _wrap_function replacement

This has been deprecated, what is the altenate?

    from torch.utils.ffi import _wrap_function
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/ffi/__init__.py", line 1, in <module>
    raise ImportError("torch.utils.ffi is deprecated. Please use cpp extensions instead.")
ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.

cpp extensions but it’s not a drop-in replacement.

Best regards

Thomas

Continuing the discussion from From torch.utils.ffi import _wrap_function replacement:

Thank u for your reply. I already change into cpp extension, but the older codes has some wrapper using this function. It seems the old code can not word anymore.

But I need a wrapper for some cuda extensions, here is my attempt

"""
this file build extensions under src/

"""
import os
import torch
# from torch.utils.ffi import create_extension
from torch.utils.cpp_extension import BuildExtension, CppExtension
from setuptools import setup, find_packages

sources = ['src/lib_cffi.cpp', 'src/bn.cu']
include_dirs = [os.path.realpath('./src')]
print('include dir: ', include_dirs)

extra_objects = ['src/bn.o']
with_cuda = True

this_file = os.path.dirname(os.path.realpath(__file__))
extra_objects = [os.path.join(this_file, fname) for fname in extra_objects]

if torch.cuda.is_available():
    enable_gpu = True
else:
    enable_gpu = False

if enable_gpu:
    from torch.utils.cpp_extension import CUDAExtension
    build_extension = CUDAExtension
else:
    build_extension = CppExtension

# ffi = create_extension(
#     '_ext',
#     headers=headers,
#     sources=sources,
#     relative_to=__file__,
#     with_cuda=with_cuda,
#     extra_objects=extra_objects,
#     extra_compile_args=["-std=c++11"]
# )

# if __name__ == '__main__':
#     ffi.build()

ext_modules = [
    build_extension(
        '_ext',
        include_dirs=include_dirs,
        sources=sources,
        extra_objects=extra_objects,
        extra_compile_args=["-std=c++11"]
    )
]


setup(
    name='ext',
    version='0.1',
    packages=find_packages(),
    ext_modules=ext_modules,
    cmdclass={'build_ext': BuildExtension}
)

I have these files under src:

bn.cu
bn.h
lib_cffi.cpp
lib_cffi.h

How should I write a wrapper to access them? I tried the setup.py, it got nothing to import in python…

Any suggestions? Here is the source locate:

What kind of error do you get?

You also need to port your C code from TH/THC to ATen.
I’d recommend to get it working using the just in time extension compilation first and then proceed to the setup.py. In the tutorial linked above, the mechanism to compile the module there is:

from torch.utils.cpp_extension import load

lltm = load(name='lltm', sources=['lltm_cuda.cpp', 'lltm_cuda_kernel.cu'])

Once that works, moving to setup.py should be straightforward.

Best regards

Thomas

1 Like