Skip to content

refactor(qdp): rename validate_tensor to validate_tensor_cpu and drop redundant check - #1415

Merged
400Ping merged 1 commit into
apache:mainfrom
ryankert01:refactor/qdp-validate-tensor-cpu
Jul 3, 2026
Merged

refactor(qdp): rename validate_tensor to validate_tensor_cpu and drop redundant check#1415
400Ping merged 1 commit into
apache:mainfrom
ryankert01:refactor/qdp-validate-tensor-cpu

Conversation

@ryankert01

@ryankert01 ryankert01 commented Jun 26, 2026

Copy link
Copy Markdown
Member

Related Issues

Closes #907

Changes

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • Test
  • CI/CD pipeline
  • Other

Why

Follow-up to the review thread on #881. validate_tensor() re-checked
is_pytorch_tensor(tensor) even though its only caller already
guarantees the object is a torch.Tensor:

  • validate_tensor has a single call site: QdpEngine::encode_from_pytorch
    (engine.rs:209).
  • encode_from_pytorch is only reached from the encode() dispatcher
    (engine.rs:125), and that branch is already gated by
    is_pytorch_tensor(data)? at engine.rs:119.

So the inner check was dead weight on the real execution path. The
function's actual job is validating device placement — the
device.type != "cpu" guard, which is what already rejects non-CPU
backends (CUDA/MPS/XLA/HPU). The old name validate_tensor didn't convey
that, hence the rename suggested in the issue.

How

  • Removed the redundant is_pytorch_tensor check (and its now-dead
    "Object is not a PyTorch Tensor" error arm) from the helper.
  • Renamed validate_tensorvalidate_tensor_cpu, with a doc comment
    stating the caller-side contract (caller has already confirmed it is a
    torch.Tensor) and that it rejects non-CPU backends.
  • Updated the import and call site in engine.rs.

No behavior change on any supported path. Verified with
cargo check (--cfg qdp_no_cuda) and cargo fmt --check.

Checklist

  • Added or updated unit tests for all changes — N/A, behavior-preserving rename; existing tests still cover the CPU-tensor path
  • Added or updated documentation for all changes — doc comment on validate_tensor_cpu

… redundant check

validate_tensor() re-checked is_pytorch_tensor() even though its only
caller (QdpEngine::encode -> encode_from_pytorch) already guarantees the
object is a torch.Tensor before dispatching to the CPU path. Remove the
redundant check and rename the helper to validate_tensor_cpu to reflect
that it only validates device placement -- which is also what rejects the
non-CPU backends (CUDA/MPS/XLA/HPU). No behavior change.
@ryankert01
ryankert01 requested a review from 400Ping as a code owner June 26, 2026 19:12

@viiccwen viiccwen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@viiccwen

viiccwen commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

cc @400Ping

@400Ping
400Ping merged commit 359f959 into apache:main Jul 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: validate_tensor()

3 participants