mirror of
https://github.com/ml-explore/mlx.git
synced 2025-06-24 09:21:16 +08:00
42 lines
1.1 KiB
C++
42 lines
1.1 KiB
C++
// Copyright © 2025 Apple Inc.
|
|
|
|
#include "mlx/backend/common/slicing.h"
|
|
#include "mlx/backend/gpu/copy.h"
|
|
#include "mlx/backend/gpu/slicing.h"
|
|
|
|
#include <numeric>
|
|
|
|
namespace mlx::core {
|
|
|
|
void concatenate_gpu(
|
|
const std::vector<array>& inputs,
|
|
array& out,
|
|
int axis,
|
|
const Stream& s) {
|
|
std::vector<int> sizes;
|
|
sizes.push_back(0);
|
|
for (auto& p : inputs) {
|
|
sizes.push_back(p.shape(axis));
|
|
}
|
|
std::partial_sum(sizes.cbegin(), sizes.cend(), sizes.begin());
|
|
|
|
out.set_data(allocator::malloc(out.nbytes()));
|
|
|
|
auto strides = out.strides();
|
|
auto flags = out.flags();
|
|
flags.row_contiguous = false;
|
|
flags.col_contiguous = false;
|
|
flags.contiguous = false;
|
|
// TODO: Handle concurrent outputs:
|
|
// https://github.com/ml-explore/mlx/pull/2145#discussion_r2070753816
|
|
for (int i = 0; i < inputs.size(); i++) {
|
|
array out_slice(inputs[i].shape(), out.dtype(), nullptr, {});
|
|
size_t data_offset = strides[axis] * sizes[i];
|
|
out_slice.copy_shared_buffer(
|
|
out, strides, flags, out_slice.size(), data_offset);
|
|
copy_gpu_inplace(inputs[i], out_slice, CopyType::GeneralGeneral, s);
|
|
}
|
|
}
|
|
|
|
} // namespace mlx::core
|