Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] Binary operator cannot be included from 3rd party projects in C++ #11883

Closed
marty1885 opened this issue Aug 25, 2024 · 7 comments
Closed
Labels
bug Something isn't working community

Comments

@marty1885
Copy link
Contributor

Describe the bug
I've been manually silencing the check as it initially think it's a bug on my side. After some look, I think it's actually a problem in TTNN. Whenever binary_device_operation.hpp is included from an outside C++ project.

/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp: In instantiation of ‘consteval void ttnn::decorators::assert_operation_in_correct_namespace() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation]’:
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:360:81:   required from ‘constexpr auto ttnn::decorators::register_operation_impl() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation; bool auto_launch_op = false]’
  360 |     assert_operation_in_correct_namespace<cpp_fully_qualified_name, operation_t>();
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:371:81:   required from ‘constexpr auto ttnn::decorators::register_operation() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation]’
  371 |     return register_operation_impl<cpp_fully_qualified_name, operation_t, false>();
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/device/binary_device_operation.hpp:232:120:   required from here
  232 | constexpr auto binary = ttnn::register_operation<"ttnn::prim::binary", ttnn::operations::binary::BinaryDeviceOperation>();
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:67: error: static assertion failed: Primitive operations must be in the `ttnn::prim` namespace.
  344 |             static_assert(tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace), "Primitive operations must be in the `ttnn::prim` namespace.");
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:67: note: ‘tt::stl::reflection::fixed_string_equals<char, 11, char, 10>(namespace_substring, ttnn::decorators::prim_namespace)’ evaluates to false

This is the minimal example that replicates the problem on my computer

#include <cstddef>
#include <ttnn/operations/eltwise/binary/binary.hpp>

int main()
{
}

The root cause seems to be the operator is put in the ttnn::prim::binary namesapce but the checker wants ttnn::prim.

To Reproduce
Steps to reproduce the behavior:

  1. Compile the provided example
  2. See error

Expected behavior
Should compile successfully.

Screenshots
If applicable, add screenshots to help explain your problem.

Please complete the following environment information:

  • OS: Arch Linux
  • Compiler: GCC 14 / Clang 18
  • Commit: 259daa2

Additional context
Add any other context about the problem here.

@marty1885 marty1885 added the bug Something isn't working label Aug 25, 2024
@ayerofieiev-tt
Copy link
Member

Hi @marty1885 !
Thank you for the report!

From what I see, the operation is registered in the right namespace, so that’s not the cause.

Let me take a deeper look later today. I will follow up here.

@dmakoviichuk-tt
Copy link
Contributor

I'd try to switch some includes order for example include binary_device_operation first to check if we see the same issue.

@ayerofieiev-tt
Copy link
Member

I can't reproduce locally with lower versions of clang.

@marty1885 can you please extend the code so we can see what compiler extracted?

Something like this:

template <reflect::fixed_string cpp_fully_qualified_name, typename operation_t>
consteval void assert_operation_in_correct_namespace() {
    constexpr auto prim_namespace = "ttnn::prim"; // Assuming this is defined somewhere

    if constexpr (PrimitiveOperationConcept<operation_t>) {
        if constexpr(cpp_fully_qualified_name.size() > sizeof(prim_namespace)) {
            constexpr auto namespace_substring = tt::stl::reflection::fixed_string_substring<0, sizeof(prim_namespace)>(cpp_fully_qualified_name);
            static_assert(tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace), 
                          "Primitive operations must be in the `ttnn::prim` namespace. "
                          "Actual namespace: " + namespace_substring.value);
        } else {
            #ifndef DISABLE_NAMESPACE_STATIC_ASSERT
            static_assert(false, "Primitive operations must be in the `ttnn::prim` namespace.");
            #endif
        }
    } else {
        if constexpr (cpp_fully_qualified_name.size() > sizeof(prim_namespace)) {
            constexpr auto namespace_substring = tt::stl::reflection::fixed_string_substring<0, sizeof(prim_namespace)>(cpp_fully_qualified_name);
            static_assert(!tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace),
                          "Composite operations must not be in the `ttnn::prim` namespace. "
                          "Actual namespace: " + namespace_substring.value);
        }
    }
}

@marty1885
Copy link
Contributor Author

@ayerofieiev-tt I've uploaded a new repo that can replicate the issue on my end. https://github.com/marty1885/ttnn-binary-error-demo

This is the full build log but I don't think it contains any more information. I also tried both libc++ and stdlibc++. Both failing for me.

With GCC 14

➜  build git:(master) make    
[ 50%] Building CXX object CMakeFiles/ttnn-compile-error.dir/main.cpp.o
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/build.hpp:12,
                 from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/device/device.hpp:17,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/types.hpp:16,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor.hpp:19,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:10,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:10,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8,
                 from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/utils.hpp: In lambda function:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/utils.hpp:48:50: warning: implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]
   48 |                 threads.emplace_back(std::thread([=]() {
      |                                                  ^
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/utils.hpp:48:50: note: add explicit ‘this’ or ‘*this’ capture
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor_impl.hpp:17,
                 from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:11:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/dispatch/command_queue.hpp: At global scope:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/dispatch/command_queue.hpp:497:5: warning: ‘volatile’-qualified return type is deprecated [-Wvolatile]
  497 |     volatile bool is_dprint_server_hung();
      |     ^~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/dispatch/command_queue.hpp:498:5: warning: ‘volatile’-qualified return type is deprecated [-Wvolatile]
  498 |     volatile bool is_noc_hung();
      |     ^~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:319:77: warning: friend declaration ‘consteval auto ttnn::decorators::get(operation_name_key_t<cpp_fully_qualified_name>)’ declares a non-template function [-Wnon-template-friend]
  319 |     friend consteval auto get(operation_name_key_t<cpp_fully_qualified_name>);
      |                                                                             ^
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:319:77: note: (if this is not what you intended, make sure the function template has already been declared and add ‘<>’ after the function name here)
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:324:59: warning: friend declaration ‘consteval auto ttnn::decorators::get(operation_key_t<operation_t>)’ declares a non-template function [-Wnon-template-friend]
  324 |     friend consteval auto get(operation_key_t<operation_t>);
      |                                                           ^
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp: In instantiation of ‘consteval void ttnn::decorators::assert_operation_in_correct_namespace() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation]’:
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:360:81:   required from ‘constexpr auto ttnn::decorators::register_operation_impl() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation; bool auto_launch_op = false]’
  360 |     assert_operation_in_correct_namespace<cpp_fully_qualified_name, operation_t>();
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:371:81:   required from ‘constexpr auto ttnn::decorators::register_operation() [with fixed_string<...auto...> cpp_fully_qualified_name = reflect::v1_1_1::fixed_string<char, 18>{"ttnn::prim::binary"}; operation_t = ttnn::operations::binary::BinaryDeviceOperation]’
  371 |     return register_operation_impl<cpp_fully_qualified_name, operation_t, false>();
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/device/binary_device_operation.hpp:232:120:   required from here
  232 | constexpr auto binary = ttnn::register_operation<"ttnn::prim::binary", ttnn::operations::binary::BinaryDeviceOperation>();
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:67: error: static assertion failed: Primitive operations must be in the `ttnn::prim` namespace.
  344 |             static_assert(tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace), "Primitive operations must be in the `ttnn::prim` namespace.");
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:67: note: ‘tt::stl::reflection::fixed_string_equals<char, 11, char, 10>(namespace_substring, ttnn::decorators::prim_namespace)’ evaluates to false
make[2]: *** [CMakeFiles/ttnn-compile-error.dir/build.make:76: CMakeFiles/ttnn-compile-error.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/ttnn-compile-error.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

With Clang 18

➜  build git:(master) make                                                                         
[ 50%] Building CXX object CMakeFiles/ttnn-compile-error.dir/main.cpp.o
In file included from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor.hpp:19:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/types.hpp:15:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/buffers/buffer.hpp:12:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:291:30: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
  291 |         bool grid[max_y + 1][max_x + 2];
      |                              ^~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:291:30: note: read of non-const variable 'max_x' is not allowed in a constant expression
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:275:60: note: declared here
  275 |         size_t min_x = std::numeric_limits<size_t>::max(), max_x = 0, min_y = std::numeric_limits<size_t>::max(),
      |                                                            ^
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:291:19: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
  291 |         bool grid[max_y + 1][max_x + 2];
      |                   ^~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:291:19: note: read of non-const variable 'max_y' is not allowed in a constant expression
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/core_coord.h:276:16: note: declared here
  276 |                max_y = 0;
      |                ^
In file included from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor.hpp:19:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/types.hpp:16:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/device/device.hpp:17:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/build.hpp:12:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/utils.hpp:52:58: warning: implicit capture of 'this' with a capture default of '=' is deprecated [-Wdeprecated-this-capture]
   52 |                         std::lock_guard<std::mutex> lock(exceptionMutex);
      |                                                          ^
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/common/utils.hpp:48:51: note: add an explicit capture of 'this' to capture '*this' by reference
   48 |                 threads.emplace_back(std::thread([=]() {
      |                                                   ^
      |                                                    , this
In file included from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor.hpp:19:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/types.hpp:16:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/device/device.hpp:17:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/build.hpp:15:
In file included from /home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/settings.hpp:11:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/hlk_desc.hpp:183:17: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
  183 |     char buffer[hlk_args_size];
      |                 ^~~~~~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/hlk_desc.hpp:183:17: note: function parameter 'hlk_args_size' with unknown value cannot be used in a constant expression
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/jit_build/hlk_desc.hpp:182:64: note: declared here
  182 | inline void hash_hlk_args(size_t& seed, void *hlk_args, size_t hlk_args_size) {
      |                                                                ^
In file included from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:10:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/core.hpp:11:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/tensor/tensor_impl.hpp:17:
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/dispatch/command_queue.hpp:497:40: warning: volatile-qualified return type 'volatile bool' is deprecated [-Wdeprecated-volatile]
  497 |     volatile bool is_dprint_server_hung();
      |                                        ^
/home/marty/Documents/not-my-projects/tt-metal/tt_metal/impl/dispatch/command_queue.hpp:498:30: warning: volatile-qualified return type 'volatile bool' is deprecated [-Wdeprecated-volatile]
  498 |     volatile bool is_noc_hung();
      |                              ^
In file included from /home/marty/Documents/ttnn-binary-error-demo/main.cpp:2:
In file included from /home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/binary.hpp:8:
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:27: error: static assertion failed due to requirement 'tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace)': Primitive operations must be in the `ttnn::prim` namespace.
  344 |             static_assert(tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace), "Primitive operations must be in the `ttnn::prim` namespace.");
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:360:5: note: in instantiation of function template specialization 'ttnn::decorators::assert_operation_in_correct_namespace<reflect::fixed_string<char, 18UL>{"ttnn::prim::binary"}, ttnn::operations::binary::BinaryDeviceOperation>' requested here
  360 |     assert_operation_in_correct_namespace<cpp_fully_qualified_name, operation_t>();
      |     ^
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:371:12: note: in instantiation of function template specialization 'ttnn::decorators::register_operation_impl<reflect::fixed_string<char, 18UL>{"ttnn::prim::binary"}, ttnn::operations::binary::BinaryDeviceOperation, false>' requested here
  371 |     return register_operation_impl<cpp_fully_qualified_name, operation_t, false>();
      |            ^
/home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/operations/eltwise/binary/device/binary_device_operation.hpp:232:31: note: in instantiation of function template specialization 'ttnn::decorators::register_operation<reflect::fixed_string<char, 18UL>{"ttnn::prim::binary"}, ttnn::operations::binary::BinaryDeviceOperation>' requested here
  232 | constexpr auto binary = ttnn::register_operation<"ttnn::prim::binary", ttnn::operations::binary::BinaryDeviceOperation>();
      |                               ^
6 warnings and 1 error generated.
make[2]: *** [CMakeFiles/ttnn-compile-error.dir/build.make:76: CMakeFiles/ttnn-compile-error.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/ttnn-compile-error.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

I can also replicate the same issue on Ubuntu 22.04 with clang-17 and GCC 13.

can you please extend the code so we can see what compiler extracted?

May you elaborate? I didn't get what you want.

@ayerofieiev-tt
Copy link
Member

ayerofieiev-tt commented Aug 26, 2024

This part of log tells us the problem, but does not tell us enough.

home/marty/Documents/not-my-projects/tt-metal/ttnn/cpp/ttnn/decorators.hpp:344:67: error: static assertion failed: Primitive operations must be in the `ttnn::prim` namespace.
  344 |             static_assert(tt::stl::reflection::fixed_string_equals(namespace_substring, prim_namespace), "Primitive operations must be in the `ttnn::prim` namespace.");
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The snippet I shared above extends logging for us to see the content of namespace_substring. This might lead us to the answer.

@marty1885
Copy link
Contributor Author

marty1885 commented Aug 26, 2024

@ayerofieiev-tt Ah ha I figured it out. <insert paragraph on how difficult it is to debug compile time issues, but rewarding non the less>

The intimidate cause is that I'm using a different of reflect then TT is pulling from CPM (I'm running a out-of-tree project, so I use whatever the OS gives me). With some code deleting. I am able to boil down and isolate the problem down to the tt::stl::reflection::fixed_string_substring. The following program demonstrates the issue.

#include <ttnn/operations/eltwise/binary/binary.hpp>
#include <iostream>
#include <string>

constexpr reflect::fixed_string prim_namespace = "ttnn::prim";
constexpr reflect::fixed_string cpp_fully_qualified_name = "ttnn::prim::binary";
constexpr auto namespace_substring = tt::stl::reflection::fixed_string_substring<0, sizeof(prim_namespace)>(cpp_fully_qualified_name);

int main()
{
    std::string_view str = (std::string_view)namespace_substring;
    std::cout << str << std::endl;
}

With 1.1.1 which is what TT is using. It outputs

ttnn::prim

But with later versions. It outputs

ttnn::prim:

The root cause is with how TT decides how long the a fixed_string is. The critical piece of code is

fixed_string_substring<0, sizeof(prim_namespace)>(cpp_fully_qualified_name)

This does not hold true in newer versions. It adds an additional \0 at the end of the string so it is null terminated.

// deceleration in reflect 1.2.2
template<class T, std::size_t Size>
struct fixed_string {
  constexpr explicit(false) fixed_string(const T* str) {
    for (decltype(Size) i{}; i < Size; ++i) { data[i] = str[i]; }
    data[Size] = T();
  }
  [[nodiscard]] constexpr auto operator<=>(const fixed_string&) const = default;
  [[nodiscard]] constexpr explicit(false) operator std::string_view() const { return {std::data(data), Size}; }
  [[nodiscard]] constexpr auto size() const { return Size; }
  T data[Size+1];
};

// deceleration in reflect 1.1.1 - the version TT is using
template<class T, std::size_t Size>
struct fixed_string {
  constexpr explicit(false) fixed_string(const T* str) { for (decltype(Size) i{}; i < Size; ++i) { data[i] = str[i]; } }
  [[nodiscard]] constexpr auto operator<=>(const fixed_string&) const = default;
  [[nodiscard]] constexpr explicit(false) operator std::string_view() const { return {std::data(data), Size}; }
  [[nodiscard]] constexpr auto size() const { return Size; }
  std::array<T, Size> data{};
};

The solution to support both case it to replace sizeof() with .size(). I'll make a PR shortly.

marty1885 added a commit to marty1885/tt-metal that referenced this issue Aug 26, 2024
marty1885 added a commit to marty1885/tt-metal that referenced this issue Aug 26, 2024
ayerofieiev-tt pushed a commit that referenced this issue Aug 28, 2024
…lity with newer versions of reflect (#11896)

* #11883: use fixed_string.size() instead of sizeof

* #11883: cleanup static check pre-condition
@marty1885
Copy link
Contributor Author

Closing as fix is merged. Thanks guys

jaykru-tt pushed a commit that referenced this issue Aug 29, 2024
…lity with newer versions of reflect (#11896)

* #11883: use fixed_string.size() instead of sizeof

* #11883: cleanup static check pre-condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community
Projects
None yet
Development

No branches or pull requests

3 participants