From 4a24d1db5e2b2d03e885d1c008f0587346032f3c Mon Sep 17 00:00:00 2001 From: Alex Merritt Date: Thu, 22 Dec 2022 12:17:05 -0500 Subject: [PATCH] Update README; rename io.kjaer.compiletime package to fix error importing from sbt console --- README.md | 13 ++++++++----- .../.js/src/main/scala/ORTOperatorBackend.scala | 2 +- .../.js/src/main/scala/ORTWebModelBackend.scala | 2 +- backends/.js/src/test/scala/SqueezeNetTest.scala | 2 +- backends/.jvm/src/main/scala/NCF.scala | 2 +- backends/.jvm/src/main/scala/ORTModelBackend.scala | 2 +- .../.jvm/src/main/scala/ORTOperatorBackend.scala | 2 +- .../.jvm/src/test/scala/BertTokenizerTest.scala | 2 +- backends/.jvm/src/test/scala/SqueezeNetTest.scala | 4 ++-- .../src/main/scala/ORTNativeOperatorBackend.scala | 2 +- build.sbt | 3 +++ common/src/main/scala/TensorShapeDenotation.scala | 6 +++--- .../main/scala/io/kjaer/compiletime/Indices.scala | 4 ++-- .../main/scala/io/kjaer/compiletime/IndicesOf.scala | 2 +- .../src/main/scala/io/kjaer/compiletime/Shape.scala | 4 ++-- .../main/scala/io/kjaer/compiletime/ShapeOf.scala | 2 +- .../main/scala/io/kjaer/compiletime/dependent.scala | 2 +- core/src/main/scala/ONNX.scala | 3 +++ core/src/main/scala/ONNXBytesDataSource.scala | 2 +- core/src/main/scala/OpToONNXBytesConverter.scala | 2 +- core/src/main/scala/Tensors.scala | 4 ++-- get_models.sh | 2 ++ project/plugins.sbt | 1 + 23 files changed, 41 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index a7cfdfa8..ea9657d3 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,10 @@ First we create an "image" tensor composed entirely of pixel value [42](https:// ```scala import java.nio.file.{Files, Paths} import org.emergentorder.onnx.Tensors._ +import org.emergentorder.onnx.Tensors.Tensor._ import org.emergentorder.onnx.backends._ import org.emergentorder.compiletime._ -import io.kjaer.compiletime._ +import org.emergentorder.io.kjaer.compiletime._ val squeezenetBytes = Files.readAllBytes(Paths.get("squeezenet1.0-12.onnx")) val squeezenet = new ORTModelBackend(squeezenetBytes) @@ -57,16 +58,18 @@ val out = squeezenet.fullModel[Float, // val out: // Tensor[Float,("ImageNetClassification", // "Batch" ##: "Class" ##: TSNil, -// 1 #: 1000 #: SNil)] = (Array(0.8230729, +// 1 #: 1000 #: 1 #: 1 SNil)] = IO(...) // ... //The output shape -out.shape -// val res0: Array[Int] = Array(1, 1000) +out.shape.unsafeRunSync() +// val res0: Array[Int] = Array(1, 1000, 1, 1) +val data = out.data.unsafeRunSync() +// val data: Array[Float] = Array(1.786191E-4, ...) //The highest scoring and thus highest probability (predicted) class -out.data.indices.maxBy(out.data) +data.indices.maxBy(data) // val res1: Int = 549 ``` diff --git a/backends/.js/src/main/scala/ORTOperatorBackend.scala b/backends/.js/src/main/scala/ORTOperatorBackend.scala index 93ac79a2..eac6f59f 100644 --- a/backends/.js/src/main/scala/ORTOperatorBackend.scala +++ b/backends/.js/src/main/scala/ORTOperatorBackend.scala @@ -28,7 +28,7 @@ import org.emergentorder.onnx.Tensors._ import org.emergentorder.onnx.Tensors.Tensor._ import org.emergentorder.compiletime._ import onnxruntimeCommon.inferenceSessionMod.InferenceSession -import io.kjaer.compiletime._ +import org.emergentorder.io.kjaer.compiletime._ //TODO: fix redundant computation due to cats-effect on the JS side //Still happening, though partially fixed by changes in core diff --git a/backends/.js/src/main/scala/ORTWebModelBackend.scala b/backends/.js/src/main/scala/ORTWebModelBackend.scala index 559da7fd..6db0924b 100644 --- a/backends/.js/src/main/scala/ORTWebModelBackend.scala +++ b/backends/.js/src/main/scala/ORTWebModelBackend.scala @@ -15,7 +15,7 @@ import org.emergentorder.onnx._ import org.emergentorder.onnx.Tensors._ import org.emergentorder.onnx.Tensors.Tensor._ import org.emergentorder.compiletime._ -import io.kjaer.compiletime._ +import org.emergentorder.io.kjaer.compiletime._ import ORTTensorUtils._ diff --git a/backends/.js/src/test/scala/SqueezeNetTest.scala b/backends/.js/src/test/scala/SqueezeNetTest.scala index df946a7f..6ce85e0d 100644 --- a/backends/.js/src/test/scala/SqueezeNetTest.scala +++ b/backends/.js/src/test/scala/SqueezeNetTest.scala @@ -6,7 +6,7 @@ import org.emergentorder.onnx.Tensors._ import org.emergentorder.onnx.Tensors.Tensor._ import org.emergentorder.onnx.backends._ import org.emergentorder.compiletime._ -import io.kjaer.compiletime._ +import org.emergentorder.io.kjaer.compiletime._ import org.emergentorder.onnx.onnxruntimeWeb.mod.{InferenceSession => OrtSession} import cats.effect.IO diff --git a/backends/.jvm/src/main/scala/NCF.scala b/backends/.jvm/src/main/scala/NCF.scala index 5bcfd301..78ee650a 100644 --- a/backends/.jvm/src/main/scala/NCF.scala +++ b/backends/.jvm/src/main/scala/NCF.scala @@ -13,7 +13,7 @@ import spire.math.Numeric import scala.language.higherKinds import scala.io.Source import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* import cats.effect.IO //TODO: Add changes to generator; Generate both full model and layerwise programs each time diff --git a/backends/.jvm/src/main/scala/ORTModelBackend.scala b/backends/.jvm/src/main/scala/ORTModelBackend.scala index a3c4cfd6..6e783714 100644 --- a/backends/.jvm/src/main/scala/ORTModelBackend.scala +++ b/backends/.jvm/src/main/scala/ORTModelBackend.scala @@ -16,7 +16,7 @@ import org.emergentorder.onnx.* import org.emergentorder.onnx.Tensors.* import org.emergentorder.onnx.Tensors.Tensor.* import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* import ORTTensorUtils.* diff --git a/backends/.jvm/src/main/scala/ORTOperatorBackend.scala b/backends/.jvm/src/main/scala/ORTOperatorBackend.scala index 0b3a5385..9c55ef66 100644 --- a/backends/.jvm/src/main/scala/ORTOperatorBackend.scala +++ b/backends/.jvm/src/main/scala/ORTOperatorBackend.scala @@ -11,7 +11,7 @@ import org.emergentorder.onnx.* import org.emergentorder.onnx.Tensors.* import org.emergentorder.onnx.Tensors.Tensor.* import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* import onnx.onnx.* import cats.implicits.* diff --git a/backends/.jvm/src/test/scala/BertTokenizerTest.scala b/backends/.jvm/src/test/scala/BertTokenizerTest.scala index 0c03e73d..ef2e2c64 100644 --- a/backends/.jvm/src/test/scala/BertTokenizerTest.scala +++ b/backends/.jvm/src/test/scala/BertTokenizerTest.scala @@ -9,7 +9,7 @@ import org.emergentorder.onnx.Tensors.* import org.emergentorder.onnx.Tensors.Tensor.* import org.emergentorder.onnx.backends.* import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.freespec.AsyncFreeSpec diff --git a/backends/.jvm/src/test/scala/SqueezeNetTest.scala b/backends/.jvm/src/test/scala/SqueezeNetTest.scala index efcbb145..4d92362c 100644 --- a/backends/.jvm/src/test/scala/SqueezeNetTest.scala +++ b/backends/.jvm/src/test/scala/SqueezeNetTest.scala @@ -9,7 +9,7 @@ import org.emergentorder.onnx.Tensors.* import org.emergentorder.onnx.Tensors.Tensor.* import org.emergentorder.onnx.backends.* import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.freespec.AsyncFreeSpec @@ -23,7 +23,7 @@ class ONNXScalaSpec extends AsyncFreeSpec with AsyncIOSpec with Matchers { ) #> new File("squeezenet1.0-12.onnx") !! "SqueezeNet ONNX-Scala model should predict dummy image class" in { - val squeezenetBytes = Files.readAllBytes(Paths.get("squeezenet1.0-12.onnx")) + val squeezenetBytes = Files.readAllBytes(Paths.get("squeezenet1.0-12.onnx")) //.quant.onnx")) val squeezenet = new ORTModelBackend(squeezenetBytes) val data = Array.fill(1 * 3 * 224 * 224) { 42f } // In NCHW tensor image format diff --git a/backends/.native/src/main/scala/ORTNativeOperatorBackend.scala b/backends/.native/src/main/scala/ORTNativeOperatorBackend.scala index 69a340e3..1764fdd7 100644 --- a/backends/.native/src/main/scala/ORTNativeOperatorBackend.scala +++ b/backends/.native/src/main/scala/ORTNativeOperatorBackend.scala @@ -10,7 +10,7 @@ import org.emergentorder.onnx._ import org.emergentorder.onnx.Tensors._ import org.emergentorder.onnx.Tensors.Tensor._ import org.emergentorder.compiletime._ -import io.kjaer.compiletime._ +import org.emergentorder.io.kjaer.compiletime._ trait ORTNativeOperatorBackend { diff --git a/build.sbt b/build.sbt index 8804339f..5c3094d1 100755 --- a/build.sbt +++ b/build.sbt @@ -89,11 +89,14 @@ lazy val backends = (crossProject(JSPlatform, JVMPlatform, NativePlatform) startWebpackDevServer / version := "4.11.1", scalaJSUseMainModuleInitializer := true, // , //Testing Compile / npmDependencies += "onnxruntime-web" -> "1.13.1", + //ORT web and node are interchangeable, given minor package name changes, and node offers a significant speed-up (at the cost of working on the web) + // Compile / npmDependencies += "onnxruntime-node" -> "1.13.1", Compile / npmDependencies += "onnxruntime-common" -> "1.13.1", Compile / npmDependencies += "typescript" -> "4.8.4", libraryDependencies += "org.typelevel" %%% "cats-effect-testing-scalatest" % "1.5.0" % Test, stOutputPackage := "org.emergentorder.onnx", stShortModuleNames := true, + Compile /packageDoc / publishArtifact := false, //This is inordinately slow, only publish doc on release scalaJSStage := FullOptStage, scalaJSLinkerConfig ~= (_.withESFeatures( _.withESVersion(org.scalajs.linker.interface.ESVersion.ES2021) diff --git a/common/src/main/scala/TensorShapeDenotation.scala b/common/src/main/scala/TensorShapeDenotation.scala index de9eab29..f8e72f86 100644 --- a/common/src/main/scala/TensorShapeDenotation.scala +++ b/common/src/main/scala/TensorShapeDenotation.scala @@ -4,9 +4,9 @@ package org.emergentorder.compiletime import scala.compiletime.ops.int.{S, +, <, <=, *} import scala.compiletime.ops.boolean.&& -import io.kjaer.compiletime.Index -import io.kjaer.compiletime.Indices -import io.kjaer.compiletime.INil +import org.emergentorder.io.kjaer.compiletime.Index +import org.emergentorder.io.kjaer.compiletime.Indices +import org.emergentorder.io.kjaer.compiletime.INil type DimensionDenotation = String & Singleton diff --git a/common/src/main/scala/io/kjaer/compiletime/Indices.scala b/common/src/main/scala/io/kjaer/compiletime/Indices.scala index 8a8cb36b..093fc7d7 100644 --- a/common/src/main/scala/io/kjaer/compiletime/Indices.scala +++ b/common/src/main/scala/io/kjaer/compiletime/Indices.scala @@ -1,5 +1,5 @@ //Author: Maxime Kjaer, taken from tf-dotty -package io.kjaer.compiletime +package org.emergentorder.io.kjaer.compiletime import scala.compiletime.ops.string.+ import scala.compiletime.ops.int @@ -8,7 +8,7 @@ type Index = Int & Singleton sealed trait Indices { def :::[H <: Index, This >: this.type <: Indices](head: H): H ::: This = - io.kjaer.compiletime.:::(head, this) + org.emergentorder.io.kjaer.compiletime.:::(head, this) def indices: Seq[Int] = this match { case INil => Nil diff --git a/common/src/main/scala/io/kjaer/compiletime/IndicesOf.scala b/common/src/main/scala/io/kjaer/compiletime/IndicesOf.scala index 48ca6243..3e1508e0 100644 --- a/common/src/main/scala/io/kjaer/compiletime/IndicesOf.scala +++ b/common/src/main/scala/io/kjaer/compiletime/IndicesOf.scala @@ -1,5 +1,5 @@ //Author: Maxime Kjaer, taken from tf-dotty -package io.kjaer.compiletime +package org.emergentorder.io.kjaer.compiletime /** Type-class used to materialize the singleton type of an [[Indices]]. * diff --git a/common/src/main/scala/io/kjaer/compiletime/Shape.scala b/common/src/main/scala/io/kjaer/compiletime/Shape.scala index e2941125..40395f37 100644 --- a/common/src/main/scala/io/kjaer/compiletime/Shape.scala +++ b/common/src/main/scala/io/kjaer/compiletime/Shape.scala @@ -1,5 +1,5 @@ //Author: Maxime Kjaer, taken from tf-dotty -package io.kjaer.compiletime +package org.emergentorder.io.kjaer.compiletime import scala.compiletime.ops.int.{S, +, <, <=, *} import scala.compiletime.ops.boolean.&& @@ -11,7 +11,7 @@ sealed trait Shape extends Product with Serializable { /** Prepend the head to this */ def #:[H <: Dimension, This >: this.type <: Shape](head: H): H #: This = - io.kjaer.compiletime.#:(head, this) + org.emergentorder.io.kjaer.compiletime.#:(head, this) /** Concat with another shape * */ def ++(that: Shape): this.type `Concat` that.type = Shape.concat(this, that) diff --git a/common/src/main/scala/io/kjaer/compiletime/ShapeOf.scala b/common/src/main/scala/io/kjaer/compiletime/ShapeOf.scala index 1edcdf21..d6b87739 100644 --- a/common/src/main/scala/io/kjaer/compiletime/ShapeOf.scala +++ b/common/src/main/scala/io/kjaer/compiletime/ShapeOf.scala @@ -1,5 +1,5 @@ //Author: Maxime Kjaer, taken from tf-dotty -package io.kjaer.compiletime +package org.emergentorder.io.kjaer.compiletime /** Type-class used to materialize the singleton type of a [[Shape]]. * diff --git a/common/src/main/scala/io/kjaer/compiletime/dependent.scala b/common/src/main/scala/io/kjaer/compiletime/dependent.scala index 63d276a1..df538c19 100644 --- a/common/src/main/scala/io/kjaer/compiletime/dependent.scala +++ b/common/src/main/scala/io/kjaer/compiletime/dependent.scala @@ -1,5 +1,5 @@ //Author: Maxime Kjaer, taken from tf-dotty -package io.kjaer.compiletime +package org.emergentorder.io.kjaer.compiletime import scala.compiletime.ops.int.* diff --git a/core/src/main/scala/ONNX.scala b/core/src/main/scala/ONNX.scala index 9095626b..b9312460 100644 --- a/core/src/main/scala/ONNX.scala +++ b/core/src/main/scala/ONNX.scala @@ -1500,6 +1500,9 @@ package object onnx { } } + //TODO: move "axes" from attributes to inputs on the rest of the Reduce ops, + //as done below with ReduceSum (when updating to opset 18) + // // TODO: new attr : noop_with_empty_axes trait ReduceSumV13 extends Operator { def ReduceSumV13[ diff --git a/core/src/main/scala/ONNXBytesDataSource.scala b/core/src/main/scala/ONNXBytesDataSource.scala index f03e42da..6a166544 100644 --- a/core/src/main/scala/ONNXBytesDataSource.scala +++ b/core/src/main/scala/ONNXBytesDataSource.scala @@ -4,7 +4,7 @@ import spire.math.Numeric import org.emergentorder.onnx.* import org.emergentorder.onnx.Tensors.* import org.emergentorder.compiletime.* -import io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.* class ONNXBytesDataSource(onnxBytes: Array[Byte]) extends DataSource { diff --git a/core/src/main/scala/OpToONNXBytesConverter.scala b/core/src/main/scala/OpToONNXBytesConverter.scala index 538f987e..b629ff0f 100644 --- a/core/src/main/scala/OpToONNXBytesConverter.scala +++ b/core/src/main/scala/OpToONNXBytesConverter.scala @@ -15,7 +15,7 @@ import onnx.onnx.TensorProto.DataType.* import cats.effect.IO import cats.implicits.* -import io.kjaer.compiletime.Shape +import org.emergentorder.io.kjaer.compiletime.Shape import org.emergentorder.compiletime.* import org.emergentorder.onnx.Tensors.* import org.emergentorder.onnx.Tensors.Tensor.* diff --git a/core/src/main/scala/Tensors.scala b/core/src/main/scala/Tensors.scala index 634e134d..24e662fd 100644 --- a/core/src/main/scala/Tensors.scala +++ b/core/src/main/scala/Tensors.scala @@ -6,8 +6,8 @@ import spire.math.UInt import spire.math.ULong import spire.math.Complex import spire.math.Numeric -import io.kjaer.compiletime.* -import io.kjaer.compiletime.Shape.* +import org.emergentorder.io.kjaer.compiletime.* +import org.emergentorder.io.kjaer.compiletime.Shape.* import scala.compiletime.ops.int.* import cats.effect.IO diff --git a/get_models.sh b/get_models.sh index 7e62f6bf..3da241c0 100644 --- a/get_models.sh +++ b/get_models.sh @@ -1,2 +1,4 @@ curl -O "https://media.githubusercontent.com/media/onnx/models/main/vision/classification/squeezenet/model/squeezenet1.0-12.onnx" curl -o absnet.onnx "https://raw.githubusercontent.com/onnx/onnx/master/onnx/backend/test/data/node/test_abs/model.onnx" + +#TODO: add bert tokenizer here diff --git a/project/plugins.sbt b/project/plugins.sbt index 97d7f8db..6414610f 100755 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,3 +1,4 @@ +addDependencyTreePlugin addSbtPlugin("ch.epfl.scala" % "sbt-version-policy" % "2.1.0") addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0") addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")