Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Softmax-13 #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import io.kinference.protobuf.message.AttributeProto

sealed class Softmax(name: String, info: OperatorInfo, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>) : Activation(name, info, attributes, inputs, outputs) {
companion object {
private val DEFAULT_VERSION = VersionInfo(sinceVersion = 1, untilVersion = 13)
private val DEFAULT_VERSION = VersionInfo(sinceVersion = 1, untilVersion = 15)

operator fun invoke(name: String, version: Int?, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>) = when (version ?: DEFAULT_VERSION.sinceVersion) {
in SoftmaxVer1.VERSION.asRange() -> SoftmaxVer1(name, attributes, inputs, outputs)
in SoftmaxVer13.VERSION.asRange() -> SoftmaxVer13(name, attributes, inputs, outputs)
else -> error("Unsupported version of Softmax operator: $version")
}
}
Expand Down Expand Up @@ -43,3 +44,27 @@ class SoftmaxVer1(name: String, attributes: Map<String, Attribute<Any>>, inputs:
return input.softmax(axis)
}
}

class SoftmaxVer13(name: String, attributes: Map<String, Attribute<Any>>, inputs: List<String>, outputs: List<String>) : Softmax(name, INFO, attributes, inputs, outputs) {
companion object {
private val TYPE_CONSTRAINTS = FLOAT_DATA_TYPES

private val ATTRIBUTES_INFO = listOf(
AttributeInfo("axis", setOf(AttributeProto.AttributeType.INT), false, default = -1)
)

private val INPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "input", optional = false))
private val OUTPUT_INFO = listOf(IOInfo(0, TYPE_CONSTRAINTS, "output", optional = false))

internal val VERSION = VersionInfo(sinceVersion = 13, untilVersion = 15)
private val INFO = OperatorInfo("Softmax", ATTRIBUTES_INFO, INPUT_INFO, OUTPUT_INFO, VERSION, OperatorInfo.DEFAULT_DOMAIN)
}

private val axis: Int by attribute { it: Number -> it.toInt() }

override suspend fun activate(input: NDArrayCore, contexts: Contexts<KIONNXData<*>>): NDArrayCore {
input as NumberNDArrayCore
// TODO: Change softmax implementation to *perform on axis* instead of converting to 2D, per Softmax-13 description of "Axis" attribute.
return input.softmax(axis)
}
}