2026-02-16 01:19:04 +00:00
|
|
|
//go:build darwin && arm64 && mlx
|
|
|
|
|
|
|
|
|
|
package mlx
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#include "mlx/c/mlx.h"
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
|
|
// RandomCategorical samples from a categorical distribution defined by logprobs.
|
|
|
|
|
// Returns indices sampled according to the log-probability distribution along the last axis.
|
|
|
|
|
func RandomCategorical(logprobs *Array) *Array {
|
|
|
|
|
out := New("RANDOM_CATEGORICAL", logprobs)
|
fix: correct 20 mlx-c API mismatches for v0.4.1
- Use _axis/_axes variants for softmax, argmax, topk, sum, mean, squeeze,
concatenate, argpartition
- Fix size_t vs int for count parameters throughout
- Fix int64_t strides in as_strided
- Add mlx_optional_int + mode param to quantized_matmul
- Use mlx_array_new() for null arrays (freqs, key, mask, sinks)
- Fix expand_dims to single-axis signature
- Fix compile callback signature (size_t index)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 01:52:29 +00:00
|
|
|
key := C.mlx_array_new()
|
|
|
|
|
defer C.mlx_array_free(key)
|
|
|
|
|
C.mlx_random_categorical(
|
2026-02-16 01:19:04 +00:00
|
|
|
&out.ctx,
|
|
|
|
|
logprobs.ctx,
|
fix: correct 20 mlx-c API mismatches for v0.4.1
- Use _axis/_axes variants for softmax, argmax, topk, sum, mean, squeeze,
concatenate, argpartition
- Fix size_t vs int for count parameters throughout
- Fix int64_t strides in as_strided
- Add mlx_optional_int + mode param to quantized_matmul
- Use mlx_array_new() for null arrays (freqs, key, mask, sinks)
- Fix expand_dims to single-axis signature
- Fix compile callback signature (size_t index)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 01:52:29 +00:00
|
|
|
C.int(-1), // axis
|
|
|
|
|
key, // null key = use default RNG
|
2026-02-16 01:19:04 +00:00
|
|
|
DefaultStream().ctx,
|
|
|
|
|
)
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RandomUniform generates uniform random values in [low, high).
|
|
|
|
|
func RandomUniform(low, high float32, shape []int32, dtype DType) *Array {
|
|
|
|
|
out := New("RANDOM_UNIFORM")
|
|
|
|
|
cShape := make([]C.int, len(shape))
|
|
|
|
|
for i, s := range shape {
|
|
|
|
|
cShape[i] = C.int(s)
|
|
|
|
|
}
|
|
|
|
|
lo := FromValue(low)
|
|
|
|
|
hi := FromValue(high)
|
fix: correct 20 mlx-c API mismatches for v0.4.1
- Use _axis/_axes variants for softmax, argmax, topk, sum, mean, squeeze,
concatenate, argpartition
- Fix size_t vs int for count parameters throughout
- Fix int64_t strides in as_strided
- Add mlx_optional_int + mode param to quantized_matmul
- Use mlx_array_new() for null arrays (freqs, key, mask, sinks)
- Fix expand_dims to single-axis signature
- Fix compile callback signature (size_t index)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 01:52:29 +00:00
|
|
|
key := C.mlx_array_new()
|
|
|
|
|
defer C.mlx_array_free(key)
|
2026-02-16 01:19:04 +00:00
|
|
|
C.mlx_random_uniform(
|
|
|
|
|
&out.ctx,
|
|
|
|
|
lo.ctx, hi.ctx,
|
fix: correct 20 mlx-c API mismatches for v0.4.1
- Use _axis/_axes variants for softmax, argmax, topk, sum, mean, squeeze,
concatenate, argpartition
- Fix size_t vs int for count parameters throughout
- Fix int64_t strides in as_strided
- Add mlx_optional_int + mode param to quantized_matmul
- Use mlx_array_new() for null arrays (freqs, key, mask, sinks)
- Fix expand_dims to single-axis signature
- Fix compile callback signature (size_t index)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 01:52:29 +00:00
|
|
|
&cShape[0], C.size_t(len(cShape)),
|
2026-02-16 01:19:04 +00:00
|
|
|
C.mlx_dtype(dtype),
|
fix: correct 20 mlx-c API mismatches for v0.4.1
- Use _axis/_axes variants for softmax, argmax, topk, sum, mean, squeeze,
concatenate, argpartition
- Fix size_t vs int for count parameters throughout
- Fix int64_t strides in as_strided
- Add mlx_optional_int + mode param to quantized_matmul
- Use mlx_array_new() for null arrays (freqs, key, mask, sinks)
- Fix expand_dims to single-axis signature
- Fix compile callback signature (size_t index)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 01:52:29 +00:00
|
|
|
key,
|
2026-02-16 01:19:04 +00:00
|
|
|
DefaultStream().ctx,
|
|
|
|
|
)
|
|
|
|
|
return out
|
|
|
|
|
}
|