From cb739785decefe57b4d5e1e6c27dd9a849a35641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Brunk?= Date: Tue, 6 Jun 2023 14:15:20 +0300 Subject: [PATCH 1/4] Update to Scala 3.3.0, JavaCPP 1.5.9 stable and other dep updates --- build.sbt | 10 +++++----- .../main/scala/torch/internal/NativeConverters.scala | 8 ++++---- core/src/main/scala/torch/nn/functional/pooling.scala | 4 ++-- .../torch/nn/modules/pooling/AdaptiveAvgPool2d.scala | 8 ++++++-- project/plugins.sbt | 8 ++++---- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/build.sbt b/build.sbt index 95d4f127..078a6393 100644 --- a/build.sbt +++ b/build.sbt @@ -23,11 +23,11 @@ ThisBuild / tlSitePublishBranch := Some("main") ThisBuild / apiURL := Some(new URL("https://storch.dev/api/")) -val scrImageVersion = "4.0.32" +val scrImageVersion = "4.0.34" val pytorchVersion = "2.0.1" val openblasVersion = "0.3.23" val mklVersion = "2023.1" -ThisBuild / scalaVersion := "3.2.2" +ThisBuild / scalaVersion := "3.3.0" ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")) @@ -37,7 +37,7 @@ ThisBuild / enableGPU := false lazy val commonSettings = Seq( Compile / doc / scalacOptions ++= Seq("-groups", "-snippet-compiler:compile"), - javaCppVersion := "1.5.9-SNAPSHOT", + javaCppVersion := "1.5.9", javaCppPlatform := Seq(), resolvers ++= Resolver.sonatypeOssRepos("snapshots") // This is a hack to avoid depending on the native libs when publishing @@ -80,8 +80,8 @@ lazy val core = project libraryDependencies ++= Seq( "org.bytedeco" % "pytorch" % s"$pytorchVersion-${javaCppVersion.value}", "org.typelevel" %% "spire" % "0.18.0", - "org.typelevel" %% "shapeless3-typeable" % "3.2.0", - "com.lihaoyi" %% "os-lib" % "0.9.0", + "org.typelevel" %% "shapeless3-typeable" % "3.3.0", + "com.lihaoyi" %% "os-lib" % "0.9.1", "com.lihaoyi" %% "sourcecode" % "0.3.0", "dev.dirs" % "directories" % "26", "org.scalameta" %% "munit" % "0.7.29" % Test, diff --git a/core/src/main/scala/torch/internal/NativeConverters.scala b/core/src/main/scala/torch/internal/NativeConverters.scala index c0c62e4a..84f13015 100644 --- a/core/src/main/scala/torch/internal/NativeConverters.scala +++ b/core/src/main/scala/torch/internal/NativeConverters.scala @@ -58,12 +58,12 @@ private[torch] object NativeConverters: toOptional(t, t => pytorch.TensorOptional(t.native)) def toArray(i: Long | (Long, Long)) = i match - case i: Long => Array(i) - case (i, j): (Long, Long) => Array(i, j) + case i: Long => Array(i) + case (i, j) => Array(i, j) def toNative(input: Int | (Int, Int)) = input match - case (h, w): (Int, Int) => LongPointer(Array(h.toLong, w.toLong)*) - case x: Int => LongPointer(Array(x.toLong, x.toLong)*) + case (h, w) => LongPointer(Array(h.toLong, w.toLong)*) + case x: Int => LongPointer(Array(x.toLong, x.toLong)*) def toScalar(x: ScalaType): pytorch.Scalar = x match case x: Boolean => pytorch.Scalar(if true then 1: Byte else 0: Byte) diff --git a/core/src/main/scala/torch/nn/functional/pooling.scala b/core/src/main/scala/torch/nn/functional/pooling.scala index 6c8ae6c2..4de60896 100644 --- a/core/src/main/scala/torch/nn/functional/pooling.scala +++ b/core/src/main/scala/torch/nn/functional/pooling.scala @@ -26,6 +26,6 @@ import torch.internal.NativeConverters.toOptional def maxPool2d[D <: DType](input: Tensor[D], kernelSize: Long | (Long, Long)): Tensor[D] = val kernelSizeNative = kernelSize match - case (h, w): (Long, Long) => Array(h, w) - case x: Long => Array(x, x) + case (h, w) => Array(h, w) + case x: Long => Array(x, x) Tensor(torchNative.max_pool2d(input.native, kernelSizeNative*)) diff --git a/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala b/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala index e4e90c82..4b021f95 100644 --- a/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala +++ b/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala @@ -26,6 +26,7 @@ import org.bytedeco.pytorch import torch.internal.NativeConverters.{toNative, toOptional} import org.bytedeco.pytorch.LongOptionalVector import org.bytedeco.pytorch.LongOptional +import scala.annotation.nowarn /** Applies a 2D adaptive average pooling over an input signal composed of several input planes. * @@ -36,9 +37,12 @@ final class AdaptiveAvgPool2d( outputSize: Int | Option[Int] | (Option[Int], Option[Int]) | (Int, Int) ) extends Module { + // TODO find a better way to remove that warning instead of just silencing + // See https://users.scala-lang.org/t/alternative-for-type-ascriptions-after-tuple-patterns-in-scala-3-3-0/9328 + @nowarn("msg=Type ascriptions after patterns other than") private def nativeOutputSize = outputSize match - case (h, w): (Int, Int) => new LongOptionalVector(new LongOptional(h), new LongOptional(w)) - case x: Int => new LongOptionalVector(new LongOptional(x), new LongOptional(x)) + case (h: Int, w: Int) => new LongOptionalVector(new LongOptional(h), new LongOptional(w)) + case x: Int => new LongOptionalVector(new LongOptional(x), new LongOptional(x)) case (h, w): (Option[Int], Option[Int]) => new LongOptionalVector(toOptional(h.map(_.toLong)), toOptional(w.map(_.toLong))) case x: Option[Int] => diff --git a/project/plugins.sbt b/project/plugins.sbt index a89b842e..a1bcc1e4 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,6 +1,6 @@ -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") addSbtPlugin("org.bytedeco" % "sbt-javacpp" % "1.17") -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.7") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") -addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.18") -addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.4.18") +addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.22") +addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.4.22") From e883fbb6c31093d77dd7396886d60b736760d5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Brunk?= Date: Tue, 6 Jun 2023 14:36:54 +0300 Subject: [PATCH 2/4] Re-generate GitHub workflows after Scala update via githubWorkflowGenerate - Update sbt --- .github/workflows/ci.yml | 14 ++++++-------- project/build.properties | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 963fd9ee..c2d94efe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [3.2.2] + scala: [3.3.0] java: [temurin@11] runs-on: ${{ matrix.os }} steps: @@ -105,7 +105,6 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [3.2.2] java: [temurin@11] runs-on: ${{ matrix.os }} steps: @@ -142,12 +141,12 @@ jobs: ~/Library/Caches/Coursier/v1 key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - - name: Download target directories (3.2.2) + - name: Download target directories (3.3.0) uses: actions/download-artifact@v3 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2 + name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.0 - - name: Inflate target directories (3.2.2) + - name: Inflate target directories (3.3.0) run: | tar xf targets.tar rm targets.tar @@ -164,14 +163,13 @@ jobs: (echo "$PGP_PASSPHRASE"; echo; echo) | gpg --command-fd 0 --pinentry-mode loopback --change-passphrase $(gpg --list-secret-keys --with-colons 2> /dev/null | grep '^sec:' | cut --delimiter ':' --fields 5 | tail -n 1) - name: Publish - run: sbt '++ ${{ matrix.scala }}' tlRelease + run: sbt tlCiRelease site: name: Generate Site strategy: matrix: os: [ubuntu-latest] - scala: [3.2.2] java: [temurin@11] runs-on: ${{ matrix.os }} steps: @@ -209,7 +207,7 @@ jobs: key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - name: Generate site - run: sbt '++ ${{ matrix.scala }}' docs/tlSite + run: sbt docs/tlSite - name: Publish site if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' diff --git a/project/build.properties b/project/build.properties index 875272df..fd5b1576 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.8.2 \ No newline at end of file +sbt.version=1.9.0 \ No newline at end of file From 4934ac72998bec7da76d1554697ac1f3b14fa381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Brunk?= Date: Wed, 7 Jun 2023 13:06:51 +0300 Subject: [PATCH 3/4] Use @unchecked instead of suppressing deprecation warning --- .../scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala b/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala index 4b021f95..05de6f99 100644 --- a/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala +++ b/core/src/main/scala/torch/nn/modules/pooling/AdaptiveAvgPool2d.scala @@ -26,7 +26,6 @@ import org.bytedeco.pytorch import torch.internal.NativeConverters.{toNative, toOptional} import org.bytedeco.pytorch.LongOptionalVector import org.bytedeco.pytorch.LongOptional -import scala.annotation.nowarn /** Applies a 2D adaptive average pooling over an input signal composed of several input planes. * @@ -37,13 +36,11 @@ final class AdaptiveAvgPool2d( outputSize: Int | Option[Int] | (Option[Int], Option[Int]) | (Int, Int) ) extends Module { - // TODO find a better way to remove that warning instead of just silencing - // See https://users.scala-lang.org/t/alternative-for-type-ascriptions-after-tuple-patterns-in-scala-3-3-0/9328 - @nowarn("msg=Type ascriptions after patterns other than") private def nativeOutputSize = outputSize match case (h: Int, w: Int) => new LongOptionalVector(new LongOptional(h), new LongOptional(w)) case x: Int => new LongOptionalVector(new LongOptional(x), new LongOptional(x)) - case (h, w): (Option[Int], Option[Int]) => + // We know this can only be int so we can suppress the type test for Option[Int] cannot be checked at runtime warning + case (h: Option[Int @unchecked], w: Option[Int @unchecked]) => new LongOptionalVector(toOptional(h.map(_.toLong)), toOptional(w.map(_.toLong))) case x: Option[Int] => new LongOptionalVector(toOptional(x.map(_.toLong)), toOptional(x.map(_.toLong))) From 33e25afa824d88e877b55dd5ca37b4c193e0a29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Brunk?= Date: Mon, 12 Jun 2023 22:17:10 +0200 Subject: [PATCH 4/4] Set javaCppVersion in root project to fix the unidoc task --- build.sbt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 078a6393..fba97ec6 100644 --- a/build.sbt +++ b/build.sbt @@ -28,6 +28,7 @@ val pytorchVersion = "2.0.1" val openblasVersion = "0.3.23" val mklVersion = "2023.1" ThisBuild / scalaVersion := "3.3.0" +ThisBuild / javaCppVersion := "1.5.9" ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")) @@ -37,7 +38,7 @@ ThisBuild / enableGPU := false lazy val commonSettings = Seq( Compile / doc / scalacOptions ++= Seq("-groups", "-snippet-compiler:compile"), - javaCppVersion := "1.5.9", + javaCppVersion := (ThisBuild / javaCppVersion).value, javaCppPlatform := Seq(), resolvers ++= Resolver.sonatypeOssRepos("snapshots") // This is a hack to avoid depending on the native libs when publishing @@ -137,3 +138,6 @@ lazy val root = project .enablePlugins(NoPublishPlugin) .in(file(".")) .aggregate(core, vision, examples, docs) + .settings( + javaCppVersion := (ThisBuild / javaCppVersion).value + )