Comments (4)
We don't have it, but you can certainly do a binary search with existing ops. Here's something for 1D arrays that works, it should be fairly straight-forward to support an axis parameter if you need it:
def searchsorted(a, b):
axis = 0
size = a.shape[axis]
steps = math.ceil(math.log2(size))
upper = size
lower = 0
indices = mx.full(b.shape, vals=size//2, dtype=mx.uint32)
for _ in range(steps):
lt = b < a[indices]
new_indices = mx.where(lt, (lower + indices) // 2, (indices + upper) // 2)
lower = mx.where(lt, lower, indices)
upper = mx.where(lt, indices, upper)
indices = new_indices
return indices
Also it will be a lot faster if you mx.compile
it. Particularly if you are using it multiple times with the same shapes.
from mlx.
I'm open to adding a little binary search implementation like that into MLX to support searchsorted
. We could use the above as a starting point.
from mlx.
Ah okay I think I can make these workarounds for time time being. Thanks!!
from mlx.
Another option is to do something like the following. It's linear in a
but probably quite a bit faster, especially for small arrays, since it's far few operations:
def searchsorted(a, b):
return (a[None, :] < b[:, None]).sum(axis=1)
from mlx.
Related Issues (20)
- Memory Leakage Issue in MLX 0.16 HOT 9
- [PERFORMANCE] grads for bitwise ops + indexing HOT 1
- [Performance] Linear Layer Benchmark HOT 2
- [BUG] expm1 handling of overflow / underflow causes wrong results
- [Feature Request] mx.pad supports the "edge" padding mode.
- [BUG] Cannot use mlx.metallib from xcode MacOS project (Swift, C++)
- [BUG] Cannot convert a list with `None` to `mx.array` HOT 1
- [BUG] Docs not building HOT 4
- How can we enable w4a8 GEMM in MLX? HOT 6
- Missing pyi file for mlx.core HOT 1
- Performance Comparison Issue: Matrix Multiplication on MLX vs. PyTorch on Mac HOT 3
- [Feature Request] Cannot create tensor from raw bytes + dtypes HOT 3
- [BUG] mx.std returns NaN HOT 1
- What is the equivalent to a Flatten layer in MLX? HOT 6
- How to mask out padding tokens when calculating the cross-entropy? HOT 2
- 使用exo+mlx多台mac运行llama-3.1-70b,返现量化时报错[BUG] HOT 1
- [Performance] PyTorch (MPS) is faster than MLX in backward of convolution layer HOT 4
- [BUG] Dropout not preserving `dtype` HOT 5
- [BUG] Convergence issue in MLX when compared to PyTorch HOT 4
- [BUG] Unable to load from a saved checkpoint, `KeyError` for all dropout modules... HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mlx.