MLX
 
Loading...
Searching...
No Matches
compiled.h
Go to the documentation of this file.
1// Copyright © 2023-2024 Apple Inc.
2#pragma once
3
4#include <iomanip>
5#include <sstream>
6#include <unordered_set>
7
8#include "mlx/array.h"
9#include "mlx/primitives.h"
10
11namespace mlx::core {
12
13inline bool is_static_cast(const Primitive& p) {
14 return (typeid(p) == typeid(Broadcast) || typeid(p) == typeid(AsType));
15}
16
17std::string build_lib_name(
18 const std::vector<array>& inputs,
19 const std::vector<array>& outputs,
20 const std::vector<array>& tape,
21 const std::unordered_set<uintptr_t>& constant_ids);
22
23std::string get_type_string(Dtype d);
24
25template <typename T>
26void print_float_constant(std::ostream& os, const array& x) {
27 auto old_precision = os.precision();
28 os << std::setprecision(std::numeric_limits<float>::digits10 + 1)
29 << x.item<T>() << std::setprecision(old_precision);
30}
31
32template <typename T>
33void print_int_constant(std::ostream& os, const array& x) {
34 os << x.item<T>();
35}
36
37template <typename T>
38void print_complex_constant(std::ostream& os, const array& x) {
39 auto old_precision = os.precision();
40 T constant = x.item<T>();
41
42 os << get_type_string(x.dtype()) << "("
43 << std::setprecision(std::numeric_limits<float>::digits10 + 1)
44 << constant.real() << ", " << constant.imag() << ")"
45 << std::setprecision(old_precision);
46}
47
48void print_constant(std::ostream& os, const array& x);
49
50inline bool is_scalar(const array& x) {
51 return x.ndim() == 0;
52}
53
54// Check if we can use a contiguous operation given inputs and the output shape
56 const std::vector<array>& inputs,
57 const Shape& shape);
58
59// Allocate space for the outputs possibly with input donation
61 const std::vector<array>& inputs,
62 std::vector<array>& outputs,
63 const std::vector<array>& inputs_,
64 const std::unordered_set<uintptr_t>& constant_ids_,
65 bool contiguous,
66 bool move_buffers = false);
67
68} // namespace mlx::core
Definition primitives.h:392
Definition primitives.h:541
Definition primitives.h:48
Definition array.h:24
size_t ndim() const
The number of dimensions of the array.
Definition array.h:98
T item()
Get the value from a scalar array.
Definition array.h:551
Dtype dtype() const
Get the arrays data type.
Definition array.h:131
array contiguous(const array &a, bool allow_col_major=false, StreamOrDevice s={})
Definition allocator.h:7
void print_complex_constant(std::ostream &os, const array &x)
Definition compiled.h:38
std::string build_lib_name(const std::vector< array > &inputs, const std::vector< array > &outputs, const std::vector< array > &tape, const std::unordered_set< uintptr_t > &constant_ids)
bool compiled_check_contiguity(const std::vector< array > &inputs, const Shape &shape)
std::vector< ShapeElem > Shape
Definition array.h:21
void print_constant(std::ostream &os, const array &x)
void print_float_constant(std::ostream &os, const array &x)
Definition compiled.h:26
void print_int_constant(std::ostream &os, const array &x)
Definition compiled.h:33
bool is_scalar(const array &x)
Definition compiled.h:50
void compiled_allocate_outputs(const std::vector< array > &inputs, std::vector< array > &outputs, const std::vector< array > &inputs_, const std::unordered_set< uintptr_t > &constant_ids_, bool contiguous, bool move_buffers=false)
std::string get_type_string(Dtype d)
bool is_static_cast(const Primitive &p)
Definition compiled.h:13
Definition dtype.h:13