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 <exception>
6#include <variant>
7
8#include "mlx/array.h"
9#include "mlx/device.h"
10#include "mlx/dtype.h"
11#include "mlx/stream.h"
12
13namespace mlx::core {
14
15using StreamOrDevice = std::variant<std::monostate, Stream, Device>;
17
19 public:
21 if (std::holds_alternative<std::monostate>(s)) {
22 throw std::runtime_error(
23 "[StreamContext] Invalid argument, please specify a stream or device.");
24 }
25 auto _s = to_stream(s);
26 set_default_device(_s.device);
28 }
29
31 set_default_device(_stream.device);
32 set_default_stream(_stream);
33 }
34
35 private:
36 Stream _stream;
37};
38
40 inline void print(std::ostream& os, bool val);
41 inline void print(std::ostream& os, int16_t val);
42 inline void print(std::ostream& os, uint16_t val);
43 inline void print(std::ostream& os, int32_t val);
44 inline void print(std::ostream& os, uint32_t val);
45 inline void print(std::ostream& os, int64_t val);
46 inline void print(std::ostream& os, uint64_t val);
47 inline void print(std::ostream& os, float16_t val);
48 inline void print(std::ostream& os, bfloat16_t val);
49 inline void print(std::ostream& os, float val);
50 inline void print(std::ostream& os, double val);
51 inline void print(std::ostream& os, complex64_t val);
52
53 bool capitalize_bool{false};
54};
55
57
59void abort_with_exception(const std::exception& error);
60
62struct finfo {
63 explicit finfo(Dtype dtype);
65 double min;
66 double max;
67};
68
70inline Dtype result_type(const array& a, const array& b) {
71 return promote_types(a.dtype(), b.dtype());
72}
73inline Dtype result_type(const array& a, const array& b, const array& c) {
74 return promote_types(result_type(a, b), c.dtype());
75}
76Dtype result_type(const std::vector<array>& arrays);
77
78Shape broadcast_shapes(const Shape& s1, const Shape& s2);
79
84 int axis,
85 int ndim,
86 const std::string& msg_prefix = "");
87
88std::ostream& operator<<(std::ostream& os, const Device& d);
89std::ostream& operator<<(std::ostream& os, const Stream& s);
90std::ostream& operator<<(std::ostream& os, const Dtype& d);
91std::ostream& operator<<(std::ostream& os, const Dtype::Kind& k);
92std::ostream& operator<<(std::ostream& os, array a);
93std::ostream& operator<<(std::ostream& os, const std::vector<int>& v);
94std::ostream& operator<<(std::ostream& os, const std::vector<int64_t>& v);
95inline std::ostream& operator<<(std::ostream& os, const complex64_t& v) {
96 return os << v.real() << (v.imag() >= 0 ? "+" : "") << v.imag() << "j";
97}
98inline std::ostream& operator<<(std::ostream& os, const float16_t& v) {
99 return os << static_cast<float>(v);
100}
101inline std::ostream& operator<<(std::ostream& os, const bfloat16_t& v) {
102 return os << static_cast<float>(v);
103}
104
105inline bool is_power_of_2(int n) {
106 return ((n & (n - 1)) == 0) && n != 0;
107}
108
109inline int next_power_of_2(int n) {
110 if (is_power_of_2(n)) {
111 return n;
112 }
113 return pow(2, std::ceil(std::log2(n)));
114}
115
116namespace env {
117
118int get_var(const char* name, int default_value);
119
120inline int bfs_max_width() {
121 static int bfs_max_width_ = get_var("MLX_BFS_MAX_WIDTH", 20);
122 return bfs_max_width_;
123}
124
125inline int max_ops_per_buffer(int default_value) {
126 static int max_ops_per_buffer_ =
127 get_var("MLX_MAX_OPS_PER_BUFFER", default_value);
128 return max_ops_per_buffer_;
129}
130
131inline int max_mb_per_buffer(int default_value) {
132 static int max_mb_per_buffer_ =
133 get_var("MLX_MAX_MB_PER_BUFFER", default_value);
134 return max_mb_per_buffer_;
135}
136
137inline bool metal_fast_synch() {
138 static bool metal_fast_synch = get_var("MLX_METAL_FAST_SYNCH", 0);
139 return metal_fast_synch;
140}
141
142} // namespace env
143
144} // namespace mlx::core
Definition array.h:24
Dtype dtype() const
Get the arrays data type.
Definition array.h:131
array operator<<(const array &a, const array &b)
Definition utils.h:116
int get_var(const char *name, int default_value)
int max_ops_per_buffer(int default_value)
Definition utils.h:125
int bfs_max_width()
Definition utils.h:120
bool metal_fast_synch()
Definition utils.h:137
int max_mb_per_buffer(int default_value)
Definition utils.h:131
Definition allocator.h:7
const Device & default_device()
int normalize_axis_index(int axis, int ndim, const std::string &msg_prefix="")
Returns the axis normalized to be in the range [0, ndim).
void set_default_device(const Device &d)
Stream to_stream(StreamOrDevice s)
Dtype promote_types(const Dtype &t1, const Dtype &t2)
int next_power_of_2(int n)
Definition utils.h:109
std::vector< ShapeElem > Shape
Definition array.h:21
Dtype result_type(const array &a, const array &b)
The type from promoting the arrays' types with one another.
Definition utils.h:70
std::variant< std::monostate, Stream, Device > StreamOrDevice
Definition utils.h:15
Stream default_stream(Device d)
Get the default stream for the given device.
struct _MLX_BFloat16 bfloat16_t
Definition half_types.h:34
bool is_power_of_2(int n)
Definition utils.h:105
void abort_with_exception(const std::exception &error)
Print the exception and then abort.
Shape broadcast_shapes(const Shape &s1, const Shape &s2)
void set_default_stream(Stream s)
Make the stream the default for its device.
struct _MLX_Float16 float16_t
Definition half_types.h:17
PrintFormatter & get_global_formatter()
Definition device.h:7
Definition dtype.h:13
Kind
Definition dtype.h:31
Definition utils.h:39
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, double 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:53
void print(std::ostream &os, bfloat16_t val)
StreamContext(StreamOrDevice s)
Definition utils.h:20
~StreamContext()
Definition utils.h:30
Definition stream.h:9
Definition complex.h:35
finfo(Dtype dtype)
double min
Definition utils.h:65
Dtype dtype
Definition utils.h:64
double max
Definition utils.h:66