lwm-tf / example_inference.py
wi-lab's picture
LWM-TF minimal release: model code, 200k weights, inference example, demo data
46bd08f verified
Raw History Blame Contribute Delete
2.41 kB
"""Minimal LWM-TF usage: load the weights, embed resource grids, fit a probe, and reconstruct masked tokens.
Usage: python example_inference.py (downloads config.json, model.safetensors, demo_data.npz)
While the repo is private, set HF_TOKEN first.
"""
import os
import numpy as np
import torch
from huggingface_hub import hf_hub_download
from sklearn.linear_model import LogisticRegression
from lwm_tf import REPO_ID, factorized_mask, load_model, loss
token = os.getenv("HF_TOKEN")
model = load_model(REPO_ID, token=token) # FactorizedMAE, eval mode, CPU
# 1) Embeddings. A grid is one slot: [8, 14, 288] = Re/Im x 4 Rx antennas, 14 OFDM symbols,
# 288 subcarriers (24 PRB), normalized to unit RMS over the whole slot.
demo = np.load(hf_hub_download(REPO_ID, "demo_data.npz", token=token), allow_pickle=False)
grids = torch.from_numpy(demo["grids"][:64].astype(np.float32)) # [64, 8, 14, 288]
with torch.no_grad():
z = model.embedding(grids) # [64, 256]
print("embedding:", tuple(z.shape))
# The bundled embeddings of all 24,576 test-set-2 slots were computed the same way (bf16 autocast on GPU).
cos = torch.nn.functional.cosine_similarity(z, torch.from_numpy(demo["embedding"][demo["grid_index"][:64]]))
print(f"cosine to the bundled embeddings: min {cos.min():.4f}")
# 2) A frozen-feature probe: 16 labeled slots per modulation class.
x, order = demo["embedding"], demo["label_modulation_order"]
y = np.searchsorted([2.0, 4.0, 6.0], order) # QPSK / 16QAM / 64QAM
rng = np.random.default_rng(0)
fit = np.concatenate([rng.choice(np.flatnonzero(y[:12288] == c), 16, replace=False) for c in range(3)])
mean, std = x[:12288].mean(0), x[:12288].std(0) + 1e-6
probe = LogisticRegression(max_iter=2000).fit((x[fit] - mean) / std, y[fit])
print(f"16-shot modulation accuracy on slots 12288+: {probe.score((x[12288:] - mean) / std, y[12288:]):.3f}")
# 3) Masked reconstruction with the pre-training mask (keep 7/14 symbols x 48/96 antenna-PRB positions).
visible, keep_t, keep_s = factorized_mask(len(grids), 14, 96, 7, 48, generator=torch.Generator().manual_seed(0))
with torch.no_grad():
total, parts = loss(model(grids, keep_t, keep_s), visible, lambda_enc=0.1, lambda_dec=0.1, phase_weight=0.0)
print(f"masked-token loss (patch-normalized log-power): {parts['recon']:.3f} (patch-mean predictor: 1.0)")