Fix build on Xcode 14 (#116)

* Fix build on Xcode 14

* Style fixes
This commit is contained in:
Angelos Katharopoulos 2023-12-10 06:58:52 -08:00 committed by GitHub
parent ef7b8756c0
commit 600db7d754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 13 deletions

View File

@ -18,7 +18,7 @@ struct Remainder {
template <typename T> T operator()(T x, T y) { return x % y; }
template <> float operator()(float x, float y) { return fmod(x, y); }
template <> half operator()(half x, half y) { return fmod(x, y); }
template <> bfloat operator()(bfloat x, bfloat y) { return fmod(x, y); }
template <> bfloat16_t operator()(bfloat16_t x, bfloat16_t y) { return fmod(x, y); }
};
struct Equal {

View File

@ -112,5 +112,7 @@ constexpr complex64_t operator*(complex64_t a, complex64_t b) {
}
constexpr complex64_t operator%(complex64_t a, complex64_t b) {
return {fmod(a.real, b.real), fmod(a.imag, b.imag)};
auto real = a.real - (b.real * static_cast<int64_t>(a.real / b.real));
auto imag = a.imag - (b.imag * static_cast<int64_t>(a.imag / b.imag));
return {real, imag};
}

View File

@ -2,6 +2,7 @@
import mlx.core as mx
def cross_entropy(
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
) -> mx.array:
@ -24,7 +25,9 @@ def cross_entropy(
return _reduce(loss, reduction)
def l1_loss(predictions: mx.array, targets: mx.array, reduction: str = "none") -> mx.array:
def l1_loss(
predictions: mx.array, targets: mx.array, reduction: str = "none"
) -> mx.array:
"""
Computes the L1 loss between predictions and targets.
@ -42,7 +45,9 @@ def l1_loss(predictions: mx.array, targets: mx.array, reduction: str = "none") -
return _reduce(loss, reduction)
def mse_loss(predictions: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
def mse_loss(
predictions: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
) -> mx.array:
"""
Computes the mean squared error loss between predictions and targets.
@ -61,7 +66,9 @@ def mse_loss(predictions: mx.array, targets: mx.array, axis: int = -1, reduction
return _reduce(loss, reduction)
def nll_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
def nll_loss(
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
) -> mx.array:
"""
Computes the negative log likelihood loss between logits and targets.
@ -80,7 +87,9 @@ def nll_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str
return _reduce(loss, reduction)
def kl_div_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
def kl_div_loss(
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
) -> mx.array:
"""
Computes the Kullback-Leibler divergence loss between logits and targets.
@ -98,7 +107,8 @@ def kl_div_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction:
return _reduce(loss, reduction)
def _reduce(loss: mx.array, reduction: str = 'none'):
def _reduce(loss: mx.array, reduction: str = "none"):
if reduction == "mean":
return mx.mean(loss)
elif reduction == "sum":

View File

@ -62,7 +62,6 @@ class TestNN(mlx_tests.MLXTestCase):
expected_sum = mx.sum(expected_none)
self.assertEqual(losses_sum, expected_sum)
def test_nll_loss(self):
logits = mx.array([[0.0, -float("inf")], [-float("inf"), 0.0]])
targets = mx.array([0, 1])
@ -82,7 +81,6 @@ class TestNN(mlx_tests.MLXTestCase):
expected_sum = mx.sum(expected_none)
self.assertEqual(losses_sum, expected_sum)
def test_kl_div_loss(self):
p_logits = mx.array([[1.0, 2.0], [0.5, 1.0]])
q_logits = mx.array([[0.8, 1.5], [0.4, 1.2]])