Skip to content

Kotlin API #165

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

Open
wants to merge 61 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
82913de
Merge in device spec changes
rnett Dec 1, 2020
25da497
Initial kotlin generation (still using Options), some helpers
rnett Dec 2, 2020
4c2ef19
Update stdlib, more helpers
rnett Dec 2, 2020
2638f47
default parameters
rnett Dec 2, 2020
14d301c
Add and use ktlint
rnett Dec 3, 2020
02494e5
use spaces
rnett Dec 3, 2020
1ae143c
disable filename rule
rnett Dec 3, 2020
07fcb24
change disable to single file
rnett Dec 3, 2020
e3871e5
Helper methods for withDevice, a combined with method, and tf(DeviceS…
rnett Dec 4, 2020
881237b
Add license
rnett Dec 6, 2020
aab7311
WIP Session/Runner API. Java API needs updates
rnett Dec 7, 2020
9334c95
Javadoc generation
rnett Dec 7, 2020
7babe57
Example, clean up session api
rnett Dec 7, 2020
cbd611d
make the test public
rnett Dec 7, 2020
c42a092
add full test dependencies
rnett Dec 7, 2020
5b5a1bb
Add ones op
rnett Dec 7, 2020
535793b
requireShape helper methods
rnett Dec 10, 2020
12c6238
fix lint
rnett Dec 10, 2020
1686cec
Rename withSession to useSession to reflect closing semantics
rnett Dec 11, 2020
7de79d3
Target JVM 1.8 for Kotlin
rnett Dec 11, 2020
4d311ed
Update Kotlin version
rnett Dec 27, 2020
ea9e171
Codegen for reified type parameters
rnett Dec 28, 2020
e948269
disable auto-format for now (ktlint bug)
rnett Dec 28, 2020
3777965
Data type helpers
rnett Dec 30, 2020
8629630
Cleanup poms
rnett Jan 14, 2021
137277c
Concrete function helpers, redo codegen
rnett Jan 14, 2021
234d3ef
formatting
rnett Jan 14, 2021
26b4a69
Shape property
rnett Jan 24, 2021
9ee2f48
New codegen, support for Java 11 builds
rnett Jan 26, 2021
63e8e25
Start of extension helpers
rnett Feb 5, 2021
8271b70
Update to new master
rnett Feb 11, 2021
3c5467a
Rebase updates
rnett Mar 19, 2021
cca2857
Add tests
rnett Mar 20, 2021
a5a5f46
Add section in CONTRIBUTING
rnett Mar 20, 2021
01257f8
Add readme w/ link to contributing instructions
rnett Mar 20, 2021
d0a0183
Add section to readme
rnett Mar 20, 2021
fee69b2
Restructure Kotlin projects
rnett Apr 23, 2021
1677979
Fix name
rnett Apr 23, 2021
9b87d40
Rename Shape.size(int) to get, add toListOrNull
rnett Apr 23, 2021
f8c236b
Rebase
rnett Apr 23, 2021
4fed8ac
Update javadoc generation
rnett Apr 23, 2021
5113983
Update to Kotlin 1.5.0
rnett May 15, 2021
b7c6660
Rebase fixes
rnett Jun 20, 2021
353d137
Update formatting to use ktfmt and spotless
rnett Jun 20, 2021
c6eef4f
No formatting on generation, update explicit API settings
rnett Jun 20, 2021
103d70e
Initial framework wrappers
rnett Jun 20, 2021
3152bf0
Add WithOps, use for KotlinOps
rnett Jun 20, 2021
775860e
Better shape assertions
rnett Jun 20, 2021
066b27f
Formatting, Jupyter integration
rnett Jun 21, 2021
45ba2dd
Fix formatting
rnett Jun 21, 2021
419afc3
Fix test
rnett Jun 21, 2021
64a8dda
Fix formatting
rnett Jun 21, 2021
ef033fb
Fix generation regression
rnett Jun 21, 2021
f017a7c
Fix format
rnett Jun 21, 2021
482b43c
Don't load extra snapshot repo. If we're on snapshots we'll have had…
rnett Jul 1, 2021
b7a85ef
Use extension instead of platform
rnett Jul 1, 2021
693e108
Update to Kotlin 1.6
rnett Dec 22, 2021
42c61b1
Fix formatting
rnett Dec 22, 2021
12f56df
Update generation
rnett Dec 22, 2021
27fcac5
Update version
rnett Dec 22, 2021
43cd7e3
Update generation
rnett Dec 22, 2021
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
Prev Previous commit
Next Next commit
No formatting on generation, update explicit API settings
Signed-off-by: Ryan Nett <JNett96@gmail.com>
  • Loading branch information
rnett committed Dec 22, 2021
commit c6eef4f78e81899c47a9f45d4d8de99684055e18
17 changes: 13 additions & 4 deletions tensorflow-kotlin-parent/tensorflow-core-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,21 @@
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<args>
<arg>-Xopt-in=kotlin.contracts.ExperimentalContracts</arg>
<arg>-Xexplicit-api=strict</arg>
</args>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<args>
<arg>-Xopt-in=kotlin.contracts.ExperimentalContracts</arg>
<arg>-Xexplicit-api=strict</arg>
</args>
</configuration>
</execution>

<execution>
<id>kapt</id>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
//
package org.tensorflow.op.kotlin

import kotlin.Boolean
import kotlin.Float
import kotlin.Long
import org.tensorflow.Operand
import org.tensorflow.op.Scope
import org.tensorflow.op.audio.AudioSpectrogram
Expand All @@ -26,9 +29,6 @@ import org.tensorflow.op.audio.Mfcc
import org.tensorflow.types.TFloat32
import org.tensorflow.types.TInt32
import org.tensorflow.types.TString
import kotlin.Boolean
import kotlin.Float
import kotlin.Long

/**
* An API for building `audio` operations as [Op][org.tensorflow.op.Op]s
Expand All @@ -54,24 +54,24 @@ public class AudioOps(
* slices of frequency information, one slice for each window of time. By joining
* these together into a sequence, they form a distinctive fingerprint of the sound
* over time.
*
*
* This op expects to receive audio data as an input, stored as floats in the range
* -1 to 1, together with a window width in samples, and a stride specifying how
* far to move the window between slices. From this it generates a three
* dimensional output. The first dimension is for the channels in the input, so a
* stereo audio input would have two here for example. The second dimension is time,
* with successive frequency slices. The third dimension has an amplitude value for
* each frequency during that time slice.
*
*
* This means the layout when converted and saved as an image is rotated 90 degrees
* clockwise from a typical spectrogram. Time is descending down the Y axis, and
* the frequency decreases from left to right.
*
*
* Each value in the result represents the square root of the sum of the real and
* imaginary parts of an FFT on the current window of samples. In this way, the
* lowest dimension represents the power of each frequency in the current window,
* and adjacent windows are concatenated in the next dimension.
*
*
* To get a more intuitive and visual look at what this operation does, you can run
* tensorflow/examples/wav_to_spectrogram to read in an audio file and save out the
* resulting spectrogram as a PNG image.
Expand All @@ -94,27 +94,27 @@ public class AudioOps(
windowSize: Long,
stride: Long,
magnitudeSquared: Boolean? = null
): AudioSpectrogram = java.audioSpectrogram(
): AudioSpectrogram = java.audioSpectrogram(
input,
windowSize,
stride,
*listOfNotNull(
magnitudeSquared?.let { org.tensorflow.op.audio.AudioSpectrogram.magnitudeSquared(it) }
magnitudeSquared?.let{ org.tensorflow.op.audio.AudioSpectrogram.magnitudeSquared(it) }
).toTypedArray()
)
)

/**
* Decode a 16-bit PCM WAV file to a float tensor.
* The -32768 to 32767 signed 16-bit values will be scaled to -1.0 to 1.0 in float.
*
*
* When desired_channels is set, if the input contains fewer channels than this
* then the last channel will be duplicated to give the requested number, else if
* the input has more channels than requested then the additional channels will be
* ignored.
*
*
* If desired_samples is set, then the audio will be cropped or padded with zeroes
* to the requested length.
*
*
* The first output contains a Tensor with the content of the audio samples. The
* lowest dimension will be the number of channels, and the second will be the
* number of samples. For example, a ten-sample-long stereo WAV file should give an
Expand All @@ -137,21 +137,21 @@ public class AudioOps(
contents: Operand<TString>,
desiredChannels: Long? = null,
desiredSamples: Long? = null
): DecodeWav = java.decodeWav(
): DecodeWav = java.decodeWav(
contents,
*listOfNotNull(
desiredChannels?.let { org.tensorflow.op.audio.DecodeWav.desiredChannels(it) },
desiredSamples?.let { org.tensorflow.op.audio.DecodeWav.desiredSamples(it) }
desiredChannels?.let{ org.tensorflow.op.audio.DecodeWav.desiredChannels(it) },
desiredSamples?.let{ org.tensorflow.op.audio.DecodeWav.desiredSamples(it) }
).toTypedArray()
)
)

/**
* Encode audio data using the WAV file format.
* This operation will generate a string suitable to be saved out to create a .wav
* audio file. It will be encoded in the 16-bit PCM format. It takes in float
* values in the range -1.0f to 1.0f, and any outside that value will be clamped to
* that range.
*
*
* `audio` is a 2-D float Tensor of shape `&#91;length, channels&#93;`.
* `sample_rate` is a scalar Tensor holding the rate to use (e.g. 44100).
*
Expand All @@ -161,9 +161,9 @@ public class AudioOps(
* @see org.tensorflow.op.AudioOps.encodeWav
*/
public fun encodeWav(audio: Operand<TFloat32>, sampleRate: Operand<TInt32>): EncodeWav =
java.encodeWav(
audio,
sampleRate
java.encodeWav(
audio,
sampleRate
)

/**
Expand Down Expand Up @@ -208,14 +208,14 @@ public class AudioOps(
lowerFrequencyLimit: Float? = null,
filterbankChannelCount: Long? = null,
dctCoefficientCount: Long? = null
): Mfcc = java.mfcc(
): Mfcc = java.mfcc(
spectrogram,
sampleRate,
*listOfNotNull(
upperFrequencyLimit?.let { org.tensorflow.op.audio.Mfcc.upperFrequencyLimit(it) },
lowerFrequencyLimit?.let { org.tensorflow.op.audio.Mfcc.lowerFrequencyLimit(it) },
filterbankChannelCount?.let { org.tensorflow.op.audio.Mfcc.filterbankChannelCount(it) },
dctCoefficientCount?.let { org.tensorflow.op.audio.Mfcc.dctCoefficientCount(it) }
upperFrequencyLimit?.let{ org.tensorflow.op.audio.Mfcc.upperFrequencyLimit(it) },
lowerFrequencyLimit?.let{ org.tensorflow.op.audio.Mfcc.lowerFrequencyLimit(it) },
filterbankChannelCount?.let{ org.tensorflow.op.audio.Mfcc.filterbankChannelCount(it) },
dctCoefficientCount?.let{ org.tensorflow.op.audio.Mfcc.dctCoefficientCount(it) }
).toTypedArray()
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class BitwiseOps(
* Elementwise computes the bitwise AND of `x` and `y`.
* The result will have those bits set, that are set in both `x` and `y`. The
* computation is performed on the underlying representations of `x` and `y`.
*
*
* For example:
* ```
* import tensorflow as tf
Expand All @@ -64,7 +64,7 @@ public class BitwiseOps(
*
* res = bitwise_ops.bitwise_and(lhs, rhs)
* tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE
*
*
* ```
*
* @param <T> data type for `z` output
Expand All @@ -75,16 +75,16 @@ public class BitwiseOps(
* @see org.tensorflow.op.BitwiseOps.bitwiseAnd
*/
public fun <T : TNumber> bitwiseAnd(x: Operand<T>, y: Operand<T>): BitwiseAnd<T> =
java.bitwiseAnd<T>(
x,
y
java.bitwiseAnd<T>(
x,
y
)

/**
* Elementwise computes the bitwise OR of `x` and `y`.
* The result will have those bits set, that are set in `x`, `y` or both. The
* computation is performed on the underlying representations of `x` and `y`.
*
*
* For example:
* ```
* import tensorflow as tf
Expand All @@ -99,7 +99,7 @@ public class BitwiseOps(
*
* res = bitwise_ops.bitwise_or(lhs, rhs)
* tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE
*
*
* ```
*
* @param <T> data type for `z` output
Expand All @@ -110,16 +110,16 @@ public class BitwiseOps(
* @see org.tensorflow.op.BitwiseOps.bitwiseOr
*/
public fun <T : TNumber> bitwiseOr(x: Operand<T>, y: Operand<T>): BitwiseOr<T> =
java.bitwiseOr<T>(
x,
y
java.bitwiseOr<T>(
x,
y
)

/**
* Elementwise computes the bitwise XOR of `x` and `y`.
* The result will have those bits set, that are different in `x` and `y`. The
* computation is performed on the underlying representations of `x` and `y`.
*
*
* For example:
* ```
* import tensorflow as tf
Expand All @@ -134,7 +134,7 @@ public class BitwiseOps(
*
* res = bitwise_ops.bitwise_xor(lhs, rhs)
* tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE
*
*
* ```
*
* @param <T> data type for `z` output
Expand All @@ -145,9 +145,9 @@ public class BitwiseOps(
* @see org.tensorflow.op.BitwiseOps.bitwiseXor
*/
public fun <T : TNumber> bitwiseXor(x: Operand<T>, y: Operand<T>): BitwiseXor<T> =
java.bitwiseXor<T>(
x,
y
java.bitwiseXor<T>(
x,
y
)

/**
Expand All @@ -156,7 +156,7 @@ public class BitwiseOps(
* Flip each bit of supported types. For example, type `int8` (decimal 2) binary 00000010
* becomes (decimal -3) binary 11111101.
* This operation is performed on each element of the tensor argument `x`.
*
*
* Example:
* ```
* import tensorflow as tf
Expand Down Expand Up @@ -192,7 +192,7 @@ public class BitwiseOps(
* inverted = bitwise_ops.invert(input_tensor)
* expected = tf.constant([dtype.max - x for x in inputs], dtype=tf.float32)
* tf.assert_equal(tf.cast(inverted, tf.float32), tf.cast(expected, tf.float32))
*
*
* ```
*
* @param <T> data type for `y` output
Expand All @@ -201,15 +201,15 @@ public class BitwiseOps(
* @return a new instance of Invert
* @see org.tensorflow.op.BitwiseOps.invert
*/
public fun <T : TNumber> invert(x: Operand<T>): Invert<T> = java.invert<T>(
public fun <T : TNumber> invert(x: Operand<T>): Invert<T> = java.invert<T>(
x
)
)

/**
* Elementwise computes the bitwise left-shift of `x` and `y`.
* If `y` is negative, or greater than or equal to the width of `x` in bits the
* result is implementation defined.
*
*
* Example:
* ```
* import tensorflow as tf
Expand All @@ -235,7 +235,7 @@ public class BitwiseOps(
* rhs = np.array([-1, -5, -3, -14], dtype=np.int8)
* bitwise_ops.left_shift(lhs, rhs)
* # <tf.Tensor: shape=(4,), dtype=int8, numpy=array([ -2, 64, 101, 32], dtype=int8)>
*
*
* ```
*
* @param <T> data type for `z` output
Expand All @@ -246,19 +246,19 @@ public class BitwiseOps(
* @see org.tensorflow.op.BitwiseOps.leftShift
*/
public fun <T : TNumber> leftShift(x: Operand<T>, y: Operand<T>): LeftShift<T> =
java.leftShift<T>(
x,
y
java.leftShift<T>(
x,
y
)

/**
* Elementwise computes the bitwise right-shift of `x` and `y`.
* Performs a logical shift for unsigned integer types, and an arithmetic shift
* for signed integer types.
*
*
* If `y` is negative, or greater than or equal to than the width of `x` in bits
* the result is implementation defined.
*
*
* Example:
* ```
* import tensorflow as tf
Expand All @@ -284,7 +284,7 @@ public class BitwiseOps(
* rhs = np.array([-1, -5, -3, -14], dtype=np.int8)
* bitwise_ops.right_shift(lhs, rhs)
* # <tf.Tensor: shape=(4,), dtype=int8, numpy=array([ -2, 64, 101, 32], dtype=int8)>
*
*
* ```
*
* @param <T> data type for `z` output
Expand All @@ -295,8 +295,8 @@ public class BitwiseOps(
* @see org.tensorflow.op.BitwiseOps.rightShift
*/
public fun <T : TNumber> rightShift(x: Operand<T>, y: Operand<T>): RightShift<T> =
java.rightShift<T>(
x,
y
java.rightShift<T>(
x,
y
)
}
Loading