Repository navigation
feat: redesign pretty repr for Node, Module, State and friends - #246
Merged
Merged
Conversation
Replace the one-attribute-per-line renderer with a width-aware compact
tree layout shared by graph.Node, nn.Module, State, TreefyState and the
util containers.
- Layout: groups print on one line when they fit (default width 88),
otherwise one item per line with trailing commas; [i] labels for
sequences of objects; <shared: path> / <cycle: path> markers.
- Arrays summarised as f32[3,4], f32(0.5), f32[3] mV, key<fry>[].
- Summary: root footer "# 26 params (104 B)" and per-child comments,
computed from unique states without triggering read hooks.
- Options: ReprOptions, repr_options(), set_repr_options(),
get_repr_options(); opt-in ANSI color for print() only (NO_COLOR,
BRAINSTATE_REPR_COLOR); pretty_format() for arbitrary values.
- Module: "(3,) -> (4,)" in/out header; State: positional value and
ParamState{bias: ..., weight: ...} brace form.
- Former hand-written reprs (elementwise activations, nn/interop
transforms, HiData, RandomState, Mode family, ParamDescriber) now
render through the engine.
Fixes: TreefyState printed full arrays and _hooks_manager; Map/Param
showed None attributes; Batching mislabelled batch_size as in_size;
PermuteAxes printed as Transpose; HiData subclasses printed as HiData.
Spec: docs/specs/pretty-repr-redesign.md
Contributor
|
Sorry @chaoming0625, you've used your own review budget of 250,000 diff characters for the last 7 days. You can request another review in 6 days by commenting |
Contributor
Reviewer's GuideIntroduces a shared, width-aware pretty-representation engine and applies it throughout nodes, modules, states, containers, and previously custom-repr classes, adding compact layouts, array formatting, reference detection, summaries, options, and color-aware printing while retaining protocol compatibility. Sequence diagram for width-aware representation renderingsequenceDiagram
participant Caller
participant Engine as pretty_repr_object
participant Object as PrettyRepr object
participant Builder as DocumentBuilder
participant Layout
Caller->>Engine: pretty_repr_object(value)
Engine->>Object: __pretty_repr__()
Object-->>Builder: PrettyType and PrettyAttr items
Builder->>Builder: format_array_like(value)
Builder->>Builder: detect shared and cyclic references
Builder-->>Layout: document tree
Layout->>Layout: compare flat layout with ReprOptions.width
Layout-->>Engine: compact or multiline text
Engine-->>Caller: representation
Flow diagram for pretty-repr renderingflowchart TD
Value["Value or PrettyRepr object"] --> Build["Build document tree"]
Build --> Detect["Format arrays and detect shared/cyclic references"]
Detect --> Measure["Measure against configured width"]
Measure -->|Fits| Compact["Render compact layout"]
Measure -->|Does not fit| Expanded["Render multiline layout with labels and commas"]
Compact --> Summary["Append optional parameter/state summary"]
Expanded --> Summary
Summary --> Result["repr() or color-aware print()"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the one-attribute-per-line repr engine with a width-aware compact tree layout shared by
graph.Node,nn.Module,State,TreefyState, thebrainstate.utilcontainers, and the classes that previously had hand-written__repr__s.[i]labels, and repeated or self-referencing objects show as<shared: .a>/<cycle: .>.f32[3,4],f32(0.5),f32[3] mV,key<fry>[].repr_options()(thread-local),set_repr_options(),get_repr_options(), andpretty_format()for arbitrary values. ANSI color is opt-in and applies toprint()only, neverrepr().NO_COLORturns it off;BRAINSTATE_REPR_COLOR=1turns it on by default.nnandinteroptransforms (via a_repr_items()hook),HiData,RandomState(RandomState(key<fry>[], data=[0, 42])), theModefamily, and the arguments shown byParamDescriber.__pretty_repr__protocol is unchanged. Additions are optional: an empty key means a positional item, plusPrettyAttr.commentandPrettyType.ref.Bug fixes
TreefyStateprinted full arrays and_hooks_manager=<object at 0x...>.Map/ParamshowedNone-valued attributes.Batchinglabelledbatch_sizeasin_size.PermuteAxesprinted asTranspose(...).HiDatasubclasses printed asHiData(...).Test plan
brainstate/util/_pretty_repr_layout_test.py(55 tests), plus golden tests for Node, Module and State.check_state_jax_tracer,check_state_value_treeand theHiDataclone example.coverage run(pytest --covcrashes Python in my environment):_pretty_repr.py99%,graph/_node.py95%,_pretty_pytree.py87%.Not included
Spec:
docs/specs/pretty-repr-redesign.mdSummary by Sourcery
Replace the legacy verbose representations with a compact, width-aware pretty-printing system across the framework and its utility containers.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: