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, complex64_t val);
51
52 bool capitalize_bool{false};
53};
54
56
58void abort_with_exception(const std::exception& error);
59
61struct finfo {
62 explicit finfo(Dtype dtype);
64 float min;
65 float max;
66};
67
69inline Dtype result_type(const array& a, const array& b) {
70 return promote_types(a.dtype(), b.dtype());
71}
72inline Dtype result_type(const array& a, const array& b, const array& c) {
73 return promote_types(result_type(a, b), c.dtype());
74}
75Dtype result_type(const std::vector<array>& arrays);
76
77Shape broadcast_shapes(const Shape& s1, const Shape& s2);
78
83 int axis,
84 int ndim,
85 const std::string& msg_prefix = "");
86
87std::ostream& operator<<(std::ostream& os, const Device& d);
88std::ostream& operator<<(std::ostream& os, const Stream& s);
89std::ostream& operator<<(std::ostream& os, const Dtype& d);
90std::ostream& operator<<(std::ostream& os, const Dtype::Kind& k);
91std::ostream& operator<<(std::ostream& os, array a);
92std::ostream& operator<<(std::ostream& os, const std::vector<int>& v);
93std::ostream& operator<<(std::ostream& os, const std::vector<int64_t>& v);
94inline std::ostream& operator<<(std::ostream& os, const complex64_t& v) {
95 return os << v.real() << (v.imag() >= 0 ? "+" : "") << v.imag() << "j";
96}
97inline std::ostream& operator<<(std::ostream& os, const float16_t& v) {
98 return os << static_cast<float>(v);
99}
100inline std::ostream& operator<<(std::ostream& os, const bfloat16_t& v) {
101 return os << static_cast<float>(v);
102}
103
104inline bool is_power_of_2(int n) {
105 return ((n & (n - 1)) == 0) && n != 0;
106}
107
108inline int next_power_of_2(int n) {
109 if (is_power_of_2(n)) {
110 return n;
111 }
112 return pow(2, std::ceil(std::log2(n)));
113}
114
115namespace env {
116
117int get_var(const char* name, int default_value);
118
119inline int bfs_max_width() {
120 static int bfs_max_width_ = get_var("MLX_BFS_MAX_WIDTH", 20);
121 return bfs_max_width_;
122}
123
124inline int max_ops_per_buffer() {
125 static int max_ops_per_buffer_ = get_var("MLX_MAX_OPS_PER_BUFFER", 10);
126 return max_ops_per_buffer_;
127}
128
129inline bool metal_fast_synch() {
130 static bool metal_fast_synch = get_var("MLX_METAL_FAST_SYNCH", 0);
131 return metal_fast_synch;
132}
133
134} // namespace env
135
136} // 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:115
int get_var(const char *name, int default_value)
int bfs_max_width()
Definition utils.h:119
int max_ops_per_buffer()
Definition utils.h:124
bool metal_fast_synch()
Definition utils.h:129
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:108
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:69
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:104
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:30
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, 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:52
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)
Dtype dtype
Definition utils.h:63
float min
Definition utils.h:64
float max
Definition utils.h:65