Skip to content

[Humble][macOS] Build Failure in nav2_mppi_controller on macOS (Apple Clang xtensor template ambiguity) #6003

Closed

Description

@idesign0

Description

When building nav2_mppi_controller from source on macOS (Tahoe, Apple Silicon ARM64) for the ROS 2 Humble distribution, the build fails during compilation of optimizer.cpp and related motion model files.

The issue is triggered by the interaction between xtensor v0.25.0 and Apple Clang (17.0+), resulting in ambiguous template specialization errors.


The Problem

The core failure originates in xtensor's internal template metaprogramming, specifically when rebinding internal storage containers such as xt::svector.

Apple Clang cannot distinguish between long and unsigned long during partial template specialization, resulting in:


Upstream Reference (Ongoing Issue)

This build failure appears to be related to an existing xtensor issue concerning ambiguous partial template specializations on Apple Clang:

🔗 xtensor-stack/xtensor#2804


The Error

clang++: warning: argument unused during compilation: '-msse4.2' [-Wunused-command-line-argument]

In file included from <workspace>/src/navigation2/nav2_mppi_controller/src/optimizer.cpp:15:
In file included from <workspace>/src/navigation2/nav2_mppi_controller/include/nav2_mppi_controller/optimizer.hpp:21:
In file included from <install>/include/xtensor/xtensor.hpp:19:
In file included from <install>/include/xtensor/xbuffer_adaptor.hpp:21:
In file included from <install>/include/xtensor/xstorage.hpp:23:
In file included from <install>/include/xtensor/xtensor_simd.hpp:17:
<install>/include/xtensor/xutils.hpp:896:31: error: ambiguous partial specializations of 'rebind_container<long, xt::svector<unsigned long>>'
  using type = typename rebind_container<std::ptrdiff_t, S>::type;
                              ^

<install>/include/xtensor/xutils.hpp:883:12: note: partial specialization matches [with X = long, C = xt::svector, T = unsigned long, N = 4]
  struct rebind_container<X, C<T, N>>
         ^

<install>/include/xtensor/xstorage.hpp:1415:12: note: partial specialization matches [with X = long, T = unsigned long, N = 4, A = std::allocator<unsigned long>, B = true]
  struct rebind_container<X, svector<T, N, A, B>>
         ^

[...]

<install>/include/xtensor/xiterable.hpp:45:50: error: no type named 'stepper' in 
'xt::xiterable_inner_types<
  xt::xarray_container<
    xt::uvector<float, xsimd::aligned_allocator<float>>,
    xt::layout_type::row_major,
    xt::svector<unsigned long>
  >
>'

<install>/include/xtensor/xcontainer.hpp:140:15: error: using declaration refers into 
'accessible_base::', which is not a base class of 
'xcontainer<
  xt::xarray_container<
    xt::uvector<float, xsimd::aligned_allocator<float>>,
    xt::layout_type::row_major,
    xt::svector<unsigned long>
  >
>'

[...]

<install>/include/xtensor/xmath.hpp:2288:16: note: in instantiation of function template specialization 
'xt::accumulate<...>' requested here

<src>/navigation2/nav2_mppi_controller/src/optimizer.cpp:344:9: note: 
in instantiation of function template specialization 
'xt::cumsum<void, xt::xfunction<...>>' requested here
  xt::cumsum(state.wz * settings_.model_dt, 1) + initial_yaw;
      ^

<src>/navigation2/nav2_mppi_controller/src/optimizer.cpp:364:5: error: no matching function for call to 'cumsum'
  xt::cumsum(dx * settings_.model_dt, 1);
      ^

<src>/navigation2/nav2_mppi_controller/src/optimizer.cpp:366:5: error: no matching function for call to 'cumsum'
  xt::cumsum(dy * settings_.model_dt, 1);
      ^

14 errors generated.
make[2]: *** [CMakeFiles/mppi_controller.dir/src/optimizer.cpp.o] Error 1
make[1]: *** [CMakeFiles/mppi_controller.dir/all] Error 2
make: *** [all] Error 2
make: INTERNAL: Exiting with 9 jobserver tokens available; should be 8!

Affected Components

  • xt::cumsum
    • Fails during trajectory integration.
  • xt::masked_view
    • Breaks in vehicle constraint applications.
  • xt::xarray
    • Recursive template instantiation failures due to dynamic rank deduction.

Root Cause Analysis

1️⃣ Type Ambiguity on macOS

On Darwin systems, size_t may resolve to types that match multiple template specializations simultaneously (long vs unsigned long).

2️⃣ Dynamic Rank Complexity

xt::xarray relies on runtime dimensionality tracking, increasing template recursion depth and exposing Clang’s strict specialization resolution behavior.

3️⃣ Apple Clang Strictness

Apple Clang enforces stricter template specialization resolution compared to GCC, exposing ambiguity in xtensor 0.25.0.


Temp Solution

Commit: idesign0@edce854

