class Libtorch < Formula include Language::Python::Virtualenv desc "Tensors and dynamic neural networks" homepage "https://pytorch.org/" url "https://github.com/pytorch/pytorch.git", tag: "v1.10.2", revision: "71f889c7d265b9636b93ede9d651c0a9c4bee191" license "BSD-3-Clause" livecheck do url :stable regex(/^v?(\d+(?:\.\d+)+)$/i) end bottle do sha256 cellar: :any, arm64_monterey: "5ce1f2a40f787dc61b762bde001027393cef039ffd94b0a63d800a78d0601d9d" sha256 cellar: :any, arm64_big_sur: "8122c594fe8d1cd192542bc0c7f7d458253d9e105743101d37b9a2cb885dad98" sha256 cellar: :any, monterey: "95cec86cdc2c96c18955058fa918b2a6d0b9e6ac536814a6866cd56f94fe6c25" sha256 cellar: :any, big_sur: "238dc4802a6684e19c4036e80313305be83ff65a8514606ddd815426fe15e397" sha256 cellar: :any, catalina: "834b65640273592324e8c718c0770be73b972ea3c25a67f71dd239946aaa220b" sha256 cellar: :any_skip_relocation, x86_64_linux: "c2ea263eb6e8fecdbba9689cdc5254191bd3f6de32fa7af0720f7cfc482fa044" end depends_on "cmake" => :build depends_on "python@3.9" => :build depends_on "eigen" depends_on "libomp" depends_on "libyaml" depends_on "protobuf" depends_on "pybind11" resource "PyYAML" do url "https://files.pythonhosted.org/packages/36/2b/61d51a2c4f25ef062ae3f74576b01638bebad5e045f747ff12643df63844/PyYAML-6.0.tar.gz" sha256 "68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2" end resource "typing-extensions" do url "https://files.pythonhosted.org/packages/0d/4a/60ba3706797b878016f16edc5fbaf1e222109e38d0fa4d7d9312cb53f8dd/typing_extensions-4.0.1.tar.gz" sha256 "4ca091dea149f945ec56afb48dae714f21e8692ef22a395223bcd328961b6a0e" end def install venv = virtualenv_create(buildpath/"venv", Formula["python@3.9"].opt_bin/"python3") venv.pip_install resources args = %W[ -DBUILD_CUSTOM_PROTOBUF=OFF -DBUILD_PYTHON=OFF -DPYTHON_EXECUTABLE=#{buildpath}/venv/bin/python -DUSE_CUDA=OFF -DUSE_METAL=OFF -DUSE_MKLDNN=OFF -DUSE_NNPACK=OFF -DUSE_OPENMP=OFF -DUSE_SYSTEM_EIGEN_INSTALL=ON -DUSE_SYSTEM_PYBIND11=ON ] # Remove when https://github.com/pytorch/pytorch/issues/67974 is addressed args << "-DUSE_SYSTEM_BIND11=ON" mkdir "build" do system "cmake", "..", *std_cmake_args, *args # Avoid references to Homebrew shims inreplace "caffe2/core/macros.h", Superenv.shims_path/ENV.cxx, ENV.cxx system "make" system "make", "install" end end test do (testpath/"test.cpp").write <<~EOS #include #include int main() { torch::Tensor tensor = torch::rand({2, 3}); std::cout << tensor << std::endl; } EOS system ENV.cxx, "-std=c++14", "test.cpp", "-o", "test", "-I#{include}/torch/csrc/api/include", "-L#{lib}", "-ltorch", "-ltorch_cpu", "-lc10" system "./test" end end