-
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
29a21b3
commit ee5c36d
Showing
7 changed files
with
222 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
71 changes: 71 additions & 0 deletions
71
inference/inference-tfjs/src/jsMain/kotlin/io.kinference.tfjs/operators/tensor/Trilu.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,71 @@ | ||
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.* | ||
import io.kinference.ndarray.extensions.trilu | ||
import io.kinference.operator.* | ||
import io.kinference.protobuf.message.AttributeProto | ||
import io.kinference.protobuf.message.TensorProto | ||
import io.kinference.tfjs.data.tensors.TFJSTensor | ||
import io.kinference.tfjs.data.tensors.asTensor | ||
|
||
sealed class Trilu( | ||
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 = 14) | ||
|
||
operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>): Trilu { | ||
return when(version ?: DEFAULT_VERSION.sinceVersion) { | ||
in TriluVer14.VERSION.asRange() -> TriluVer14(name, attributes, inputs, outputs) | ||
else -> error("Unsupported version of Trilu operator: $version") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class TriluVer14( | ||
name: String, | ||
attributes: Map<String, Attribute<Any>>, | ||
inputs: List<String>, | ||
outputs: List<String> | ||
) : Trilu(name, INFO, attributes, inputs, outputs) { | ||
companion object { | ||
private val TYPE_CONSTRAINTS = ALL_DATA_TYPES | ||
|
||
private val INPUTS_INFO = listOf( | ||
IOInfo(0, TYPE_CONSTRAINTS, "input", optional = false, differentiable = true), | ||
IOInfo(1, setOf(TensorProto.DataType.INT64), "k", optional = true, differentiable = false) | ||
) | ||
|
||
private val OUTPUTS_INFO = listOf( | ||
IOInfo(0, TYPE_CONSTRAINTS, "output", optional = false, differentiable = true) | ||
) | ||
|
||
private val ATTRIBUTES_INFO = listOf( | ||
AttributeInfo("upper", setOf(AttributeProto.AttributeType.INT), required = false, default = 1L) | ||
) | ||
|
||
internal val VERSION = VersionInfo(sinceVersion = 14) | ||
private val INFO = OperatorInfo("Trilu", ATTRIBUTES_INFO, INPUTS_INFO, OUTPUTS_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN) | ||
} | ||
|
||
private val upper: Boolean by attribute { it: Number -> it != 0L } | ||
|
||
override suspend fun <D : ONNXData<*, *>> apply(contexts: Contexts<D>, inputs: List<TFJSTensor?>): List<TFJSTensor?> { | ||
val input = inputs[0]!!.data | ||
val kTensor = inputs.getOrNull(1)?.data as? NumberNDArrayTFJS | ||
|
||
val k = kTensor?.singleValue()?.toInt() ?: 0 | ||
val output = input.trilu(k, upper) | ||
|
||
return listOf(output.asTensor("output")) | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
inference/inference-tfjs/src/jsTest/kotlin/io/kinference/tfjs/operators/tensor/TriluTest.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,99 @@ | ||
package io.kinference.tfjs.operators.tensor | ||
|
||
import io.kinference.tfjs.runners.TFJSTestEngine.TFJSAccuracyRunner | ||
import io.kinference.utils.TestRunner | ||
import kotlin.test.Test | ||
|
||
class TriluTest { | ||
private fun getTargetPath(dirName: String) = "trilu/$dirName/" | ||
|
||
@Test | ||
fun test_tril() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril")) | ||
} | ||
|
||
@Test | ||
fun test_tril_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_neg")) | ||
} | ||
|
||
@Test | ||
fun test_tril_one_row_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_one_row_neg")) | ||
} | ||
|
||
@Test | ||
fun test_tril_out_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_out_neg")) | ||
} | ||
|
||
@Test | ||
fun test_tril_out_pos() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_out_pos")) | ||
} | ||
|
||
@Test | ||
fun test_tril_pos() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_pos")) | ||
} | ||
|
||
@Test | ||
fun test_tril_square() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_square")) | ||
} | ||
|
||
@Test | ||
fun test_tril_square_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_square_neg")) | ||
} | ||
|
||
@Test | ||
fun test_tril_zero() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_tril_zero")) | ||
} | ||
|
||
@Test | ||
fun test_triu() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu")) | ||
} | ||
|
||
@Test | ||
fun test_triu_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_neg")) | ||
} | ||
|
||
@Test | ||
fun test_triu_one_row() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_one_row")) | ||
} | ||
|
||
@Test | ||
fun test_triu_out_neg_out() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_out_neg_out")) | ||
} | ||
|
||
@Test | ||
fun test_triu_out_pos() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_out_pos")) | ||
} | ||
|
||
@Test | ||
fun test_triu_pos() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_pos")) | ||
} | ||
|
||
@Test | ||
fun test_triu_square() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_square")) | ||
} | ||
|
||
@Test | ||
fun test_triu_square_neg() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_square_neg")) | ||
} | ||
|
||
@Test | ||
fun test_triu_zero() = TestRunner.runTest { | ||
TFJSAccuracyRunner.runFromResources(getTargetPath("test_triu_zero")) | ||
} | ||
} |
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