kerzgrr commited on
Commit
798980d
·
verified ·
1 Parent(s): 916b9b2

Fix Windows inference: SDPA math fallback, FLA lazy-import patches, clearer install docs

Browse files
README.md CHANGED
@@ -144,12 +144,35 @@ Monostich-2 needs **GatedDeltaNet-2** from a pinned `flash-linear-attention` com
144
 
145
  ```bash
146
  pip install safetensors tokenizers huggingface_hub einops numpy
 
 
 
147
 
148
- # Pin used for Monostich-2 training / inference
149
  pip install --no-deps "git+https://github.com/fla-org/flash-linear-attention.git@cbb0a72efb55c18ca0ef4f298298317573ad2cb3"
150
  ```
151
 
152
- > If the git install fails on Windows, install [Git for Windows](https://git-scm.com/download/win) first, or clone the repo and `pip install --no-deps -e flash-linear-attention` after checking out that commit.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  ### 5) Download the inference script
155
 
 
144
 
145
  ```bash
146
  pip install safetensors tokenizers huggingface_hub einops numpy
147
+ ```
148
+
149
+ #### Linux / WSL (recommended)
150
 
151
+ ```bash
152
  pip install --no-deps "git+https://github.com/fla-org/flash-linear-attention.git@cbb0a72efb55c18ca0ef4f298298317573ad2cb3"
153
  ```
154
 
155
+ #### Windows (native)
156
+
157
+ Stock FLA + Triton 3.7 (bundled with recent PyTorch wheels) crashes on import.
158
+ Use UTF-8 mode, install editable from a clone, then apply the Hub patches:
159
+
160
+ ```powershell
161
+ $env:PYTHONUTF8 = "1"
162
+ pip install safetensors tokenizers huggingface_hub einops numpy
163
+
164
+ git clone https://github.com/fla-org/flash-linear-attention.git
165
+ cd flash-linear-attention
166
+ git checkout cbb0a72efb55c18ca0ef4f298298317573ad2cb3
167
+
168
+ hf download kerzgrr/Monostich-2-base --include "windows_fla_patches/**" --local-dir "$env:TEMP\m2patches"
169
+ Copy-Item -Recurse -Force "$env:TEMP\m2patches\windows_fla_patches\fla\*" .\fla\
170
+
171
+ pip install --no-build-isolation --no-deps -e .
172
+ cd ..
173
+ ```
174
+
175
+ > Alternative that Just Works: run inference inside **WSL** with the Linux install path above.
176
 
177
  ### 5) Download the inference script
178
 
__pycache__/inference.cpython-311.pyc ADDED
Binary file (17.6 kB). View file
 
inference.py CHANGED
@@ -239,18 +239,30 @@ def main() -> int:
239
  sys.path.insert(0, str(package_root))
240
 
241
  try:
242
- from tiny_gdn import TinyGDNConfig, TinyGDNForCausalLM
243
- except ImportError as error:
244
  print(
245
- "Could not import tiny_gdn / GatedDeltaNet-2.\n"
246
  "Install dependencies first (see README):\n"
247
  " pip install torch safetensors tokenizers huggingface_hub einops\n"
 
248
  f" pip install --no-deps git+https://github.com/fla-org/flash-linear-attention.git@{FLA_COMMIT}\n"
 
 
249
  f"Import error: {error}",
250
  file=sys.stderr,
251
  )
252
  return 1
253
 
 
 
 
 
 
 
 
 
 
254
  weights_path = _download("model.safetensors", local_dir)
255
  tok_path = _download("tokenizer.json", local_dir)
256
  cfg_path = _download("config.json", local_dir)
 
239
  sys.path.insert(0, str(package_root))
240
 
241
  try:
242
+ from fla.layers.gdn2 import GatedDeltaNet2 # noqa: F401
243
+ except Exception as error: # noqa: BLE001
244
  print(
245
+ "Could not import GatedDeltaNet-2 from flash-linear-attention.\n"
246
  "Install dependencies first (see README):\n"
247
  " pip install torch safetensors tokenizers huggingface_hub einops\n"
248
+ f" # Linux/WSL:\n"
249
  f" pip install --no-deps git+https://github.com/fla-org/flash-linear-attention.git@{FLA_COMMIT}\n"
250
+ " # Windows: clone that commit, apply windows_fla_patches/ from this repo, then\n"
251
+ " # pip install --no-build-isolation --no-deps -e .\n"
252
  f"Import error: {error}",
253
  file=sys.stderr,
254
  )
255
  return 1
256
 
257
+ try:
258
+ from tiny_gdn import TinyGDNConfig, TinyGDNForCausalLM
259
+ except ImportError as error:
260
+ print(
261
+ f"Could not import tiny_gdn: {error}",
262
+ file=sys.stderr,
263
+ )
264
+ return 1
265
+
266
  weights_path = _download("model.safetensors", local_dir)
267
  tok_path = _download("tokenizer.json", local_dir)
268
  cfg_path = _download("config.json", local_dir)
tiny_gdn/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (407 Bytes). View file
 
tiny_gdn/__pycache__/config.cpython-311.pyc ADDED
Binary file (9.12 kB). View file
 
tiny_gdn/__pycache__/model.cpython-311.pyc ADDED
Binary file (31.3 kB). View file
 
tiny_gdn/model.py CHANGED
@@ -15,7 +15,9 @@ from torch.utils.checkpoint import checkpoint
15
  from tiny_gdn.config import TinyGDNConfig
16
 
17
  try:
18
- from fla.layers import GatedDeltaNet2
 
 
19
  except ImportError as import_error:
20
  GatedDeltaNet2 = None
21
  FLA_IMPORT_ERROR: ImportError | None = import_error
@@ -198,15 +200,19 @@ class GatedGroupedQueryAttention(nn.Module):
198
  "is_causal": sdpa_mask is None,
199
  "enable_gqa": True,
200
  }
