-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75d1976
commit 5f28272
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/math/Sqrt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.kinference.tfjs.operators.math | ||
|
||
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.sqrt | ||
import io.kinference.operator.* | ||
import io.kinference.tfjs.data.tensors.TFJSTensor | ||
import io.kinference.tfjs.data.tensors.asTensor | ||
|
||
sealed class Sqrt( | ||
name: String, | ||
info: OperatorInfo, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Operator<TFJSTensor, TFJSTensor>(name, info, attributes, inputs, outputs) { | ||
companion object { | ||
private val DEFAULT_VERSION = VersionInfo(sinceVersion = 6) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): Sqrt { | ||
return when (version ?: DEFAULT_VERSION.sinceVersion) { | ||
in SqrtVer6.VERSION.asRange() -> SqrtVer6(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of Sqrt operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
class SqrtVer6( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Sqrt(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val TYPE_CONSTRAINTS = FLOAT_DATA_TYPES | ||
|
||
private val INPUTS_INFO = listOf( | ||
IOInfo(0, TYPE_CONSTRAINTS, "X", optional = false) | ||
) | ||
|
||
private val OUTPUTS_INFO = listOf( | ||
IOInfo(0, TYPE_CONSTRAINTS, "Y", optional = false) | ||
) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 6) | ||
private val INFO = OperatorInfo("Sqrt", emptyMap(), INPUTS_INFO, OUTPUTS_INFO, VERSION, domain = OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<TFJSTensor?>): List<TFJSTensor?> { | ||
val input = inputs[0]!!.data as NumberNDArrayTFJS | ||
|
||
return listOf(input.sqrt().asTensor("Y")) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/math/SqrtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.kinference.tfjs.operators.math | ||
|
||
import io.kinference.tfjs.runners.TFJSTestEngine | ||
import io.kinference.utils.TestRunner | ||
import kotlin.test.Test | ||
|
||
class SqrtTest { | ||
private fun getTargetPath(dirName: String) = "sqrt/$dirName/" | ||
|
||
@Test | ||
fun test_sqrt() = TestRunner.runTest { | ||
TFJSTestEngine.TFJSAccuracyRunner.runFromResources(getTargetPath("test_sqrt")) | ||
} | ||
|
||
@Test | ||
fun test_sqrt_example() = TestRunner.runTest { | ||
TFJSTestEngine.TFJSAccuracyRunner.runFromResources(getTargetPath("test_sqrt_example")) | ||
} | ||
} |