2024-02-15 06:04:25 +08:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
#include <nanobind/nanobind.h>
|
|
|
|
#include <nanobind/stl/optional.h>
|
|
|
|
#include <nanobind/stl/variant.h>
|
2024-02-15 06:04:25 +08:00
|
|
|
|
|
|
|
#include "mlx/fast.h"
|
|
|
|
#include "mlx/ops.h"
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
namespace nb = nanobind;
|
|
|
|
using namespace nb::literals;
|
2024-02-15 06:04:25 +08:00
|
|
|
using namespace mlx::core;
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
void init_fast(nb::module_& parent_module) {
|
2024-02-15 06:04:25 +08:00
|
|
|
auto m =
|
|
|
|
parent_module.def_submodule("fast", "mlx.core.fast: fast operations");
|
|
|
|
|
2024-03-21 22:20:54 +08:00
|
|
|
m.def(
|
|
|
|
"rms_norm",
|
|
|
|
[](const array& x,
|
|
|
|
const array& weight,
|
|
|
|
float eps,
|
|
|
|
const StreamOrDevice& s /* = {} */) {
|
|
|
|
return fast::rms_norm(x, weight, eps, s);
|
|
|
|
},
|
|
|
|
"x"_a,
|
|
|
|
"weight"_a,
|
|
|
|
"eps"_a,
|
|
|
|
nb::kw_only(),
|
|
|
|
"stream"_a = nb::none(),
|
|
|
|
nb::sig(
|
|
|
|
"def rms_norm(x: array, weight: array, eps: float, *, stream: Union[None, Stream, Device] = None) -> array"),
|
|
|
|
R"pbdoc(
|
|
|
|
Root Mean Square normalization (RMS norm).
|
|
|
|
|
|
|
|
The normalization is with respect to the last axis of the input ``x``.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
x (array): Input array.
|
|
|
|
weight (array): A multiplicative weight to scale the result by.
|
|
|
|
The ``weight`` should be one-dimensional with the same size
|
|
|
|
as the last axis of ``x``.
|
|
|
|
eps (float): A small additive constant for numerical stability.
|
2024-03-22 04:55:51 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
array: The output array.
|
|
|
|
)pbdoc");
|
|
|
|
|
|
|
|
m.def(
|
|
|
|
"layer_norm",
|
|
|
|
[](const array& x,
|
|
|
|
const std::optional<array>& weight,
|
|
|
|
const std::optional<array>& bias,
|
|
|
|
float eps,
|
|
|
|
const StreamOrDevice& s /* = {} */) {
|
|
|
|
return fast::layer_norm(x, weight, bias, eps, s);
|
|
|
|
},
|
|
|
|
"x"_a,
|
|
|
|
"weight"_a.none(),
|
|
|
|
"bias"_a.none(),
|
|
|
|
"eps"_a,
|
|
|
|
nb::kw_only(),
|
|
|
|
"stream"_a = nb::none(),
|
|
|
|
nb::sig(
|
|
|
|
"def layer_norm(x: array, weight: Optional[array], bias: Optional[array], eps: float, *, stream: Union[None, Stream, Device] = None) -> array"),
|
|
|
|
R"pbdoc(
|
|
|
|
Layer normalization.
|
|
|
|
|
|
|
|
The normalization is with respect to the last axis of the input ``x``.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
x (array): Input array.
|
|
|
|
weight (array, optional): A multiplicative weight to scale the result by.
|
|
|
|
The ``weight`` should be one-dimensional with the same size
|
|
|
|
as the last axis of ``x``. If set to ``None`` then no scaling happens.
|
|
|
|
bias (array, optional): An additive offset to be added to the result.
|
|
|
|
The ``bias`` should be one-dimensional with the same size
|
|
|
|
as the last axis of ``x``. If set to ``None`` then no translation happens.
|
|
|
|
eps (float): A small additive constant for numerical stability.
|
2024-03-21 22:20:54 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
array: The output array.
|
|
|
|
)pbdoc");
|
|
|
|
|
2024-02-15 06:04:25 +08:00
|
|
|
m.def(
|
|
|
|
"rope",
|
|
|
|
[](const array& a,
|
|
|
|
int dims,
|
|
|
|
bool traditional,
|
|
|
|
float base,
|
|
|
|
float scale,
|
|
|
|
int offset,
|
|
|
|
const StreamOrDevice& s /* = {} */) {
|
|
|
|
return fast::rope(a, dims, traditional, base, scale, offset, s);
|
|
|
|
},
|
|
|
|
"a"_a,
|
|
|
|
"dims"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::kw_only(),
|
2024-02-15 06:04:25 +08:00
|
|
|
"traditional"_a,
|
|
|
|
"base"_a,
|
|
|
|
"scale"_a,
|
|
|
|
"offset"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
"stream"_a = nb::none(),
|
|
|
|
nb::sig(
|
|
|
|
"def rope(a: array, dims: int, *, traditinoal: bool, base: float, scale: float, offset: int, stream: Union[None, Stream, Device] = None) -> array"),
|
2024-02-15 06:04:25 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Apply rotary positional encoding to the input.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
a (array): Input array.
|
|
|
|
dims (int): The feature dimensions to be rotated. If the input feature
|
|
|
|
is larger than dims then the rest is left unchanged.
|
|
|
|
traditional (bool): If set to ``True`` choose the traditional
|
|
|
|
implementation which rotates consecutive dimensions.
|
|
|
|
base (float): The base used to compute angular frequency for
|
|
|
|
each dimension in the positional encodings.
|
|
|
|
scale (float): The scale used to scale the positions.
|
|
|
|
offset (int): The position offset to start at.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
array: The output array.
|
|
|
|
)pbdoc");
|
2024-03-05 13:06:11 +08:00
|
|
|
|
|
|
|
m.def(
|
|
|
|
"scaled_dot_product_attention",
|
|
|
|
[](const array& q,
|
|
|
|
const array& k,
|
|
|
|
const array& v,
|
|
|
|
const float scale,
|
|
|
|
const std::optional<array>& mask,
|
|
|
|
const StreamOrDevice& s) {
|
|
|
|
return fast::scaled_dot_product_attention(q, k, v, scale, mask, s);
|
|
|
|
},
|
|
|
|
"q"_a,
|
|
|
|
"k"_a,
|
|
|
|
"v"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::kw_only(),
|
2024-03-05 13:06:11 +08:00
|
|
|
"scale"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
"mask"_a = nb::none(),
|
|
|
|
"stream"_a = nb::none(),
|
|
|
|
nb::sig(
|
|
|
|
"def scaled_dot_product_attention(q: array, k: array, v: array, *, scale: float, mask: Union[None, array] = None, stream: Union[None, Stream, Device] = None) -> array"),
|
2024-03-05 13:06:11 +08:00
|
|
|
R"pbdoc(
|
2024-03-19 11:12:25 +08:00
|
|
|
A fast implementation of multi-head attention: ``O = softmax(Q @ K.T, dim=-1) @ V``.
|
2024-03-05 13:06:11 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
Supports:
|
|
|
|
* [Multi-Head Attention](https://arxiv.org/abs/1706.03762)
|
|
|
|
* [Grouped Query Attention](https://arxiv.org/abs/2305.13245)
|
|
|
|
* [Multi-Query Attention](https://arxiv.org/abs/1911.02150).
|
2024-03-05 13:06:11 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
Note: The softmax operation is performed in ``float32`` regardless of
|
|
|
|
input precision.
|
2024-03-05 13:06:11 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
Note: For Grouped Query Attention and Multi-Query Attention, the ``k``
|
|
|
|
and ``v`` inputs should not be pre-tiled to match ``q``.
|
2024-03-05 13:06:11 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
Args:
|
|
|
|
q (array): Input query array.
|
|
|
|
k (array): Input keys array.
|
|
|
|
v (array): Input values array.
|
|
|
|
scale (float): Scale for queries (typically ``1.0 / sqrt(q.shape(-1)``)
|
|
|
|
mask (array, optional): An additive mask to apply to the query-key scores.
|
2024-03-05 13:06:11 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
Returns:
|
|
|
|
array: The output array.
|
|
|
|
)pbdoc");
|
2024-02-15 06:04:25 +08:00
|
|
|
}
|