nnsight 0.5.0.dev7__tar.gz → 0.5.0.dev8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/PKG-INFO +1 -1
  2. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/envoy.py +40 -8
  3. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/interleaver.py +30 -10
  4. nnsight-0.5.0.dev8/src/nnsight/intervention/tracing/backwards.py +58 -0
  5. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/language.py +10 -2
  6. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight.egg-info/PKG-INFO +1 -1
  7. nnsight-0.5.0.dev8/src/nnsight.egg-info/SOURCES.txt +69 -0
  8. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/tests/test_lm.py +5 -6
  9. nnsight-0.5.0.dev7/docs/.DS_Store +0 -0
  10. nnsight-0.5.0.dev7/docs/Makefile +0 -28
  11. nnsight-0.5.0.dev7/docs/contributing.md +0 -31
  12. nnsight-0.5.0.dev7/docs/make.bat +0 -35
  13. nnsight-0.5.0.dev7/docs/requirements.txt +0 -8
  14. nnsight-0.5.0.dev7/docs/source/.DS_Store +0 -0
  15. nnsight-0.5.0.dev7/docs/source/_static/.DS_Store +0 -0
  16. nnsight-0.5.0.dev7/docs/source/_static/css/custom.css +0 -65
  17. nnsight-0.5.0.dev7/docs/source/_static/css/home.css +0 -28
  18. nnsight-0.5.0.dev7/docs/source/_static/css/pygments.css +0 -75
  19. nnsight-0.5.0.dev7/docs/source/_static/css/status.css +0 -13
  20. nnsight-0.5.0.dev7/docs/source/_static/images/.DS_Store +0 -0
  21. nnsight-0.5.0.dev7/docs/source/_static/images/execution.png +0 -0
  22. nnsight-0.5.0.dev7/docs/source/_static/images/icon.ico +0 -0
  23. nnsight-0.5.0.dev7/docs/source/_static/images/interleaved.png +0 -0
  24. nnsight-0.5.0.dev7/docs/source/_static/images/ioi.png +0 -0
  25. nnsight-0.5.0.dev7/docs/source/_static/images/nnsight_logo.svg +0 -1
  26. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/.DS_Store +0 -0
  27. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/activation_patching.webp +0 -0
  28. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/attribution_patching.webp +0 -0
  29. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/dictionary_learning.webp +0 -0
  30. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/function_vectors.webp +0 -0
  31. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/future_lens.webp +0 -0
  32. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/logit_lens.webp +0 -0
  33. nnsight-0.5.0.dev7/docs/source/_static/images/tutorials/walkthrough.webp +0 -0
  34. nnsight-0.5.0.dev7/docs/source/_static/js/.DS_Store +0 -0
  35. nnsight-0.5.0.dev7/docs/source/_static/js/code.js +0 -82
  36. nnsight-0.5.0.dev7/docs/source/_static/js/custom.js +0 -81
  37. nnsight-0.5.0.dev7/docs/source/_templates/layout.html +0 -7
  38. nnsight-0.5.0.dev7/docs/source/_templates/ndif_status.html +0 -35
  39. nnsight-0.5.0.dev7/docs/source/about.rst +0 -102
  40. nnsight-0.5.0.dev7/docs/source/conf.py +0 -76
  41. nnsight-0.5.0.dev7/docs/source/documentation/intervention.rst +0 -77
  42. nnsight-0.5.0.dev7/docs/source/documentation/modeling.rst +0 -19
  43. nnsight-0.5.0.dev7/docs/source/documentation/schema.rst +0 -22
  44. nnsight-0.5.0.dev7/docs/source/documentation/tracing.rst +0 -41
  45. nnsight-0.5.0.dev7/docs/source/documentation/util.rst +0 -6
  46. nnsight-0.5.0.dev7/docs/source/documentation.rst +0 -12
  47. nnsight-0.5.0.dev7/docs/source/features.rst +0 -359
  48. nnsight-0.5.0.dev7/docs/source/index.rst +0 -18
  49. nnsight-0.5.0.dev7/docs/source/notebooks/features/conditionals.ipynb +0 -173
  50. nnsight-0.5.0.dev7/docs/source/notebooks/features/cross_prompt.ipynb +0 -150
  51. nnsight-0.5.0.dev7/docs/source/notebooks/features/custom_functions.ipynb +0 -102
  52. nnsight-0.5.0.dev7/docs/source/notebooks/features/early_stopping.ipynb +0 -260
  53. nnsight-0.5.0.dev7/docs/source/notebooks/features/editing_tutorial.ipynb +0 -213
  54. nnsight-0.5.0.dev7/docs/source/notebooks/features/getting.ipynb +0 -96
  55. nnsight-0.5.0.dev7/docs/source/notebooks/features/gradients.ipynb +0 -214
  56. nnsight-0.5.0.dev7/docs/source/notebooks/features/iterator.ipynb +0 -173
  57. nnsight-0.5.0.dev7/docs/source/notebooks/features/lora_training.ipynb +0 -2967
  58. nnsight-0.5.0.dev7/docs/source/notebooks/features/model_editing.ipynb +0 -158
  59. nnsight-0.5.0.dev7/docs/source/notebooks/features/modules.ipynb +0 -103
  60. nnsight-0.5.0.dev7/docs/source/notebooks/features/multiple_token.ipynb +0 -361
  61. nnsight-0.5.0.dev7/docs/source/notebooks/features/operations.ipynb +0 -107
  62. nnsight-0.5.0.dev7/docs/source/notebooks/features/remote_execution.ipynb +0 -167
  63. nnsight-0.5.0.dev7/docs/source/notebooks/features/scan_validate.ipynb +0 -381
  64. nnsight-0.5.0.dev7/docs/source/notebooks/features/sessions.ipynb +0 -167
  65. nnsight-0.5.0.dev7/docs/source/notebooks/features/setting.ipynb +0 -104
  66. nnsight-0.5.0.dev7/docs/source/notebooks/features/streaming.ipynb +0 -448
  67. nnsight-0.5.0.dev7/docs/source/notebooks/features/vllm_support.ipynb +0 -3213
  68. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/LoRA_tutorial.ipynb +0 -12235
  69. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/activation_patching.ipynb +0 -4391
  70. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/attribution_patching.ipynb +0 -9038
  71. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/boundless_DAS.ipynb +0 -578
  72. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/dict_learning.ipynb +0 -369
  73. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/diffusion_lens.ipynb +0 -713
  74. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/images/Activation_patching-figure1.png +0 -0
  75. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/images/LoRA_tutorial_figure_1.png +0 -0
  76. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/images/LoRA_tutorial_figure_2.png +0 -0
  77. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/logit_lens.ipynb +0 -3431
  78. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/start_remote_access.ipynb +0 -3159
  79. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/tutorial_price_tagging_utils.py +0 -439
  80. nnsight-0.5.0.dev7/docs/source/notebooks/tutorials/walkthrough.ipynb +0 -6445
  81. nnsight-0.5.0.dev7/docs/source/pages/home.html +0 -159
  82. nnsight-0.5.0.dev7/docs/source/start.rst +0 -78
  83. nnsight-0.5.0.dev7/docs/source/status.rst +0 -290
  84. nnsight-0.5.0.dev7/docs/source/tutorials.rst +0 -174
  85. nnsight-0.5.0.dev7/docs/sourcelatex/conf.py +0 -52
  86. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/contexts.rst +0 -16
  87. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/intervention.rst +0 -5
  88. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/models.rst +0 -18
  89. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/module.rst +0 -5
  90. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/patching.rst +0 -6
  91. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/tracing.rst +0 -14
  92. nnsight-0.5.0.dev7/docs/sourcelatex/documentation/util.rst +0 -6
  93. nnsight-0.5.0.dev7/docs/sourcelatex/index.rst +0 -13
  94. nnsight-0.5.0.dev7/src/nnsight/intervention/tracing/backwards.py +0 -38
  95. nnsight-0.5.0.dev7/src/nnsight.egg-info/SOURCES.txt +0 -154
  96. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/.github/workflows/publish.yml +0 -0
  97. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/.github/workflows/python-app.yml +0 -0
  98. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/.gitignore +0 -0
  99. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/CHANGELOG.md +0 -0
  100. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/CODE_OF_CONDUCT.md +0 -0
  101. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/LICENSE +0 -0
  102. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/NNsight_Walkthrough.ipynb +0 -0
  103. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/NNsight_v0_3_guide.ipynb +0 -0
  104. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/NNsight_v0_4_guide.ipynb +0 -0
  105. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/README.md +0 -0
  106. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/conftest.py +0 -0
  107. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/pyproject.toml +0 -0
  108. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/pytest.ini +0 -0
  109. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/setup.cfg +0 -0
  110. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/setup.py +0 -0
  111. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/__init__.py +0 -0
  112. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/_c/__init__.py +0 -0
  113. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/_c/py_mount.c +0 -0
  114. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/config.yaml +0 -0
  115. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/__init__.py +0 -0
  116. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/backends/__init__.py +0 -0
  117. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/backends/base.py +0 -0
  118. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/backends/editing.py +0 -0
  119. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/backends/execution.py +0 -0
  120. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/backends/remote.py +0 -0
  121. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/batching.py +0 -0
  122. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/inject.py +0 -0
  123. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/serialization.py +0 -0
  124. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/__init__.py +0 -0
  125. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/base.py +0 -0
  126. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/editing.py +0 -0
  127. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/globals.py +0 -0
  128. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/invoker.py +0 -0
  129. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/iterator.py +0 -0
  130. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/tracer.py +0 -0
  131. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/intervention/tracing/util.py +0 -0
  132. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/log.py +0 -0
  133. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/__init__.py +0 -0
  134. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/base.py +0 -0
  135. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/diffusion.py +0 -0
  136. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/mixins/__init__.py +0 -0
  137. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/mixins/loadable.py +0 -0
  138. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/mixins/meta.py +0 -0
  139. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/mixins/remoteable.py +0 -0
  140. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/__init__.py +0 -0
  141. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/model_runners/GPUModelRunner.py +0 -0
  142. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/model_runners/__init__.py +0 -0
  143. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/sampling.py +0 -0
  144. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/vllm.py +0 -0
  145. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/workers/GPUWorker.py +0 -0
  146. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/modeling/vllm/workers/__init__.py +0 -0
  147. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/schema/__init__.py +0 -0
  148. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/schema/config.py +0 -0
  149. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/schema/request.py +0 -0
  150. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/schema/response.py +0 -0
  151. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight/util.py +0 -0
  152. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight.egg-info/dependency_links.txt +0 -0
  153. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight.egg-info/requires.txt +0 -0
  154. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/src/nnsight.egg-info/top_level.txt +0 -0
  155. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/tests/__init__.py +0 -0
  156. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/tests/test_diffusion.py +0 -0
  157. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/tests/test_tiny.py +0 -0
  158. {nnsight-0.5.0.dev7 → nnsight-0.5.0.dev8}/tests/test_vllm.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nnsight
