MLX
Loading...
Searching...
No Matches
unary.h
Go to the documentation of this file.
1// Copyright © 2024 Apple Inc.
2
3template <typename T, typename Op>
4[[kernel]] void unary_v(
5 device const T* in,
6 device T* out,
7 uint index [[thread_position_in_grid]]) {
8 out[index] = Op()(in[index]);
9}
10
11template <typename T, typename Op>
12[[kernel]] void unary_v2(
13 device const T* in,
14 device T* out,
15 uint2 index [[thread_position_in_grid]],
16 uint2 grid_dim [[threads_per_grid]]) {
17 size_t offset = index.x + grid_dim.x * size_t(index.y);
18 out[offset] = Op()(in[offset]);
19}
20
21template <typename T, typename Op>
22[[kernel]] void unary_g(
23 device const T* in,
24 device T* out,
25 device const int* in_shape,
26 device const size_t* in_strides,
27 device const int& ndim,
28 uint index [[thread_position_in_grid]]) {
29 auto idx = elem_to_loc(index, in_shape, in_strides, ndim);
30 out[index] = Op()(in[idx]);
31}
METAL_FUNC stride_t elem_to_loc(uint elem, device const int *shape, device const stride_t *strides, int ndim)
Definition utils.h:87
void unary_g(device const T *in, device T *out, device const int *in_shape, device const size_t *in_strides, device const int &ndim, uint index)
Definition unary.h:22
void unary_v2(device const T *in, device T *out, uint2 index, uint2 grid_dim)
Definition unary.h:12
void unary_v(device const T *in, device T *out, uint index)
Definition unary.h:4