Compiler error with torch compile example

I am getting a compiler error using torch.compile for with the example given in the docs. I am using wheel distributions of torch and related deps, though doing some manual install like unzipping the wheel files, due to some constraints with our build system. So it’s probable the issue is related to my install and I would appreciate pointers as to what might be going wrong.

Repro code:

def foo(x, y):
    a = torch.sin(x)
    b = torch.cos(y)
    return a + b
opt_foo1 = torch.compile(foo)
print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))

Error:

  File "<redacted>/tools/performance_test/compile_test.py", line 7, in <module>
    print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 451, in _fn
    return fn(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 921, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state, skip=1)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 786, in _convert_frame
    result = inner_convert(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 400, in _convert_frame_assert
    return _compile(
  File "<redacted>/python3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 676, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 535, in compile_inner
    out_code = transform_code_object(code, transform)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/bytecode_transformation.py", line 1036, in transform_code_object
    transformations(instructions, code_options)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 165, in _fn
    return fn(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 500, in transform
    tracer.run()
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 2149, in run
    super().run()
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 810, in run
    and self.step()
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 773, in step
    getattr(self, inst.opname)(inst)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 2268, in RETURN_VALUE
    self.output.compile_subgraph(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 981, in compile_subgraph
    self.compile_and_call_fx_graph(tx, list(reversed(stack_values)), root)
  File "<redacted>/python3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 1178, in compile_and_call_fx_graph
    compiled_fn = self.call_user_compiler(gm)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 1251, in call_user_compiler
    raise BackendCompilerFailed(self.compiler_fn, e).with_traceback(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 1232, in call_user_compiler
    compiled_fn = compiler_fn(gm, self.example_inputs())
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/repro/after_dynamo.py", line 117, in debug_wrapper
    compiled_gm = compiler_fn(gm, example_inputs)
  File "<redacted>/lib/python3.8/site-packages/torch/__init__.py", line 1731, in __call__
    return compile_fx(model_, inputs_, config_patches=self.config)
  File "<redacted>/python3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/compile_fx.py", line 1330, in compile_fx
    return aot_autograd(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/backends/common.py", line 58, in compiler_fn
    cg = aot_module_simplified(gm, example_inputs, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_functorch/aot_autograd.py", line 903, in aot_module_simplified
    compiled_fn = create_aot_dispatcher_function(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_functorch/aot_autograd.py", line 628, in create_aot_dispatcher_function
    compiled_fn = compiler_fn(flat_fn, fake_flat_args, aot_config, fw_metadata=fw_metadata)
  File "<redacted>/lib/python3.8/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 443, in aot_wrapper_dedupe
    return compiler_fn(flat_fn, leaf_flat_args, aot_config, fw_metadata=fw_metadata)
  File "<redacted>/lib/python3.8/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 648, in aot_wrapper_synthetic_base
    return compiler_fn(flat_fn, flat_args, aot_config, fw_metadata=fw_metadata)
  File "<redacted>/lib/python3.8/site-packages/torch/_functorch/_aot_autograd/jit_compile_runtime_wrappers.py", line 119, in aot_dispatch_base
    compiled_fw = compiler(fw_module, updated_flat_args)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/compile_fx.py", line 1257, in fw_compiler_base
    return inner_compile(
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/repro/after_aot.py", line 83, in debug_wrapper
    inner_compiled_fn = compiler_fn(gm, example_inputs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/debug.py", line 304, in inner
    return fn(*args, **kwargs)
  File "<redacted>/python3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<redacted>/python3.8/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/compile_fx.py", line 438, in compile_fx_inner
    compiled_graph = fx_codegen_and_compile(
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/compile_fx.py", line 714, in fx_codegen_and_compile
    compiled_fn = graph.compile_to_fn()
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/graph.py", line 1307, in compile_to_fn
    return self.compile_to_module().call
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/graph.py", line 1254, in compile_to_module
    mod = PyCodeCache.load_by_key_path(
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/codecache.py", line 2160, in load_by_key_path
    exec(code, mod.__dict__, mod.__dict__)
  File "/tmp/torchinductor_matthost/xr/cxr2uq5kkpkl4ba3ol5ldz7tb6kormb3wkmhsdzih7ocm5mif5xp.py", line 59, in <module>
    async_compile.wait(globals())
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/codecache.py", line 2715, in wait
    scope[key] = result.result()
  File "<redacted>/python3.8/lib/python3.8/concurrent/futures/_base.py", line 444, in result
    return self.__get_result()
  File "<redacted>/python3.8/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "<redacted>/python3.8/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/codecache.py", line 2074, in load_pybinding
    result = cls.load(source_code + suffix, cuda)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/codecache.py", line 1948, in load
    compile_file(input_path, output_path, cmd)
  File "<redacted>/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 262, in time_wrapper
    r = func(*args, **kwargs)
  File "<redacted>/lib/python3.8/site-packages/torch/_inductor/codecache.py", line 1888, in compile_file
    raise exc.CppCompileError(cmd, output) from e
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
CppCompileError: C++ compile error

Command:
g++ /tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp -shared -fPIC -Wall -std=c++17 -Wno-unused-variable -Wno-unknown-pragmas -D_GLIBCXX_USE_CXX11_ABI=0 -I<redacted>/lib/python3.8/site-packages/torch/include -I<redacted>/lib/python3.8/site-packages/torch/include/torch/csrc/api/include -I<redacted>/lib/python3.8/site-packages/torch/include/TH -I<redacted>/lib/python3.8/site-packages/torch/include/THC -I<redacted>/python3.8/include/python3.8 -L<redacted>/lib/python3.8/site-packages/torch/lib -L<redacted>/python3.8/lib -L<redacted>/lib/python3.8/site-packages/torch/lib -ltorch -ltorch_cpu -lgomp -ltorch_python -lc10 -mavx2 -mfma -DCPU_CAPABILITY_AVX2 -O3 -DNDEBUG -ffast-math -fno-finite-math-only -fno-unsafe-math-optimizations -ffp-contract=off -march=native -fopenmp -D C10_USING_CUSTOM_GENERATED_MACROS -o /tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.so

Output:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In function ‘T parse_arg(PyObject*, size_t) [with T = long int; PyObject = _object; size_t = long unsigned int]’:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:47:10: error: expected identifier before ‘[’ token
         [[unlikely]] throw std::runtime_error("expected int arg");
          ^
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In lambda function:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:47:22: error: expected ‘{’ before ‘throw’
         [[unlikely]] throw std::runtime_error("expected int arg");
                      ^~~~~
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In function ‘T parse_arg(PyObject*, size_t) [with T = long int; PyObject = _object; size_t = long unsigned int]’:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:47:22: error: expected ‘;’ before ‘throw’
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In function ‘PyObject* kernel_py(PyObject*, PyObject*)’:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:56:14: error: expected identifier before ‘[’ token
             [[unlikely]] throw std::runtime_error("tuple args required");
              ^
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In lambda function:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:56:26: error: expected ‘{’ before ‘throw’
             [[unlikely]] throw std::runtime_error("tuple args required");
                          ^~~~~
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In function ‘PyObject* kernel_py(PyObject*, PyObject*)’:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:56:26: error: expected ‘;’ before ‘throw’
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:58:14: error: expected identifier before ‘[’ token
             [[unlikely]] throw std::runtime_error("requires 3 args");
              ^
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In lambda function:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:58:26: error: expected ‘{’ before ‘throw’
             [[unlikely]] throw std::runtime_error("requires 3 args");
                          ^~~~~
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp: In function ‘PyObject* kernel_py(PyObject*, PyObject*)’:
/tmp/torchinductor_matthost/gh/cgh4k4j6gbdzid4qngg3a7qoima5mxmrpouen6fpu3rzi3szqnga.cpp:58:26: error: expected ‘;’ before ‘throw’

@matthost it worked for me… Can you give the pytorch version that you are using?

I believe this to be an error when c++20 compilation is not available. There was a PR related to fixing it but it was not in the 2.3.1 branch. CppCompileError with tutorial example · Issue #130389 · pytorch/pytorch · GitHub