3
- Version: 0.5.0.dev7
3
+ Version: 0.5.0.dev8
4
4
  Summary: Package for interpreting and manipulating the internals of deep learning models.
5
5
  Author-email: Jaden Fiotto-Kaufman <jadenfk@outlook.com>
6
6
  License: MIT License
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
  import inspect
4
4
  import os
5
5
  import warnings
6
+ from functools import wraps
6
7
  from types import BuiltinFunctionType, BuiltinMethodType, FunctionType, MethodType
7
8
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
8
9
 
@@ -26,6 +27,19 @@ else:
26
27
  Interleaver = Any
27
28
 
28
29
 
30
+ def trace_only(fn: Callable):
31
+
32
+ @wraps(fn)
33
+ def wrapper(self: Envoy, *args, **kwargs):
34
+
35
+ if self._interleaver is None:
36
+ raise ValueError(f"Must be within a trace to use `.{fn.__name__}(...)`")
37
+
38
+ return fn(self, *args, **kwargs)
39
+
40
+ return wrapper
41
+
42
+
29
43
  class Envoy(Batchable):
30
44
  """
31
45
  A proxy class that wraps a PyTorch module to enable intervention during execution.
@@ -542,14 +556,17 @@ class Envoy(Batchable):
542
556
  # TODO legacy
543
557
  @property
544
558
  @deprecated(message="Use `tracer.iter` instead.")
559
+ @trace_only
545
560
  def iter(self):
546
561
  return IteratorProxy(self._interleaver)
547
562
 
548
563
  # TODO legacy
549
564
  @deprecated(message="Use `tracer.all()` instead.")
565
+ @trace_only
550
566
  def all(self):
551
567
  return self.iter[:]
552
568
 
569
+ @trace_only
553
570
  def skip(self, replacement: Any):
554
571
  """Skips the execution of this module duting execution / interleaving.
