Skip to content

fix(optim): support like factories for placeholder contexts - #785

Open
Zhaoxian-Wu wants to merge 1 commit into
IBM:masterfrom
Zhaoxian-Wu:fix/placeholder-like-factories
Open

Zhaoxian-Wu wants to merge 1 commit into
IBM:masterfrom
Zhaoxian-Wu:fix/placeholder-like-factories

Conversation

@Zhaoxian-Wu

@Zhaoxian-Wu Zhaoxian-Wu commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Why

AnalogContext exposes placeholder data by default so public tensor operations cannot accidentally read meaningless backing weights. Tensor *_like factories only need metadata such as shape, dtype, and device, and should be valid in this mode. Before this change, they were rejected as value-reading operations.

This is related to the placeholder-data feature introduced by IBM/aihwkit#765, and completes its compatibility with standard PyTorch *_like factories.

Failure case

model = AnalogLinear(1, 1, bias=False, rpu_config=SingleRPUConfig(
    device=ConstantStepDevice()
)).to("cuda")
ctx = next(model.parameters())
torch.zeros_like(ctx)

Before this fix, the final line raised:

RuntimeError: AnalogContext data is in placeholder mode, so operation 'zeros_like' cannot read weight values.

This prevented metadata-only tensor construction during CUDA model setup and other generic PyTorch code paths.

What changed

  • Allow empty_like, full_like, ones_like, rand_like, randint_like, randn_like, and zeros_like in placeholder mode.
  • Keep the placeholder backing storage unread while allowing PyTorch to allocate the result from metadata.
  • Add CPU coverage for all seven public *_like factories and a CUDA regression test for zeros_like.

Allow torch like-factory operations to use placeholder metadata without
reading backing values, with CPU and CUDA regression tests.

Signed-off-by: Zhaoxian Wu <wuzhaoxian97@gmail.com>
@Zhaoxian-Wu
Zhaoxian-Wu force-pushed the fix/placeholder-like-factories branch from c729df2 to 836cdd8 Compare August 6, 2026 17:52
@Zhaoxian-Wu

Copy link
Copy Markdown
Contributor Author

Hi @PabloCarmona,
All the test passed in my local environment. However, the test failed intermittently (test-and-lint / Test 3.12) because two random noisy outputs happened to quantize to the same value. Could you please review this stochastic test case?

Possible solution: tests/test_simulator_tiles.py:686 compares only one randomly selected noisy-output pair, so it can falsely fail when both samples quantize to the same value.

@Zhaoxian-Wu
Zhaoxian-Wu marked this pull request as ready for review August 7, 2026 00:34
@PabloCarmona

Copy link
Copy Markdown
Collaborator

Thanks @Zhaoxian-Wu ! I will take a look at the PR and what are you talking about the tests for the random noisy value, we are aware of it, we will take a look again.

Thanks again!

@PabloCarmona
PabloCarmona requested review from PabloCarmona and maljoras and a lite review from Copilot and removed request for Copilot August 25, 2026 10:59
@PabloCarmona

Copy link
Copy Markdown
Collaborator

@Zhaoxian-Wu could you sync up this branch with the master branch? We are planning to launch a new release very soon. Thanks again!

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.

2 participants