From f4de147ca21bf05ec9554333acd3d2e9782bb7e3 Mon Sep 17 00:00:00 2001 From: AnastasiaTuchina Date: Mon, 17 Jul 2023 16:28:28 +0200 Subject: [PATCH] KI-43 [core, tfjs] Add Sin operator --- .../operators/KIOperatorFactory.kt | 1 + .../operators/activations/Sin.kt | 53 ++++++++++++++++++ .../operators/activations/SinTest.kt | 19 +++++++ .../operators/TFJSOperatorFactory.kt | 1 + .../operators/activations/Sin.kt | 53 ++++++++++++++++++ .../tfjs/operators/activations/SinTest.kt | 19 +++++++ .../activations/PrimitiveSimpleActivations.kt | 1 + .../ndarray/stubs/PrimitiveMathStubs.kt | 1 + .../io.kinference.ndarray/core/Functions.kt | 2 + .../extensions/NDArrayExtension.kt | 2 + .../extensions/TensorExtenstion.kt | 2 + .../resources/sin/test_sin/descriptor.txt | 2 + .../resources/sin/test_sin/model.onnx | Bin 0 -> 97 bytes .../sin/test_sin/test_data_set_0/input_0.pb | 1 + .../sin/test_sin/test_data_set_0/output_0.pb | 1 + .../sin/test_sin_example/descriptor.txt | 2 + .../resources/sin/test_sin_example/model.onnx | Bin 0 -> 89 bytes .../test_data_set_0/input_0.pb | Bin 0 -> 21 bytes .../test_data_set_0/output_0.pb | Bin 0 -> 21 bytes 19 files changed, 160 insertions(+) create mode 100644 inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sin.kt create mode 100644 inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinTest.kt create mode 100644 inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sin.kt create mode 100644 inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinTest.kt create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin/descriptor.txt create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin/model.onnx create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin/test_data_set_0/input_0.pb create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin/test_data_set_0/output_0.pb create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin_example/descriptor.txt create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin_example/model.onnx create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin_example/test_data_set_0/input_0.pb create mode 100644 utils/utils-testing/src/commonMain/resources/sin/test_sin_example/test_data_set_0/output_0.pb diff --git a/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/KIOperatorFactory.kt b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/KIOperatorFactory.kt index ade212a36..c510409a1 100755 --- a/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/KIOperatorFactory.kt +++ b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/KIOperatorFactory.kt @@ -126,6 +126,7 @@ object KIOperatorFactory : OperatorFactory> { "ScatterND" -> ScatterND(name, version, attributes, inputs, outputs) "Shape" -> Shape(name, version, attributes, inputs, outputs) "Sigmoid" -> Sigmoid(name, version, attributes, inputs, outputs) + "Sin" -> Sin(name, version, attributes, inputs, outputs) "Size" -> Size(name, version, attributes, inputs, outputs) "SkipLayerNormalization" -> SkipLayerNormalization(name, version, attributes, inputs, outputs) "Slice" -> Slice(name, version, attributes, inputs, outputs) diff --git a/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sin.kt b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sin.kt new file mode 100644 index 000000000..356b2965c --- /dev/null +++ b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sin.kt @@ -0,0 +1,53 @@ +package io.kinference.core.operators.activations + +import io.kinference.attribute.Attribute +import io.kinference.core.KIONNXData +import io.kinference.graph.Contexts +import io.kinference.ndarray.arrays.* +import io.kinference.ndarray.extensions.activations.sin +import io.kinference.operator.* +import io.kinference.primitives.types.DataType + +sealed class Sin( + name: String, + info: OperatorInfo, + attributes: Map>, + inputs: List, + outputs: List +) : Activation(name, info, attributes, inputs, outputs) { + companion object { + private val DEFAULT_VERSION = VersionInfo(sinceVersion = 7) + + operator fun invoke(name: String, version: Int?, attributes: Map>, inputs: List, outputs: List) = + when (version ?: DEFAULT_VERSION.sinceVersion) { + in SinVer7.VERSION.asRange() -> SinVer7(name, attributes, inputs, outputs) + else -> error("Unsupported version of Sin operator: $version") + } + } +} + + +class SinVer7( + name: String, + attributes: Map>, + inputs: List, + outputs: List +) : Sin(name, INFO, attributes, inputs, outputs) { + companion object { + private val TYPE_CONSTRAINTS = FLOAT_DATA_TYPES + + private val INPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "input", optional = false)) + private val OUTPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "output", optional = false)) + + internal val VERSION = VersionInfo(sinceVersion = 7) + private val INFO = OperatorInfo("Sin", emptySet(), INPUT_INFO, OUTPUT_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) + } + + override suspend fun activate(input: NDArrayCore, contexts: Contexts>): NDArrayCore { + return when (val type = input.type) { + DataType.FLOAT -> (input as FloatNDArray).sin() + DataType.DOUBLE -> (input as DoubleNDArray).sin() + else -> error("Unsupported data type for this operation: $type") + } + } +} diff --git a/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinTest.kt b/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinTest.kt new file mode 100644 index 000000000..10e7f0992 --- /dev/null +++ b/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinTest.kt @@ -0,0 +1,19 @@ +package io.kinference.operators.activations + +import io.kinference.KITestEngine +import io.kinference.utils.TestRunner +import kotlin.test.Test + +class SinTest { + private fun getTargetPath(dirName: String) = "sin/$dirName/" + + @Test + fun test_sin() = TestRunner.runTest { + KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sin")) + } + + @Test + fun test_sin_example() = TestRunner.runTest { + KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sin_example")) + } +} diff --git a/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/TFJSOperatorFactory.kt b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/TFJSOperatorFactory.kt index dc1bf81fe..06c73186b 100755 --- a/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/TFJSOperatorFactory.kt +++ b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/TFJSOperatorFactory.kt @@ -109,6 +109,7 @@ object TFJSOperatorFactory : OperatorFactory> { "ScatterND" -> ScatterND(name, version, attributes, inputs, outputs) "Shape" -> Shape(name, version, attributes, inputs, outputs) "Sigmoid" -> Sigmoid(name, version, attributes, inputs, outputs) + "Sin" -> Sin(name, version, attributes, inputs, outputs) "Size" -> Size(name, version, attributes, inputs, outputs) "SkipLayerNormalization" -> SkipLayerNormalization(name, version, attributes, inputs, outputs) "Slice" -> Slice(name, version, attributes, inputs, outputs) diff --git a/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sin.kt b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sin.kt new file mode 100644 index 000000000..5ca97f59e --- /dev/null +++ b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sin.kt @@ -0,0 +1,53 @@ +package io.kinference.tfjs.operators.activations + +import io.kinference.attribute.Attribute +import io.kinference.data.ONNXData +import io.kinference.graph.Contexts +import io.kinference.ndarray.arrays.NumberNDArrayTFJS +import io.kinference.ndarray.extensions.sin +import io.kinference.operator.* +import io.kinference.tfjs.data.tensors.TFJSTensor +import io.kinference.tfjs.data.tensors.asTensor + +sealed class Sin( + name: String, + info: OperatorInfo, + attributes: Map>, + inputs: List, + outputs: List +) : Operator(name, info, attributes, inputs, outputs) { + companion object { + private val DEFAULT_VERSION = VersionInfo(sinceVersion = 7) + + operator fun invoke(name: String, version: Int?, attributes: Map>, inputs: List, outputs: List): Sin { + return when (version ?: DEFAULT_VERSION.sinceVersion) { + in SinVer7.VERSION.asRange() -> SinVer7(name, attributes, inputs, outputs) + else -> error("Unsupported version of Sin operator: $version") + } + } + } +} + +class SinVer7( + name: String, + attributes: Map>, + inputs: List, + outputs: List +) : Sin(name, INFO, attributes, inputs, outputs) { + companion object { + private val TYPE_CONSTRAINTS = FLOAT_DATA_TYPES + + private val ATTRIBUTES_INFO = emptyList() + + private val INPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "input", optional = false)) + private val OUTPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "output", optional = false)) + + internal val VERSION = VersionInfo(sinceVersion = 7) + private val INFO = OperatorInfo("Sin", ATTRIBUTES_INFO, INPUT_INFO, OUTPUT_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) + } + + override suspend fun > apply(contexts: Contexts, inputs: List): List { + val input = inputs[0]!!.data as NumberNDArrayTFJS + return listOf(input.sin().asTensor("output")) + } +} diff --git a/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinTest.kt b/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinTest.kt new file mode 100644 index 000000000..72199ba6d --- /dev/null +++ b/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinTest.kt @@ -0,0 +1,19 @@ +package io.kinference.tfjs.operators.activations + +import io.kinference.tfjs.runners.TFJSTestEngine.TFJSAccuracyRunner +import io.kinference.utils.TestRunner +import kotlin.test.Test + +class SinTest { + private fun getTargetPath(dirName: String) = "sin/$dirName/" + + @Test + fun test_sin() = TestRunner.runTest { + TFJSAccuracyRunner.runFromResources(getTargetPath("test_sin")) + } + + @Test + fun test_sin_example() = TestRunner.runTest { + TFJSAccuracyRunner.runFromResources(getTargetPath("test_sin_example")) + } +} diff --git a/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/activations/PrimitiveSimpleActivations.kt b/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/activations/PrimitiveSimpleActivations.kt index ba1b66740..e92c5a263 100644 --- a/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/activations/PrimitiveSimpleActivations.kt +++ b/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/activations/PrimitiveSimpleActivations.kt @@ -19,6 +19,7 @@ fun PrimitiveNDArray.cosh(): PrimitiveNDArray = applyElementWise { cosh(it) } fun PrimitiveNDArray.asin(): PrimitiveNDArray = applyElementWise { asin(it) } fun PrimitiveNDArray.asinh(): PrimitiveNDArray = applyElementWise { asinh(it) } +fun PrimitiveNDArray.sin(): PrimitiveNDArray = applyElementWise { sin(it) } fun PrimitiveNDArray.atan(): PrimitiveNDArray = applyElementWise { atan(it) } fun PrimitiveNDArray.atanh(): PrimitiveNDArray = applyElementWise { atanh(it) } diff --git a/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/stubs/PrimitiveMathStubs.kt b/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/stubs/PrimitiveMathStubs.kt index fbb3552f2..c724a567f 100644 --- a/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/stubs/PrimitiveMathStubs.kt +++ b/ndarray/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/stubs/PrimitiveMathStubs.kt @@ -11,6 +11,7 @@ internal fun cosh(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationE internal fun asin(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationException() internal fun asinh(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationException() +internal fun sin(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationException() internal fun atan(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationException() internal fun atanh(x: PrimitiveType): PrimitiveType = throw UnsupportedOperationException() diff --git a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/core/Functions.kt b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/core/Functions.kt index f8def3b7a..860540541 100644 --- a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/core/Functions.kt +++ b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/core/Functions.kt @@ -168,3 +168,5 @@ internal external val floor: (x: ArrayTFJS) -> ArrayTFJS internal external val isInf: (x: ArrayTFJS) -> ArrayTFJS internal external val isNaN: (x: ArrayTFJS) -> ArrayTFJS + +internal external val sin: (x: ArrayTFJS) -> ArrayTFJS diff --git a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/NDArrayExtension.kt b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/NDArrayExtension.kt index 1f71586c4..56e10f0ba 100644 --- a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/NDArrayExtension.kt +++ b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/NDArrayExtension.kt @@ -128,6 +128,8 @@ fun NumberNDArrayTFJS.atanh() = NumberNDArrayTFJS(tfjsArray.atanh()) fun NumberNDArrayTFJS.tan() = NumberNDArrayTFJS(tfjsArray.tan()) +fun NumberNDArrayTFJS.sin() = NumberNDArrayTFJS(tfjsArray.sin()) + fun NumberNDArrayTFJS.moments(axis: Int, keepDims: Boolean = false) = tfjsArray.moments(axis, keepDims).toNDArray() fun NumberNDArrayTFJS.moments(axes: Array, keepDims: Boolean = false) = tfjsArray.moments(axes, keepDims).toNDArray() diff --git a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/TensorExtenstion.kt b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/TensorExtenstion.kt index b5f2a2afe..929610214 100644 --- a/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/TensorExtenstion.kt +++ b/ndarray/ndarray-tfjs/src/jsMain/kotlin/io.kinference.ndarray/extensions/TensorExtenstion.kt @@ -298,3 +298,5 @@ internal fun ArrayTFJS.isInf() = isInf(this) internal fun ArrayTFJS.isNaN() = isNaN(this) internal fun ArrayTFJS.bandPart(numLower: Int = 0, numUpper: Int = 0) = linalg.bandPart(this, numLower, numUpper) + +internal fun ArrayTFJS.sin() = sin(this) diff --git a/utils/utils-testing/src/commonMain/resources/sin/test_sin/descriptor.txt b/utils/utils-testing/src/commonMain/resources/sin/test_sin/descriptor.txt new file mode 100644 index 000000000..7967d1896 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sin/test_sin/descriptor.txt @@ -0,0 +1,2 @@ +test_data_set_0/input_0.pb +test_data_set_0/output_0.pb \ No newline at end of file diff --git a/utils/utils-testing/src/commonMain/resources/sin/test_sin/model.onnx b/utils/utils-testing/src/commonMain/resources/sin/test_sin/model.onnx new file mode 100644 index 0000000000000000000000000000000000000000..724fc74d610f215ab69815bf04283d976a682021 GIT binary patch literal 97 zcmd;J7UD@tOwLZtOVKS!EiSS0z?j@$ ?.z8s?bhdӽ9>(>%?^B?0= B>]ת>=?RiJ>Z/d#S'?K]?=C@(Hm;= ?2??>>Ec! >*z??Oƾmǚ6&õgڿ?xFKྙ[ G?4οYL=e> kQN>.:=ݚ>b"6 \ No newline at end of file diff --git a/utils/utils-testing/src/commonMain/resources/sin/test_sin/test_data_set_0/output_0.pb b/utils/utils-testing/src/commonMain/resources/sin/test_sin/test_data_set_0/output_0.pb new file mode 100644 index 000000000..d35bacf4d --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sin/test_sin/test_data_set_0/output_0.pb @@ -0,0 +1 @@ +ByJ;{?t>]mT?H?Rt?7T_?P?f]ӽv^>>C~?m0?v=t>L>=??Pa>A!?*B?-C?LD~\;=>?~?3> >Fmj#*s>OMq?un?cn4]u}}zm?E0پ.*s 3?#4XGO0>sNlF>x =Ճ>` \ No newline at end of file diff --git a/utils/utils-testing/src/commonMain/resources/sin/test_sin_example/descriptor.txt b/utils/utils-testing/src/commonMain/resources/sin/test_sin_example/descriptor.txt new file mode 100644 index 000000000..7967d1896 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sin/test_sin_example/descriptor.txt @@ -0,0 +1,2 @@ +test_data_set_0/input_0.pb +test_data_set_0/output_0.pb \ No newline at end of file diff --git a/utils/utils-testing/src/commonMain/resources/sin/test_sin_example/model.onnx b/utils/utils-testing/src/commonMain/resources/sin/test_sin_example/model.onnx new file mode 100644 index 0000000000000000000000000000000000000000..abdb5ae6e3e107894e337b530de9da824c0d8dd6 GIT binary patch literal 89 zcmd;J7UD@tOwLZtOVKS!EiSQg