-
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 #110 from JetBrains-Research/size-op
KI-40 Size operator
- Loading branch information
Showing
16 changed files
with
165 additions
and
4 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-core/src/commonMain/kotlin/io/kinference.core/operators/tensor/Size.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.core.operators.tensor | ||
|
||
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.operator.* | ||
import io.kinference.protobuf.message.TensorProto | ||
|
||
sealed class Size( | ||
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 = 1) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): Size { | ||
return when (version ?: DEFAULT_VERSION.sinceVersion) { | ||
in SizeVer1.VERSION.asRange() -> SizeVer1(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of Size operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class SizeVer1( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Size(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val ATTRIBUTES_INFO = emptyList<AttributeInfo>() | ||
|
||
private val INPUTS_INFO = listOf( | ||
IOInfo(0, PRIMITIVE_DATA_TYPES, "data", differentiable = true, optional = false) | ||
) | ||
|
||
private val OUTPUTS_INFO = listOf( | ||
IOInfo(0, setOf(TensorProto.DataType.INT64), "size", differentiable = true, optional = false) | ||
) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 1) | ||
private val INFO = OperatorInfo("Size", ATTRIBUTES_INFO, INPUTS_INFO, OUTPUTS_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<KITensor?>): List<KITensor?> { | ||
val inputSize = inputs[0]!!.data.linearSize | ||
val dataSize = LongNDArray.scalar(inputSize.toLong()) | ||
return listOf(dataSize.asTensor("size")) | ||
} | ||
} |
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
...rence/inference-core/src/commonTest/kotlin/io/kinference/operators/operations/SizeTest.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.operations | ||
|
||
import io.kinference.KITestEngine | ||
import io.kinference.utils.TestRunner | ||
import kotlin.test.Test | ||
|
||
class SizeTest { | ||
private fun getTargetPath(dirName: String) = "size/$dirName/" | ||
|
||
@Test | ||
fun test_size_example() = TestRunner.runTest { | ||
KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_size_example")) | ||
} | ||
|
||
@Test | ||
fun test_size() = TestRunner.runTest { | ||
KITestEngine.KIAccuracyRunner.runFromResources(getTargetPath("test_size")) | ||
} | ||
} |
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/tensor/Size.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.tensor | ||
|
||
import io.kinference.attribute.Attribute | ||
import io.kinference.data.ONNXData | ||
import io.kinference.graph.Contexts | ||
import io.kinference.ndarray.arrays.NDArrayTFJS | ||
import io.kinference.operator.* | ||
import io.kinference.protobuf.message.TensorProto | ||
import io.kinference.tfjs.data.tensors.TFJSTensor | ||
import io.kinference.tfjs.data.tensors.asTensor | ||
|
||
sealed class Size( | ||
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 = 1) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): Size { | ||
return when (version ?: DEFAULT_VERSION.sinceVersion) { | ||
in SizeVer1.VERSION.asRange() -> SizeVer1(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of Size operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class SizeVer1( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Size(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val ATTRIBUTES_INFO = emptyList<AttributeInfo>() | ||
|
||
private val INPUTS_INFO = listOf( | ||
IOInfo(0, PRIMITIVE_DATA_TYPES, "data", differentiable = true, optional = false) | ||
) | ||
|
||
private val OUTPUTS_INFO = listOf( | ||
IOInfo(0, setOf(TensorProto.DataType.INT64), "size", differentiable = true, optional = false) | ||
) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 1) | ||
private val INFO = OperatorInfo("Size", ATTRIBUTES_INFO, INPUTS_INFO, OUTPUTS_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<TFJSTensor?>): List<TFJSTensor?> { | ||
val inputSize = inputs[0]!!.data.linearSize | ||
val dataSize = NDArrayTFJS.intScalar(inputSize) | ||
return listOf(dataSize.asTensor("size")) | ||
} | ||
} |
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-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/tensor/SizeTest.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.tensor | ||
|
||
import io.kinference.tfjs.runners.TFJSTestEngine.TFJSAccuracyRunner | ||
import io.kinference.utils.TestRunner | ||
import kotlin.test.Test | ||
|
||
class SizeTest { | ||
private fun getTargetPath(dirName: String) = "size/$dirName/" | ||
|
||
@Test | ||
fun test_size_example() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_size_example")) | ||
} | ||
|
||
@Test | ||
fun test_size() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_size")) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
utils/utils-testing/src/commonMain/resources/size/test_size/descriptor.txt
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,2 @@ | ||
test_data_set_0/input_0.pb | ||
test_data_set_0/output_0.pb |
Binary file added
BIN
+87 Bytes
utils/utils-testing/src/commonMain/resources/size/test_size/model.onnx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
utils/utils-testing/src/commonMain/resources/size/test_size/test_data_set_0/input_0.pb
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 @@ | ||
BxJ�x��?h��>��z?�j@$�?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�=B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6��� |
Binary file added
BIN
+15 Bytes
utils/utils-testing/src/commonMain/resources/size/test_size/test_data_set_0/output_0.pb
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
utils/utils-testing/src/commonMain/resources/size/test_size_example/descriptor.txt
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,2 @@ | ||
test_data_set_0/input_0.pb | ||
test_data_set_0/output_0.pb |
Binary file added
BIN
+91 Bytes
utils/utils-testing/src/commonMain/resources/size/test_size_example/model.onnx
Binary file not shown.
Binary file added
BIN
+35 Bytes
.../utils-testing/src/commonMain/resources/size/test_size_example/test_data_set_0/input_0.pb
Binary file not shown.
Binary file added
BIN
+15 Bytes
...utils-testing/src/commonMain/resources/size/test_size_example/test_data_set_0/output_0.pb
Binary file not shown.