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..a6da4684f 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) + "Sinh" -> Sinh(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/Sinh.kt b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sinh.kt new file mode 100644 index 000000000..e7135ffb7 --- /dev/null +++ b/inference/inference-core/src/commonMain/kotlin/io/kinference.core/operators/activations/Sinh.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.sinh +import io.kinference.operator.* +import io.kinference.primitives.types.DataType + +sealed class Sinh( + name: String, + info: OperatorInfo, + attributes: Map>, + inputs: List, + outputs: List +) : Activation(name, info, attributes, inputs, outputs) { + companion object { + private val DEFAULT_VERSION = VersionInfo(sinceVersion = 9) + + operator fun invoke(name: String, version: Int?, attributes: Map>, inputs: List, outputs: List) = + when (version ?: DEFAULT_VERSION.sinceVersion) { + in SinhVer9.VERSION.asRange() -> SinhVer9(name, attributes, inputs, outputs) + else -> error("Unsupported version of Sinh operator: $version") + } + } +} + + +class SinhVer9( + name: String, + attributes: Map>, + inputs: List, + outputs: List +) : Sinh(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 = 9) + private val INFO = OperatorInfo("Sinh", 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).sinh() + DataType.DOUBLE -> (input as DoubleNDArray).sinh() + else -> error("Unsupported data type for this operation: $type") + } + } +} diff --git a/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinhTest.kt b/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinhTest.kt new file mode 100644 index 000000000..f373bd86e --- /dev/null +++ b/inference/inference-core/src/commonTest/kotlin/io/kinference/operators/activations/SinhTest.kt @@ -0,0 +1,19 @@ +package io.kinference.operators.activations + +import io.kinference.KITestEngine +import io.kinference.utils.TestRunner +import kotlin.test.Test + +class SinhTest { + private fun getTargetPath(dirName: String) = "sinh/$dirName/" + + @Test + fun test_sinh() = TestRunner.runTest { + KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sinh")) + } + + @Test + fun test_sinh_example() = TestRunner.runTest { + KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sinh_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..4a6a7dd47 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) + "Sinh" -> Sinh(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/Sinh.kt b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sinh.kt new file mode 100644 index 000000000..2b6ce9cd2 --- /dev/null +++ b/inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/activations/Sinh.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.sinh +import io.kinference.operator.* +import io.kinference.tfjs.data.tensors.TFJSTensor +import io.kinference.tfjs.data.tensors.asTensor + +sealed class Sinh( + name: String, + info: OperatorInfo, + attributes: Map>, + inputs: List, + outputs: List +) : Operator(name, info, attributes, inputs, outputs) { + companion object { + private val DEFAULT_VERSION = VersionInfo(sinceVersion = 9) + + operator fun invoke(name: String, version: Int?, attributes: Map>, inputs: List, outputs: List): Sinh { + return when (version ?: DEFAULT_VERSION.sinceVersion) { + in SinhVer9.VERSION.asRange() -> SinhVer9(name, attributes, inputs, outputs) + else -> error("Unsupported version of Sinh operator: $version") + } + } + } +} + +class SinhVer9( + name: String, + attributes: Map>, + inputs: List, + outputs: List +) : Sinh(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 = 9) + private val INFO = OperatorInfo("Sinh", 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.sinh().asTensor("output")) + } +} diff --git a/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinhTest.kt b/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinhTest.kt new file mode 100644 index 000000000..0066ae9d3 --- /dev/null +++ b/inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/activations/SinhTest.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 SinhTest { + private fun getTargetPath(dirName: String) = "sinh/$dirName/" + + @Test + fun test_sinh() = TestRunner.runTest { + TFJSAccuracyRunner.runFromResources(getTargetPath("test_sinh")) + } + + @Test + fun test_sinh_example() = TestRunner.runTest { + TFJSAccuracyRunner.runFromResources(getTargetPath("test_sinh_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..5f6dcd14b 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.sinh(): PrimitiveNDArray = applyElementWise { sinh(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..f4f4502c8 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 sinh(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..d43972ccd 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 sinh: (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..8716294d1 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 @@ -122,6 +122,8 @@ fun NumberNDArrayTFJS.asin() = NumberNDArrayTFJS(tfjsArray.asin()) fun NumberNDArrayTFJS.asinh() = NumberNDArrayTFJS(tfjsArray.asinh()) +fun NumberNDArrayTFJS.sinh() = NumberNDArrayTFJS(tfjsArray.sinh()) + fun NumberNDArrayTFJS.atan() = NumberNDArrayTFJS(tfjsArray.atan()) fun NumberNDArrayTFJS.atanh() = NumberNDArrayTFJS(tfjsArray.atanh()) 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..4e5dc204b 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 @@ -260,6 +260,8 @@ internal fun ArrayTFJS.asin() = asin(this) internal fun ArrayTFJS.asinh() = asinh(this) +internal fun ArrayTFJS.sinh() = sinh(this) + internal fun ArrayTFJS.atan() = atan(this) internal fun ArrayTFJS.atanh() = atanh(this) diff --git a/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/descriptor.txt b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/descriptor.txt new file mode 100644 index 000000000..7967d1896 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/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/sinh/test_sinh/model.onnx b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/model.onnx new file mode 100644 index 000000000..496db2d08 Binary files /dev/null and b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/model.onnx differ diff --git a/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/test_data_set_0/input_0.pb b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/test_data_set_0/input_0.pb new file mode 100644 index 000000000..bae0ffd63 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/test_data_set_0/input_0.pb @@ -0,0 +1 @@ +BxJx?h>z?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/sinh/test_sinh/test_data_set_0/output_0.pb b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/test_data_set_0/output_0.pb new file mode 100644 index 000000000..aa7c58789 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh/test_data_set_0/output_0.pb @@ -0,0 +1 @@ +ByJ$E5@gd>B?̹@,J@6? ӽ.>>@//V?F=E>w>b@S>7vHa3?y?6O-@~;=,@uH @W@DM>cD>)c" >0R?^?N˾%`2Oru*\@g1ݸͿ[?W5~[\>¼>>S=<>V-ҽ \ No newline at end of file diff --git a/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/descriptor.txt b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/descriptor.txt new file mode 100644 index 000000000..7967d1896 --- /dev/null +++ b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_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/sinh/test_sinh_example/model.onnx b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/model.onnx new file mode 100644 index 000000000..640cf0a01 Binary files /dev/null and b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/model.onnx differ diff --git a/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/input_0.pb b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/input_0.pb new file mode 100644 index 000000000..8a9445744 Binary files /dev/null and b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/input_0.pb differ diff --git a/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/output_0.pb b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/output_0.pb new file mode 100644 index 000000000..4cb4991cd Binary files /dev/null and b/utils/utils-testing/src/commonMain/resources/sinh/test_sinh_example/test_data_set_0/output_0.pb differ