Skip to content

Commit e65cfb8

Browse files
authored
Add sinc to keras.ops.numpy (#22365)
* feat(ops): add sinc to keras.ops.numpy * style(ops): fix api init formatting for api_gen compatibility * fix(ops): add sinc to keras.ops exports and fix api formatting * style(ops): sync api init files with api_gen output * style: regenerate api files with api_gen * style: fix import sorting in api files
1 parent 1b55025 commit e65cfb8

File tree

12 files changed

+120
-0
lines changed

12 files changed

+120
-0
lines changed

keras/api/_tf_keras/keras/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
from keras.src.ops.numpy import sign as sign
291291
from keras.src.ops.numpy import signbit as signbit
292292
from keras.src.ops.numpy import sin as sin
293+
from keras.src.ops.numpy import sinc as sinc
293294
from keras.src.ops.numpy import sinh as sinh
294295
from keras.src.ops.numpy import size as size
295296
from keras.src.ops.numpy import slogdet as slogdet

keras/api/_tf_keras/keras/ops/numpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
from keras.src.ops.numpy import sign as sign
172172
from keras.src.ops.numpy import signbit as signbit
173173
from keras.src.ops.numpy import sin as sin
174+
from keras.src.ops.numpy import sinc as sinc
174175
from keras.src.ops.numpy import sinh as sinh
175176
from keras.src.ops.numpy import size as size
176177
from keras.src.ops.numpy import slogdet as slogdet

keras/api/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
from keras.src.ops.numpy import sign as sign
291291
from keras.src.ops.numpy import signbit as signbit
292292
from keras.src.ops.numpy import sin as sin
293+
from keras.src.ops.numpy import sinc as sinc
293294
from keras.src.ops.numpy import sinh as sinh
294295
from keras.src.ops.numpy import size as size
295296
from keras.src.ops.numpy import slogdet as slogdet

keras/api/ops/numpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
from keras.src.ops.numpy import sign as sign
172172
from keras.src.ops.numpy import signbit as signbit
173173
from keras.src.ops.numpy import sin as sin
174+
from keras.src.ops.numpy import sinc as sinc
174175
from keras.src.ops.numpy import sinh as sinh
175176
from keras.src.ops.numpy import size as size
176177
from keras.src.ops.numpy import slogdet as slogdet

keras/src/backend/jax/numpy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,16 @@ def sin(x):
12491249
return jnp.sin(x)
12501250

12511251

1252+
def sinc(x):
1253+
x = convert_to_tensor(x)
1254+
if standardize_dtype(x.dtype) == "int64":
1255+
dtype = config.floatx()
1256+
else:
1257+
dtype = dtypes.result_type(x.dtype, float)
1258+
x = cast(x, dtype)
1259+
return jnp.sinc(x)
1260+
1261+
12521262
@sparse.elementwise_unary(linear=False)
12531263
def sinh(x):
12541264
x = convert_to_tensor(x)

keras/src/backend/numpy/numpy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,16 @@ def sin(x):
12331233
return np.sin(x)
12341234

12351235

1236+
def sinc(x):
1237+
x = convert_to_tensor(x)
1238+
if standardize_dtype(x.dtype) == "int64":
1239+
dtype = config.floatx()
1240+
else:
1241+
dtype = dtypes.result_type(x.dtype, float)
1242+
x = x.astype(dtype)
1243+
return np.sinc(x).astype(dtype)
1244+
1245+
12361246
def sinh(x):
12371247
x = convert_to_tensor(x)
12381248
if standardize_dtype(x.dtype) == "int64":

keras/src/backend/openvino/excluded_concrete_tests.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ NumpyDtypeTest::test_minimum_python_types
357357
NumpyDtypeTest::test_nanargmax
358358
NumpyDtypeTest::test_nanargmin
359359
NumpyDtypeTest::test_power
360+
NumpyDtypeTest::test_sinc
360361
NumpyDtypeTest::test_view
361362
NumpyOneInputOpsCorrectnessTest::test_array
362363
NumpyOneInputOpsCorrectnessTest::test_conj
@@ -366,10 +367,13 @@ NumpyOneInputOpsCorrectnessTest::test_nanargmax
366367
NumpyOneInputOpsCorrectnessTest::test_nanargmin
367368
NumpyOneInputOpsCorrectnessTest::test_real
368369
NumpyOneInputOpsCorrectnessTest::test_reshape
370+
NumpyOneInputOpsCorrectnessTest::test_sinc
369371
NumpyOneInputOpsCorrectnessTest::test_slogdet
370372
NumpyOneInputOpsCorrectnessTest::test_vectorize
371373
NumpyOneInputOpsCorrectnessTest::test_view
374+
NumpyOneInputOpsDynamicShapeTest::test_sinc
372375
NumpyOneInputOpsDynamicShapeTest::test_view
376+
NumpyOneInputOpsStaticShapeTest::test_sinc
373377
NumpyOneInputOpsStaticShapeTest::test_view
374378
OptimizerTest::test_constraints_are_applied
375379
OptimizerTest::test_ema

keras/src/backend/openvino/numpy.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,12 @@ def sin(x):
35563556
return OpenVINOKerasTensor(ov_opset.sin(x).output(0))
35573557

35583558

3559+
def sinc(x):
3560+
raise NotImplementedError(
3561+
"`sinc` is not supported with the OpenVINO backend."
3562+
)
3563+
3564+
35593565
def sinh(x):
35603566
x = get_ov_output(x)
35613567
x_type = x.get_element_type()

keras/src/backend/tensorflow/numpy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,21 @@ def sin(x):
27072707
return tf.math.sin(x)
27082708

27092709

2710+
def sinc(x):
2711+
x = convert_to_tensor(x)
2712+
if standardize_dtype(x.dtype) == "int64":
2713+
dtype = config.floatx()
2714+
else:
2715+
dtype = dtypes.result_type(x.dtype, float)
2716+
x = tf.cast(x, dtype)
2717+
pi_x = x * tf.constant(np.pi, dtype=x.dtype)
2718+
return tf.where(
2719+
tf.equal(x, 0),
2720+
tf.ones_like(x),
2721+
tf.math.sin(pi_x) / pi_x,
2722+
)
2723+
2724+
27102725
@sparse.elementwise_unary
27112726
def sinh(x):
27122727
x = convert_to_tensor(x)

keras/src/backend/torch/numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,11 @@ def sin(x):
17211721
return torch.sin(x)
17221722

17231723

1724+
def sinc(x):
1725+
x = convert_to_tensor(x)
1726+
return torch.sinc(x)
1727+
1728+
17241729
def sinh(x):
17251730
x = convert_to_tensor(x)
17261731
return torch.sinh(x)

0 commit comments

Comments
 (0)