201
- if query.is_cuda:
202
- with sdpa_kernel(SDPBackend.FLASH_ATTENTION):
203
- attention_output = F.scaled_dot_product_attention(
204
- query,
205
- key,
206
- value,
207
- **sdpa_options,
208
- )
209
- else:
 
 
 
 
210
  attention_output = F.scaled_dot_product_attention(
211
  query,
212
  key,
 
15
  from tiny_gdn.config import TinyGDNConfig
16
 
17
  try:
18
+ # Import the module directly — `from fla.layers import GatedDeltaNet2`
19
+ # executes layers/__init__.py and eagerly loads every attention kernel.
20
+ from fla.layers.gdn2 import GatedDeltaNet2
21
  except ImportError as import_error:
22
  GatedDeltaNet2 = None
23
  FLA_IMPORT_ERROR: ImportError | None = import_error
 
200
  "is_causal": sdpa_mask is None,
201
  "enable_gqa": True,
202
  }
203
+ # Prefer Flash / mem-efficient when available; fall back to MATH for
204
+ # Windows PyTorch builds that ship without FlashAttention kernels.
205
+ backends = (
206
+ [
207
+ SDPBackend.FLASH_ATTENTION,
208
+ SDPBackend.EFFICIENT_ATTENTION,
209
+ SDPBackend.CUDNN_ATTENTION,
210
+ SDPBackend.MATH,
211
+ ]
212
+ if query.is_cuda
213
+ else [SDPBackend.MATH]
214
+ )
215
+ with sdpa_kernel(backends):
216
  attention_output = F.scaled_dot_product_attention(
217
  query,
218
  key,
windows_fla_patches/fla/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
2
+ #
3
+ # Keep package import light. Eagerly importing fla.layers pulls every Triton
4
+ # kernel and breaks on Windows + Triton 3.7.
5
+
6
+ from pkgutil import extend_path
7
+
8
+ __path__ = extend_path(__path__, __name__)
9
+ __version__ = "0.5.2"
10
+ __all__: list[str] = []
windows_fla_patches/fla/layers/__init__.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
2
+ #
3
+ # Lazy layer exports — avoid compiling every Triton kernel at import time.
4
+
5
+ from __future__ import annotations
6
+
7
+ import importlib
8
+ from typing import Any
9
+
10
+ _EXPORTS: dict[str, tuple[str, str]] = {
11
+ "ABCAttention": (".abc", "ABCAttention"),
12
+ "Attention": (".attn", "Attention"),
13
+ "BasedLinearAttention": (".based", "BasedLinearAttention"),
14
+ "BitAttention": (".bitattn", "BitAttention"),
15
+ "Comba": (".comba", "Comba"),
16
+ "DeltaNet": (".delta_net", "DeltaNet"),
17
+ "DeltaFormerAttention": (".deltaformer", "DeltaFormerAttention"),
18
+ "ForgettingAttention": (".forgetting_attn", "ForgettingAttention"),
19
+ "GatedDeltaNet": (".gated_deltanet", "GatedDeltaNet"),
20
+ "GatedDeltaProduct": (".gated_deltaproduct", "GatedDeltaProduct"),
21
+ "GatedDeltaNet2": (".gdn2", "GatedDeltaNet2"),
22
+ "GatedLinearAttention": (".gla", "GatedLinearAttention"),
23
+ "GatedSlotAttention": (".gsa", "GatedSlotAttention"),
24
+ "HGRNAttention": (".hgrn", "HGRNAttention"),
25
+ "HGRN2Attention": (".hgrn2", "HGRN2Attention"),
26
+ "KimiDeltaAttention": (".kda", "KimiDeltaAttention"),
27
+ "LightNetAttention": (".lightnet", "LightNetAttention"),
28
+ "LinearAttention": (".linear_attn", "LinearAttention"),
29
+ "LogLinearMamba2": (".log_linear_mamba2", "LogLinearMamba2"),
30
+ "Mamba": (".mamba", "Mamba"),
31
+ "Mamba2": (".mamba2", "Mamba2"),
32
+ "Mamba3": (".mamba3", "Mamba3"),
33
+ "MesaNet": (".mesa_net", "MesaNet"),
34
+ "MultiheadLatentAttention": (".mla", "MultiheadLatentAttention"),
35
+ "MoBA": (".moba", "MoBA"),
36
+ "MomAttention": (".mom", "MomAttention"),
37
+ "MultiScaleRetention": (".multiscale_retention", "MultiScaleRetention"),
38
+ "NativeSparseAttention": (".nsa", "NativeSparseAttention"),
39
+ "Parallax": (".parallax", "Parallax"),
40
+ "PaTHAttention": (".path_attn", "PaTHAttention"),
41
+ "Raven": (".raven", "Raven"),
42
+ "ReBasedLinearAttention": (".rebased", "ReBasedLinearAttention"),
43
+ "RodimusAttention": (".rodimus", "RodimusAttention"),
44
+ "SlidingWindowSharedKeyAttention": (".rodimus", "SlidingWindowSharedKeyAttention"),
45
+ "RWKV6Attention": (".rwkv6", "RWKV6Attention"),
46
+ "RWKV7Attention": (".rwkv7", "RWKV7Attention"),
47
+ "WallAttention": (".wall_attn", "WallAttention"),
48
+ "YOCOCrossAttention": (".yoco", "YOCOCrossAttention"),
49
+ "YOCOGatedRetention": (".yoco", "YOCOGatedRetention"),
50
+ "YOCOSharedKVBuilder": (".yoco", "YOCOSharedKVBuilder"),
51
+ }
52
+
53
+ __all__ = list(_EXPORTS)
54
+
55
+
56
+ def __getattr__(name: str) -> Any:
57
+ spec = _EXPORTS.get(name)
58
+ if spec is None:
59
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
60
+ module_name, attr = spec
61
+ value = getattr(importlib.import_module(module_name, __name__), attr)
62
+ globals()[name] = value
63
+ return value
windows_fla_patches/fla/ops/__init__.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
2
+ #
3
+ # Lazy public exports so importing fla.ops.utils / fla.ops.gdn2 does not
4
+ # eagerly compile every Triton kernel (needed on Windows + Triton 3.7).
5
+
6
+ from __future__ import annotations
7
+
8
+ import importlib
9
+ from typing import Any
10
+
11
+ _EXPORTS: dict[str, str] = {
12
+ "chunk_abc": "fla.ops.abc",
13
+ "parallel_attn": "fla.ops.attn",
14
+ "fused_attnres": "fla.ops.attnres",
15
+ "fused_chunk_based": "fla.ops.based",
16
+ "parallel_based": "fla.ops.based",
17
+ "chunk_comba": "fla.ops.comba",
18
+ "fused_recurrent_comba": "fla.ops.comba",
19
+ "chunk_delta_rule": "fla.ops.delta_rule",
20
+ "fused_chunk_delta_rule": "fla.ops.delta_rule",
21
+ "fused_recurrent_delta_rule": "fla.ops.delta_rule",
22
+ "parallel_forgetting_attn": "fla.ops.forgetting_attn",
23
+ "chunk_gated_delta_rule": "fla.ops.gated_delta_rule",
24
+ "chunk_gdn": "fla.ops.gated_delta_rule",
25
+ "fused_recurrent_gated_delta_rule": "fla.ops.gated_delta_rule",
26
+ "fused_recurrent_gdn": "fla.ops.gated_delta_rule",
27
+ "chunk_dplr_delta_rule": "fla.ops.generalized_delta_rule",
28
+ "chunk_iplr_delta_rule": "fla.ops.generalized_delta_rule",
29
+ "fused_recurrent_dplr_delta_rule": "fla.ops.generalized_delta_rule",
30
+ "fused_recurrent_iplr_delta_rule": "fla.ops.generalized_delta_rule",
31
+ "chunk_gla": "fla.ops.gla",
32
+ "fused_chunk_gla": "fla.ops.gla",
33
+ "fused_recurrent_gla": "fla.ops.gla",
34
+ "chunk_gsa": "fla.ops.gsa",
35
+ "fused_recurrent_gsa": "fla.ops.gsa",
36
+ "fused_recurrent_hgrn": "fla.ops.hgrn",
37
+ "chunk_kda": "fla.ops.kda",
38
+ "fused_recurrent_kda": "fla.ops.kda",
39
+ "chunk_lightning_attn": "fla.ops.lightning_attn",
40
+ "fused_recurrent_lightning_attn": "fla.ops.lightning_attn",
41
+ "chunk_linear_attn": "fla.ops.linear_attn",
42
+ "fused_chunk_linear_attn": "fla.ops.linear_attn",
43
+ "fused_recurrent_linear_attn": "fla.ops.linear_attn",
44
+ "chunk_log_linear_attn": "fla.ops.log_linear_attn",
45
+ "chunk_mesa_net": "fla.ops.mesa_net",
46
+ "parallel_nsa": "fla.ops.nsa",
47
+ "parallel_parallax": "fla.ops.parallax",
48
+ "parallel_path_attn": "fla.ops.path_attn",
49
+ "chunk_retention": "fla.ops.retention",
50
+ "fused_chunk_retention": "fla.ops.retention",
51
+ "fused_recurrent_retention": "fla.ops.retention",
52
+ "parallel_retention": "fla.ops.retention",
53
+ "chunk_rwkv6": "fla.ops.rwkv6",
54
+ "fused_recurrent_rwkv6": "fla.ops.rwkv6",
55
+ "chunk_rwkv7": "fla.ops.rwkv7",
56
+ "fused_recurrent_rwkv7": "fla.ops.rwkv7",
57
+ "chunk_simple_gla": "fla.ops.simple_gla",
58
+ "fused_chunk_simple_gla": "fla.ops.simple_gla",
59
+ "fused_recurrent_simple_gla": "fla.ops.simple_gla",
60
+ "parallel_simple_gla": "fla.ops.simple_gla",
61
+ "parallel_wall_attn": "fla.ops.wall_attn",
62
+ "parallel_wall_attn_decode": "fla.ops.wall_attn",
63
+ "chunk_gdn2": "fla.ops.gdn2",
64
+ "fused_recurrent_gdn2": "fla.ops.gdn2",
65
+ }
66
+
67
+ __all__ = list(_EXPORTS)
68
+
69
+
70
+ def __getattr__(name: str) -> Any:
71
+ module_name = _EXPORTS.get(name)
72
+ if module_name is None:
73
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
74
+ value = getattr(importlib.import_module(module_name), name)
75
+ globals()[name] = value
76
+ return value
windows_fla_patches/fla/ops/simple_gla/__init__.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+ # For a list of all contributors, visit:
6
+ # https://github.com/fla-org/flash-linear-attention/graphs/contributors
7
+
8
+ from .chunk import chunk_simple_gla
9
+ from .fused_chunk import fused_chunk_simple_gla
10
+ from .fused_recurrent import fused_recurrent_simple_gla
11
+
12
+ # Triton 3.7 on Windows can fail while decorating parallel kernels at import time.
13
+ try:
14
+ from .parallel import parallel_simple_gla
15
+ except Exception: # noqa: BLE001
16
+ parallel_simple_gla = None
17
+
18
+ __all__ = [
19
+ 'chunk_simple_gla',
20
+ 'fused_chunk_simple_gla',
21
+ 'fused_recurrent_simple_gla',
22
+ 'parallel_simple_gla',
23
+ ]