Skip to main content

fastpragma

An easy-to-use API for foundation model development, based on the PRAGMA framework.

Installation

Install latest from the GitHub repository:

pip install git+https://github.com/risheekkumarb/fastpragma.git

Install from conda:

conda install -c risheekkumarb fastpragma

Install from pypi:

pip install fastpragma

Documentation

Documentation can be found on this GitHub repository's pages. Package-manager-specific pages are also available on conda and pypi.

Usage

fastpragma currently exposes three main layers:

  1. Data — declare profile/event sources with DataSource, then tokenize them with PRAGMADataset
  2. Dataloading — read tokenized parquet shards with pragma_dl / pragma_dls
  3. Model + training — build a PRAGMA-style model with PRAGMAModel, pragma_model, or pragma_learner

The API is still evolving, so this README focuses on the pieces implemented in the notebooks today.

import polars as pl
from fastai.data.external import untar_data, URLs
from fastcore.all import *

from fastpragma.data import DataSource, PRAGMADataset
from fastpragma.dataloader import pragma_dl
from fastpragma.model import pragma_dls, pragma_model, pragma_learner

Data format

fastpragma expects data in two broad forms:

  1. Profile data — one row per entity, containing relatively static attributes.
  2. Event data — many rows per entity, each with a timestamp.

Each source declares which columns are:

  • cats: categorical fields
  • conts: continuous numerical fields
  • signed_conts: continuous numerical fields where sign matters separately
  • texts: free-text fields
  • lifelong: timestamp/milestone fields in profile data

Example: MovieLens 100K

This example loads the classic MovieLens 100K dataset into polars DataFrames.

Creating data sources

Use DataSource to declare how each DataFrame should be interpreted.

Profile sources use is_profile=True and normally do not need a time_col. Event sources provide a time_col.

path = untar_data(URLs.ML_100k)
events_df = pl.scan_csv(
    path/'u.data',
    separator='\t',
    has_header=False,
    new_columns=['user_id','movie_id','rating','timestamp'],
)
events_df = events_df.with_columns(pl.from_epoch('timestamp', time_unit='s').alias('timestamp'))
ratings = DataSource(events_df, entity_col='user_id', cats=['movie_id','rating'], time_col='timestamp', name='events_df')
ratings
profile_df = pl.scan_csv(
    path/'u.user',
    separator='|',
    has_header=False,
    new_columns=['user_id','age','gender','occupation','zip_code'],
)
profile = DataSource(
    profile_df,
    entity_col='user_id',
    cats=['gender','zip_code'],
    conts=['age'],
    texts=['occupation'],
    name='users',
    is_profile=True,
)
profile

Building a PRAGMADataset

PRAGMADataset combines one optional profile source and one or more event sources.

It fits a tokenizer, converts sources into key-value-time tokens, and writes entity-sharded parquet files.

dataset = PRAGMADataset(profile=profile, events=[ratings], entity_col="user_id", out_path="data")

# Fit vocabularies and numerical buckets.
tok = dataset.fit_tokenizer(num_buckets=10, cardinality_threshold=100)

# Write tokenized entity shards.
shard_dir = dataset.write_kv(eval_time="1998-04-01T00:00:00", n_shards=4)

Dataloaders

pragma_dl creates a PyTorch/fastai-compatible dataloader from tokenized parquet shards.

For masked-language-model pre-training, pass mask=True and the fitted tokenizer.

shards = sorted(Path(shard_dir).glob("shard_*.parquet"))
dl = pragma_dl(shards, entity_col="user_id", max_tokens=1500, shuffle=True, tok=tok, mask=True)

For training with fastai, pragma_dls creates train/validation DataLoaders from the shard list.

dls = pragma_dls(shards, tok=tok, max_tokens=1500)

Model and training

The implemented model is an encoder-only PRAGMA-style architecture with three main pieces:

  1. A profile encoder
  2. An event encoder
  3. A history encoder

The model predicts masked event value tokens during pre-training.

# Small preset model.
model = pragma_model("S", n_keys=len(tok.key_vocab), n_vals=len(tok.val_vocab))
model

For quick experiments, use pragma_learner, which builds a compact PRAGMAModel and wraps it in a fastai Learner.

learn = pragma_learner(dls, n_keys=len(tok.key_vocab), n_vals=len(tok.val_vocab))
# learn.fit(1)

Current implemented API

Data

  • DataSource

    • wraps a polars LazyFrame
    • validates declared columns
    • supports from_df(...) and from_file(...)
    • handles categorical, continuous, signed continuous, textual, event-time, and lifelong/profile fields
  • Tokenizer

    • builds key/value vocabularies
    • bucketizes numerical fields
    • tokenizes text fields
    • converts sources into key-value-time form
  • PRAGMADataset

    • combines profile and event sources
    • fits/saves a tokenizer
    • writes sharded parquet token data with write_kv(...)

Dataloading

  • PRAGMADataLoader

    • streams parquet shards
    • groups rows by entity
    • packs variable-length records up to max_tokens
    • optionally applies MLM masking
  • pragma_dl

    • convenience function returning a DataLoader
  • pragma_dls

    • convenience function returning fastai DataLoaders

Model

  • PRAGMAModel

    • profile encoder
    • event encoder
    • calendar/time embeddings
    • history encoder
    • MLM head
  • pragma_model

    • creates preset model sizes: "S", "M", "L"
  • pragma_learner

    • creates a fastai learner for masked-token pre-training

Planned additions

The following pieces are planned for future versions:

  • A more polished public API, possibly including SourceSchema as a friendlier alias or replacement for DataSource
  • A .dataloaders() convenience method directly on PRAGMADataset
  • A top-level PRAGMA.load(size="S"|"M"|"L") model-loading API
  • Better README examples using tiny synthetic data that can run without downloading MovieLens
  • A richer show_batch() display for inspecting tokenized profile and event records
  • Embedding extraction APIs such as model.embed(dataset) and model.embed_record(record)
  • Task-specific heads for classification, regression, recommendation, and retrieval
  • LoRA fine-tuning utilities for adapting the backbone efficiently
  • Linear probing helpers for evaluating frozen embeddings
  • Save/load helpers for trained learners, heads, tokenizers, and model weights
  • Optional text encoder integration for richer free-text fields
  • More complete documentation of temporal features, calendar features, and lifelong events
  • More tests and smoke-test notebooks covering data → tokenizer → shards → dataloader → model → learner

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastpragma-0.0.10.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastpragma-0.0.10-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file fastpragma-0.0.10.tar.gz.

File metadata

  • Download URL: fastpragma-0.0.10.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fastpragma-0.0.10.tar.gz
Algorithm Hash digest
SHA256 4276c4453b70e7a40627660bc114feac7680fc37c0ea85e4cf00a366c5388ed7
MD5 70f297f0a342228a709befa14283c787
BLAKE2b-256 2942d32702a8de0a60b73fea4b8a23d195400071b6192693f5b15d3411ea5beb

See more details on using hashes here.

File details

Details for the file fastpragma-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: fastpragma-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fastpragma-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 120e0b7abc1c886674ffe3a81ca9146056153e5a9d0f511620b754cf2bcd0974
MD5 843e767e8ffae4506dc1e9f054d9720a
BLAKE2b-256 3c7098c1b915c811e542c941847c333b3e000f7e861d89b4915ed9d9b2ea54d9

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

This release

0.0.10 This release

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page