MLX
Loading...
Searching...
No Matches
copy.h
Go to the documentation of this file.
1// Copyright © 2024 Apple Inc.
2
3template <typename T, typename U>
4[[kernel]] void copy_s(
5 device const T* src [[buffer(0)]],
6 device U* dst [[buffer(1)]],
7 uint index [[thread_position_in_grid]]) {
8 dst[index] = static_cast<U>(src[0]);
9}
10
11template <typename T, typename U>
12[[kernel]] void copy_v(
13 device const T* src [[buffer(0)]],
14 device U* dst [[buffer(1)]],
15 uint index [[thread_position_in_grid]]) {
16 dst[index] = static_cast<U>(src[index]);
17}
18
19template <typename T, typename U>
20[[kernel]] void copy_s2(
21 device const T* src [[buffer(0)]],
22 device U* dst [[buffer(1)]],
23 uint2 index [[thread_position_in_grid]],
24 uint2 grid_dim [[threads_per_grid]]) {
25 size_t offset = index.x + grid_dim.x * size_t(index.y);
26 dst[offset] = static_cast<U>(src[0]);
27}
28
29template <typename T, typename U>
30[[kernel]] void copy_v2(
31 device const T* src [[buffer(0)]],
32 device U* dst [[buffer(1)]],
33 uint2 index [[thread_position_in_grid]],
34 uint2 grid_dim [[threads_per_grid]]) {
35 size_t offset = index.x + grid_dim.x * size_t(index.y);
36 dst[offset] = static_cast<U>(src[offset]);
37}
38
39template <typename T, typename U>
40[[kernel]] void copy_g_nd1(
41 device const T* src [[buffer(0)]],
42 device U* dst [[buffer(1)]],
43 constant const int64_t& src_stride [[buffer(3)]],
44 uint index [[thread_position_in_grid]]) {
45 auto src_idx = elem_to_loc_1(index, src_stride);
46 dst[index] = static_cast<U>(src[src_idx]);
47}
48
49template <typename T, typename U>
50[[kernel]] void copy_g_nd2(
51 device const T* src [[buffer(0)]],
52 device U* dst [[buffer(1)]],
53 constant const int64_t* src_strides [[buffer(3)]],
54 uint2 index [[thread_position_in_grid]],
55 uint2 grid_dim [[threads_per_grid]]) {
56 auto src_idx = elem_to_loc_2(index, src_strides);
57 int64_t dst_idx = index.x + (int64_t)grid_dim.x * index.y;
58 dst[dst_idx] = static_cast<U>(src[src_idx]);
59}
60
61template <typename T, typename U>
62[[kernel]] void copy_g_nd3(
63 device const T* src [[buffer(0)]],
64 device U* dst [[buffer(1)]],
65 constant const int64_t* src_strides [[buffer(3)]],
66 uint3 index [[thread_position_in_grid]],
67 uint3 grid_dim [[threads_per_grid]]) {
68 auto src_idx = elem_to_loc_3(index, src_strides);
69 int64_t dst_idx =
70 index.x + (int64_t)grid_dim.x * (index.y + (int64_t)grid_dim.y * index.z);
71 dst[dst_idx] = static_cast<U>(src[src_idx]);
72}
73
74template <typename T, typename U, int N = 1>
75[[kernel]] void copy_g(
76 device const T* src [[buffer(0)]],
77 device U* dst [[buffer(1)]],
78 constant const int* src_shape [[buffer(2)]],
79 constant const int64_t* src_strides [[buffer(3)]],
80 constant const int& ndim [[buffer(5)]],
81 uint3 index [[thread_position_in_grid]],
82 uint3 grid_dim [[threads_per_grid]]) {
83 auto src_idx = elem_to_loc(
84 {N * index.x, index.y, index.z}, src_shape, src_strides, ndim);
85 if (N == 1) {
86 int64_t dst_idx =
87 index.x + grid_dim.x * (index.y + int64_t(grid_dim.y) * index.z);
88 dst[dst_idx] = static_cast<U>(src[src_idx]);
89 return;
90 }
91 auto xshape = src_shape[ndim - 1];
92 int64_t dst_idx =
93 N * index.x + xshape * (index.y + int64_t(grid_dim.y) * index.z);
94 auto src_xstride = src_strides[ndim - 1];
95 for (int i = 0; i < N && (int(N * index.x) + i) < xshape; ++i) {
96 dst[dst_idx + i] = static_cast<U>(src[src_idx]);
97 src_idx += src_xstride;
98 }
99}
100
101template <typename T, typename U>
102[[kernel]] void copy_gg_nd1(
103 device const T* src [[buffer(0)]],
104 device U* dst [[buffer(1)]],
105 constant const int64_t& src_stride [[buffer(3)]],
106 constant const int64_t& dst_stride [[buffer(4)]],
107 uint index [[thread_position_in_grid]]) {
108 auto src_idx = elem_to_loc_1(index, src_stride);
109 auto dst_idx = elem_to_loc_1(index, dst_stride);
110 dst[dst_idx] = static_cast<U>(src[src_idx]);
111}
112
113template <typename T, typename U>
114[[kernel]] void copy_gg_nd2(
115 device const T* src [[buffer(0)]],
116 device U* dst [[buffer(1)]],
117 constant const int64_t* src_strides [[buffer(3)]],
118 constant const int64_t* dst_strides [[buffer(4)]],
119 uint2 index [[thread_position_in_grid]]) {
120 auto src_idx = elem_to_loc_2(index, src_strides);
121 auto dst_idx = elem_to_loc_2(index, dst_strides);
122 dst[dst_idx] = static_cast<U>(src[src_idx]);
123}
124
125template <typename T, typename U>
126[[kernel]] void copy_gg_nd3(
127 device const T* src [[buffer(0)]],
128 device U* dst [[buffer(1)]],
129 constant const int64_t* src_strides [[buffer(3)]],
130 constant const int64_t* dst_strides [[buffer(4)]],
131 uint3 index [[thread_position_in_grid]]) {
132 auto src_idx = elem_to_loc_3(index, src_strides);
133 auto dst_idx = elem_to_loc_3(index, dst_strides);
134 dst[dst_idx] = static_cast<U>(src[src_idx]);
135}
136
137template <typename T, typename U, int N = 1>
138[[kernel]] void copy_gg(
139 device const T* src [[buffer(0)]],
140 device U* dst [[buffer(1)]],
141 constant const int* src_shape [[buffer(2)]],
142 constant const int64_t* src_strides [[buffer(3)]],
143 constant const int64_t* dst_strides [[buffer(4)]],
144 constant const int& ndim [[buffer(5)]],
145 uint3 index [[thread_position_in_grid]]) {
146 auto idx = elem_to_loc_2_nd(
147 {N * index.x, index.y, index.z},
148 src_shape,
149 src_strides,
150 dst_strides,
151 ndim);
152 if (N == 1) {
153 dst[idx.y] = static_cast<U>(src[idx.x]);
154 return;
155 }
156 auto src_xstride = src_strides[ndim - 1];
157 auto dst_xstride = dst_strides[ndim - 1];
158 auto xshape = src_shape[ndim - 1];
159 for (int i = 0; i < N && (int(N * index.x) + i) < xshape; ++i) {
160 dst[idx.y] = static_cast<U>(src[idx.x]);
161 idx.x += src_xstride;
162 idx.y += dst_xstride;
163 }
164}
METAL_FUNC ulong2 elem_to_loc_2_nd(uint3 elem, constant const int *shape, constant const stride_t *a_strides, constant const stride_t *b_strides, int ndim)
Definition utils.h:153
METAL_FUNC stride_t elem_to_loc_1(uint elem, constant const stride_t &stride)
Definition utils.h:133
METAL_FUNC stride_t elem_to_loc_3(uint3 elem, constant const stride_t strides[3])
Definition utils.h:145
METAL_FUNC stride_t elem_to_loc(uint elem, constant const int *shape, constant const stride_t *strides, int ndim)
Definition utils.h:87
METAL_FUNC stride_t elem_to_loc_2(uint2 elem, constant const stride_t strides[2])
Definition utils.h:139
void copy_gg(device const T *src, device U *dst, constant const int *src_shape, constant const int64_t *src_strides, constant const int64_t *dst_strides, constant const int &ndim, uint3 index)
Definition copy.h:138
void copy_gg_nd1(device const T *src, device U *dst, constant const int64_t &src_stride, constant const int64_t &dst_stride, uint index)
Definition copy.h:102
void copy_gg_nd2(device const T *src, device U *dst, constant const int64_t *src_strides, constant const int64_t *dst_strides, uint2 index)
Definition copy.h:114
void copy_gg_nd3(device const T *src, device U *dst, constant const int64_t *src_strides, constant const int64_t *dst_strides, uint3 index)
Definition copy.h:126
void copy_g(device const T *src, device U *dst, constant const int *src_shape, constant const int64_t *src_strides, constant const int &ndim, uint3 index, uint3 grid_dim)
Definition copy.h:75
void copy_s2(device const T *src, device U *dst, uint2 index, uint2 grid_dim)
Definition copy.h:20
void copy_g_nd3(device const T *src, device U *dst, constant const int64_t *src_strides, uint3 index, uint3 grid_dim)
Definition copy.h:62
void copy_g_nd1(device const T *src, device U *dst, constant const int64_t &src_stride, uint index)
Definition copy.h:40
void copy_v(device const T *src, device U *dst, uint index)
Definition copy.h:12
void copy_v2(device const T *src, device U *dst, uint2 index, uint2 grid_dim)
Definition copy.h:30
void copy_g_nd2(device const T *src, device U *dst, constant const int64_t *src_strides, uint2 index, uint2 grid_dim)
Definition copy.h:50
void copy_s(device const T *src, device U *dst, uint index)
Definition copy.h:4