fit_to_data compiles about 26 XLA programs for every new window size

Open
#366 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
49/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

The issue points to fit_to_data, fit_flow, and the step calls in the training loop, but names no source file or test. Start by locating those entry points and tracing how changing window and batch shapes trigger compilation; use the supplied reproduction to measure compilations across window sizes. Done means reducing repeated XLA compilations while preserving the training behavior.

Written by the indexing model from the issue text.

Description

normalizing-flows

fit_to_data, the flow's training loop, compiles about 26 XLA programs each time the adapter hands it a window of a new size. Window sizes change until the window reaches its maximum length. The shuffle, the train/validation split, the batching and count_fruitless run as eager JAX ops on arrays whose shapes change, and every batch is a separate jitted step call.

import logging

import jax
import numpy as np
from nutpie.transform_adapter import FisherLoss, fit_flow, make_transform_adapter

compiles = []


class CountCompiles(logging.Handler):
    def emit(self, record):
        if "Finished XLA compilation" in record.getMessage():
            compiles.append(record.getMessage())


jax.config.update("jax_log_compiles", True)
logging.getLogger("jax").addHandler(CountCompiles())
logging.getLogger("jax").setLevel(logging.WARNING)

rng = np.random.default_rng(0)
draws = rng.normal(size=(600, 20))
make_flow_fn = make_transform_adapter(num_layers=4).keywords["make_flow_fn"]
flow = make_flow_fn(0, draws, -draws, n_layers=4)

for n_draws in [137, 265, 393, 521, 600, 600]:  # the window sizes nutpie's adapter trains on
    before = len(compiles)
    fit_flow(jax.random.PRNGKey(0), flow, FisherLoss(), draws[:n_draws], -draws[:n_draws], np.zeros(n_draws),
             max_epochs=30, max_patience=30, batch_size=128, show_progress=False)
    print(f"n_draws={n_draws}: {len(compiles) - before} XLA compilations")
# n_draws=137: 47, 265: 26, 393: 27, 521: 26, 600: 26, 600: 2
Dominant language
Python
Stars
214
Forks
31
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pymc-devs/nutpie

All issues in pymc-devs/nutpie

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.