Skip to content

Instantly share code, notes, and snippets.

View FL33TW00D's full-sized avatar
🎯
Focusing

Christopher Fleetwood FL33TW00D

🎯
Focusing
View GitHub Profile
@ivormetcalf
ivormetcalf / upload_stream.rs
Last active April 25, 2024 20:28
Rust Module to upload a stream to Amazon S3.
use rusoto_core::ByteStream;
use rusoto_s3::{S3Client, S3};
use futures::{future, stream, Stream, StreamExt, TryStreamExt};
pub async fn upload_stream(
client: S3Client,
bucket: String,
key: String,
data_stream: impl Stream<Item = String>,
@thomwolf
thomwolf / top-k-top-p.py
Last active May 14, 2024 00:20
Sample the next token from a probability distribution using top-k and/or nucleus (top-p) sampling
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
""" Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
Args:
logits: logits distribution shape (vocabulary size)
top_k >0: keep only top k tokens with highest probability (top-k filtering).
top_p >0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
"""
assert logits.dim() == 1 # batch size 1 for now - could be updated for more but the code would be less clear
top_k = min(top_k, logits.size(-1)) # Safety check