Download src/sgjm/modules/backbone.py from adampippert/SGJM: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/adampippert/SGJM/resolve/main/src/sgjm/modules/backbone.py
- Command line
-
hf download hf://adampippert/SGJM/src/sgjm/modules/backbone.py
-
curl -L -o backbone.py https://huggingface.co/adampippert/SGJM/resolve/main/src/sgjm/modules/backbone.py
1.29 kB
| from __future__ import annotations | |
| import math | |
| from dataclasses import dataclass | |
| from typing import Protocol, Sequence, runtime_checkable | |
| class BackboneState: | |
| tokens: tuple[int, ...] | |
| latent: tuple[float, ...] | |
| class Backbone(Protocol): | |
| latent_dim: int | |
| def encode(self, tokens: Sequence[int]) -> BackboneState: ... | |
| def step(self, state: BackboneState, token: int) -> BackboneState: ... | |
| class StubBackbone: | |
| latent_dim: int = 16 | |
| seed: int = 0 | |
| def encode(self, tokens: Sequence[int]) -> BackboneState: | |
| latent = self._latent_for(tokens) | |
| return BackboneState(tokens=tuple(tokens), latent=latent) | |
| def step(self, state: BackboneState, token: int) -> BackboneState: | |
| new_tokens = state.tokens + (token,) | |
| return BackboneState(tokens=new_tokens, latent=self._latent_for(new_tokens)) | |
| def _latent_for(self, tokens: Sequence[int]) -> tuple[float, ...]: | |
| acc = [0.0] * self.latent_dim | |
| for i, t in enumerate(tokens): | |
| for d in range(self.latent_dim): | |
| acc[d] += math.sin((self.seed + 1) * (i + 1) * (d + 1) * (int(t) + 1) * 0.017) | |
| norm = math.sqrt(sum(x * x for x in acc)) or 1.0 | |
| return tuple(x / norm for x in acc) | |