Environment

  • ROS 2 Distro: Humble
  • OS: macOS 26.3 (25D125) — Apple Silicon (ARM64)
  • Compiler: Apple Clang 17.0.0 (clang-1700.6.4.2)
  • Target: arm64-apple-darwin25.3.0
  • Toolchain: XcodeDefault.xctoolchain
  • xtensor Version: 0.25.0

Activity

  1. changed the title [-][Humble] Build Failure in nav2_mppi_controller on macOS (Apple Clang xtensor template ambiguity)[/-] [+][Humble][macOS] Build Failure in nav2_mppi_controller on macOS (Apple Clang xtensor template ambiguity)[/+] on Mar 4, 2026
  2. SteveMacenski commented on Mar 4, 2026

    @SteveMacenski
    Member

    Thanks for the report. Sounds like it may have been fixed in xtensor-stack/xtensor#2833 but I assume that's not the apt version (nor going to be released on your OS? Not sure).

    In terms of your fixes: have you done apples to apples benchmark to see what the impact on performance is?

    What I'd suggest, if you need this on Apple hardware:

    • Ifdef the work on nav2_mppi_controller/include/nav2_mppi_controller/motion_models.hpp checking for apple -- unless your method is actually faster either way (then we can just convert to yours)
    • Understand the performance differences from nav2_mppi_controller/include/nav2_mppi_controller/tools/utils.hpp, which I assume are negligible. If so, we can keep those suggested changes.
    • Create a new apple-named header for your custom cumsum methods. Then in the optimizer typedef to use which depending on the hardware present. Use that typedef in the code.

    That would be the least intrusive solution in my opinion :-)

  3. idesign0 commented on Mar 4, 2026

    @idesign0
    ContributorAuthor

    @SteveMacenski

    Thank you for the detailed suggestions.

    I haven’t performed any performance benchmarking yet, so I can’t comment on the performance impact of the changes at this stage. I’ll incorporate the modifications as you suggested.

    I haven’t done structured performance benchmarking in this context before, so any guidance on recommended tools, metrics, or benchmarking methodology for nav2_mppi_controller would be greatly appreciated.

    I’ll get back to you with results and further observations.

  4. SteveMacenski commented on Mar 4, 2026

    @SteveMacenski
    Member

    If you add some timers for individual functions that can help. Otherwise full optimization run timers are builtin and just need to uncomment this line https://github.com/ros-navigation/navigation2/blob/main/nav2_mppi_controller/src/controller.cpp#L20 to print results (still need to capture/get some statistics from them though).

    This is highly optimized on the compiler, so doing debug mode with gdb will not yield meaningful metrics. Calling your version and the default version in a loop of 1000000 times and recording the times is as good as anything.

  5. idesign0 commented on Mar 5, 2026

    @idesign0
    ContributorAuthor

    @SteveMacenski

    Update: Performance Benchmarks and Refactored Implementation

    Following the suggestion for a "least intrusive" fix, I have refactored the temp solution to isolate Apple-specific workarounds and verified the performance impact.

    Commit: idesign0@87f9c4d

    Summary of Changes

    1. New Isolated Header: Created apple_utils.hpp containing manual cumsum implementations to bypass the xtensor template ambiguity on macOS.
    2. Conditional Compilation: Used #ifdef __APPLE__ in optimizer.cpp, motion_models.hpp, and utils.hpp. This ensures the Linux/GCC build remains completely untouched, maintaining the original xarray and xt::cumsum paths for non-Apple systems.
    3. Type Stabilization: In utils.hpp, I applied fixed-rank xtensor<float, 1> strictly for Apple builds to assist the Clang compiler with type deduction, while keeping xarray for all other platforms.

    Performance Results (macOS ARM64)

    I ran an "apples-to-apples" micro-benchmark comparing the manual implementation against xt::cumsum using a 100-step horizon over 1,000,000 iterations in Release mode (-O3).

    On the Apple Clang compiler, the manual loops significantly outperform the library templates:

    Operation Original xtensor Manual (My Fix) Improvement
    1D Cumsum (Size 100) 0.367784 µs 0.13083 µs ~35% faster
    2D Cumsum (100x3) 0.509195 µs 0.218972 µs ~43% faster

    Benchmark Compilation & Script

    The benchmark was compiled using the following command:

    clang++ -O3 -std=c++17 benchmark.cpp \
      -I ~/humble-ros2/install/include \
      -I ./include \
      -o bench

    The following script was used to generate these metrics:

    #include <chrono>
    #include <iostream>
    #include <xtensor/xtensor.hpp>
    #include <xtensor/xmath.hpp> 
    #include "nav2_mppi_controller/tools/apple_utils.hpp"
    
    void benchmark_1d_manual(const size_t size, const int iterations) {
        xt::xtensor<float, 1> data = xt::ones<float>({size}) * 0.1f;
        auto start = std::chrono::high_resolution_clock::now();
        float sink = 0.0f;
        for (int i = 0; i < iterations; ++i) {
            auto res = mppi::utils::manual_cumsum_1d(data);
            sink += res(size - 1);
        }
        auto end = std::chrono::high_resolution_clock::now();
        std::cout << "1D Manual:   " << (std::chrono::duration<double>(end-start).count() / iterations) * 1e6 << " us" << std::endl;
    }
    
    void benchmark_1d_original(const size_t size, const int iterations) {
        xt::xtensor<float, 1> data = xt::ones<float>({size}) * 0.1f;
        auto start = std::chrono::high_resolution_clock::now();
        float sink = 0.0f;
        for (int i = 0; i < iterations; ++i) {
            auto res = xt::cumsum(data, 0); 
            sink += res(size - 1);
        }
        auto end = std::chrono::high_resolution_clock::now();
        std::cout << "1D Original: " << (std::chrono::duration<double>(end-start).count() / iterations) * 1e6 << " us" << std::endl;
    }
    
    void benchmark_2d_manual(const size_t rows, const size_t cols, const int iterations) {
        xt::xtensor<float, 2> data = xt::ones<float>({rows, cols}) * 0.1f;
        auto start = std::chrono::high_resolution_clock::now();
        float sink = 0.0f;
        for (int i = 0; i < iterations; ++i) {
            auto res = mppi::utils::manual_cumsum_2d(data, 0);
            sink += res(rows - 1, cols - 1);
        }
        auto end = std::chrono::high_resolution_clock::now();
        std::cout << "2D Manual:   " << (std::chrono::duration<double>(end-start).count() / iterations) * 1e6 << " us" << std::endl;
    }
    
    void benchmark_2d_original(const size_t rows, const size_t cols, const int iterations) {
        xt::xtensor<float, 2> data = xt::ones<float>({rows, cols}) * 0.1f;
        auto start = std::chrono::high_resolution_clock::now();
        float sink = 0.0f;
        for (int i = 0; i < iterations; ++i) {
            auto res = xt::cumsum(data, 0);
            sink += res(rows - 1, cols - 1);
        }
        auto end = std::chrono::high_resolution_clock::now();
        std::cout << "2D Original: " << (std::chrono::duration<double>(end-start).count() / iterations) * 1e6 << " us" << std::endl;
    }
    
    int main() {
        const int iterations = 1000000;
        const size_t size = 100;
        benchmark_1d_manual(size, iterations);
        benchmark_1d_original(size, iterations);
        benchmark_2d_manual(size, 3, iterations);
        benchmark_2d_original(size, 3, iterations);
        return 0;
    }
  6. SteveMacenski commented on Mar 6, 2026

    @SteveMacenski
    Member
      #ifdef __APPLE__
        xt::xtensor<float, 1> filter = {-21.0, 14.0, 39.0, 54.0, 59.0, 54.0, 39.0, 14.0, -21.0};
      #else
        xt::xarray<float> filter = {-21.0, 14.0, 39.0, 54.0, 59.0, 54.0, 39.0, 14.0, -21.0};
      #endif
    

    You may simply change to use <float, 1> if it improves / remains the same performance (which I suspect one of the two is the case). You don't need to make it apple-specific.

      #ifdef __APPLE__
        xt::noalias(traj_yaws) = utils::manual_cumsum_1d(wz * settings_.model_dt) + initial_yaw;
      #else
        xt::noalias(traj_yaws) = xt::cumsum(wz * settings_.model_dt, 0) + initial_yaw;
      #endif
    

    Can you move into the header of the optimizer a typedef for cumsum where we set one vs the other to use, rather than putting this into the code itself (in many places)?

    Otherwise, can you open a PR for all of this? I tihnk that's it and I can merge that 👍

    How did you test it if it doesn't compile on macOS?

  7. idesign0 commented on Mar 7, 2026

    @idesign0
    ContributorAuthor

    @SteveMacenski

    Thanks for the suggestions; I will make the changes.

    Regarding testing, you are correct—the full project does not compile with xt::cumsum on macOS. I obtained the original benchmark figures using an isolated script that bypasses the complex template headers of the full Nav2 environment. You can review the details under the Benchmark Compilation & Script section of my previous response.

    i will make the PR and Target the humble branch soon.

  8. SteveMacenski commented on Mar 7, 2026

    @SteveMacenski
    Member

    It needs to target the main branch as well if you want this reflected in new ROS distros over time. Its OK to also have humble, but main’s important as well

  9. idesign0 commented on Mar 7, 2026

    @idesign0
    ContributorAuthor

    @SteveMacenski

    Thanks for the clarification.

    From looking at the code, it seems the part I’m modifying (related to the xtensor dependency) isn’t present in newer distros like Kilted. As I also manage Kilted for macOS, I don’t see any issue there with nav2_mppi_controller.

    I did try targeting main, but GitHub showed a “merge not possible” message in the PR dialog. I’ll check main again and see what adjustments are needed so it can be targeted there as well.

  10. SteveMacenski commented on Mar 9, 2026

    @SteveMacenski
    Member

    Oh got it - for the MPPI changes we use Eigen now so that may not be required at all then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions