MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / __init__

Method __init__

accessory/model/peft.py:79–136  ·  view source on GitHub ↗
(
        self,
        in_features: int,
        out_features: int,
        bias: bool = True,
        gather_output: bool = True,
        init_method: Callable[[torch.Tensor], torch.Tensor] = init.xavier_normal_,
        stride: int = 1,
        keep_master_weight_for_test: bool = False,
        lora_rank=0
    )

Source from the content-addressed store, hash-verified

77 """
78
79 def __init__(
80 self,
81 in_features: int,
82 out_features: int,
83 bias: bool = True,
84 gather_output: bool = True,
85 init_method: Callable[[torch.Tensor], torch.Tensor] = init.xavier_normal_,
86 stride: int = 1,
87 keep_master_weight_for_test: bool = False,
88 lora_rank=0
89 ) -> None:
90 nn.Module.__init__(self)
91
92 # Keep input parameters
93 self.in_features = in_features
94 self.out_features = out_features
95 self.gather_output = gather_output
96 # Divide the weight matrix along the last dimension.
97 world_size = get_model_parallel_world_size()
98 self.output_size_per_partition = divide_and_check_no_remainder(out_features, world_size)
99
100 # Parameters.
101 # Note: torch.nn.functional.linear performs XA^T + b and as a result
102 # we allocate the transpose.
103 self.weight = Parameter(torch.Tensor(self.output_size_per_partition, self.in_features))
104 if bias:
105 self.bias = Parameter(torch.Tensor(self.output_size_per_partition))
106 # Always initialize bias to zero.
107 with torch.no_grad():
108 self.bias.zero_()
109 else:
110 self.register_parameter("bias", None)
111
112 # Initialize weight.
113 self.master_weight = _initialize_affine_weight(
114 self.weight,
115 self.out_features,
116 self.in_features,
117 self.output_size_per_partition,
118 0,
119 init_method,
120 stride=stride,
121 return_master_weight=keep_master_weight_for_test,
122 )
123
124 self.lora_rank = lora_rank
125 if self.lora_rank > 0:
126 # if world_size > 1:
127 # raise NotImplemented("Lora with model parallel with change the original behavior, not yet supported")
128 self.lora_a = nn.Linear(self.in_features, self.lora_rank, bias=False)
129 # workaround because trunc_normal_ does not currently support bfloat16
130 _ = init.trunc_normal_(self.lora_a.weight.data.to(torch.float32), std=.02)
131 self.lora_a.weight.data.copy_(_)
132 self.lora_b = ColumnParallelLinear(self.lora_rank, self.out_features, bias=False, gather_output=gather_output)
133 nn.init.zeros_(self.lora_b.weight)
134 else:
135 self.lora_a = None
136 self.lora_b = None

Callers

nothing calls this directly

Calls 1

__init__Method · 0.45

Tested by

no test coverage detected