MCPcopy Create free account
hub / github.com/ml-explore/mlx / stack

Function stack

mlx/ops.cpp:1267–1287  ·  view source on GitHub ↗

Stack arrays along a new axis */

Source from the content-addressed store, hash-verified

1265
1266/** Stack arrays along a new axis */
1267array stack(
1268 const std::vector<array>& arrays,
1269 int axis,
1270 StreamOrDevice s /* = {} */) {
1271 if (arrays.empty()) {
1272 throw std::invalid_argument("[stack] No arrays provided for stacking");
1273 }
1274 if (!std::all_of(arrays.begin(), arrays.end(), [&](const auto& a) {
1275 return arrays[0].shape() == a.shape();
1276 })) {
1277 throw std::invalid_argument("[stack] All arrays must have the same shape");
1278 }
1279 auto normalized_axis =
1280 normalize_axis_index(axis, arrays[0].ndim() + 1, "[stack] ");
1281 std::vector<array> new_arrays;
1282 new_arrays.reserve(arrays.size());
1283 for (auto& a : arrays) {
1284 new_arrays.emplace_back(expand_dims(a, normalized_axis, s));
1285 }
1286 return concatenate(new_arrays, axis, s);
1287}
1288
1289array stack(const std::vector<array>& arrays, StreamOrDevice s /* = {} */) {
1290 return stack(arrays, 0, s);

Callers 4

array_from_list_implFunction · 0.85
init_opsFunction · 0.85
ops_tests.cppFile · 0.85
multi_threeFunction · 0.85

Calls 9

normalize_axis_indexFunction · 0.85
expand_dimsFunction · 0.85
reserveMethod · 0.80
emplace_backMethod · 0.80
concatenateFunction · 0.70
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by 1

multi_threeFunction · 0.68