Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / modules / engram / host_engram.py: 96%
24 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Host-table Engram; planner, sparse optimizer and checkpoint policy stay caller-owned."""
16from __future__ import annotations
18from typing import Any
20import torch
21from torch import nn
23from .engram import EngramModule
24from .host_engram_table import HostEngramTable
25from .parallel_engram import host_sparse_lookup
28class HostEngramModule(EngramModule):
29 """Reuse fusion math and stage only touched rows from an FP32 CPU table."""
31 _sparse_lookup = staticmethod(host_sparse_lookup)
33 def _create_embedding(self, rows: int, width: int) -> nn.Embedding:
34 """Defer the full FP32 table to the caller-owned materialization lifecycle."""
35 return nn.Embedding(rows, width, device="meta", dtype=torch.float32)
38 def __init__(self, *, config: Any = None, module: nn.Module | None = None,
39 module_fqn: str = "", context: Any = None) -> None:
40 """Create a meta table or adopt the source table before planner materialization."""
41 super().__init__(config=config, module=module, module_fqn=module_fqn, context=context)
42 if isinstance(self.embed, HostEngramTable):
43 return
44 training = self.embed.training
45 source = module if module is not None else config
46 weight = self.embed.weight
47 if tuple(weight.shape) != (self.padded_num_embeddings, self.embed.embedding_dim):
48 raise ValueError("Host Engram replacement requires the full physical table before planning")
49 if module is None:
50 weight = nn.Parameter(torch.empty(weight.shape, device="meta", dtype=torch.float32))
51 self.embed = HostEngramTable(
52 source_weight=weight, logical_rows=self.logical_num_embeddings,
53 max_pending_entries=getattr(source, "engram_max_pending_entries", 1000000),
54 max_sparse_rows_per_step=getattr(source, "engram_max_sparse_rows_per_step", 1000000),
55 )
56 self.embed.train(training)