torchnative
Guide가이드

Test-time adaptation테스트 타임 적응

A model that ships to a device meets data its training set never had. Let it adapt in place, and be able to take it back.

기기로 간 모델은 학습 데이터에 없던 데이터를 만납니다. 제자리에서 적응하게 하고, 언제든 되돌릴 수 있게 합니다.

Switch online, step, reset온라인으로 전환하고, 스텝하고, 초기화하기

from torch.nn.modules.test_time import TestTimeModuleMixin
from torch.optim.test_time import EntropyMinimization

model.online()      # eval mode, only online_parameters() can move
optimizer = torch.optim.SGD(model.online_parameters(), lr=1e-3)
tta = EntropyMinimization(model, optimizer)
for batch in stream:
    prediction = tta.step(batch)   # predicts, then adapts
tta.reset()         # parameters, buffers and optimizer state, bit for bit

Entropy minimisation lowers prediction entropy over the parameters the optimizer holds. On SmolLM2-135M, ten steps over the normalisation weights took entropy on unlabelled text from 4.1604 to 2.9828, and a held-out sentence fell from 3.7237 to 2.9439, agreeing with upstream's own autograd to a median relative 1.5e-06. That measurement was taken through the earlier adapt.wrap wrapper, which torch.optim.test_time replaced (#237).

엔트로피 최소화는 옵티마이저가 쥔 파라미터에 대해 예측 엔트로피를 낮춥니다. SmolLM2-135M 에서 정규화 가중치에 대한 열 스텝이 레이블 없는 텍스트의 엔트로피를 4.1604 에서 2.9828 로, 한 번도 적응에 쓰지 않은 문장을 3.7237 에서 2.9439 로 낮췄고, upstream autograd 와 중앙 상대오차 1.5e-06 으로 일치했습니다. 이 측정은 이전의 adapt.wrap 래퍼로 했고, 그 래퍼는 torch.optim.test_time 으로 대체되었습니다 (#237).

Two layers두 계층

Layer계층Where위치What it does하는 일
Module모듈torch.nn.modules.test_time.TestTimeModuleMixinonline() sets eval mode and freezes every parameter except online_parameters(). offline() sets train mode and restores each requires_grad.online() 은 eval 모드로 두고 online_parameters() 를 뺀 모든 파라미터를 얼립니다. offline() 은 train 모드로 두고 각 requires_grad 를 되돌립니다.
Rule규칙torch.optim.test_timeTestTimeAdaptation(model, optimizer) defines loss() (what to minimise) and should_update() (when). Generic rules: EntropyMinimization, PseudoLabel, and NormStatistics, which needs no optimizer and no gradient.TestTimeAdaptation(model, optimizer) 는 loss() (무엇을 최소화할지) 와 should_update() (언제 갱신할지) 를 정의합니다. 일반 규칙은 EntropyMinimization, PseudoLabel, 그리고 옵티마이저도 기울기도 필요 없는 NormStatistics 입니다.

step() predicts then adapts, and reset() restores everything bit for bit, so no rule reimplements checkpointing. With TORCHNATIVE_BACKWARD=off, the gradient rules are refused by name and NormStatistics still works. Paper-named methods (Tent, EATA, SAR, CoTTA) live in the test-time-adapters library, not here.

step() 은 예측한 다음 적응하고, reset() 은 모든 것을 비트 단위로 되돌리므로 어떤 규칙도 체크포인팅을 다시 구현하지 않습니다. TORCHNATIVE_BACKWARD=off 에서는 기울기 규칙이 이름으로 거부되고 NormStatistics 는 그대로 동작합니다. 논문 이름의 방법 (Tent, EATA, SAR, CoTTA) 은 여기가 아니라 test-time-adapters 라이브러리에 있습니다.

The controls are part of the claim: the same loop with the sign flipped sends entropy up, lr=0 holds it to the last digit, and an objective on a detached tensor is refused by name. A loop that silently does nothing would pass any test that only checks it finished.

대조군도 주장의 일부입니다: 부호를 뒤집으면 엔트로피가 올라가고, lr=0 은 마지막 자리까지 그대로이며, 분리된 텐서 위의 목적함수는 이름으로 거부됩니다. 아무것도 안 하는 루프는 끝났는지만 보는 테스트를 모두 통과하기 때문입니다.