555
572
  Behavior is the module will not be executed and will return a replacement value instead.
@@ -570,12 +587,14 @@ class Envoy(Batchable):
570
587
 
571
588
  self._interleaver.current.skip(requester, replacement)
572
589
 
590
+ @trace_only
573
591
  def wait_for_input(self):
574
592
  """
575
593
  Wait for the input to the module to be available.
576
594
  """
577
595
  self.inputs
578
596
 
597
+ @trace_only
579
598
  def wait_for_output(self):
580
599
  """
581
600
  Wait for the output to the module to be available.
@@ -710,7 +729,7 @@ class Envoy(Batchable):
710
729
 
711
730
  #### Private methods ####
712
731
 
713
- def _add_envoy(self, module: torch.nn.Module, name: str) -> None:
732
+ def _add_envoy(self, module: torch.nn.Module, name: str) -> Envoy:
714
733
  """
715
734
  Adds a new Envoy for a given torch module under this Envoy.
716
735
 
@@ -741,6 +760,8 @@ class Envoy(Batchable):
741
760
  else:
742
761
  super().__setattr__(name, envoy)
743
762
 
763
+ return envoy
764
+
744
765
  def _handle_overloaded_mount(self, envoy: Envoy, mount_point: str) -> None:
745
766
  """If a given module already has an attribute of the same name as something nnsight wants to add, we need to rename it.
746
767
 
@@ -1025,6 +1046,15 @@ class Envoy(Batchable):
1025
1046
  return value(*args, **kwargs)
1026
1047
 
1027
1048
  return trace
1049
+
1050
+ elif isinstance(value, torch.nn.Module):
1051
+ # If the _module has a module in its __dict__ but wasn't picked up when creating the Envoy,
1052
+ # Hopefully it is alrady an Envoy somewhere in the tree.
1053
+ # https://github.com/ndif-team/nnsight/issues/479
1054
+ # This happened because some transformers models set this class attr: _checkpoint_conversion_mapping
1055
+ if hasattr(value, "__path__"):
1056
+ return util.fetch_attr(self, value.__path__[len(self.path) :])
1057
+ return self._add_envoy(value, name)
1028
1058
  else:
1029
1059
  return value
1030
1060
  else:
@@ -1225,21 +1255,22 @@ class OperationEnvoy:
1225
1255
 
1226
1256
  if self._source is None:
1227
1257
  fn = self._interleaver.current.request(f"{self.name}.fn")
1228
-
1229
- #TODO maybe do something else here
1258
+
1259
+ # TODO maybe do something else here
1230
1260
  if isinstance(fn, torch.nn.Module):
1231
-
1261
+
1232
1262
  msg = f"Don't call .source on a module ({getattr(fn, '__path__', '')}) from within another .source. Call it directly with: {getattr(fn, '__path__', '')}.source"
1233
1263
  raise ValueError(msg)
1234
1264
 
1235
1265
  def wrap(fn: Callable, **kwargs):
1236
-
1266
+
1237
1267
  bound_obj = (
1238
1268
  fn.__self__
1239
- if getattr(fn, "__name__", None) != "forward" and inspect.ismethod(fn)
1269
+ if getattr(fn, "__name__", None) != "forward"
1270
+ and inspect.ismethod(fn)
1240
1271
  else None
1241
1272
  )
1242
-
1273
+
1243
1274
  return self._interleaver.wrap_operation(
1244
1275
  fn, **kwargs, bound_obj=bound_obj
1245
1276
  )
@@ -1252,7 +1283,8 @@ class OperationEnvoy:
1252
1283
 
1253
1284
  self._fn = fn
1254
1285
 
1255
- self._interleaver.current.swap(f"{self.name}.fn", self._fn)
1286
+ if f"{self.name}.fn" not in self._interleaver.current.history:
1287
+ self._interleaver.current.swap(f"{self.name}.fn", self._fn)
1256
1288
 
1257
1289
  return self._source
1258
1290
 
@@ -4,12 +4,14 @@ import ctypes
4
4
  import inspect
5
5
  import threading
6
6
  import time
7
+ import warnings
7
8
  from collections import defaultdict
8
9
  from enum import Enum
9
10
  from functools import wraps
10
11
  from queue import Queue
11
12
  from threading import Thread
12
- from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
13
+ from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple,
14
+ Union)
13
15
 
14
16
  import torch
15
17
 
@@ -19,7 +21,7 @@ from .tracing.base import WithBlockNotFoundError
19
21
  from .tracing.util import wrap_exception
20
22
 
21
23
  if TYPE_CHECKING:
22
- from .tracing.tracer import Cache, Tracer, InterleavingTracer
24
+ from .tracing.tracer import Cache, InterleavingTracer, Tracer
23
25
 
24
26
 
25
27
 
@@ -76,7 +78,7 @@ class Interleaver:
76
78
  self.invokers = invokers
77
79
  self.batcher = batcher
78
80
  self.tracer = tracer
79
- self.mediators = {}
81
+ self.mediators: Dict[str, Mediator] = {}
80
82
 
81
83
  self.patcher = None
82
84
 
@@ -317,14 +319,32 @@ class Interleaver:
317
319
  except EarlyStopException:
318
320
  pass
319
321
 
320
- # TODO check all mediator events
322
+ # If any mediators are still waiting for their values for their events, they probably called an Envoy out of order
323
+ # Or their Envoy was not called.
324
+ for mediator in self.mediators.values():
325
+
326
+ if mediator.child is not None:
327
+ mediator = mediator.child
328
+
329
+ if not mediator.event_queue.empty():
330
+ requested_event, requester = mediator.event_queue.get()
331
+
332
+ if isinstance(requester, tuple):
333
+ requester = requester[0]
334
+
335
+
321
336
 
322
- # TODO
323
- # for mediator in self.mediators:
324
- # if mediator.has_pending_event():
325
- # requested_event, requester = mediator.get_event()
326
- # print(requested_event, requester)
327
- # raise ValueError(f"Execution complete but {requester} was not provided. Did you call an Envoy out of order? Investigate why this module was not called.")
337
+ mediator.respond(ValueError(f"Execution complete but `{requester}` was not provided. Did you call an Envoy out of order? Investigate why this module was not called?"))
338
+ mediator.wait()
339
+
340
+ if mediator.name.startswith("Iterator"):
341
+ try:
342
+ mediator.handle()
343
+ except ValueError as e:
344
+ msg = f"Execution complete but `{requester}` was not provided. This was in an Iterator at iteration {mediator.iteration} so likely this iteration did not happen. If you were using `.iter[:]`, this is likely not an error."
345
+ warnings.warn(msg)
346
+ else:
347
+ mediator.handle()
328
348
 
329
349
  ### Provider Methods ###
330
350
 
@@ -0,0 +1,58 @@
1
+ from typing import TYPE_CHECKING, Any, Callable
2
+
3
+ import torch
4
+
5
+ from ...util import Patch
6
+ from ..interleaver import Mediator
7
+ from .invoker import Invoker
8
+
9
+ if TYPE_CHECKING:
10
+ from ..interleaver import Interleaver
11
+ else:
12
+ Interleaver = Any
13
+
14
+
15
+ class BackwardsMediator(Mediator):
16
+
17
+ def request(self, requester: Any):
18
+
19
+ if not requester.endswith(".grad"):
20
+ raise ValueError(
21
+ f"Cannot request `{requester}` in a backwards tracer. You can only request `.grad`. Please define your Tensors before the Backwards Tracer and interact with their gradients within the Backwards Tracer."
22
+ )
23
+
24
+ return super().request(requester)
25
+
26
+
27
+ class BackwardsTracer(Invoker):
28
+
29
+ def __init__(
30
+ self,
31
+ tensor: torch.Tensor,
32
+ fn: Callable,
33
+ interleaver: Interleaver,
34
+ *args,
35
+ **kwargs,
36
+ ):
37
+
38
+ super().__init__(None, *args, **kwargs)
39
+
40
+ self.tensor = tensor
41
+ self.fn = fn
42
+ self.interleaver = interleaver
43
+
44
+ def execute(self, fn: Callable):
45
+
46
+ mediator = BackwardsMediator(fn, self.info)
47
+
48
+ grad_patch = Patch(torch.Tensor, self.interleaver.wrap_grad(), "grad")
49
+
50
+ self.interleaver.patcher.add(grad_patch)
51
+
52
+ def inner():
53
+
54
+ self.fn(self.tensor, *self.args, **self.kwargs)
55
+
56
+ grad_patch.restore()
57
+
58
+ self.interleaver.current.register(mediator, inner)
@@ -82,8 +82,10 @@ class LanguageModel(RemoteableMixin):
82
82
 
83
83
  self.config = config
84
84
  self.tokenizer = tokenizer
85
- self.repo_id: str = args[0] if isinstance(args[0], str) else None
86
-
85
+ # If the user passed in a pre-loaded model, might be able to get repo id off of it.
86
+ # That way if they dont provide a tokenizer, we can load it for them later.
87
+ self.repo_id: str = args[0] if isinstance(args[0], str) else getattr(args[0], 'name_or_path', None)
88
+
87
89
  super().__init__(*args, **kwargs)
88
90
 
89
91
  if import_edits:
@@ -234,6 +236,12 @@ class LanguageModel(RemoteableMixin):
234
236
  ],
235
237
  **kwargs,
236
238
  ):
239
+
240
+ if self.tokenizer is None:
241
+ if self.repo_id is not None:
242
+ self._load_tokenizer(self.repo_id, **kwargs)
243
+ else:
244
+ raise AttributeError("Tokenizer not found. If you passed a pre-loaded model to `LanguageModel`, you need to provide a tokenizer when initializing: `LanguageModel(model, tokenizer=tokenizer)`.")
237
245
 
238
246
  if isinstance(inputs, str) or (
239
247
  isinstance(inputs, list) and isinstance(inputs[0], int)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nnsight
3
- Version: 0.5.0.dev7
3
+ Version: 0.5.0.dev8
4
4
  Summary: Package for interpreting and manipulating the internals of deep learning models.
5
5
  Author-email: Jaden Fiotto-Kaufman <jadenfk@outlook.com>
6
6
  License: MIT License
@@ -0,0 +1,69 @@
1
+ .gitignore
2
+ CHANGELOG.md
3
+ CODE_OF_CONDUCT.md
4
+ LICENSE
5
+ NNsight_Walkthrough.ipynb
6
+ NNsight_v0_3_guide.ipynb
7
+ NNsight_v0_4_guide.ipynb
8
+ README.md
9
+ conftest.py
10
+ pyproject.toml
11
+ pytest.ini
12
+ setup.py
13
+ .github/workflows/publish.yml
14
+ .github/workflows/python-app.yml
15
+ src/nnsight/__init__.py
16
+ src/nnsight/config.yaml
17
+ src/nnsight/log.py
18
+ src/nnsight/util.py
19
+ src/nnsight.egg-info/PKG-INFO
20
+ src/nnsight.egg-info/SOURCES.txt
21
+ src/nnsight.egg-info/dependency_links.txt
22
+ src/nnsight.egg-info/requires.txt
23
+ src/nnsight.egg-info/top_level.txt
24
+ src/nnsight/_c/__init__.py
25
+ src/nnsight/_c/py_mount.c
26
+ src/nnsight/intervention/__init__.py
27
+ src/nnsight/intervention/batching.py
28
+ src/nnsight/intervention/envoy.py
29
+ src/nnsight/intervention/inject.py
30
+ src/nnsight/intervention/interleaver.py
31
+ src/nnsight/intervention/serialization.py
32
+ src/nnsight/intervention/backends/__init__.py
33
+ src/nnsight/intervention/backends/base.py
34
+ src/nnsight/intervention/backends/editing.py
35
+ src/nnsight/intervention/backends/execution.py
36
+ src/nnsight/intervention/backends/remote.py
37
+ src/nnsight/intervention/tracing/__init__.py
38
+ src/nnsight/intervention/tracing/backwards.py
39
+ src/nnsight/intervention/tracing/base.py
40
+ src/nnsight/intervention/tracing/editing.py
41
+ src/nnsight/intervention/tracing/globals.py
42
+ src/nnsight/intervention/tracing/invoker.py
43
+ src/nnsight/intervention/tracing/iterator.py
44
+ src/nnsight/intervention/tracing/tracer.py
45
+ src/nnsight/intervention/tracing/util.py
46
+ src/nnsight/modeling/__init__.py
47
+ src/nnsight/modeling/base.py
48
+ src/nnsight/modeling/diffusion.py
49
+ src/nnsight/modeling/language.py
50
+ src/nnsight/modeling/mixins/__init__.py
51
+ src/nnsight/modeling/mixins/loadable.py
52
+ src/nnsight/modeling/mixins/meta.py
53
+ src/nnsight/modeling/mixins/remoteable.py
54
+ src/nnsight/modeling/vllm/__init__.py
55
+ src/nnsight/modeling/vllm/sampling.py
56
+ src/nnsight/modeling/vllm/vllm.py
57
+ src/nnsight/modeling/vllm/model_runners/GPUModelRunner.py
58
+ src/nnsight/modeling/vllm/model_runners/__init__.py
59
+ src/nnsight/modeling/vllm/workers/GPUWorker.py
60
+ src/nnsight/modeling/vllm/workers/__init__.py
61
+ src/nnsight/schema/__init__.py
62
+ src/nnsight/schema/config.py
63
+ src/nnsight/schema/request.py
64
+ src/nnsight/schema/response.py
65
+ tests/__init__.py
66
+ tests/test_diffusion.py
67
+ tests/test_lm.py
68
+ tests/test_tiny.py
69
+ tests/test_vllm.py
@@ -689,13 +689,12 @@ def test_multiple_skip(gpt2: nnsight.LanguageModel):
689
689
  @torch.no_grad()
690
690
  @pytest.mark.skips
691
691
  def test_skip_inner_module(gpt2: nnsight.LanguageModel):
692
- with gpt2.trace("Hello World"):
693
- inp = gpt2.transformer.h[0].output
694
- gpt2.transformer.h[1].skip(inp)
695
- hs = gpt2.transformer.h[1].attn.output.save()
692
+ with pytest.raises(ValueError):
693
+ with gpt2.trace("Hello World"):
694
+ inp = gpt2.transformer.h[0].output
695
+ gpt2.transformer.h[1].skip(inp)
696
+ hs = gpt2.transformer.h[1].attn.output.save()
696
697
 
697
- with pytest.raises(UnboundLocalError):
698
- hs
699
698
 
700
699
 
701
700
  @torch.no_grad()
Binary file
@@ -1,28 +0,0 @@
1
- # Minimal makefile for Sphinx documentation
2
- #
3
-
4
- # You can set these variables from the command line, and also
5
- # from the environment for the first two.
6
- SPHINXOPTS ?=
7
- SPHINXBUILD ?= sphinx-build
8
- SOURCEDIR = source
9
- BUILDDIR = build
10
-
11
- # Put it first so that "make" without argument is like "make help".
12
- help:
13
- @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14
-
15
- .PHONY: help Makefile
16
-
17
- bld:
18
- rm -rf build
19
- make dirhtml
20
-
21
- run:
22
- cd build/dirhtml && python3 -m http.server
23
-
24
-
25
- # Catch-all target: route all unknown targets to Sphinx using the new
26
- # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
27
- %: Makefile
28
- @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@@ -1,31 +0,0 @@
1
- # Installation
2
-
3
- Run `pip install -r requirements.txt` while in the `nnsight/docs` directory.
4
-
5
- Optionally, run `pip install -e nnsight` from the root directory to use the working dir
6
-
7
- If you haven't already, you should also install pandoc. With brew run: `brew install pandoc`
8
-
9
- # Adding Tutorials
10
-
11
- Tutorials are written as Jupyter Notebooks in `.ipynb` format, and automatically converted to html by the `nbsphinx` extension.
12
-
13
- The only requirement of `nbsphinx` is that you add a header to the notebook to act as the rendered title on Sphinx docs. To do this, create a markdown cell at the top of your notebook with a header `#`.
14
-
15
- Then, just add your notebook to the `nnsight/docs/source/notebooks/tutorials` directory.
16
-
17
- If you're adding a new notebook, navigate to `nnsight/docs/source/tutorials.rst`. At the bottom of the page, add the path to your notebook under the `toctree`.
18
-
19
- ```
20
- .. toctree::
21
- :maxdepth: 1
22
-
23
- notebooks/tutorials/walkthrough.ipynb
24
- ...
25
- <YOUR NOTEBOOK PATH>
26
-
27
- ```
28
-
29
- # Compiling Sphinx to HTML
30
-
31
- Run `make dirhtml` from the `nnsight/docs` directory. The build is located in `nnsight/docs/build/dirhtml`. You can run `python3 -m http.server <PORT>` from that directory, and see the site at `http://localhost:<PORT>`.
@@ -1,35 +0,0 @@
1
- @ECHO OFF
2
-
3
- pushd %~dp0
4
-
5
- REM Command file for Sphinx documentation
6
-
7
- if "%SPHINXBUILD%" == "" (
8
- set SPHINXBUILD=sphinx-build
9
- )
10
- set SOURCEDIR=source
11
- set BUILDDIR=build
12
-
13
- %SPHINXBUILD% >NUL 2>NUL
14
- if errorlevel 9009 (
15
- echo.
16
- echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
17
- echo.installed, then set the SPHINXBUILD environment variable to point
18
- echo.to the full path of the 'sphinx-build' executable. Alternatively you
19
- echo.may add the Sphinx directory to PATH.
20
- echo.
21
- echo.If you don't have Sphinx installed, grab it from
22
- echo.https://www.sphinx-doc.org/
23
- exit /b 1
24
- )
25
-
26
- if "%1" == "" goto help
27
-
28
- %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29
- goto end
30
-
31
- :help
32
- %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33
-
34
- :end
35
- popd
@@ -1,8 +0,0 @@
1
- nnsight
2
- pydata_sphinx_theme
3
- sphinx
4
- sphinx-copybutton
5
- sphinx-design
6
- nbsphinx
7
- plotly
8
- ipython
Binary file
@@ -1,65 +0,0 @@
1
- /* Website light and dark theme colors */
2
-
3
- html[data-theme="dark"] {
4
- --pst-color-secondary: #ce5d97;
5
- --pst-color-primary: #4385be;
6
- --pst-color-primary-highlight: #ce5d97;
7
- --pst-color-background: #100F0F;
8
- --pst-color-on-background: #1C1B1A;
9
- --pst-color-surface: #282726;
10
- --pst-color-on-surface: #CECDC3;
11
- --pst-color-text-base: #CECDC3;
12
- --pst-color-text-muted: #CECDC3;
13
- --pst-color-heading-color: #CECDC3;
14
- --pst-color-border: #1C1B1A;
15
- --pst-color-panel-background: #1C1B1A;
16
- --output-bg-color: #343331;
17
- }
18
-
19
- html[data-theme="light"] {
20
- --output-bg-color: #d8ebfb;
21
- --pst-color-primary: #4385be;
22
- --pst-color-secondary: #ce5d97;
23
- --pst-color-primary-highlight: #ce5d97;
24
- }
25
-
26
-
27
- /* Notebook h1 section heading styles */
28
- .nb-heading {
29
- margin-top: 2.5rem;
30
- }
31
-
32
- /* Remove the shadow on the pydata navbar */
33
- .bd-header {
34
- box-shadow: none !important;
35
- }
36
-
37
- /* Hide the theme version note at the bottom of the page */
38
- .theme-version {
39
- visibility: hidden;
40
- }
41
-
42
- /* code cell output */
43
- .output_area {
44
- background-color: var(--output-bg-color);
45
- margin-left: 10px !important;
46
- }
47
-
48
- /* Coloring, mostly for buttons */
49
- .primary {
50
- background-color : var(--pst-color-primary) !important;
51
- border-color : var(--pst-color-primary) !important;
52
- color: var(--pst-color-background) !important;
53
- }
54
-
55
- .secondary {
56
- background-color : transparent !important;
57
- border-color : #878580 !important;
58
- color: #878580 !important;
59
- }
60
-
61
- .secondary:hover {
62
- background-color : transparent !important;
63
- border-color : var(--pst-color-on-surface) !important;
64
- color: var(--pst-color-on-surface) !important;
65
- }
@@ -1,28 +0,0 @@
1
- .hero {
2
- height: 100vh !important;
3
- }
4
-
5
- .hero-image {
6
- width: 100% !important;
7
- }
8
-
9
- .fixed-background {
10
- background-attachment: fixed;
11
- background-position: center;
12
- background-repeat: no-repeat;
13
- background-size: cover;
14
- left: 0 !important;
15
- top: 0 !important;
16
- position: fixed !important;
17
- height: 100vh;
18
- width: 100%;
19
- z-index: -1;
20
- }
21
-
22
- .surface {
23
- background-color: var(--pst-color-background);
24
- }
25
-
26
- .transparent {
27
- background: transparent !important;
28
- }