mirror of
https://github.com/ml-explore/mlx.git
synced 2025-12-16 01:49:05 +08:00
Reduce vmap + some fixes (#601)
This commit is contained in:
@@ -548,9 +548,8 @@ std::pair<std::vector<array>, std::vector<array>> vmap_trace(
|
||||
"[vmap] The number of in axes must match the number of inputs.");
|
||||
}
|
||||
|
||||
// Run the function on placeholder inputs
|
||||
// to get the original graph
|
||||
std::vector<array> s_inputs;
|
||||
// Some error checking and get the vmap axis size
|
||||
size_t vmap_ax_size;
|
||||
for (int i = 0; i < inputs.size(); ++i) {
|
||||
if (in_axes[i] != -1) {
|
||||
if (inputs[i].ndim() == 0) {
|
||||
@@ -563,7 +562,26 @@ std::pair<std::vector<array>, std::vector<array>> vmap_trace(
|
||||
<< inputs[i].ndim() << " dimensions.";
|
||||
throw std::invalid_argument(msg.str());
|
||||
}
|
||||
vmap_ax_size = inputs[i].shape(in_axes[i]);
|
||||
}
|
||||
}
|
||||
// Check that all vmapped axes have the same size
|
||||
for (int i = 0; i < inputs.size(); ++i) {
|
||||
if (in_axes[i] != -1) {
|
||||
if (size_t in_ax = inputs[i].shape(in_axes[i]); vmap_ax_size != in_ax) {
|
||||
std::ostringstream msg;
|
||||
msg << "[vmap] Inconsistent axis sizes: " << in_ax << " and "
|
||||
<< vmap_ax_size << ".";
|
||||
throw std::invalid_argument(msg.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the function on placeholder inputs
|
||||
// to get the original graph
|
||||
std::vector<array> s_inputs;
|
||||
for (int i = 0; i < inputs.size(); ++i) {
|
||||
if (in_axes[i] != -1) {
|
||||
std::vector<int> shape = inputs[i].shape();
|
||||
shape.erase(shape.begin() + in_axes[i]);
|
||||
array in(shape, inputs[i].dtype(), nullptr, {});
|
||||
|
||||
Reference in New Issue
Block a user