Skip to content

Instantly share code, notes, and snippets.

@FoobarProtocol
Created October 21, 2023 23:44
Show Gist options
  • Save FoobarProtocol/578c996f43478b703688d3c62ce255e4 to your computer and use it in GitHub Desktop.
Save FoobarProtocol/578c996f43478b703688d3c62ce255e4 to your computer and use it in GitHub Desktop.
Flan T5 XXL normally has a fixed content window (512 tokens). This can be prohihbitive, especially when considering the plethora of tasks that this model is capable of performing. However one of the researchers from Google gave us the code for allowing the model to generate past the 512 `max_tokens` limit and understanding well beyond the 512 co…
from transformers import AutoTokenizer, BitsAndBytesConfig, AutoModelForSeq2SeqLM
model_id = "google/flan-t5-xxl"
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=False
)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id, quantization_config=quantization_config)
tok = AutoTokenizer.from_pretrained(model_id)
text = """Summarize the following news article in detail:\n
A capsule containing precious samples from an asteroid landed safely on Earth on Sunday, the culmination of a roughly 4-billion-mile journey over the past seven years.
The asteroid samples were collected by NASAs OSIRIS-REx spacecraft, which flew by Earth early Sunday morning and jettisoned the capsule over a designated landing zone in the Utah desert. The unofficial touchdown time was 8:52 a.m. MT, 3 minutes ahead of the predicted landing time.
The dramatic event — which the NASA livestream narrator described as “opening a time capsule to our ancient solar system” — marked a major milestone for the United States: The collected rocks and soil were NASAs first samples brought back to Earth from an asteroid. Experts have said the bounty could help scientists unlock secrets about the solar system and how it came to be, including how life emerged on this planet.
Bruce Betts, chief scientist at The Planetary Society, a nonprofit organization that conducts research, advocacy and outreach to promote space exploration, congratulated the NASA team on what he called an “impressive and very complicated mission,” adding that the asteroid samples are the start of a thrilling new chapter in space history.
“It's exciting because this mission launched in 2016 and so there's a feeling of, Wow, this day has finally come,'” he said. “But scientifically, it's exciting because this is an amazing opportunity to study a very complex story that goes way back to the dawn of the solar system.”
The samples were gathered from the surface of a near-Earth asteroid known as Bennu. The space rock, which is roughly as tall as the Empire State Building, is located more than 200 million miles away from Earth but orbits in such a way that it occasionally swings within 4.6 million miles of the planet.
Bennu's main draw owes to its age. The asteroid is estimated to have formed in the first 10 million years of the solar system's existence, making it a pristine remnant from a chaotic time more than 4.5 billion years ago. As such, studying an asteroid's chemical and physical properties is thought to be one of the best ways to understand the earliest days of the solar system.
“They're pretty well untouched from right around 4.5 billion years ago,” Betts said. “To get insights into these rocks gives real power to not just the science of asteroids but to everything in our solar system.”
Researchers are keen to understand what role — if any — asteroids played in the emergence of life on Earth. There are theories, for instance, that asteroids and comets may have delivered water and other building blocks of life to the planet.
Bennu has also been of interest to scientists because, like other near-Earth asteroids, it is classified as a potentially hazardous object. NASA's Planetary Defense Coordination Office previously estimated that there is a 1 in 2,700 chance of Bennu slamming into Earth sometime between the years 2175 and 2199.
"""
input_ids = tok(text, return_tensors="pt", padding=True).to(0)
out = model.generate(**input_ids, max_new_tokens=100, do_sample=False)
print(tok.batch_decode(out, skip_special_tokens=True))
>>>["The samples were collected by NASA's OSIRIS-REx spacecraft, which flew by Earth early Sunday morning and jettisoned the capsule over a designated landing zone in the Utah desert."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment