Replace assert with ValueError in public-API input validation#77
Open
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
Open
Replace assert with ValueError in public-API input validation#77lonexreb wants to merge 1 commit intoNVlabs:mainfrom
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
Conversation
Continues the pattern of NVlabs#50 (helper.create_message) and NVlabs#73 (load_physical_aiavdataset) by converting two more user-facing input validations from assert to ValueError so they survive python -O and emit a clear exception type: - tokenize_history_trajectory(): missing 'ego_history_xyz' key, and rank check on the tensor (base_model.py:104-105). Both errors now include the offending value (key set or actual shape) in the message. - AlpamayoR1.sample_trajectories_from_data_with_vlm_rollout(): the n_traj_group == 1 contract (alpamayo_r1.py:157) now reports the actual n_traj_group value. These three call sites (the assert in helper.create_message that NVlabs#50 already fixed, the t0_us check NVlabs#73 already fixed, and the two here) are the only public-facing input validators that were still using assert. Internal invariants (e.g. base_model.py:266, action_in_proj.py:43) are left as assert because they guard implementation correctness, not caller-supplied input. Signed-off-by: lonexreb <[email protected]>
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
Continues the pattern of merged PR #50 (
helper.create_message) and PR #73 (load_physical_aiavdataset): convert the remaining public-facing input validators fromasserttoValueError, so they survivepython -Oand emit a clear exception type users can catch.Changes
1.
tokenize_history_trajectory(src/alpamayo_r1/models/base_model.py:104-105)The first assert had no message; the second one's was correct but
assertgot stripped under-O. Both errors now include the actual offending value (key set or shape) for faster diagnosis.2.
AlpamayoR1.sample_trajectories_from_data_with_vlm_rollout(src/alpamayo_r1/models/alpamayo_r1.py:157)Scope
These three call sites — plus the two already addressed in #50 and #73 — are all the public-facing input validators that were using
assert. I deliberately left the remainingasserts as-is because they guard internal invariants, not caller-supplied input:assertmodels/base_model.py:266len(discrete_tokens) == num_new_tokens— internal post-condition oftokenizer.add_tokensmodels/action_in_proj.py:431 <= num_enc_layers— config-time check inside private mixinmodels/alpamayo_r1.py(sample loop, internal)chat_template/conversation.py:38, 115, 195, 227processor/qwen_processor.py:128, 189, 195, 294utils/get_label_mask.py:66, 69, 123, 134action_space/discrete_action_space.py:37If you'd prefer a sweeping conversion to
ValueErroreverywhere, happy to do that in a follow-up — but the convention here mirrors the way #50 was framed (only the user-input boundary).Test plan
python -c \"import ast; [ast.parse(open(f).read()) for f in ['src/alpamayo_r1/models/base_model.py', 'src/alpamayo_r1/models/alpamayo_r1.py']]\"python -O: the validation now runs (previously stripped). For valid inputs this is a no-op; for invalid inputs callers seeValueErrorinstead of an opaque downstreamAttributeError/KeyError.