diff --git a/pkgs/metal/default.nix b/pkgs/metal/default.nix index 4175528..f05d356 100644 --- a/pkgs/metal/default.nix +++ b/pkgs/metal/default.nix @@ -14,6 +14,7 @@ libz, runCommand, libexecinfo, + callPackage, }: let @@ -27,7 +28,7 @@ let dontBuild = true; - outputHash = "sha256-UOBBqIP2KKEn2pfv7l5v2Of9RoZY0+3TCEu94MQUVYo="; + outputHash = "sha256-hhLjEssXID+uiPQ3kexMCOVB6DB9m/eAVmfr2OleGXc="; outputHashAlgo = "sha256"; cmakeFlags = [ @@ -54,6 +55,7 @@ let postInstall = ""; }); + # NOTE: When changing something remember to make sure the outputHash above doesn't change, or if it changes then update it. metal = llvmPackages.libcxxStdenv.mkDerivation { pname = "tt-metal"; @@ -61,12 +63,16 @@ let src = fetchFromGitHub { owner = "tenstorrent"; repo = "tt-metal"; - rev = "0fb4249a94a99714de8f91d93d338832694c09e0"; - # this takes a while and we don't need all of them + rev = "697ccc724048108e9f11a3c9ed5171a17ac9fd9c"; + hash = "sha256-pBfJAtSyRKtU3iG2P3hVuo2LXHBFOU+3XmRkbDuDvDQ="; fetchSubmodules = true; - hash = "sha256-0tcIwaJzM75S7SFKCJ2UbfElwASpFwdySmzt2LUTT4A="; + fetchLFS = true; }; + patches = [ + ./rpath.patch + ]; + env.NIX_CFLAGS_COMPILE = "-Wno-unused-command-line-argument"; nativeBuildInputs = [ @@ -92,8 +98,15 @@ let postPatch = '' cp ${cpm-cmake}/share/cpm/CPM.cmake cmake/CPM.cmake - rm -rf tt_metal/third_party/sfpi/compiler - ln -s ${sfpi.tt-gcc} tt_metal/third_party/sfpi/compiler + + substituteInPlace tt_metal/CMakeLists.txt ttnn/CMakeLists.txt \ + --replace-fail "REPLACETHIS\"" "$out/lib\"" \ + --replace-warn "REPLACETHIS1" "$out/build/lib" + + substituteInPlace tt_metal/hw/CMakeLists.txt \ + --replace-fail "FetchContent_MakeAvailable(sfpi)" "" + mkdir -p runtime + ln -s ${sfpi.sfpi} runtime/sfpi ''; ARCH_NAME = "wormhole_b0"; @@ -108,41 +121,26 @@ let ]; postInstall = '' - mkdir -p $out/{lib,include} - cp -r ../tt_metal $out/include - cp -r lib $out/lib - cp -r deps $out/deps - cp ./deps/reflect/*/reflect $out/include/ - for f in $(find "$out" -type f -name '*.so*'); do - sed -i "s|/build/source/build/lib|$out/lib|g" $f - sed -i "s|/build/source/tt_metal|$out/include/tt_metal|g" $f - sed -i 's|$ORIGIN/build/lib:||g' $f - done + # Have to do this until cpp-ttnn-project-template is fixed + # ttnn-template> ninja: error: '/nix/store/-tt-metal-unstable-2024-10-04/build/lib/_ttnn.so', needed by 'sources/examples/sample_app/sample_app', missing and no known rule to make it + cp -r ../ $out + rm -rf $out/.cpmcache + ln -s $out/build/deps $out/.cpmcache + # Nix checks for references to /build/source so these should be different but not a different size to prevent corruption + find "$out" -type f -print0 | while IFS= read -r -d $'\0' f; do + sed -i "s|/build/source|/suild/source|g" "$f" + sed -i 's|$ORIGIN/build/lib:|$ORIGIN/suild/lib:|g' "$f" + done ''; + dontPatchELF = true; + dontStrip = true; + passthru = { inherit metal-deps; tests = { - include = runCommand "test" { buildInputs = [ metal]; } '' - mkdir -p $out - # In the tests the paths in the includes are weird and don't use the full `tt_metal/something` paths - ${sfpi.sfpi}/compiler/bin/riscv32-unknown-elf-c++ \ - -std=c++20 \ - -mwormhole \ - -I ${libexecinfo}/include \ - -I ${metal}/include \ - -I ${metal}/include/tt_metal \ - -I ${metal}/include/tt_metal/impl \ - -I ${metal}/include/tt_metal/hw/inc/wormhole \ - -I ${metal}/include/tt_metal/hw/inc \ - -I ${metal}/include/tt_metal/third_party/umd/src/firmware/riscv/wormhole \ - -I ${metal}/include/tt_metal/hw/inc/wormhole/wormhole_b0_defines \ - -I ${metal}/include/tt_metal/third_party/umd \ - -I ${metal}/include/tt_metal/third_party/fmt \ - -o add2 ${metal.src}/tt_metal/programming_examples/add_2_integers_in_compute/add_2_integers_in_compute.cpp - mv add2 $out - ''; + template = callPackage ./ttnn-template.nix { inherit metal; }; }; }; diff --git a/pkgs/metal/rpath.patch b/pkgs/metal/rpath.patch new file mode 100644 index 0000000..26d0201 --- /dev/null +++ b/pkgs/metal/rpath.patch @@ -0,0 +1,28 @@ +diff --git a/tt_metal/CMakeLists.txt b/tt_metal/CMakeLists.txt +index cba3743fbc..e580f6696c 100644 +--- a/tt_metal/CMakeLists.txt ++++ b/tt_metal/CMakeLists.txt +@@ -55,7 +55,7 @@ set_target_properties( + tt_metal + PROPERTIES + INSTALL_RPATH +- "${PROJECT_BINARY_DIR}/lib" ++ "REPLACETHIS" + ADDITIONAL_CLEAN_FILES + "${PROJECT_BINARY_DIR}/lib;${PROJECT_BINARY_DIR}/obj" + ) +diff --git a/ttnn/CMakeLists.txt b/ttnn/CMakeLists.txt +index 45ee3dd8f8..4401f51b39 100644 +--- a/ttnn/CMakeLists.txt ++++ b/ttnn/CMakeLists.txt +@@ -683,8 +683,8 @@ TT_ENABLE_UNITY_BUILD(ttnn) + #We move the library binaries to a different path rather than PROJECT_BINARY_DIR + #in the Python wheel + set(TTNN_INSTALL_RPATH +- "${PROJECT_BINARY_DIR}/lib" +- "$ORIGIN/build/lib" ++ "REPLACETHIS" ++ "REPLACETHIS1" + ) + + #Make sure library built is _ttnn.so and that it can find all it's linked libraries diff --git a/pkgs/metal/ttnn-template.nix b/pkgs/metal/ttnn-template.nix new file mode 100644 index 0000000..b98a37c --- /dev/null +++ b/pkgs/metal/ttnn-template.nix @@ -0,0 +1,111 @@ +{ + stdenv, + fetchFromGitHub, + metal, + cmake, + ninja, + git, + cacert, + python3, + numactl, + hwloc, + libz, + llvmPackages_17, + cpm-cmake, + sfpi, + +}: + +let + version = "0"; + llvmPackages = llvmPackages_17; + depsDir = "deps"; + + ttnn-template-deps = ttnn-template.overrideAttrs (previousAttrs: { + name = "ttnn-template-deps-deps-${version}.tar.gz"; + + dontBuild = true; + + outputHash = "sha256-qt3PLKE3lwqiYQq6m06V1xk1qDOyHAtGj8lw0Q99qgE="; + outputHashAlgo = "sha256"; + + cmakeFlags = [ + "-DCPM_DOWNLOAD_ALL=ON" + "-DCPM_SOURCE_CACHE=${depsDir}" + ]; + + # Infinite recursion + postUnpack = ""; + + installPhase = '' + runHook preInstall + + # Prune the `.git` directories + find ${depsDir} -name .git -type d -prune -exec rm -rf {} \;; + # Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/ + tar --owner=0 --group=0 --numeric-owner --format=gnu \ + --sort=name --mtime="@$SOURCE_DATE_EPOCH" \ + -czf $out \ + ${depsDir} \ + + runHook postInstall + ''; + + postInstall = ""; + }); + + ttnn-template = llvmPackages.libcxxStdenv.mkDerivation { + pname = "ttnn-template"; + version = "0"; + src = fetchFromGitHub { + owner = "tenstorrent"; + repo = "cpp-ttnn-project-template"; + rev = "702b453aa7000daa56692b7559ec77adf407828d"; + hash = "sha256-NzcZCVujJCyHQgALAewJjoWj+6bQXncONO7nRK8zSx4="; + }; + + nativeBuildInputs = [ + cmake + #ninja + python3 + # for cpm + git + cacert + ]; + + buildInputs = [ + numactl + # umd + hwloc + libz + ]; + + ARCH_NAME = "wormhole_b0"; + TT_METAL_HOME = metal; + + postUnpack = '' + mkdir -p $sourceRoot/build + tar -xf ${ttnn-template-deps} -C $sourceRoot/build + ''; + + postPatch = '' + cp ${cpm-cmake}/share/cpm/CPM.cmake cmake/CPM.cmake + + # Upstream changed these locations and removed libfmt but the template hasn't been updated yet + # https://github.com/tenstorrent/tt-metal/pull/13788 + substituteInPlace sources/sample_lib/CMakeLists.txt \ + --replace-fail '$ENV{TT_METAL_HOME}/build/lib/_ttnn.so' '${metal}/lib/_ttnn.so' \ + --replace-fail '$ENV{TT_METAL_HOME}/build/lib/libdevice.so' '${metal}/lib/libdevice.so' \ + --replace-fail '$ENV{TT_METAL_HOME}/build/lib/libfmt.so' "" \ + --replace-fail '$ENV{TT_METAL_HOME}/build/lib/libnng.so.1' '${metal}/lib/libnng.so.1.8.0' \ + + substituteInPlace sources/sample_lib/CMakeLists.txt \ + --replace-fail '$ENV{TT_METAL_HOME}/build/lib' '${metal}/lib ${metal}/build/lib' + ''; + + cmakeFlags = [ + "-DCPM_SOURCE_CACHE=${depsDir}" + ]; + }; +in +ttnn-template diff --git a/pkgs/sfpi/default.nix b/pkgs/sfpi/default.nix index 0739c1d..fb6f1a5 100644 --- a/pkgs/sfpi/default.nix +++ b/pkgs/sfpi/default.nix @@ -3,12 +3,13 @@ rec { sfpi = pkgs.stdenv.mkDerivation { pname = "sfpi"; - version = "unstable-2024-10-10"; + version = "unstable-2024-09-25"; src = pkgs.fetchFromGitHub { owner = "tenstorrent-metal"; repo = "sfpi"; - rev = "899b8b6c90fc3e18ad081fd556eaa1a473c8a357"; + # One commit before they started to use gcc 12 + rev = "08d39202f283ab9122e162bcead5b7c90014fa86"; hash = "sha256-EZHUhAqn9/r02IC+TVoxfIyzEctYQKd5azcrXE5DIgg="; }; @@ -24,36 +25,6 @@ rec { mkdir -p $out/compiler/libexec bin/release.sh $out ''; - - passthru = { inherit sfpi-no-lfs; }; - }; - - - sfpi-no-lfs = pkgs.stdenv.mkDerivation { - pname = "sfpi"; - version = "unstable-2024-11-15"; - - src = pkgs.fetchFromGitHub { - owner = "tenstorrent-metal"; - repo = "sfpi"; - rev = "fcb552bc66d274fc4ffadc35eb4e6d1f20b2e6b3"; - hash = ""; - fetchSubmodules = true; - }; - - postPatch = '' - patchShebangs --build $scripts/build.sh - ''; - - buildPhase = '' - ./scripts/build.sh - ''; - - installPhase = '' - mkdir -p "$out" - cp -r ./sfpi/* "$out" - ''; - }; tt-gcc = import ./tt-gcc.nix { inherit pkgs; }; diff --git a/pkgs/sfpi/tt-gcc.nix b/pkgs/sfpi/tt-gcc.nix index 3a91c61..f0dd667 100644 --- a/pkgs/sfpi/tt-gcc.nix +++ b/pkgs/sfpi/tt-gcc.nix @@ -8,10 +8,10 @@ pkgs.stdenv.mkDerivation rec { owner = "tenstorrent-metal"; repo = "sfpi-tt-gcc"; # https://github.com/tenstorrent/sfpi-tt-gcc/commits/tt-rel/gcc-12.2 - rev = "a5c31334942e9df66427837f7268269311fdfc2e"; + rev = "fad3c23c8972adb739ef62d2f35d770062a7cd73"; # this takes a while and we don't need all of them fetchSubmodules = true; - hash = "sha256-1d77+6Yv3i4M7J+vUwcUuFg56wCG9oVeDt+fvBKtHPI="; + hash = "sha256-X2uVyk4uo0TuDu/2nUXk2v8A4RURvhg0MAoCssAuljI="; }; nativeBuildInputs = with pkgs; [