feat: enhancement of the error message for mlx.core.mean (#608)

* add error message
This commit is contained in:
Avikant Srivastava 2024-02-05 14:51:49 +05:30 committed by GitHub
parent e319383ef9
commit 31fea3758e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1319,6 +1319,15 @@ array mean(
const std::vector<int>& axes, const std::vector<int>& axes,
bool keepdims /* = false */, bool keepdims /* = false */,
StreamOrDevice s /* = {}*/) { StreamOrDevice s /* = {}*/) {
int ndim = a.ndim();
for (int axis : axes) {
if (axis < -ndim || axis >= ndim) {
std::ostringstream msg;
msg << "[mean] axis " << axis + " is out of bounds for array with "
<< ndim + " dimensions.";
throw std::invalid_argument(msg.str());
}
}
auto nelements = compute_number_of_elements(a, axes); auto nelements = compute_number_of_elements(a, axes);
auto dtype = at_least_float(a.dtype()); auto dtype = at_least_float(a.dtype());
return multiply(sum(a, axes, keepdims, s), array(1.0 / nelements, dtype), s); return multiply(sum(a, axes, keepdims, s), array(1.0 / nelements, dtype), s);