MLX
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// Copyright © 2023-2024 Apple Inc.
2
3#pragma once
4
5#include <variant>
6
7#include "array.h"
8#include "device.h"
9#include "dtype.h"
10#include "stream.h"
11
12namespace mlx::core {
13
14using StreamOrDevice = std::variant<std::monostate, Stream, Device>;
16
18 public:
20 if (std::holds_alternative<std::monostate>(s)) {
21 throw std::runtime_error(
22 "[StreamContext] Invalid argument, please specify a stream or device.");
23 }
24 auto _s = to_stream(s);
25 set_default_device(_s.device);
27 }
28
31 set_default_stream(_stream);
32 }
33
34 private:
35 Stream _stream;
36};
37
39 inline void print(std::ostream& os, bool val);
40 inline void print(std::ostream& os, int16_t val);
41 inline void print(std::ostream& os, uint16_t val);
42 inline void print(std::ostream& os, int32_t val);
43 inline void print(std::ostream& os, uint32_t val);
44 inline void print(std::ostream& os, int64_t val);
45 inline void print(std::ostream& os, uint64_t val);
46 inline void print(std::ostream& os, float16_t val);
47 inline void print(std::ostream& os, bfloat16_t val);
48 inline void print(std::ostream& os, float val);
49 inline void print(std::ostream& os, complex64_t val);
50
51 bool capitalize_bool{false};
52};
53
54extern PrintFormatter global_formatter;
55
57inline Dtype result_type(const array& a, const array& b) {
58 return promote_types(a.dtype(), b.dtype());
59}
60inline Dtype result_type(const array& a, const array& b, const array& c) {
61 return promote_types(result_type(a, b), c.dtype());
62}
63Dtype result_type(const std::vector<array>& arrays);
64
65std::vector<int> broadcast_shapes(
66 const std::vector<int>& s1,
67 const std::vector<int>& s2);
68
69bool is_same_shape(const std::vector<array>& arrays);
70
72template <typename T>
73int check_shape_dim(const T dim) {
74 constexpr bool is_signed = std::numeric_limits<T>::is_signed;
75 using U = std::conditional_t<is_signed, ssize_t, size_t>;
76 constexpr U min = static_cast<U>(std::numeric_limits<int>::min());
77 constexpr U max = static_cast<U>(std::numeric_limits<int>::max());
78
79 if ((is_signed && dim < min) || dim > max) {
80 throw std::invalid_argument(
81 "Shape dimension falls outside supported `int` range.");
82 }
83
84 return static_cast<int>(dim);
85}
86
87inline bool is_big_endian() {
88 union ByteOrder {
89 int32_t i;
90 uint8_t c[4];
91 };
92 ByteOrder b = {0x01234567};
93
94 return b.c[0] == 0x01;
95}
96
102int normalize_axis(int axis, int ndim);
103
104std::ostream& operator<<(std::ostream& os, const Device& d);
105std::ostream& operator<<(std::ostream& os, const Stream& s);
106std::ostream& operator<<(std::ostream& os, const Dtype& d);
107std::ostream& operator<<(std::ostream& os, const Dtype::Kind& k);
108std::ostream& operator<<(std::ostream& os, array a);
109std::ostream& operator<<(std::ostream& os, const std::vector<int>& v);
110std::ostream& operator<<(std::ostream& os, const std::vector<size_t>& v);
111std::ostream& operator<<(std::ostream& os, const std::vector<int64_t>& v);
112inline std::ostream& operator<<(std::ostream& os, const complex64_t& v) {
113 return os << v.real() << (v.imag() >= 0 ? "+" : "") << v.imag() << "j";
114}
115inline std::ostream& operator<<(std::ostream& os, const float16_t& v) {
116 return os << static_cast<float>(v);
117}
118inline std::ostream& operator<<(std::ostream& os, const bfloat16_t& v) {
119 return os << static_cast<float>(v);
120}
121
122inline bool is_power_of_2(int n) {
123 return ((n & (n - 1)) == 0) && n != 0;
124}
125
126inline int next_power_of_2(int n) {
127 if (is_power_of_2(n)) {
128 return n;
129 }
130 return pow(2, std::ceil(std::log2(n)));
131}
132
133} // namespace mlx::core
Definition array.h:20
Dtype dtype() const
Get the arrays data type.
Definition array.h:127
array max(const array &a, bool keepdims, StreamOrDevice s={})
The maximum of all elements of the array.
array min(const array &a, bool keepdims, StreamOrDevice s={})
The minimum of all elements of the array.
array operator<<(const array &a, const array &b)
Definition allocator.h:7
int normalize_axis(int axis, int ndim)
Returns the axis normalized to be in the range [0, ndim).
const Device & default_device()
std::vector< int > broadcast_shapes(const std::vector< int > &s1, const std::vector< int > &s2)
void set_default_device(const Device &d)
Stream to_stream(StreamOrDevice s)
Dtype promote_types(const Dtype &t1, const Dtype &t2)
bool is_big_endian()
Definition utils.h:87
int next_power_of_2(int n)
Definition utils.h:126
int check_shape_dim(const T dim)
Returns the shape dimension if it's within allowed range.
Definition utils.h:73
Dtype result_type(const array &a, const array &b)
The type from promoting the arrays' types with one another.
Definition utils.h:57
std::variant< std::monostate, Stream, Device > StreamOrDevice
Definition utils.h:14
Stream default_stream(Device d)
Get the default stream for the given device.
bool is_same_shape(const std::vector< array > &arrays)
bool is_power_of_2(int n)
Definition utils.h:122
void set_default_stream(Stream s)
Make the stream the default for its device.
PrintFormatter global_formatter
Definition bf16.h:21
Definition fp16.h:21
Definition device.h:7
Definition dtype.h:15
Kind
Definition dtype.h:32
Definition utils.h:38
void print(std::ostream &os, uint32_t val)
void print(std::ostream &os, float val)
void print(std::ostream &os, bool val)
void print(std::ostream &os, int16_t val)
void print(std::ostream &os, uint16_t val)
void print(std::ostream &os, complex64_t val)
void print(std::ostream &os, int64_t val)
void print(std::ostream &os, float16_t val)
void print(std::ostream &os, uint64_t val)
void print(std::ostream &os, int32_t val)
bool capitalize_bool
Definition utils.h:51
void print(std::ostream &os, bfloat16_t val)
Definition utils.h:17
StreamContext(StreamOrDevice s)
Definition utils.h:19
~StreamContext()
Definition utils.h:29
Definition stream.h:9
Device device
Definition stream.h:11
Definition complex.h:34