-
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.
Merge pull request #109 from JetBrains-Research/sqrt-op
KI-39 Sqrt operator
- Loading branch information
Showing
45 changed files
with
302 additions
and
441 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
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
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
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
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
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
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
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
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
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
64 changes: 64 additions & 0 deletions
64
inference/inference-core/src/commonMain/kotlin/io/kinference.core/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,64 @@ | ||
package io.kinference.core.operators.math | ||
|
||
import io.kinference.attribute.Attribute | ||
import io.kinference.core.data.tensor.KITensor | ||
import io.kinference.core.data.tensor.asTensor | ||
import io.kinference.data.ONNXData | ||
import io.kinference.graph.Contexts | ||
import io.kinference.ndarray.arrays.* | ||
import io.kinference.ndarray.extensions.sqrt | ||
import io.kinference.operator.* | ||
import io.kinference.primitives.types.DataType | ||
|
||
sealed class Sqrt( | ||
name: String, | ||
info: OperatorInfo, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Operator<KITensor, KITensor>(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<KITensor?>): List<KITensor?> { | ||
val input = inputs[0]!!.data as NumberNDArrayCore | ||
val output = when(val type = input.type) { | ||
DataType.FLOAT -> (input as FloatNDArray).sqrt() | ||
DataType.DOUBLE -> (input as DoubleNDArray).sqrt() | ||
else -> error("Unsupported data type: $type") | ||
} | ||
|
||
return listOf(output.asTensor("Y")) | ||
} | ||
} |
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
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
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
19 changes: 19 additions & 0 deletions
19
inference/inference-core/src/commonTest/kotlin/io/kinference/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.operators.math | ||
|
||
import io.kinference.KITestEngine | ||
import io.kinference.utils.TestRunner | ||
import kotlin.test.Test | ||
|
||
class SqrtTest { | ||
private fun getTargetPath(dirName: String) = "sqrt/$dirName/" | ||
|
||
@Test | ||
fun test_sqrt() = TestRunner.runTest { | ||
KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sqrt")) | ||
} | ||
|
||
@Test | ||
fun test_sqrt_example() = TestRunner.runTest { | ||
KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_sqrt_example")) | ||
} | ||
} |
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")) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...array-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/PrimitiveElementWise.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,46 @@ | ||
@file:GeneratePrimitives(DataType.ALL) | ||
|
||
package io.kinference.ndarray.extensions | ||
|
||
import io.kinference.ndarray.arrays.* | ||
import io.kinference.primitives.annotations.GeneratePrimitives | ||
import io.kinference.primitives.types.DataType | ||
import io.kinference.primitives.types.PrimitiveType | ||
|
||
fun PrimitiveNDArray.applyElementWise(func: (PrimitiveType) -> PrimitiveType): MutablePrimitiveNDArray { | ||
val output = MutablePrimitiveNDArray(strides) | ||
|
||
val inputBlockIter = array.blocks.iterator() | ||
val outputBlockIter = output.array.blocks.iterator() | ||
val blockSize = output.array.blockSize | ||
|
||
repeat(output.array.blocksNum) { | ||
val inputBlock = inputBlockIter.next() | ||
val outputBlock = outputBlockIter.next() | ||
|
||
for (idx in 0 until blockSize) { | ||
outputBlock[idx] = func(inputBlock[idx]) | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
fun PrimitiveNDArray.predicateElementWise(predicate: (PrimitiveType) -> Boolean): BooleanNDArray { | ||
val output = MutableBooleanNDArray(strides) | ||
|
||
val inputBlockIter = array.blocks.iterator() | ||
val outputBlockIter = output.array.blocks.iterator() | ||
val blockSize = output.array.blockSize | ||
|
||
repeat(output.array.blocksNum) { | ||
val inputBlock = inputBlockIter.next() | ||
val outputBlock = outputBlockIter.next() | ||
|
||
for (idx in 0 until blockSize) { | ||
outputBlock[idx] = predicate(inputBlock[idx]) | ||
} | ||
} | ||
|
||
return output | ||
} |
20 changes: 20 additions & 0 deletions
20
...ay/ndarray-core/src/commonMain/kotlin/io/kinference/ndarray/extensions/PrimitiveFPMath.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,20 @@ | ||
@file:GeneratePrimitives( | ||
DataType.DOUBLE, | ||
DataType.FLOAT | ||
) | ||
|
||
package io.kinference.ndarray.extensions | ||
|
||
import io.kinference.ndarray.arrays.BooleanNDArray | ||
import io.kinference.ndarray.arrays.PrimitiveNDArray | ||
import io.kinference.ndarray.stubs.* | ||
import io.kinference.primitives.annotations.GeneratePrimitives | ||
import io.kinference.primitives.types.DataType | ||
import kotlin.math.* | ||
|
||
fun PrimitiveNDArray.isNaN(): BooleanNDArray = predicateElementWise { it.isNaN() } | ||
|
||
fun PrimitiveNDArray.ceil(): PrimitiveNDArray = applyElementWise { ceil(it) } | ||
fun PrimitiveNDArray.floor(): PrimitiveNDArray = applyElementWise { floor(it) } | ||
|
||
fun PrimitiveNDArray.sqrt(): PrimitiveNDArray = applyElementWise { sqrt(it) } |
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
Oops, something went wrong.