Skip to content

Commit

Permalink
refactor(dataflow): Refactor conversion to binary contents (#4786)
Browse files Browse the repository at this point in the history
* Remove unused import

* Fix indentation

* Remove unnecessary braces in short-form conditionals

* Refactor per-data type conversion to bytes to extension methods

This has two key benefits.
Firstly, the same conversion logic was used for both requests and responses.
Thus, we are removing duplication and minimising the risk of differences creeping in.
Secondly, the business is easier to read, as the handling is moved outside.

* Move business logic functions above implementation details

* Rename file to better reflect logic

* Refactor typed -> binary contents function as extension methods

* Use run context method to ensure no mutations happen on the original object

* Reword comments for concision

* Reorder enum values into related groups

* Add comment on FP16 data type

* Add value for required byte order for binary contents

This order is defined by the specification, so we should not allow for it to change.
https://github.com/triton-inference-server/server/blob/c47b2392a4f81a35f88d416f3356eec60ff83dbf/docs/protocol/extension_binary_data.md#binary-tensor-request

* Restrict visibility of per-data type to-binary methods to private
  • Loading branch information
agrski committed May 1, 2023
1 parent 7e3e22c commit d92faed
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
Copyright 2023 Seldon Technologies Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.seldon.dataflow.kafka

import com.google.protobuf.kotlin.toByteString
import io.seldon.mlops.inference.v2.V2Dataplane.InferTensorContents
import io.seldon.mlops.inference.v2.V2Dataplane.ModelInferRequest
import io.seldon.mlops.inference.v2.V2Dataplane.ModelInferResponse
import java.nio.ByteBuffer
import java.nio.ByteOrder

// FP16 is only supported for binary contents, as Protobuf has no corresponding type.
enum class DataType {
BOOL,
BYTES,
UINT8, UINT16, UINT32, UINT64,
INT8, INT16, INT32, INT64,
FP16, FP32, FP64,
}

private val binaryContentsByteOrder = ByteOrder.LITTLE_ENDIAN

fun ModelInferRequest.withBinaryContents(): ModelInferRequest {
return this.toBuilder().run {
inputsList.forEachIndexed { idx, input ->
val v = when (DataType.valueOf(input.datatype)) {
DataType.UINT8 -> input.contents.toUint8Bytes()
DataType.UINT16 -> input.contents.toUint16Bytes()
DataType.UINT32 -> input.contents.toUint32Bytes()
DataType.UINT64 -> input.contents.toUint64Bytes()
DataType.INT8 -> input.contents.toInt8Bytes()
DataType.INT16 -> input.contents.toInt16Bytes()
DataType.INT32 -> input.contents.toInt32Bytes()
DataType.INT64 -> input.contents.toInt64Bytes()
DataType.BOOL -> input.contents.toBoolBytes()
DataType.FP16, // may need to handle this separately in future
DataType.FP32 -> input.contents.toFp32Bytes()
DataType.FP64 -> input.contents.toFp64Bytes()
DataType.BYTES -> input.contents.toRawBytes()
}

// Add binary data and clear corresponding contents.
addRawInputContents(v.toByteString())
getInputsBuilder(idx).clearContents()
}

build()
}
}

fun ModelInferResponse.withBinaryContents(): ModelInferResponse {
return this.toBuilder().run {
outputsList.forEachIndexed { idx, output ->
val v = when (DataType.valueOf(output.datatype)) {
DataType.UINT8 -> output.contents.toUint8Bytes()
DataType.UINT16 -> output.contents.toUint16Bytes()
DataType.UINT32 -> output.contents.toUint32Bytes()
DataType.UINT64 -> output.contents.toUint64Bytes()
DataType.INT8 -> output.contents.toInt8Bytes()
DataType.INT16 -> output.contents.toInt16Bytes()
DataType.INT32 -> output.contents.toInt32Bytes()
DataType.INT64 -> output.contents.toInt64Bytes()
DataType.BOOL -> output.contents.toBoolBytes()
DataType.FP16, // may need to handle this separately in future
DataType.FP32 -> output.contents.toFp32Bytes()
DataType.FP64 -> output.contents.toFp64Bytes()
DataType.BYTES -> output.contents.toRawBytes()
}

// Add binary data and clear corresponding contents.
addRawOutputContents(v.toByteString())
getOutputsBuilder(idx).clearContents()
}

build()
}
}

private fun InferTensorContents.toUint8Bytes(): ByteArray = this.uintContentsList
.flatMap {
ByteBuffer
.allocate(1)
.put(it.toByte())
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toUint16Bytes(): ByteArray = this.uintContentsList
.flatMap {
ByteBuffer
.allocate(UShort.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putShort(it.toShort())
.array()
.toList()
}.toByteArray()

private fun InferTensorContents.toUint32Bytes(): ByteArray = this.uintContentsList
.flatMap {
ByteBuffer
.allocate(UInt.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putInt(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toUint64Bytes(): ByteArray = this.uint64ContentsList
.flatMap {
ByteBuffer
.allocate(ULong.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putLong(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toInt8Bytes(): ByteArray = this.intContentsList
.flatMap {
ByteBuffer
.allocate(1)
.put(it.toByte())
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toInt16Bytes(): ByteArray = this.intContentsList
.flatMap {
ByteBuffer
.allocate(Short.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putShort(it.toShort())
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toInt32Bytes(): ByteArray = this.intContentsList
.flatMap {
ByteBuffer
.allocate(Int.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putInt(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toInt64Bytes(): ByteArray = this.int64ContentsList
.flatMap {
ByteBuffer
.allocate(Long.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putLong(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toFp32Bytes(): ByteArray = this.fp32ContentsList
.flatMap {
ByteBuffer
.allocate(Float.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putFloat(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toFp64Bytes(): ByteArray = this.fp64ContentsList
.flatMap {
ByteBuffer
.allocate(Double.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putDouble(it)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toBoolBytes(): ByteArray = this.boolContentsList
.flatMap {
ByteBuffer
.allocate(1)
.put(if (it) 1 else 0)
.array()
.toList()
}
.toByteArray()

private fun InferTensorContents.toRawBytes(): ByteArray = this.bytesContentsList
.flatMap {
ByteBuffer
.allocate(it.size() + Int.SIZE_BYTES)
.order(binaryContentsByteOrder)
.putInt(it.size())
.put(it.toByteArray())
.array()
.toList()
}
.toByteArray()
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ class Joiner(
var leftRequest = V2Dataplane.ModelInferRequest.parseFrom(left)
var rightRequest = V2Dataplane.ModelInferRequest.parseFrom(right)
if (leftRequest.rawInputContentsCount > 0 && rightRequest.rawInputContentsCount == 0) {
rightRequest = convertRequestToRawInputContents(rightRequest)
rightRequest = rightRequest.withBinaryContents()
} else if (rightRequest.rawInputContentsCount > 0 && leftRequest.rawInputContentsCount == 0) {
leftRequest = convertRequestToRawInputContents(leftRequest)
leftRequest = leftRequest.withBinaryContents()
}
val request = V2Dataplane.ModelInferRequest
.newBuilder()
Expand All @@ -223,9 +223,9 @@ class Joiner(
var leftResponse = V2Dataplane.ModelInferResponse.parseFrom(left)
var rightResponse = V2Dataplane.ModelInferResponse.parseFrom(right)
if (leftResponse.rawOutputContentsCount > 0 && rightResponse.rawOutputContentsCount == 0) {
rightResponse = convertResponseToRawOutputContents(rightResponse)
rightResponse = rightResponse.withBinaryContents()
} else if (rightResponse.rawOutputContentsCount > 0 && leftResponse.rawOutputContentsCount == 0) {
leftResponse = convertResponseToRawOutputContents(leftResponse)
leftResponse = leftResponse.withBinaryContents()
}
val response = V2Dataplane.ModelInferResponse
.newBuilder()
Expand Down
Loading

0 comments on commit d92faed

Please sign in to comment.