Download code/tt_autoe2e/reference/tree.py from changh95/auto_e2e-p150: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/reference/tree.py
- Command line
-
hf download hf://changh95/auto_e2e-p150/code/tt_autoe2e/reference/tree.py
-
curl -L -o tree.py https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/reference/tree.py
2.11 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """The AutoE2E checkpoint as an attribute tree, without the upstream code (no timm): ``load_tree(path)`` returns an | |
| object on which ``tree.Reactive_E2E.FeatureFusion.view_fusion.layers[0].self_attention.value_proj.weight`` is the | |
| state-dict tensor of that name (numeric path segments index: ``layers[0]``, ``ffn[3]``). Enough for | |
| ``PortForm`` and the device modules, which read parameters only. | |
| """ | |
| from __future__ import annotations | |
| from typing import Any, Dict | |
| FRONT_CAMERA_INDEX = 0 # view order: front, front_left, front_right, rear_left, rear, rear_right | |
| class Node: | |
| def __init__(self): | |
| self._c: Dict[str, Any] = {} | |
| def __getattr__(self, name: str) -> Any: | |
| c = self.__dict__.get("_c", {}) | |
| if name in c: | |
| return c[name] | |
| raise AttributeError(name) | |
| def __getitem__(self, i: Any) -> Any: | |
| return self._c[str(i)] | |
| def __iter__(self): | |
| keys = sorted(self._c, key=lambda k: (not k.isdigit(), int(k) if k.isdigit() else 0, k)) | |
| return iter([self._c[k] for k in keys]) | |
| def __len__(self) -> int: | |
| return len(self._c) | |
| def tree(state: Dict[str, Any]) -> Node: | |
| root = Node() | |
| for key, value in state.items(): | |
| node = root | |
| parts = key.split(".") | |
| for p in parts[:-1]: | |
| node = node._c.setdefault(p, Node()) | |
| node._c[parts[-1]] = value | |
| return root | |
| def load_tree(path: str) -> Node: | |
| """The checkpoint at ``path`` (``weights_only``: tensors only, no pickle execution) as a :class:`Node` tree, with | |
| the constructor constants the parameter tree lacks (``front_camera_index``).""" | |
| import torch | |
| ck = torch.load(path, map_location="cpu", weights_only=True) | |
| root = tree({k: v.float() if v.is_floating_point() else v for k, v in ck["model_state_dict"].items()}) | |
| root.Reactive_E2E.FeatureFusion.view_fusion._c["front_camera_index"] = FRONT_CAMERA_INDEX | |
| layers = root.Reactive_E2E.FeatureFusion.view_fusion.layers | |
| root.Reactive_E2E.FeatureFusion.view_fusion._c["layers"] = list(layers) | |
| return root | |