Skip to content

Convert Tensor from Java to Kotlin (#19823)#19823

Merged
meta-codesync[bot] merged 1 commit into
mainfrom
export-D106557156
Jun 1, 2026
Merged

Convert Tensor from Java to Kotlin (#19823)#19823
meta-codesync[bot] merged 1 commit into
mainfrom
export-D106557156

Conversation

@kirklandsign
Copy link
Copy Markdown
Contributor

@kirklandsign kirklandsign commented May 27, 2026

Summary:

Convert Tensor.java to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

  • abstract class Tensor with protected constructor for subclass access
  • JvmField DoNotStrip val shape for direct JNI field access
  • JvmStatic on all companion factory methods (fromBlob, ones, zeros, numel, allocate*Buffer)
  • DoNotStrip on JNI-called methods (nativeNewTensor, dtypeJniCode, getRawDataBuffer)
  • Nested tensor type classes (Tensor_uint8, Tensor_int8, etc.) with internal constructor
  • halfBitsToFloat moved to Tensor_float16.Companion, using Float.fromBits()
  • when expressions replace switch statements and if-else chains
  • Kotlin array initializers for ones()/zeros()
  • Updated BUCK to reference .kt source

Reviewed By: psiddh

Differential Revision: D106557156

Copilot AI review requested due to automatic review settings May 27, 2026 21:04
@kirklandsign kirklandsign requested a review from psiddh as a code owner May 27, 2026 21:04
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 27, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19823

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 27, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 27, 2026

@kirklandsign has exported this pull request. If you are a Meta employee, you can view the originating Diff in D106557156.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates the Android ExecuTorch Tensor API from Java to Kotlin, aiming to keep JNI integration and Java-callable factory methods working while making the implementation more idiomatic Kotlin.

Changes:

  • Added a Kotlin implementation of org.pytorch.executorch.Tensor with nested dtype-specific tensor classes and companion factory helpers.
  • Removed the legacy Tensor.java implementation.
  • Updated the Android BUCK target to compile Tensor.kt instead of Tensor.java.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
extension/android/executorch_android/src/main/java/org/pytorch/executorch/Tensor.kt Kotlin rewrite of the Tensor API, including factories, JNI glue, serialization, and dtype-specific nested implementations.
extension/android/executorch_android/src/main/java/org/pytorch/executorch/Tensor.java Deleted the previous Java implementation after migration to Kotlin.
extension/android/BUCK Switched BUCK source list from Tensor.java to Tensor.kt.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +476 to +494
fun fromBlobUnsigned(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_uint8(byteBuffer, shape)
}

@JvmStatic
fun fromBlob(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_int8(byteBuffer, shape)
*/
@JvmStatic
fun fromByteArray(bytes: ByteArray): Tensor {
require(bytes != null) { "bytes cannot be null" }
Comment on lines +728 to +735
return when (dtype.toInt()) {
DType.UINT8.jniCode -> Tensor_uint8(buffer, shape)
DType.INT8.jniCode -> Tensor_int8(buffer, shape)
DType.HALF.jniCode -> Tensor_float16(buffer.asShortBuffer(), shape)
DType.INT32.jniCode -> Tensor_int32(buffer.asIntBuffer(), shape)
DType.INT64.jniCode -> Tensor_int64(buffer.asLongBuffer(), shape)
DType.FLOAT.jniCode -> Tensor_float32(buffer.asFloatBuffer(), shape)
DType.DOUBLE.jniCode -> Tensor_float64(buffer.asDoubleBuffer(), shape)
Comment on lines +718 to +727
val buffer = ByteBuffer.wrap(bytes)
require(buffer.hasRemaining()) { "invalid buffer" }
val dtype = buffer.get()
val shapeLength = buffer.get()
val shape = LongArray(shapeLength.toInt())
for (i in shape.indices) {
val dim = buffer.getInt()
require(dim >= 0) { "invalid shape" }
shape[i] = dim.toLong()
}
kirklandsign added a commit that referenced this pull request May 27, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Differential Revision: D106557156
@meta-codesync meta-codesync Bot changed the title Convert Tensor from Java to Kotlin Convert Tensor from Java to Kotlin (#19823) May 27, 2026
@kirklandsign
Copy link
Copy Markdown
Contributor Author

@claude review

@claude
Copy link
Copy Markdown

claude Bot commented May 27, 2026

Claude finished @kirklandsign's task in 2m 15s —— View job


Code Review: Convert Tensor from Java to Kotlin (#19823)

  • Read the new Tensor.kt file
  • Review BUCK changes
  • Analyze JNI compatibility and correctness
  • Check for bugs and potential issues
  • Post review findings

Overall this is a clean and faithful conversion. The when expressions, Kotlin array initializers, and companion object structure all look good. The JNI annotations (@DoNotStrip, @JvmStatic, @JvmField) are correctly placed. A few issues worth addressing:


1. Null checks on non-null parameters are unreachable (behavioral change)

Tensor.kt:477-478, 487-488, and all fromBlob/fromBlobUnsigned overloads:

Parameters are declared non-null (e.g., data: ByteArray, shape: LongArray), but the code still calls checkArgument(data != null, ...). Kotlin generates an intrinsic null-check that throws NullPointerException before your checkArgument is reached.

This changes behavior for Java callers: they previously got IllegalArgumentException("Data array must be not null") and now get a Kotlin-generated NPE. If preserving the original exception type matters for existing Java callers, either:

  • Make parameters nullable (data: ByteArray?) and keep the explicit checks, or
  • Accept the NPE behavior and remove the dead checkArgument calls (cleaner Kotlin)

Same issue in checkShape(shape) at line 748 and fromByteArray at line 717 (require(bytes != null)).


2. shape field visibility widened from package-private to public

Tensor.kt:47: @DoNotStrip @JvmField val shape: LongArray

The original Java had @DoNotStrip final long[] shape; which is package-private. With @JvmField, this becomes a public final field in bytecode. While val prevents reassignment, LongArray is mutable — external callers can now do tensor.shape[0] = 999 and corrupt the tensor's metadata. The constructor's shape.copyOf() protects against mutation of the input array, but the internal array is now exposed.

Consider adding a @JvmField with reduced visibility, or removing @JvmField and relying on the existing shape() method (which returns a defensive copy) for public access. If @JvmField is required for JNI field access, this may be intentional — just noting the behavioral difference.


3. fromByteArray buffer corruption for UINT8/INT8 (pre-existing bug, opportunity to fix)

Tensor.kt:729-730:

DType.UINT8.jniCode -> Tensor_uint8(buffer, shape)
DType.INT8.jniCode -> Tensor_int8(buffer, shape)

The buffer has its position past the header at this point. When Tensor_uint8.getDataAsUnsignedByteArray() (line 214) calls data.rewind(), it resets position to 0 — including the serialized dtype and shape header bytes — corrupting the returned data.

This was the same in the original Java, but since you're rewriting this code anyway, you could fix it with buffer.slice():

DType.UINT8.jniCode -> Tensor_uint8(buffer.slice(), shape)
DType.INT8.jniCode -> Tensor_int8(buffer.slice(), shape)

(The other dtypes use buffer.asShortBuffer() / asIntBuffer() / etc., which already create views from the current position, so they're fine.)


4. Minor: shapeLength sign handling in fromByteArray

Tensor.kt:721-722:

val shapeLength = buffer.get()
val shape = LongArray(shapeLength.toInt())

Byte.toInt() preserves sign, so a byte value of e.g., 0xFF would produce -1, causing NegativeArraySizeException. This is unlikely (no tensor has > 127 dims) and matches the original Java behavior, but a toInt() and 0xFF would be more defensive:

val shape = LongArray(shapeLength.toInt() and 0xFF)

5. BUCK change looks correct

Tensor.javaTensor.kt in the source list, consistent with the language = "KOTLIN" and pure_kotlin = False settings.


Summary

The conversion is well-executed and preserves JNI compatibility. The main actionable item is issue #1 (null-check behavioral change), which could break existing Java callers that catch IllegalArgumentException for null inputs. Issue #3 is a good opportunity to fix a pre-existing bug.

Copilot AI review requested due to automatic review settings May 27, 2026 23:45
@meta-codesync meta-codesync Bot changed the title Convert Tensor from Java to Kotlin (#19823) Convert Tensor from Java to Kotlin May 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment on lines +497 to +534
fun fromBlobUnsigned(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_uint8(byteBuffer, shape)
}

@JvmStatic
fun fromBlob(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_int8(byteBuffer, shape)
}

@JvmStatic
fun fromBlob(data: IntArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val intBuffer = allocateIntBuffer(numel(shape).toInt())
intBuffer.put(data)
return Tensor_int32(intBuffer, shape)
}

@JvmStatic
fun fromBlob(data: FloatArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
Comment on lines +737 to +739
fun fromByteArray(bytes: ByteArray): Tensor {
require(bytes != null) { "bytes cannot be null" }
val buffer = ByteBuffer.wrap(bytes)
Comment on lines +664 to +691
fun ones(shape: LongArray, dtype: DType): Tensor {
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
val numElements = numel(shape).toInt()
return when (dtype) {
DType.UINT8 -> fromBlobUnsigned(ByteArray(numElements) { 1 }, shape)
DType.INT8 -> fromBlob(ByteArray(numElements) { 1 }, shape)
DType.INT32 -> fromBlob(IntArray(numElements) { 1 }, shape)
DType.FLOAT -> fromBlob(FloatArray(numElements) { 1.0f }, shape)
DType.INT64 -> fromBlob(LongArray(numElements) { 1L }, shape)
DType.DOUBLE -> fromBlob(DoubleArray(numElements) { 1.0 }, shape)
else ->
throw IllegalArgumentException("Tensor.ones() cannot be used with DType $dtype")
}
}

@JvmStatic
fun zeros(shape: LongArray, dtype: DType): Tensor {
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
val numElements = numel(shape).toInt()
return when (dtype) {
DType.UINT8 -> fromBlobUnsigned(ByteArray(numElements), shape)
DType.INT8 -> fromBlob(ByteArray(numElements), shape)
DType.INT32 -> fromBlob(IntArray(numElements), shape)
DType.FLOAT -> fromBlob(FloatArray(numElements), shape)
DType.INT64 -> fromBlob(LongArray(numElements), shape)
DType.DOUBLE -> fromBlob(DoubleArray(numElements), shape)
Copilot AI review requested due to automatic review settings May 28, 2026 04:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment on lines +498 to +510
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_uint8(byteBuffer, shape)
}

@JvmStatic
fun fromBlob(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
Comment on lines +437 to +438
override fun toString(): String =
"Unsupported tensor(${Arrays.toString(shape)}, dtype=$mDtype)"
kirklandsign added a commit that referenced this pull request May 28, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Differential Revision: D106557156
@meta-codesync meta-codesync Bot changed the title Convert Tensor from Java to Kotlin Convert Tensor from Java to Kotlin (#19823) May 28, 2026
@meta-codesync meta-codesync Bot changed the title Convert Tensor from Java to Kotlin (#19823) Convert Tensor from Java to Kotlin May 28, 2026
Copilot AI review requested due to automatic review settings May 28, 2026 16:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

}
}

@DoNotStrip @JvmField protected val shape: LongArray = shape.copyOf()
Comment on lines +497 to +515
fun fromBlobUnsigned(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_uint8(byteBuffer, shape)
}

@JvmStatic
fun fromBlob(data: ByteArray, shape: LongArray): Tensor {
checkArgument(data != null, ERROR_MSG_DATA_ARRAY_NOT_NULL)
checkArgument(shape != null, ERROR_MSG_SHAPE_NOT_NULL)
checkShape(shape)
checkShapeAndDataCapacityConsistency(data.size, shape)
val byteBuffer = allocateByteBuffer(numel(shape).toInt())
byteBuffer.put(data)
return Tensor_int8(byteBuffer, shape)
Comment on lines +709 to +716
@DoNotStrip
@JvmStatic
private fun nativeNewTensor(
data: ByteBuffer,
shape: LongArray,
dtype: Int,
hybridData: HybridData,
): Tensor {
kirklandsign added a commit that referenced this pull request May 29, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Differential Revision: D106557156
@meta-codesync meta-codesync Bot changed the title Convert Tensor from Java to Kotlin Convert Tensor from Java to Kotlin (#19823) May 29, 2026
kirklandsign added a commit that referenced this pull request May 29, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Differential Revision: D106557156
kirklandsign added a commit that referenced this pull request Jun 1, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Reviewed By: psiddh

Differential Revision: D106557156
Copilot AI review requested due to automatic review settings June 1, 2026 16:14
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

kirklandsign added a commit that referenced this pull request Jun 1, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Reviewed By: psiddh

Differential Revision: D106557156
kirklandsign added a commit that referenced this pull request Jun 1, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Reviewed By: psiddh

Differential Revision: D106557156
Copilot AI review requested due to automatic review settings June 1, 2026 16:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

kirklandsign added a commit that referenced this pull request Jun 1, 2026
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Reviewed By: psiddh

Differential Revision: D106557156
Summary:
Pull Request resolved: #19823

Convert `Tensor.java` to idiomatic Kotlin while preserving full JNI and Java interop compatibility:

- `abstract class Tensor` with `protected constructor` for subclass access
- `JvmField DoNotStrip val shape` for direct JNI field access
- `JvmStatic` on all companion factory methods (`fromBlob`, `ones`, `zeros`, `numel`, `allocate*Buffer`)
- `DoNotStrip` on JNI-called methods (`nativeNewTensor`, `dtypeJniCode`, `getRawDataBuffer`)
- Nested tensor type classes (`Tensor_uint8`, `Tensor_int8`, etc.) with `internal constructor`
- `halfBitsToFloat` moved to `Tensor_float16.Companion`, using `Float.fromBits()`
- `when` expressions replace `switch` statements and `if-else` chains
- Kotlin array initializers for `ones()`/`zeros()`
- Updated BUCK to reference `.kt` source

Reviewed By: psiddh

Differential Revision: D106557156
Copilot AI review requested due to automatic review settings June 1, 2026 17:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

@meta-codesync meta-codesync Bot merged commit 3d8ca48 into main Jun 1, 2026
179 of 180 checks passed
@meta-codesync meta-codesync Bot deleted the export-D106557156 branch June 1, 2026 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants