-
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 #115 from JetBrains-Research/seqLen-op
KI-46 [core, tfjs] Add SequenceLength operator
- Loading branch information
Showing
4 changed files
with
116 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
56 changes: 56 additions & 0 deletions
56
...e/inference-core/src/commonMain/kotlin/io/kinference.core/operators/seq/SequenceLength.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,56 @@ | ||
package io.kinference.core.operators.seq | ||
|
||
import io.kinference.attribute.Attribute | ||
import io.kinference.core.data.seq.KIONNXSequence | ||
import io.kinference.core.data.tensor.KITensor | ||
import io.kinference.core.data.tensor.asTensor | ||
import io.kinference.data.ONNXData | ||
import io.kinference.data.ONNXDataType | ||
import io.kinference.graph.Contexts | ||
import io.kinference.ndarray.arrays.LongNDArray | ||
import io.kinference.operator.* | ||
import io.kinference.protobuf.message.TensorProto | ||
|
||
sealed class SequenceLength( | ||
name: String, | ||
info: OperatorInfo, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Operator<KIONNXSequence, KITensor>(name, info, attributes, inputs, outputs) { | ||
companion object { | ||
private val DEFAULT_VERSION = VersionInfo(sinceVersion = 11) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): SequenceLength { | ||
return when (version ?: DEFAULT_VERSION.sinceVersion) { | ||
in SequenceLengthVer11.VERSION.asRange() -> SequenceLengthVer11(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of SequenceLength operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class SequenceLengthVer11 internal constructor( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : SequenceLength(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val TYPE_CONSTRAINTS = ALL_DATA_TYPES | ||
|
||
private val INPUTS_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "input_sequence", optional = false, onnxDataType = ONNXDataType.ONNX_SEQUENCE)) | ||
|
||
private val OUTPUTS_INFO = listOf(IOInfo(0, setOf(TensorProto.DataType.INT64), "length", optional = false)) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 11) | ||
private val INFO = OperatorInfo("SequenceLength", emptyMap(), INPUTS_INFO, OUTPUTS_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<KIONNXSequence?>): List<KITensor?> { | ||
val seq = inputs.first()!!.data | ||
val seqLength = LongNDArray.scalar(seq.size.toLong()) | ||
return listOf(seqLength.asTensor("length")) | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...rence/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/seq/SequenceLength.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,56 @@ | ||
package io.kinference.tfjs.operators.seq | ||
|
||
import io.kinference.attribute.Attribute | ||
import io.kinference.data.ONNXData | ||
import io.kinference.data.ONNXDataType | ||
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.seq.TFJSSequence | ||
import io.kinference.tfjs.data.tensors.TFJSTensor | ||
import io.kinference.tfjs.data.tensors.asTensor | ||
|
||
sealed class SequenceLength( | ||
name: String, | ||
info: OperatorInfo, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Operator<TFJSSequence, TFJSTensor>(name, info, attributes, inputs, outputs) { | ||
companion object { | ||
private val DEFAULT_VERSION = VersionInfo(sinceVersion = 11) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): SequenceLength { | ||
return when (version ?: DEFAULT_VERSION.sinceVersion) { | ||
in SequenceLengthVer11.VERSION.asRange() -> SequenceLengthVer11(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of SequenceLength operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class SequenceLengthVer11 internal constructor( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : SequenceLength(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val TYPE_CONSTRAINTS = ALL_DATA_TYPES | ||
|
||
private val INPUTS_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "input_sequence", optional = false, onnxDataType = ONNXDataType.ONNX_SEQUENCE)) | ||
|
||
private val OUTPUTS_INFO = listOf(IOInfo(0, setOf(TensorProto.DataType.INT64), "length", optional = false)) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 11) | ||
private val INFO = OperatorInfo("SequenceLength", emptyMap(), INPUTS_INFO, OUTPUTS_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<TFJSSequence?>): List<TFJSTensor?> { | ||
val seq = inputs.first()!!.data | ||
val seqLength = NDArrayTFJS.intScalar(seq.size) | ||
return listOf(seqLength.asTensor("length")) | ||
} | ||
} |