MCPcopy Create free account
hub / github.com/pytorch/executorch / __init__

Method __init__

examples/qualcomm/oss_scripts/t5/t5_model.py:23–70  ·  view source on GitHub ↗
(
        self,
        config,
        max_hidden_seq_length=4096,
        max_cache_length=1024,
    )

Source from the content-addressed store, hash-verified

21# Copy from transformers/models/t5/modeling_t5.py (transformers=4.47.1)
22class CustomT5Stack(T5Stack):
23 def __init__(
24 self,
25 config,
26 max_hidden_seq_length=4096,
27 max_cache_length=1024,
28 ):
29 super().__init__(config)
30
31 # ====================Qualcomm Changed=================================
32 # Customized position bias computation:
33 # Since the calculation in `T5Attention._relative_position_bucket` is not QNN-friendly,
34 # we precompute the relative position buckets as constant tensors during initialization.
35 # For the encoder: use the precomputed `encoder_self_attn_position_bias`.
36 # For the decoder: use the precomputed `decoder_self_attn_position_bias`.
37
38 self.max_hidden_seq_length = max_hidden_seq_length
39 self.max_cache_length = max_cache_length
40
41 # Create relative position table for encoder
42 encoder_self_attn_relative_position_bucket = (
43 T5Attention._relative_position_bucket(
44 torch.arange(max_hidden_seq_length)[None, :]
45 - torch.arange(max_hidden_seq_length)[:, None],
46 bidirectional=(not self.is_decoder),
47 num_buckets=config.relative_attention_num_buckets,
48 max_distance=config.relative_attention_max_distance,
49 )
50 )
51 self.register_buffer(
52 "encoder_self_attn_position_bias",
53 encoder_self_attn_relative_position_bucket,
54 )
55
56 # Create relative position table for decoder
57 decoder_self_attn_relative_position_bucket = (
58 T5Attention._relative_position_bucket(
59 torch.arange(max_cache_length)[None, :]
60 - torch.arange(max_cache_length)[:, None],
61 bidirectional=(not self.is_decoder),
62 num_buckets=config.relative_attention_num_buckets,
63 max_distance=config.relative_attention_max_distance,
64 )
65 )
66 self.register_buffer(
67 "decoder_self_attn_position_bias",
68 decoder_self_attn_relative_position_bucket,
69 )
70 # ========================================================================
71
72 def forward( # noqa: C901
73 self,

Callers 3

__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45

Calls 1

register_bufferMethod · 0.80

Tested by

no test coverage detected