2024-03-20 10:39:25 -07:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
2023-11-30 11:12:53 -08:00
|
|
|
|
2023-11-29 10:52:08 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-06-09 22:45:08 +09:00
|
|
|
#include "mlx/backend/common/utils.h"
|
2023-11-29 10:52:08 -08:00
|
|
|
|
|
|
|
|
namespace mlx::core {
|
|
|
|
|
|
|
|
|
|
enum class CopyType {
|
|
|
|
|
// Copy a raw scalar input into the full contiguous output
|
|
|
|
|
Scalar,
|
|
|
|
|
|
|
|
|
|
// Copy the raw input buffer contiguously into a raw output buffer of the same
|
|
|
|
|
// size
|
|
|
|
|
Vector,
|
|
|
|
|
|
|
|
|
|
// Copy the full virtual input to the full contiguous output
|
|
|
|
|
General,
|
|
|
|
|
|
|
|
|
|
// Copy the full virtual input to the full virtual output. We assume the
|
|
|
|
|
// input and output have the same shape.
|
|
|
|
|
GeneralGeneral
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-05 16:05:23 -08:00
|
|
|
inline bool set_copy_output_data(
|
|
|
|
|
const array& in,
|
|
|
|
|
array& out,
|
|
|
|
|
CopyType ctype,
|
|
|
|
|
std::function<allocator::Buffer(size_t)> mallocfn = allocator::malloc) {
|
2025-03-06 19:23:38 -08:00
|
|
|
if (ctype == CopyType::Vector) {
|
|
|
|
|
// If the input is donateable, we are doing a vector copy and the types
|
|
|
|
|
// have the same size, then the input buffer can hold the output.
|
2025-06-09 22:45:08 +09:00
|
|
|
if (is_donatable(in, out)) {
|
2025-03-06 19:23:38 -08:00
|
|
|
out.copy_shared_buffer(in);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
out.set_data(
|
2025-11-05 16:05:23 -08:00
|
|
|
mallocfn(in.data_size() * out.itemsize()),
|
2025-03-06 19:23:38 -08:00
|
|
|
in.data_size(),
|
|
|
|
|
in.strides(),
|
|
|
|
|
in.flags());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-11-05 16:05:23 -08:00
|
|
|
out.set_data(mallocfn(out.nbytes()));
|
2025-03-06 19:23:38 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 10:52:08 -08:00
|
|
|
} // namespace mlx::core
|