Run this model with HF Jobs
Run PerceptronAI/Isaac-0.1 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/PerceptronAI/Isaac-0.1. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/PerceptronAI/Isaac-0.1 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/PerceptronAI/Isaac-0.1. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/PerceptronAI/Isaac-0.1. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use PerceptronAI/Isaac-0.1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use PerceptronAI/Isaac-0.1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="PerceptronAI/Isaac-0.1", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("PerceptronAI/Isaac-0.1", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use PerceptronAI/Isaac-0.1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "PerceptronAI/Isaac-0.1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "PerceptronAI/Isaac-0.1", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/PerceptronAI/Isaac-0.1
- SGLang
How to use PerceptronAI/Isaac-0.1 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "PerceptronAI/Isaac-0.1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "PerceptronAI/Isaac-0.1", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "PerceptronAI/Isaac-0.1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "PerceptronAI/Isaac-0.1", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use PerceptronAI/Isaac-0.1 with Docker Model Runner:
docker model run hf.co/PerceptronAI/Isaac-0.1
Upload modular_isaac.py
Browse filesHello and congrats for the release!
This PR makes this model load with no additional dependency, you can also add a small inference notebook I made by replacing the username to Perceptron: https://colab.research.google.com/drive/1BHl2ZT8cYZ0HlP_q4HllFuCXWIBX_R_2?usp=sharing
if you add the "notebook.ipynb" repo to it's one-click open in the repository, making it easier for people to try out your model as well!
123456725 unmodified lines33343536373839404142434445464748495051525354419 unmodified lines47447547647747847948010 unmodified lines4914924934944954964974984995005015025031119 unmodified lines1623162416251626from __future__ import annotationsfrom collections import defaultdictfrom typing import Any, Union, TypedDictimport mathimport numpy as np25 unmodified linesSiglip2MLP,)from transformers.models.siglip2.configuration_siglip2 import Siglip2VisionConfigfrom perceptron.tensorstream import (Event,Stream,TensorStream,TextType,VisionType,create_stream,group_streams,)from perceptron.tensorstream.ops import (compute_mrope_pos_tensor,modality_mask,reconstruct_tensor_stream_from_compact_dict,slice as ts_slice,tensor_stream_token_view,)class PixelShuffleSiglip2VisionConfig(Siglip2VisionConfig):419 unmodified lines# Configuration# ============================================================================MAX_PIXELS = 60_000_000 # 60‑megapixel ceiling ≈ 8200 × 7300 px# Vision preprocessing constantsVISION_MEAN = (0.5, 0.5, 0.5)10 unmodified linesif arr.flags.writeable:return arr# First, try the cheap path — in‑place flag toggle (works for mmap'd arrays# and some shared memory buffers):try:arr.setflags(write=True)return arr # success: no data copyexcept ValueError:# Buffer is inherently read‑only (e.g. backed by PyAV / PIL): make copyreturn arr.copy()1119 unmodified lines"IsaacModel","IsaacForConditionalGeneration","IsaacProcessor",]123456725 unmodified lines33343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976419 unmodified lines139613971398139914001401140210 unmodified lines14131414141514161417141814191420142114221423142414251119 unmodified lines2545254625472548from __future__ import annotationsfrom collections import defaultdictfrom typing import Any, NewType, Union, TypedDictimport mathimport numpy as np25 unmodified linesSiglip2MLP,)from transformers.models.siglip2.configuration_siglip2 import Siglip2VisionConfigimport itertoolsfrom collections.abc import Callable, Iterableimport heapqfrom collections.abc import Callable, Iterablefrom dataclasses import dataclass, field, fields, replacefrom enum import Enumfrom torch.profiler import record_functionclass ModalityType(Enum):"""Base class for modality-type enumerations.Each derived class (VisionType, TextType) holdsan integer value that identifies a specific modality.Example usage:If you have an object `my_event` of class `Event`,you might write:if my_event.type == VisionType.image:# process an image frameThe methods below implement ordering and hashingbased on the integer `.value` of each enum member."""@propertydef modality(self):return self.__class__def __lt__(self, other):if isinstance(other, ModalityType):return self.value < other.valueraise NotImplementedError()def __eq__(self, other):if isinstance(other, ModalityType):return self.value == other.valueraise NotImplementedError()def __hash__(self):return hash(self.value)# NOTE: modality types need to be uniqueclass VisionType(ModalityType):"""Enum for vision modalities such as key video frames.Typically used in video processing or image sequences.Members:image: A single image frame."""image = 0class TextType(ModalityType):"""Enum for text tokens and padding.Members:text: Actual textual tokens.padding: Padding tokens used in sequence batching."""text = 1padding = 2# maps idx -> typeALL_TYPES = [tpfor types in [list(VisionType),list(TextType),]for tp in types]# @dataclass@dataclass(slots=True)class Event:"""Represents a single data occurrence (with a specific type, time interval, and data payload).Attributes:data (Any): The actual data payload (e.g. a torch.Tensor, a string, etc.).type (ModalityType): The modality type of the data (e.g., VisionType.image).time (Tuple[float, float]): (start_time, end_time) indicating when this Event occurs.role (Optional[str]): The role associated with this event (e.g., "user", "agent", "system").If None, the event is always included in loss calculation.Example usage:evt = Event(data=torch.zeros((1, 224, 224, 3)), # e.g. a single image frametype=VisionType.image,time=(0.0, 0.04),role="user")"""# Descriptorsdata: Anytime: tuple[float, float]type: ModalityTyperole: str | None = None# Structuredims_virtual: list[int] | None = None # virtual/processed dimensions (e.g., pixel-shuffled)dims_real: list[int] | None = None # real/actual tensor dimensionsidx_range: tuple[int, int] | None = None# Misc Tags (data source, shard idx, etc.)tags: dict = field(default_factory=dict)def dims(self, virtual: bool = True) -> list[int] | None:"""Get the dimensions of this event.Args:virtual: If True (default), return virtual/processed dimensions (e.g., pixel-shuffled).If False, return real/actual tensor dimensions.Returns:Dimensions list or None if not measured."""if virtual:return self.dims_virtualelse:return self.dims_real@propertydef is_measured(self):return self.dims_virtual is not Nonedef slice_tokens(self, start: int | None = None, end: int | None = None):"""Converts into a partial event where the only valid data is between start and end indices of the flattened data"""assert self.is_measuredassert start is not None and end is not Noneassert self.idx_range[0] <= start <= end <= self.idx_range[1]self.idx_range = (start or 0, end or math.prod(self.dims()))def num_tokens(self, partial=True, virtual=True) -> int:if not virtual:assert partial is False and isinstance(self.data, torch.Tensor)return math.prod(self.dims(virtual=False))return self.idx_range[1] - self.idx_range[0] if partial else math.prod(self.dims())def shallow_copy(self) -> Event:return replace(self)def __hash__(self) -> int:"""Hash Event based on structure, excluding data."""def make_hashable(obj):"""Convert any object to hashable form."""if obj is None:return Noneelif isinstance(obj, str | int | float | bool | tuple):return objelif isinstance(obj, list):return tuple(make_hashable(item) for item in obj) if obj else Noneelif isinstance(obj, dict):return tuple(sorted((k, make_hashable(v)) for k, v in obj.items())) if obj else Noneelif hasattr(obj, "value"): # Enum typesreturn obj.valueelse:return str(obj) # Fallback for other typeshash_values = []for fld in fields(self):if fld.name == "data":continue # Skip tensor datavalue = getattr(self, fld.name)hash_values.append(make_hashable(value))return hash(tuple(hash_values))def __eq__(self, other) -> bool:"""Compares two Event objects for strict equality,allowing for float tolerances in torch.Tensors (via torch.allclose)."""if not isinstance(other, Event):return Falsefor fld in fields(self):self_value = getattr(self, fld.name)other_value = getattr(other, fld.name)if fld.name == "data":# Special handling for tensor data with float toleranceif isinstance(self_value, torch.Tensor) and isinstance(other_value, torch.Tensor):if not torch.allclose(self_value, other_value):return Falseelse:if self_value != other_value:return Falseelif fld.name == "role":# Special handling for role: both must be None or both must be set and equalif (self_value is None) != (other_value is None):return Falseif self_value is not None and self_value != other_value:return Falseelse:# Standard equality for all other fieldsif self_value != other_value:return Falsereturn True@dataclassclass Stream:"""Represents an ordered sequence of Event objects, each witha specific ModalityType and a time range.Attributes:events (List[Event]): The list of Event objects in the stream.priority (List[ModalityType]): A list of modality types that definehow we might want to reorder or prioritize events if scheduling is needed.Example usage:# Create two events of different typesevt1 = Event(torch.zeros((1, 224, 224, 3)), VisionType.image, (0.0, 0.04))evt2 = Event(torch.randint(0, 1000, (16, 1)), TextType.text, (0.0, 0.32))# Make a stream with a given prioritys = Stream(events=[evt1, evt2],priority=[VisionType.image, TextType.text])print(s)"""events: list[Event]priority: list[ModalityType] # priority of stream orderingdef __len__(self):"""Returns the number of Event objects in this Stream."""return len(self.events)def __getitem__(self, key: int) -> Stream | Event:return self.events[key]def __iter__(self):"""Yields each Event in the Stream, enabling iteration like:for event in my_stream:..."""yield from self.events# --- after ------------------------------------------------------------@record_function("Stream.map")def map(self,func: Callable[[Event], dict[str, Any]],*,copy_unchanged: bool = False, # opt-in if you really need isolation) -> Stream:"""Apply *func* to every event and return a new Stream.*func* must return a **dict of fields that actually change**.We create **one shallow copy** only when something changes;unchanged events are reused directly, which is inexpensive andkeeps autograd graphs intact."""mapped: list[Event] = []for ev in self.events:delta = func(ev)if not delta: # fast-path: nothing changesmapped.append(ev if not copy_unchanged else ev.shallow_copy())continuenew_ev = ev.shallow_copy() # ⚡ no tensor clonefor k, v in delta.items():setattr(new_ev, k, v)mapped.append(new_ev)return create_stream(mapped, priority=self.priority, schedule=False)@record_function("Stream.compact")def compact(self) -> torch.Tensor:assert all([(isinstance(ev.data, torch.Tensor) and ev.is_measured) for ev in self.events]), ("Stream.compact only works for streams with events that have measured tensor data")return torch.cat([ev.data for ev in self.events]).contiguous()@record_function("Stream.map_compact")def map_compact(self, event_tf: Callable[[Event], list[Any]]) -> torch.Tensor:mapped_list = []for event in self:mapped_list.extend(event_tf(event))tensor = torch.tensor(mapped_list,dtype=torch.long,device=next((ev.data.device for ev in self.events if isinstance(ev.data, torch.Tensor)),"cpu",),).contiguous()return tensordef flatten(self) -> Stream:return self.map(lambda ev: {"data": ev.data.reshape(-1, ev.data.shape[-1])})def shallow_copy(self) -> Stream:events_copy = [ev.shallow_copy() for ev in self.events]return create_stream(events=events_copy, priority=self.priority, schedule=False)def __hash__(self) -> int:"""Hash Stream based on structure."""return hash((tuple(p.value for p in self.priority), # Convert enums to valuestuple(hash(event) for event in self.events), # Use Event.__hash__))def __eq__(self, other) -> bool:"""Compare Streams structurally."""if not isinstance(other, Stream):return Falsereturn (self.priority == other.priorityand len(self.events) == len(other.events)and all(e1 == e2 for e1, e2 in zip(self.events, other.events, strict=False)))# TODO: implement all types of cool indexing which can happen since TensorStream assuems Event.data = Tensor@dataclassclass TensorStream:streams: list[Stream]_device: torch.device | None = Nonedef __post_init__(self):for stream in self.streams:for event in stream.events:assert isinstance(event.data, torch.Tensor)if self._device is None:self._device = torch.device(event.data.device)# TODO: implement non-strict compaction modes@record_function("TensorStream.compact")def compact(self, mode="strict") -> torch.Tensor:compact_tensor_stream = torch.stack([stream.compact() for stream in self.streams]).contiguous()return compact_tensor_stream@record_function("TensorStream.map")def map(self, event_tf: Callable[[Event], dict[str, Any]]) -> TensorStream:mapped_streams = [stream.map(event_tf) for stream in self.streams]return TensorStream(mapped_streams)@record_function("TensorStream.map_compact")def map_compact(self, event_tf: Callable[[Event], list[Any]]) -> torch.Tensor:mapped_list = []for stream in self.streams:for event in stream:mapped_list.extend(event_tf(event))B, T = self.shapetensor = torch.tensor(mapped_list, dtype=torch.long, device=self.device).reshape(B, T)return tensordef flat_stream(self) -> Stream:if not self.streams:return create_stream([], priority=[], schedule=False)return create_stream([event for stream in self.streams for event in stream], priority=self.streams[0].priority, schedule=False)@propertydef device(self):return self._device@propertydef shape(self):seq_lens = [sum([ev.num_tokens() for ev in stream]) for stream in self.streams]assert all([sl == seq_lens[0] for sl in seq_lens]), (f"each stream must have same token count to have a shape: {seq_lens}")return (len(seq_lens), seq_lens[0])@record_function("TensorStream.to")def to(self,device: torch.device | str,dtype: torch.dtype | None = None,non_blocking: bool = True,) -> TensorStream:"""Move **all** `Event.data` tensors to *device*.We send each tensor individually instead of theflatten → unflatten round-trip:* one async H2D copy per tensor (still overlapped when`pin_memory=True` is set on the DataLoader),* no extra host-side concat, no extra device allocation,* `requires_grad` flags are preserved.NOTE: textual modalities are always cast to `torch.long`;everything else keeps its originaldtype unless an explicit *dtype* argument is supplied."""target_device = torch.device(device)for stream in self.streams:for ev in stream:# ------------------------------------------------------------------# Decide the dtype for *this* event.# ------------------------------------------------------------------if ev.type in list(TextType):tgt_dtype = torch.longelse:tgt_dtype = dtype or ev.data.dtype# ------------------------------------------------------------------# Perform the device / dtype move.# ------------------------------------------------------------------# We clone no tensor here; torch will reuse storage# if `dtype` and `device` are unchanged.moved = ev.data.to(device=target_device,dtype=tgt_dtype,non_blocking=non_blocking,)# Preserve autograd leaf & grad-enabled state.moved.requires_grad_(ev.data.requires_grad)ev.data = moved# Remember where the whole TensorStream lives now.self._device = target_devicereturn self@record_function("TensorStream.pin_memory")def pin_memory(self, non_blocking: bool = True) -> TensorStream:"""Page-lock (aka *pin*) all **CPU** tensors contained in this`TensorStream`. Pinned tensors make subsequent asynchronousH2D copies (e.g. inside `TensorStream.to("cuda")`) faster and,when used together with a `DataLoader(pin_memory=True)`,enable overlap of host-to-device transfers with GPU execution.The call is a no-op for tensors that are already on a CUDA /MPS / other non-CPU device.Parameters----------non_blocking : bool, default = TrueForwarded to `Tensor.pin_memory()`; should almost alwaysstay *True* so later `to(device, non_blocking=True)` callscan overlap.Returns-------self : TensorStreamThe same object (mutated in-place) to allow call chaining."""for stream in self.streams:for ev in stream:if ev.data.device.type == "cpu":# `pin_memory()` clones only when neededpinned = ev.data.pin_memory() # noqa: F841# NB: pin_memory() preserves dtype/shape/grad/etc.if not non_blocking:# ensure the pinning work is done nowtorch.cuda.current_stream().synchronize() # safe on CPU tooev.data = pinned# `_device` **stays** the same (still CPU) – no change neededreturn selfdef __hash__(self) -> int:"""Hash TensorStream based on structure."""return hash((tuple(hash(stream) for stream in self.streams), # Use Stream.__hash__str(self._device) if self._device else None,self.shape,))def __eq__(self, other) -> bool:"""Compare TensorStreams structurally."""if not isinstance(other, TensorStream):return Falsereturn (self._device == other._deviceand self.shape == other.shapeand len(self.streams) == len(other.streams)and all(s1 == s2 for s1, s2 in zip(self.streams, other.streams, strict=False)))def collate_tensor_stream(tensor_streams: list[TensorStream],) -> TensorStream:return TensorStream([stream for ts in tensor_streams for stream in ts.streams])def _schedule_stream(stream: Stream) -> Stream:"""Internal function that reorders (schedules) the events in a Streambased on the stream's priority.By default, this calls schedule_events(...) and reorders the events accordingly.The new ordering is assigned in-place to stream.events.Example usage (indirect):new_stream = _schedule_stream(old_stream)"""scheduled_inds = schedule_events(stream, priority=stream.priority)stream.events = [stream.events[i] for i in scheduled_inds]return streamdef create_stream(events: list[Event], priority: list[ModalityType], schedule: bool = True) -> Stream:"""Creates a new Stream with the given events and priority.If 'schedule' is True, the events are reordered by calling _schedule_stream.Example usage:evt1 = Event(torch.zeros(10), TextType.text, (0.0, 1.0))evt2 = Event(torch.ones(10), TextType.text, (1.0, 2.0))my_stream = create_stream(events=[evt1, evt2],priority=[TextType.text],schedule=False)print(my_stream)"""stream = Stream(events, priority)if schedule:stream = _schedule_stream(stream)return streamdef merge_streams(streams: Iterable[Stream]) -> Stream:"""Merges multiple Stream objects into one.The priority of the merged stream is chosen from the longest priority list among the inputs.Stream priorities must be consistent with the chosen priority.All events are concatenated, and a new Stream is created (and scheduled).Example usage:merged = merge_streams([stream1, stream2])"""chosen_priority = max([stream.priority for stream in streams], key=len)assert all([str(stream.priority) in str([p for p in chosen_priority if p in stream.priority]) for stream in streams]), "One or more streams has a priority order that doesn't match the merged stream"merged_event_list = [ev for stream in streams for ev in stream.events]merged_stream = create_stream(merged_event_list, chosen_priority) # non-root stream creationreturn merged_streamEventDescriptor = NewType("EventDescriptor", Any)# NOTE: actually not used now but thought it *might* be usefuldef get_stream_descriptor(stream: Stream, measure_fn: Callable[[Event], EventDescriptor] = lambda ev: ev.type) -> set[Any]:"""Create a set of descriptors for each Event in a Stream based on measure_fn.measure_fn maps an Event to a descriptive key.For example, if events have different data shapes, one might use:measure_fn = lambda ev: ev.data.shapei.e.stream of VisionTypes with tensors of shapes [(1, 3, 3), (1, 3, 3), (1, 4, 4)]get_stream_descriptor(stream, measure_fn=lambda t: t.shape) = {(1, 3, 3), (1, 4, 4)}now we can pass this into group_streams which will split out vision sub-streams which can be bundledReturns:A set of descriptors representing the Events in the stream.Example usage:descriptor = get_stream_descriptor(my_stream, lambda ev: ev.type)"""stream_descriptor = set()for ev in stream.events:ev_measurement = measure_fn(ev)stream_descriptor.add(ev_measurement)return stream_descriptordef group_streams(stream: Stream, group_fn: Callable[[Event], EventDescriptor], schedule=True) -> dict[EventDescriptor, Stream]:"""Splits a single Stream into multiple sub-Streams, grouped by the output of group_fn(event).For example, group_fn could be:- lambda ev: ev.type- lambda ev: ev.type.modality- lambda ev: (ev.type.modality, ev.data.shape)Returns:A dictionary mapping each group key to a Stream of events belonging to that group.If 'schedule' is True, each sub-Stream is scheduled via create_stream(..., schedule=True).Example usage:substreams = group_streams(my_stream, lambda ev: ev.type)"""split_streams: defaultdict[EventDescriptor, list[Event]] = defaultdict(list)for ev in stream:group = group_fn(ev)split_streams[group].append(ev)for g, events in split_streams.items():split_streams[g] = create_stream(events, stream.priority, schedule=schedule)return dict(split_streams)# Define Category for clarityCategory = NewType("Category", Any)def schedule_events(stream: Stream, priority: list[Category]) -> list[int]:"""Schedule events based on their start time and priority using a topological sort algorithm.The priority list defines the ordering of categories.This function:1. Pairs each event with its original index.2. Sorts events by start time.3. Builds a dependency graph based on overlapping events.4. Uses a heap to perform a deterministic topological sort with tie-breakers.Raises:ValueError: If a cycle is detected in the events (i.e., no valid ordering exists).Returns:List[int]: A list of original indices representing the scheduled order of events."""priority_index: dict[Category, int] = {category: idx for idx, category in enumerate(priority)}# Pair each event metadata with its original indexevents = []for i, event in enumerate(stream.events):events.append((i,event.time[0],event.time[1],event.type,))sorted_events = sorted(events, key=lambda e: e[1]) # sort by start timenum_events = len(sorted_events)# Build dependency graphgraph = defaultdict(set)indegree = {i: 0 for i in range(num_events)}for i in range(num_events):idx_i, start_i, end_i, category_i = sorted_events[i]prio_i = priority_index[category_i]for j in range(i + 1, num_events):idx_j, start_j, end_j, category_j = sorted_events[j]if start_j >= end_i:breakif end_i > start_j and end_j > start_i:prio_j = priority_index[category_j]if prio_i < prio_j:graph[i].add(j)indegree[j] += 1elif prio_i > prio_j:graph[j].add(i)indegree[i] += 1# Use heap for deterministic tie-breakers: (start_time, priority, original_index)heap = [(sorted_events[i][1],priority_index[sorted_events[i][3]],sorted_events[i][0],i,)for i in range(num_events)if indegree[i] == 0]heapq.heapify(heap)resolved_order = []while heap:_, _, _, u = heapq.heappop(heap)resolved_order.append(u)for v in graph[u]:indegree[v] -= 1if indegree[v] == 0:heapq.heappush(heap,(sorted_events[v][1],priority_index[sorted_events[v][3]],sorted_events[v][0],v,),)if len(resolved_order) != num_events:raise ValueError("Cycle detected in events, cannot resolve order")return [sorted_events[i][0] for i in resolved_order]def compute_mrope_pos_tensor(ts: TensorStream, n_pos_dims: int = 3) -> torch.Tensor:"""Create a (batch, T, n_pos_dims) position tensor in one sweep.The first dim is the running “time” index, the rest are spatial (or 1-fillers).Args:ts : TensorStreamn_pos_dims : total coordinate dimensions (default 3)Returns:torch.LongTensor - shape (batch_size, seq_len, n_pos_dims)"""# Manually iterate through streams and events like map_compact does,# but maintain cumulative time offset for each streamall_coords = []for stream in ts.streams: # one Stream == one batch samplecumulative_offset = 0 # running time index for this streamfor event in stream:# --- build coordinate grid for THIS event using itertools (no tensor ops) ---dims = (event.dims() or [1]) + [1] * (n_pos_dims - len(event.dims() or []))# Create ranges for each dimension (similar to old _finalize implementation)first_dim = range(cumulative_offset, cumulative_offset + dims[0])cumulative_offset += dims[0] # advance time for the next eventother_dims = [range(d) for d in dims[1:]]# Use itertools.product to create all coordinate combinationsfull_coords = list(itertools.product(first_dim, *other_dims))# Slice if the event is partials, e = event.idx_rangecoords = full_coords[s:e]# Extend the flattened coordinate listall_coords.extend(coords)# Convert to tensor and reshape to (B, T, n_pos_dims)B, T = ts.shapereturn torch.tensor(all_coords, dtype=torch.long, device=ts.device).reshape(B, T, n_pos_dims)# ──────────────────────────────────────────────────────────────────────────# Generic event-labelling helper# ──────────────────────────────────────────────────────────────────────────def event_mask(ts: TensorStream,tag_fn: Callable[[Event], int | None],default: int = -1,) -> torch.Tensor:"""Build a (batch, seq_len) LongTensor whose value for every *token*is given by `tag_fn(event)`, falling back to `default` when thefunction returns None.The work is done in a single pass via `map → compact`."""def to_label(ev: Event) -> Any:label = tag_fn(ev)if label is None:label = defaultreturn [label] * ev.num_tokens()return ts.map_compact(to_label).squeeze(-1)def event_mask_by_key(ts: TensorStream,key: str,tag_index: dict[str, int],default: int = -1,) -> torch.Tensor:"""Faster call-site syntax when you just want to look up`event.tags[key]` and map it through `tag_index`."""return event_mask(ts,lambda ev: tag_index.get(ev.tags.get(key)) if key in ev.tags else None,default=default,)def modality_mask(ts: TensorStream) -> torch.Tensor:return event_mask(ts, lambda ev: ev.type.value)ROLE_TO_IDX = {None: -1,"": -1,"agent": 0,"user": 1,"system": 2,# … add more if you like}def role_mask(ts: TensorStream) -> torch.Tensor:return event_mask(ts, lambda ev: ROLE_TO_IDX.get(ev.role, -1))def tensor_stream_token_view(ts: TensorStream) -> torch.Tensor:"""Return a (B, T) token view by summing across the last dim of everyevent and flattening over the selected token range."""def to_token_view(ev: Event) -> list[int]:# collapse all but the last dim, cast to longflat = ev.data.sum(dim=-1).long().reshape(-1)if ev.idx_range is not None:s, e = ev.idx_rangereturn flat[s:e].tolist()else:return flat.tolist()return ts.map_compact(to_token_view) # shape (B, T)def reconstruct_tensor_stream_from_compact_dict(ts: TensorStream, compact_dict: dict[ModalityType, torch.Tensor]) -> TensorStream:streams = []for stream in ts.streams:event_list = []for event in stream:new_event = event.shallow_copy()new_event.data = compact_dict[event.type][event.idx_range[0] : event.idx_range[1]]compact_dict[event.type] = compact_dict[event.type][event.num_tokens(partial=False) :]event_list.append(new_event)streams.append(Stream(event_list, priority=stream.priority))return TensorStream(streams)def set_data(tensor_stream: TensorStream,stream_types: Iterable[ModalityType],roles: Iterable[str] = ROLE_TO_IDX.keys(),) -> tuple[torch.Tensor, torch.Tensor]:"""Gathers data from a TensorStream according to the given stream typesand returns (data, mask) where 'data' has valid entries foreach requested stream type and 'mask' indicates which elementsin 'data' are valid.NOTE: Currently assumes stream_types are text-based types, but can be extended.Args:tensor_stream (TensorStream):The input TensorStream which contains data for multiple modalities.stream_types (Iterable[ModalityType]):A list or iterable of modality types (e.g., TextType, VisionType, etc.)to retrieve from the TensorStream.exclude_non_agent_roles (bool, optional):If True, only include tokens with role="agent" or role=None in the loss calculation.Defaults to False.Returns:Tuple[torch.Tensor, torch.Tensor]:- data: A tensor of the same shape as the internal metadata shape,containing valid entries from the given stream types.- mask: A boolean tensor of the same shape, where True indicatesthe corresponding element in 'data' is valid/used."""# Retrieve indexing and shape metadatast_tensor = modality_mask(tensor_stream) # (B, T) modality-idsroles_tensor = role_mask(tensor_stream) # (B, T) role-ids# Create output data placeholders on the same devicedata = torch.zeros_like(st_tensor).to(tensor_stream.device)set_data_mask = torch.zeros_like(st_tensor).bool().to(tensor_stream.device).bool()per_modality_stream = group_streams(tensor_stream.flat_stream(), group_fn=lambda ev: ev.type, schedule=False)per_modality_compact_stream = {k: v.compact() for k, v in per_modality_stream.items()}# Fill 'data' and 'set_data_mask' for each requested stream typefor st in stream_types:data_mask = st_tensor == st.valuepartial_mask = (per_modality_stream[st].map_compact(lambda ev: [int(ev.idx_range[0] <= i < ev.idx_range[1]) for i in range(ev.num_tokens(partial=False))]).bool())data[data_mask] = per_modality_compact_stream[st].reshape(-1)[partial_mask]roles_mask = torch.zeros_like(st_tensor).bool().to(tensor_stream.device).bool()for role in roles:roles_mask |= roles_tensor == ROLE_TO_IDX[role]data_mask = data_mask & roles_maskset_data_mask[data_mask] = Truereturn data, set_data_maskdef ts_slice(tensor_stream: TensorStream, start: int, end: int) -> TensorStream:"""Return a new TensorStream that contains *only* the tokens in thehalf-open interval ``[start, end)`` (0-based, inclusive-exclusive)."""B, T = tensor_stream.shapeassert 0 <= start <= end <= T, f"slice [{start}, {end}) is out of bounds for sequence length {T}"sliced_streams: list[Stream] = []for stream in tensor_stream.streams:# current position in tensor stream token dimscurr_global_index = 0new_events: list[Event] = []# iterate over each of the events in the stream only selecting# the events that fall within the rangefor ev in stream:ev_len = ev.num_tokens()# ev_start, ev_end are the start and end indicies of the# event within the tensor stream token dimglobal_ev_start, global_ev_end = curr_global_index, curr_global_index + ev_lenif global_ev_end <= start:# The event occurs before the start skip it and move the cursor# forwardcurr_global_index = global_ev_endcontinueif global_ev_start >= end:# event occurs after the end we can exitbreak# only consider the part of the event that falls within the rangekeep_from = max(0, start - global_ev_start)keep_to = min(ev_len, end - global_ev_start)part = ev.shallow_copy()if keep_from == 0 and keep_to == ev_len:# Event lies wholly inside the slicenew_events.append(part)else:# Partial overlap → trim.assert ev.is_measured# update the local event ranges for the slicessliced_event_start = part.idx_range[0] + keep_fromsliced_event_end = part.idx_range[0] + keep_topart.slice_tokens(sliced_event_start, sliced_event_end)new_events.append(part)curr_global_index = global_ev_endsliced_streams.append(create_stream(new_events, stream.priority, schedule=False))return TensorStream(sliced_streams)class PixelShuffleSiglip2VisionConfig(Siglip2VisionConfig):419 unmodified lines# Configuration# ============================================================================MAX_PIXELS = 60_000_000 # 60-megapixel ceiling ≈ 8200 × 7300 px# Vision preprocessing constantsVISION_MEAN = (0.5, 0.5, 0.5)10 unmodified linesif arr.flags.writeable:return arr# First, try the cheap path — in-place flag toggle (works for mmap'd arrays# and some shared memory buffers):try:arr.setflags(write=True)return arr # success: no data copyexcept ValueError:# Buffer is inherently read-only (e.g. backed by PyAV / PIL): make copyreturn arr.copy()1119 unmodified lines"IsaacModel","IsaacForConditionalGeneration","IsaacProcessor",]